3. What method is used to determine the type of an object?
Correct answer is d. I don’t find in JDK any method instanceOf. And other names also can be used as method names.
4. Which of the following are valid casts?
a. num1 = num2;
b. num1 = (int)num2;
c. num1 = (float)num2;
d. num1(int) = num2;
supposed right answer is b. But how we can deside about casts correctness if we don’t know data types of num1 and num2? For example if num1 were of type short, b would be incorrect or if num1 were float and num2 int, c would be correct.
8. Handling Exceptions in an Application
1. Which of the following implement checked exceptions?
a. Class A extends RuntimeException b. Class A extends Throwable
c. Class A extends Exception
d. Class A extends IOException
Correct answers are c and d. Why Throwable is not considered as checked exception? I can throw it and must deal with it explicitly.
3. What method is used to determine the type of an object? Weird question and even more weird answer options. The Object class has a getClass-method which can be used to get the class (type) of the object. The Class class has an isInstance-method to check if an object can be assigned to this class. The JDK has an instanceof operator to check if an object is an instance of a given class (type). So none of the answers are correct!
4. Which of the following are valid casts? If the questions just wants to know which options use valid cast syntax (independent on the variable types) the correct answers are (b) & (c). (a) is not a cast, just an assignment (althought it could be an implicit cast, but unable to know without the variable types), (d) is just incorrect syntax.
1. Which of the following implement checked exceptions? All Exceptions are Throwable, but a Throwable is not always an exception (e.g. OutOfMemoryError is a Throwable, is an Error, but it's not an Exception). Because the question asks for checked exceptions, only (c) and (d) are correct.
5. What exceptions may the following code generate at runtime? This little snippet does not compile, because all local variables should be initialized before use. Variable s is not initialized, so the program will not compile (compilation fails would be the correct answer). If we assign null to variable s, the correct answer would be (a). It's tempting to think the string concatenation will throw a NullPointerException, but it won't (printing variable s will result in nullnext on the standard output).
The mock exam (test) questions sections are (one of) the most important parts in a study guide for a certification exam. If they contain a bunch of mistakes, the book becomes quite useless because these mistakes make students doubt about what they have learned instead of preparing them for the exam.