Question: What is generic syntax?
Answer: Generic syntax is when you put the type in angle brackets.
Question: What do you call the type in angle brackets <String>?
Answer: The type in angle brackets is referred to as either 1) parameterized type 2) type parameter
Question: How can you declare a generic type parameter for a method argument?
Answer:
void takeListOfString(List<String> strings){
Strings.add(βString 1β);
}
Question:What is a Java ClassCastException?
Object obj = "Hello, world!"; Integer num = (Integer) obj; // This throws ClassCastExceptionIn the above example:
obj is a String, but you're trying to cast it to an Integer.String is not a subclass of Integer, the cast is invalid and a ClassCastException is thrown.java.lang.RuntimeException.
if (obj instanceof Integer) {
Integer num = (Integer) obj;
}
Answer:
Question:In Java SE 22, is a caller expected to recover from checked exceptions?
Answer:
In Java SE 22, as in earlier versions, a caller is expected to recover from checked exceptions, which is the core philosophy behind checked exceptions in Java.
What does this mean?
A checked exception (i.e., any subclass of `java.lang.Exception` *except* `RuntimeException`) represents conditions that a reasonable application should anticipate and recover from.
Example:
public void readFile(String path) throws IOException {
Files.readAllLines(Path.of(path)); // may throw IOException
}
Any caller of `readFile()` must either:
try {
readFile("data.txt");
} catch (IOException e) {
// recovery logic: maybe retry, notify user, log error, etc.
}
public void processFile() throws IOException {
readFile("data.txt");
}
Question: Generic Question (See pdf)
Answer:
Question: How do you update non generic code to make it generic?
Answer:
<> Diamond Syntax. Add a type an angle brackets(<>) immediately following the collection type.
List<Integer> myList = new ArrayList<Integer>();
1. After type in variable declaration 2. After type in constructor call
Question: How were pre-Java 5 Generics dealt with?
Answer:
Before Java 5 introduced generics, Java used raw types to work with collections and other parameterized classes. Here's how things were typically handled:
Without generics, Java collections (like List, Map, etc.) did not specify a type.
This meant:
Object into a collection.Example (Pre-Java 5):
import java.util.*;
List list = new ArrayList(); // raw type
list.add("Hello");
list.add(42); // no compile-time error
String s = (String) list.get(0); // explicit cast
Integer i = (Integer) list.get(1);
ClassCastException at runtimeinstanceof
Developers often used instanceof to ensure correctness:
Object obj = list.get(0);
if (obj instanceof String) {
String s = (String) obj;
}
List<String>) for clarity and safety.To migrate from old code:
// Before (raw type)
List names = new ArrayList();
// After (with generics)
List<String> names = new ArrayList<>();
| Feature | Pre-Java 5 (Raw Types) | Java 5+ (Generics) |
|---|---|---|
| Type safety | β No | β Yes |
| Type cast on retrieval | β Required | β Not needed |
| Compile-time errors | β Rare | β Enforced |
| Syntax clarity | β Poor | β Improved |
Question: What kind of data types could a programmer put into a pre Java 5 collection?
Answer: The pre Java 5 non generic collections allowed a developer to put any data type into a collection except for primitives.
Question: What is the difference between 1) compilation fails 2) compiles without error 3) compiles without warnings 4) compiles with warnings?
Answer: In most questions on the exam, you care only about compiles vs. compilation fails, compiler warnings donβt matter for most of the exam. But when you are using generics, and mixing both typed and untyped code, warnings matter. [P641 SCJP_Sun_Certified_Programmer_for_Java6 _PDFDrive]