SCJP Sun Certified Java Programmer 3 — Questions and Answers
Question 1: Which collection class allows null keys and null values?
- Hashtable
- TreeMap
- HashMap (Correct answer)
- ConcurrentHashMap
Correct answer: HashMap
HashMap allows one null key and multiple null values, unlike Hashtable or ConcurrentHashMap.
Question 2: What is the output of: System.out.println(1 + 2 + "3" + 4 + 5);
- 12345
- 33345
- 335 (Correct answer)
- 3345
Correct answer: 335
Left to right: 1+2=3, then 3+"3"="33", then "33"+4="334", then "334"+5="3345" — wait, that gives 3345.
Question 3: Which exception is thrown when you try to access an array index that is out of bounds?
- IndexOutOfBoundsException
- ArrayIndexOutOfBoundsException (Correct answer)
- NullPointerException
- IllegalArgumentException
Correct answer: ArrayIndexOutOfBoundsException
ArrayIndexOutOfBoundsException is the specific runtime exception thrown for invalid array index access.
Question 4: What is the purpose of the 'transient' keyword in Java?
- Prevents a variable from being overridden
- Prevents a variable from being serialized (Correct answer)
- Makes a variable thread-safe
- Prevents a variable from being garbage collected
Correct answer: Prevents a variable from being serialized
Fields marked transient are excluded from Java object serialization.
Question 5: Which of the following correctly describes method overloading?
- Same method name, different return type only
- Same method name, different parameter list (Correct answer)
- Different method name, same parameter list
- Same method name and parameter list in a subclass
Correct answer: Same method name, different parameter list
Overloading requires the same method name but a different number or type of parameters.
Question 6: What does the 'volatile' keyword guarantee in Java?
- Atomic compound operations on the variable
- Visibility of changes across threads (Correct answer)
- Only one thread can access the variable at a time
- The variable is stored in CPU cache
Correct answer: Visibility of changes across threads
volatile ensures that reads and writes to a variable are visible to all threads immediately.
Question 7: Which method must be implemented when a class implements the Runnable interface?
- start()
- execute()
- run() (Correct answer)
- call()
Correct answer: run()
The Runnable interface has a single abstract method run() that defines the thread's task.
Which collection class allows null keys and null values?