SCJP Sun Certified Java Programmer 4 — Questions and Answers
Question 1: What is autoboxing in Java?
- Converting a String to a primitive
- Automatic conversion between primitives and their wrapper types (Correct answer)
- Casting a subclass to a superclass
- Converting an array to a List automatically
Correct answer: Automatic conversion between primitives and their wrapper types
Autoboxing automatically converts primitives (like int) to their wrapper objects (like Integer) and vice versa.
Question 2: Which of the following is NOT a valid way to create a thread in Java?
- Extending Thread class
- Implementing Runnable interface
- Implementing Callable interface
- Implementing Threadable interface (Correct answer)
Correct answer: Implementing Threadable interface
There is no 'Threadable' interface in Java; threads are created via Thread, Runnable, or Callable.
Question 3: What is the difference between '==' and 'equals()' when comparing String objects?
- No difference; they work identically
- '==' compares content; equals() compares references
- '==' compares references; equals() compares content (Correct answer)
- equals() is faster than '=='
Correct answer: '==' compares references; equals() compares content
'==' compares object references (memory addresses), while String's equals() compares character content.
Question 4: In Java, which keyword is used to prevent a class from being subclassed?
- static
- abstract
- final (Correct answer)
- sealed
Correct answer: final
Declaring a class as final prevents it from being extended by any subclass.
Question 5: What is the output of: System.out.println(Math.floor(-2.5));
- -2.0
- -3.0 (Correct answer)
- 2.0
- 3.0
Correct answer: -3.0
Math.floor() returns the largest integer less than or equal to the argument, so floor(-2.5) = -3.0.
Question 6: Which of the following statements about abstract classes is correct?
- Abstract classes cannot have constructors
- Abstract classes can be instantiated directly
- Abstract classes can have both abstract and concrete methods (Correct answer)
- Abstract classes must have at least one abstract method
Correct answer: Abstract classes can have both abstract and concrete methods
Abstract classes may contain any mix of abstract and fully implemented (concrete) methods.
Question 7: What is the scope of a variable declared inside a for-loop initialization?
- Entire class
- Entire method
- Only within the for-loop block (Correct answer)
- Only the initialization statement
Correct answer: Only within the for-loop block
A variable declared in the for-loop initializer is scoped to the entire for-loop block, including its body.
What is autoboxing in Java?