Sun Certified Java Programmer (SCJP) - Java SE 6 (CX-310-065) — Questions and Answers
Question 1: Which class in `java.util.concurrent.atomic` provides a thread-safe integer that supports atomic increment operations?
- AtomicInteger (Correct answer)
- SynchronizedInteger
- ThreadSafeInteger
- ConcurrentInt
Correct answer: AtomicInteger
`AtomicInteger` uses low-level compare-and-swap (CAS) hardware instructions to perform atomic operations without explicit synchronization.
Question 2: Can a non-static inner class declare static members?
- Yes, but only static methods, not static fields
- Yes, but only static final constant fields (Correct answer)
- Yes, it can declare both static fields and static methods
- No, non-static inner classes cannot have any static members
Correct answer: Yes, but only static final constant fields
In Java, a non-static inner class may declare static final constant fields (compile-time constants) but cannot declare other static members. This is because the inner class is associated with an outer instance and full static context would be ambiguous.
Question 3: What is the output of calling a method on an object reference declared as the superclass type but pointing to a subclass instance?
- Superclass method runs
- NullPointerException
- Compilation error
- Subclass method runs due to dynamic dispatch (Correct answer)
Correct answer: Subclass method runs due to dynamic dispatch
Java uses dynamic method dispatch (runtime polymorphism), so the overridden subclass method is called.
Question 4: 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 5 to x by rounding up
- Assigns 4 to x after an implicit narrowing cast (Correct answer)
- Assigns 4.5 to x
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 5: What will be the result of 7 % 3 in Java?
- 2.33
- 2
- 0
- 1 (Correct answer)
Correct answer: 1
The modulo operator returns the remainder: 7 divided by 3 is 2 with remainder 1.
Question 6: Which of the following is a valid way to create an anonymous class in Java?
- Runnable r = Runnable() { public void run() {} };
- Runnable r = new class() implements Runnable { public void run() {} };
- Runnable r = new Runnable() { public void run() {} }; (Correct answer)
- anonymous Runnable r = new Runnable() { public void run() {} };
Correct answer: Runnable r = new Runnable() { public void run() {} };
Anonymous classes are created with `new InterfaceOrClass() { /* body */ }`. There is no `anonymous` keyword, no `class` keyword in the syntax, and no parentheses-only form without `new`.
Question 7: Which access modifier allows a method to be accessed within the same package and by subclasses in other packages?
- public
- private
- default (no modifier)
- protected (Correct answer)
Correct answer: protected
`protected` grants access within the same package and to subclasses regardless of package.
Question 8: In Java, can a class implement more than one interface?
- Only if interfaces share a common parent
- Yes, with a comma-separated list (Correct answer)
- Only in Java 8+
- No, only one interface is allowed
Correct answer: Yes, with a comma-separated list
A Java class can implement multiple interfaces by listing them with commas: `class Foo implements A, B, C`.
Question 9: What happens when the following code is compiled? void test() { return; System.out.println("hello"); }
- It compiles and 'hello' is never printed
- It prints 'hello' because println is always executed
- It fails to compile due to unreachable code (Correct answer)
- It compiles and throws a RuntimeException
Correct answer: It fails to compile due to unreachable code
The Java compiler detects unreachable statements and reports a compile-time error. The `System.out.println` after an unconditional `return` can never execute, so the class will not compile.
Question 10: What is the difference between String.equals() and String.equalsIgnoreCase()?
- There is no difference between the two methods
- equals() works only with literals; equalsIgnoreCase() works with all Strings
- equals() compares references; equalsIgnoreCase() compares content
- equals() is case-sensitive; equalsIgnoreCase() ignores letter case differences (Correct answer)
Correct answer: equals() is case-sensitive; equalsIgnoreCase() ignores letter case differences
equals() performs a case-sensitive content comparison, while equalsIgnoreCase() treats uppercase and lowercase letters as equal.
Question 11: 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.Inner i = Outer.new Inner();
- Outer o = new Outer(); Outer.Inner i = o.new Inner(); (Correct answer)
- Inner i = 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 12: In Java, what is the result if a subclass defines a static method with the same signature as a static method in its superclass?
- Runtime polymorphism occurs
- Method hiding occurs (Correct answer)
- The superclass method is deleted
- Compilation error
Correct answer: Method hiding occurs
When a subclass defines a static method with the same signature as the superclass, it hides the superclass method rather than overriding it.
Question 13: What is the purpose of `Thread.join()` in Java?
- To make the current thread wait until the specified thread finishes (Correct answer)
- To add a thread to a thread pool
- To merge two threads into one
- To synchronize two threads
Correct answer: To make the current thread wait until the specified thread finishes
`Thread.join()` causes the calling thread to block until the thread on which `join()` was called completes its execution.
Question 14: Which of the following are keywords used in Java?
- boolean (Correct answer)
- super (Correct answer)
- array
- Integer
Correct answer: boolean
Java keywords are reserved words with predefined meanings that cannot be used as identifiers. `boolean` is a primitive data type keyword used to declare variables that can hold `true` or `false` values. `super` is a keyword used to refer to the immediate parent class object or to invoke a superclass's constructor or method. `Integer` is a class in the `java.lang` package, and `array` is a general concept, not a Java keyword.
Question 15: Which keyword is used to prevent method overriding in Java?
- final (Correct answer)
- synchronized
- static
- abstract
Correct answer: final
Declaring a method as 'final' prevents it from being overridden in any subclass.
Question 16: Which of the following is NOT a valid exception handling structure in Java?
- try-catch
- catch-finally (without try) (Correct answer)
- try-finally
- try-catch-finally
Correct answer: catch-finally (without try)
A `catch` or `finally` block cannot exist without a preceding `try` block — Java requires `try` to begin an exception handling structure.
Question 17: What does autoboxing in Java do?
- Wraps exceptions in RuntimeException
- Converts arrays to ArrayLists automatically
- Automatically converts primitive types to their wrapper class equivalents (Correct answer)
- Boxes multiple return values into a tuple
Correct answer: Automatically converts primitive types to their wrapper class equivalents
Autoboxing is the automatic conversion of primitives (like `int`) to their wrapper classes (like `Integer`) when needed by the context.
Question 18: What is the value of `byte b = (byte) 130;` in Java?
- Compilation error
- 127
- -126 (Correct answer)
- 130
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 19: Which assignment causes a compile-time error?
- byte b = 100L; (Correct answer)
- float f = 100L;
- int i = 'A';
- long l = 100;
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 20: Which interface does `PriorityQueue` implement that defines its ordering behavior?
- Comparable
- Queue (Correct answer)
- SortedSet
- Comparator
Correct answer: Queue
PriorityQueue implements the Queue interface; ordering is determined by the natural order of elements or a Comparator passed at construction.
Question 21: What method must be overridden when implementing the `Runnable` interface?
- execute()
- start()
- run() (Correct answer)
- launch()
Correct answer: run()
The `Runnable` interface has a single abstract method `run()`, which contains the code the thread will execute.
Question 22: What is the result of `System.out.println(5 >> 1);`?
- 3
- 10
- 2 (Correct answer)
- 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 23: 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`?
- super.x
- this.outer.x
- outer.x
- Outer.this.x (Correct answer)
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 24: Which Java I/O class is used for object serialization (writing objects to a stream)?
- ObjectOutputStream (Correct answer)
- PrintStream
- FileWriter
- DataOutputStream
Correct answer: ObjectOutputStream
`ObjectOutputStream` serializes Java objects to a byte stream; the object's class must implement `Serializable`.
Question 25: Which of the following is a valid use of a bounded type parameter in Java generics?
- <T extends String, Integer>
- <T super Object>
- <T implements Serializable>
- <T extends Comparable<T>> (Correct answer)
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 26: 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");
- done
- 00 01 02 done
- 00 done (Correct answer)
- 00 10 20 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 27: What is the output of `System.out.printf("%05.1f%n", 3.5);`?
- 0003.5
- 3.5
- 003.5 (Correct answer)
- 3.5
Correct answer: 003.5
The format `%05.1f` means: width 5, zero-padded, 1 decimal place. '3.5' formatted to 1 decimal is '3.5' (3 chars). To reach width 5, two zeros are prepended: '003.5'. The `%n` emits a platform-specific newline.
Question 28: In Java, which method retrieves an enum constant by its name string?
- Enum.parse(name)
- EnumType.valueOf(name) (Correct answer)
- Enum.find(name)
- EnumType.fromString(name)
Correct answer: EnumType.valueOf(name)
`EnumType.valueOf("CONSTANT_NAME")` returns the enum constant with the specified name, throwing `IllegalArgumentException` if not found.
Question 29: Which statement about `java.util.Calendar` is TRUE in Java SE 6?
- Calendar.MONTH is zero-based (January = 0) (Correct answer)
- Calendar.DAY_OF_MONTH is zero-based (first day = 0)
- You instantiate Calendar directly with new Calendar()
- Calendar instances are immutable
Correct answer: Calendar.MONTH is zero-based (January = 0)
Calendar.MONTH is notoriously zero-based: January = 0, February = 1, … December = 11. DAY_OF_MONTH is 1-based. Calendar is mutable, and you cannot instantiate it directly — you use Calendar.getInstance() because the constructor is protected.
Question 30: What is thread starvation in Java?
- A thread waiting for I/O indefinitely
- A thread being perpetually denied CPU access due to other higher-priority threads (Correct answer)
- A thread that never calls wait()
- A thread consuming too much memory
Correct answer: A thread being perpetually denied CPU access due to other higher-priority threads
Thread starvation occurs when a thread cannot gain regular access to shared resources because other threads are always prioritized.
Question 31: Which of the following initialization strategies for the array "dayhigh" with seven values is correct?
- int dayhigh[] = { 24, 23, 24, 25, 25, 23, 21 }; (Correct answer)
- int dayhigh = { 24, 23, 24, 25, 25, 23, 21 };
- int dayhigh [] = new int[24, 23, 24, 25, 25, 23, 21];
- int[] dayhigh = { 24, 23, 24, 25, 25, 23, 21 }; (Correct answer)
Correct answer: int dayhigh[] = { 24, 23, 24, 25, 25, 23, 21 };
In Java, an array can be initialized directly at the time of declaration using an initializer list enclosed in curly braces. The syntax `int dayhigh[]` correctly declares an integer array named `dayhigh`. The `{ 24, 23, 24, 25, 25, 23, 21 }` part then initializes this array with the specified seven integer values, with the compiler inferring the array's size from the list.
Question 32: What is the time complexity of `contains()` on a `HashSet` in the average case?
- O(1) (Correct answer)
- O(n log n)
- O(log n)
- O(n)
Correct answer: O(1)
HashSet.contains() is O(1) on average because it computes the hash code to directly locate the bucket, then checks equality.
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
These practice questions were written with AI tools. They are not real exam questions. We have not yet matched this exam's facts to an official source, so confirm fees, dates and rules with the exam body. Not reviewed by a subject-matter expert. Editorial policy