SCJP Sun Certified Java Programmer MCQ 4 — Questions and Answers
Question 1: What is the output of the following code? String s1 = "Java"; String s2 = "Java"; System.out.println(s1 == s2);
- false
- true (Correct answer)
- Compile-time error
- NullPointerException
Correct answer: true
String literals are stored in the string pool, so s1 and s2 reference the same object, making == return true.
Question 2: Which collection class allows duplicate elements and maintains insertion order?
- HashSet
- TreeSet
- ArrayList (Correct answer)
- HashMap
Correct answer: ArrayList
ArrayList allows duplicate elements and preserves the order in which elements were inserted.
Question 3: What does the 'super' keyword refer to when used inside a constructor?
- The current class instance
- The parent class constructor (Correct answer)
- A static parent method
- The outermost class in a nested structure
Correct answer: The parent class constructor
super() in a constructor explicitly calls the parent class's constructor.
Question 4: Which of the following correctly describes method overloading in Java?
- Same method name, same parameter list, different return type
- Same method name, different parameter list, in the same class (Correct answer)
- Same method name, same parameter list, in a subclass
- Different method name, same parameter list
Correct answer: Same method name, different parameter list, in the same class
Overloading occurs when methods share the same name but differ in number or type of parameters within the same class.
Question 5: Which statement about abstract classes in Java is TRUE?
- Abstract classes can be instantiated directly
- Abstract classes cannot have constructors
- A class with at least one abstract method must be declared abstract (Correct answer)
- Abstract classes cannot have concrete methods
Correct answer: A class with at least one abstract method must be declared abstract
If a class contains any abstract method, the class itself must also be declared abstract.
Question 6: What is the range of the Java 'short' data type?
- -128 to 127
- -32768 to 32767 (Correct answer)
- -2147483648 to 2147483647
- 0 to 65535
Correct answer: -32768 to 32767
short is a 16-bit signed integer ranging from -32768 to 32767.
Question 7: Which of the following is true about the 'finally' block in Java?
- It executes only if no exception is thrown
- It executes only if an exception is thrown
- It always executes after try/catch regardless of exceptions (Correct answer)
- It is mandatory with every try block
Correct answer: It always executes after try/catch regardless of exceptions
The finally block always executes after the try and catch blocks, whether or not an exception occurred.
What is the output of the following code?
String s1 = "Java";
String s2 = "Java";
System.out.println(s1 == s2);