Sun Certified Java Programmer (SCJP) - Java SE 6 (CX-310-065) โ Questions and Answers
Question 1: Which concurrent collection class is designed for high-concurrency scenarios and partitions its data to reduce lock contention?
- ConcurrentHashMap (Correct answer)
- Vector
- Hashtable
- SynchronizedMap
Correct answer: ConcurrentHashMap
`ConcurrentHashMap` uses segment-level (or node-level in Java 8+) locking for better throughput than a fully synchronized map.
Question 2: Which of the following is a valid use of a bounded type parameter in Java generics?
- <T extends Comparable<T>> (Correct answer)
- <T implements Serializable>
- <T super Object>
- <T extends String, Integer>
Correct answer: <T extends Comparable<T>>
`<T extends Comparable<T>>` bounds T to types that implement Comparable, enabling comparison operations within the generic code.
Question 3: Which of the following is true about abstract classes in Java?
- An abstract class cannot have constructors
- An abstract class cannot implement interfaces
- An abstract class can have both abstract and concrete methods (Correct answer)
- An abstract class must have at least one abstract method
Correct answer: An abstract class can have both abstract and concrete methods
An abstract class can contain any combination of abstract and concrete methods, and it can also have constructors.
Question 4: Which collection class is best suited for implementing a LIFO (Last-In-First-Out) stack in modern Java?
- java.util.LinkedList used as a Deque
- java.util.ArrayDeque (Correct answer)
- java.util.PriorityQueue
- java.util.Stack
Correct answer: java.util.ArrayDeque
ArrayDeque is the preferred stack implementation because it is faster than Stack (which is synchronized) and more memory-efficient than LinkedList.
Question 5: Which of the following correctly defines a constructor in Java?
- It must call super() explicitly
- It has the same name as the class and no return type (Correct answer)
- It has a return type of void
- It cannot be overloaded
Correct answer: It has the same name as the class and no return type
A constructor shares the class name and has no return type declared (not even void).
Question 6: What is the result of `System.out.println(5 >> 1);`?
- 2 (Correct answer)
- 10
- 3
- 1
Correct answer: 2
The >> operator is the signed right-shift. Shifting 5 (binary 101) right by 1 position gives 010, which is 2. Each right-shift by 1 is equivalent to integer division by 2.
Question 7: Which of the following is NOT a characteristic of `ConcurrentHashMap`?
- It is thread-safe for updates
- It allows null keys (Correct answer)
- It allows concurrent reads without locking
- It uses segment-level locking (or node-level in Java 8+)
Correct answer: It allows null keys
ConcurrentHashMap does not permit null keys or null values, unlike HashMap which allows one null key.
Question 8: What is the correct order of execution when an exception is thrown in a try block that has both catch and finally blocks?
- try โ catch โ finally (Correct answer)
- try โ finally โ catch
- catch โ try โ finally
- try โ catch (only if exception)
Correct answer: try โ catch โ finally
When an exception occurs, control goes to the matching catch block, then finally always executes last.
Question 9: What does the 'static' keyword mean when applied to a variable?
- The variable cannot be modified
- The variable is shared across all instances of the class (Correct answer)
- The variable is stored on the stack
- The variable is only accessible within the method
Correct answer: The variable is shared across all instances of the class
A static variable belongs to the class itself rather than any specific instance.
Question 10: What does the instanceof operator return when the left operand is null?
- false (Correct answer)
- Compile error
- NullPointerException
- true
Correct answer: false
The instanceof operator always returns false when the left-hand operand is null, regardless of the type on the right.
Question 11: What is the output of: int x = 5; System.out.println(x++ + ++x);
- 13
- 10
- 11
- 12 (Correct answer)
Correct answer: 12
x++ returns 5 (then x becomes 6), ++x increments x to 7 and returns 7, so 5+7=12.
Question 12: What is the value of `byte b = (byte) 130;` in Java?
- Compilation error
- 130
- 127
- -126 (Correct answer)
Correct answer: -126
byte holds values from -128 to 127. 130 in binary (8 bits) is 10000010. With a signed byte, the leading 1 indicates a negative number. Using two's complement, this equals -126.
Question 13: Which of the following is a correct way to create a new thread in Java?
- Use the Process class
- Extend Runnable class
- Implement Callable interface only
- Extend Thread class or implement Runnable interface (Correct answer)
Correct answer: Extend Thread class or implement Runnable interface
In Java, threads can be created by extending `Thread` or implementing `Runnable` and passing it to a `Thread` constructor.
Question 14: What is the correct syntax to create an instance of the non-static inner class `Inner` defined inside class `Outer`?
- Outer.Inner i = new Outer.Inner();
- Outer o = new Outer(); Outer.Inner i = o.new Inner(); (Correct answer)
- Inner i = new Inner();
- Outer.Inner i = Outer.new Inner();
Correct answer: Outer o = new Outer(); Outer.Inner i = o.new Inner();
A non-static inner class is tied to an instance of its enclosing class. You must first create an Outer instance, then use the `outerRef.new Inner()` syntax. The other forms are not valid Java.
Question 15: Which assignment causes a compile-time error?
- float f = 100L;
- long l = 100;
- int i = 'A';
- byte b = 100L; (Correct answer)
Correct answer: byte b = 100L;
Assigning a long literal to a byte requires an explicit cast because it is a narrowing conversion. longโbyte cannot be done implicitly. The other assignments are widening conversions (intโlong, longโfloat) or a char-to-int widening, all of which are implicit.
Question 16: A method-local inner class defined inside an instance method can access which variables from the enclosing method?
- Only local variables declared final (or effectively final in Java 8+) (Correct answer)
- No variables from the enclosing method
- All local variables of the enclosing method
- Only static variables of the enclosing class
Correct answer: Only local variables declared final (or effectively final in Java 8+)
A method-local inner class can access the enclosing class's instance/static members freely, but can only access local variables that are final (Java 6 SCJP rule). This is because the class instance may outlive the method's stack frame.
Question 17: Which method correctly checks whether a String starts with a given prefix?
- contains()
- startsWith() (Correct answer)
- beginsWith()
- startWith()
Correct answer: startsWith()
The correct method name is startsWith(), which returns true if the string begins with the specified prefix.
Question 18: What happens when you call `Thread.sleep(0)` in Java?
- The thread terminates immediately
- The thread blocks indefinitely until interrupted
- Nothing happens; the call is ignored
- The thread yields the CPU to other threads of equal or higher priority (Correct answer)
Correct answer: The thread yields the CPU to other threads of equal or higher priority
`Thread.sleep(0)` causes the current thread to yield the processor, allowing other threads of equal or higher priority to execute.
Question 19: What is method overloading in Java?
- Preventing a method from being overridden
- Defining multiple methods with the same name but different parameter lists (Correct answer)
- Making a method run faster
- Providing a new implementation for a parent class method
Correct answer: Defining multiple methods with the same name but different parameter lists
Overloading lets you define multiple methods with the same name differing in number or type of parameters.
Question 20: What is the correct order of exception hierarchy evaluation in a multi-catch chain?
- Alphabetical order
- More specific (child) exceptions first (Correct answer)
- More general (parent) exceptions first
- Order does not matter
Correct answer: More specific (child) exceptions first
Catch blocks must go from most specific (child) to most general (parent); otherwise the compiler raises an error because the general block would shadow the specific one.
Question 21: Which of the following interface features was introduced in Java 8 that affects how classes implement interfaces?
- Interfaces can have instance fields
- Interfaces can have default methods with concrete implementations (Correct answer)
- Interfaces can now have private methods
- Interfaces can have constructors
Correct answer: Interfaces can have default methods with concrete implementations
Java 8 introduced default methods in interfaces, allowing interfaces to provide concrete method implementations that classes inherit without being forced to override.
Question 22: What does the compound assignment `x += 1.5;` do when x is declared as `int x = 3;`?
- Causes a compile-time error due to loss of precision
- Assigns 4.5 to x
- Assigns 4 to x after an implicit narrowing cast (Correct answer)
- Assigns 5 to x by rounding up
Correct answer: Assigns 4 to x after an implicit narrowing cast
Compound assignment operators include an implicit narrowing cast. `x += 1.5` is equivalent to `x = (int)(x + 1.5)`, so 3 + 1.5 = 4.5 is truncated to 4. A plain `x = x + 1.5` would fail to compile.
Question 23: What is the effect of casting a superclass reference to a subclass type when the actual object is of the superclass type?
- Compile error
- ClassCastException at runtime (Correct answer)
- The cast succeeds and the object gains subclass methods
- null is returned
Correct answer: ClassCastException at runtime
Downcasting a reference to a subclass type when the actual object is not of that subclass results in a ClassCastException at runtime.
Question 24: What is the output of: System.out.println(1 + 2 + "3" + 4 + 5);
- 1235
- 33345
- 3345 (Correct answer)
- 12345
Correct answer: 3345
Left-to-right evaluation: 1+2=3, then 3+"3"="33", then "33"+4="334", then "334"+5="3345".
Question 25: What does the following code print? outer: for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (j == 1) break outer; System.out.print(i + "" + j + " "); } } System.out.print("done");
- 00 done (Correct answer)
- done
- 00 10 20 done
- 00 01 02 done
Correct answer: 00 done
`break outer` terminates the entire outer for-loop when j==1. The only iteration that prints before that happens is i=0, j=0, producing '00'. Then j becomes 1, break outer fires, and execution jumps to the `System.out.print("done")` line.
Question 26: What is the correct way for an inner class to reference the enclosing class's instance variable `x` when the inner class has its own variable `x`?
- this.outer.x
- outer.x
- Outer.this.x (Correct answer)
- super.x
Correct answer: Outer.this.x
`Outer.this` is the qualified `this` reference that refers to the enclosing outer class instance. `super.x` refers to a superclass field, `outer.x` requires an explicit reference variable named `outer`, and `this.outer.x` is not valid Java syntax.
Question 27: What happens when `Thread.start()` is called more than once on the same Thread object?
- The JVM silently ignores the second call
- The thread restarts from the beginning
- A new thread is created automatically
- An IllegalThreadStateException is thrown (Correct answer)
Correct answer: An IllegalThreadStateException is thrown
Once a thread has been started and has terminated, calling `start()` again on the same Thread object throws `IllegalThreadStateException`.
Question 28: What is the try-with-resources statement used for in Java 7+?
- To automatically close resources that implement AutoCloseable (Correct answer)
- To log exceptions automatically
- To handle multiple exceptions in one catch block
- To retry failed operations
Correct answer: To automatically close resources that implement AutoCloseable
Try-with-resources automatically calls `close()` on any `AutoCloseable` resource declared in the try header when the block exits.
Question 29: What is printed by: `Integer a = 127; Integer b = 127; System.out.println(a == b);`?
- true (Correct answer)
- false
- Compilation error
- NullPointerException
Correct answer: true
Integer values between -128 and 127 are cached, so autoboxed instances with the same value share the same reference.
Question 30: Which of the following data types can be used as a switch expression in Java SE 6?
- boolean
- long
- int (Correct answer)
- String
Correct answer: int
In Java SE 6, switch expressions are limited to byte, short, char, and int (and their wrappers, plus enum). String support was added in Java 7. long and boolean are never valid switch types.
Question 31: Which one or ones of the following AWT classes are in charge of carrying out the components layout?
- FlowLayout (Correct answer)
- LayoutManager
- WindowAdapter
- GridBagLayout (Correct answer)
Correct answer: FlowLayout
`FlowLayout` and `GridBagLayout` are concrete layout manager classes in AWT that are responsible for arranging components within a container. `LayoutManager` is an interface that defines the contract for layout managers, not a class that performs layout itself. `WindowAdapter` is an adapter class for handling window events, unrelated to component layout.
Question 32: 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.
Sun Certified Java Programmer (SCJP) - Java SE 6 (CX-310-065)
The SCJP exam validates proficiency in Java SE 6 programming including OOP principles, collections, generics, concurrency, and exception handling. Formerly offered by Sun Microsystems and now superseded by Oracle Certified Professional Java Programmer (OCPJP).
Exam Rules
- You can skip questions and return to them later
- Flag questions for review before submitting
- No feedback shown until you submit the entire exam
- Unanswered questions count as wrong โ answer everything
- 10 pretest questions are mixed in and don't affect your score
- Timer auto-submits when time runs out
- Your progress is auto-saved every 30 seconds