SCJP Sun Certified Java Programmer 5 — Questions and Answers
Question 1: Which of the following correctly describes the behavior of the finally block?
- It executes only if no exception occurs
- It executes only if an exception occurs
- It always executes unless System.exit() is called (Correct answer)
- It executes before the try block
Correct answer: It always executes unless System.exit() is called
The finally block always executes after try/catch unless the JVM is terminated via System.exit().
Question 2: What is the default value of an instance variable of type boolean in Java?
- null
- true
- false (Correct answer)
- 0
Correct answer: false
The default value for an instance boolean variable is false in Java.
Question 3: Which interface must be implemented for a class to be used with the Collections.sort() method without a Comparator?
- Sortable
- Comparable (Correct answer)
- Comparator
- Ordered
Correct answer: Comparable
A class must implement Comparable and its compareTo() method to support natural ordering with Collections.sort().
Question 4: What happens if you call Thread.sleep() with a negative value?
- The thread sleeps indefinitely
- Nothing happens
- An IllegalArgumentException is thrown (Correct answer)
- A NegativeArraySizeException is thrown
Correct answer: An IllegalArgumentException is thrown
Thread.sleep() throws IllegalArgumentException if the millisecond value is negative.
Question 5: In Java generics, what does the wildcard '? extends T' indicate?
- Any type that is a supertype of T
- Any type that is a subtype of T or T itself (Correct answer)
- Any type unrelated to T
- Only the exact type T
Correct answer: Any type that is a subtype of T or T itself
'? extends T' is an upper-bounded wildcard accepting T or any subclass of T.
Question 6: Which of the following is true about constructors in Java?
- Constructors can be marked as abstract
- Constructors are inherited by subclasses
- Constructors do not have a return type, not even void (Correct answer)
- Constructors can be synchronized
Correct answer: Constructors do not have a return type, not even void
Constructors have no return type — not even void — which distinguishes them from regular methods.
Question 7: What is the purpose of the 'strictfp' keyword in Java?
- Enforces strict access modifiers
- Ensures floating-point calculations are platform-independent (Correct answer)
- Makes all methods in a class final
- Prevents the class from using native methods
Correct answer: Ensures floating-point calculations are platform-independent
strictfp restricts floating-point calculations to IEEE 754 standard for consistent cross-platform results.
Which of the following correctly describes the behavior of the finally block?