SCJP Basic 5 — Questions and Answers
Question 1: Which of the following correctly describes an abstract class in Java?
- It cannot have any methods
- It can be instantiated directly
- It may contain abstract and concrete methods (Correct answer)
- It must implement all interface methods
Correct answer: It may contain abstract and concrete methods
An abstract class can have a mix of abstract (no body) and concrete (with body) methods.
Question 2: What is the result of comparing two String objects with == in Java?
- Always true if contents are equal
- Compares object references, not content (Correct answer)
- Causes a compile error
- Throws NullPointerException
Correct answer: Compares object references, not content
== on objects checks reference equality (same memory address), not the content of the strings.
Question 3: Which of the following is true about a Java interface before Java 8?
- It can have instance variables
- It can have constructors
- All methods are implicitly public and abstract (Correct answer)
- It can extend multiple abstract classes
Correct answer: All methods are implicitly public and abstract
Prior to Java 8, interface methods were implicitly public and abstract and could not have implementations.
Question 4: What is the scope of a variable declared inside a for-loop initializer?
- The entire class
- The entire method
- Only within the for-loop block (Correct answer)
- The entire package
Correct answer: Only within the for-loop block
A variable declared in a for-loop initializer is scoped to the for-loop block only.
Question 5: Which of the following statements about Java arrays is true?
- Arrays can grow dynamically
- Arrays in Java are objects stored on the heap (Correct answer)
- Array indices start at 1
- Arrays can hold multiple types simultaneously
Correct answer: Arrays in Java are objects stored on the heap
Java arrays are objects allocated on the heap with a fixed size determined at creation.
Question 6: What is the output of the following code? int x = 5; System.out.println(x++);
- 6
- 5 (Correct answer)
- Compile error
- 4
Correct answer: 5
The post-increment operator returns the original value (5) before incrementing x to 6.
Question 7: Which of the following correctly describes encapsulation in Java?
- Inheriting methods from a parent class
- Hiding internal state and requiring access through public methods (Correct answer)
- Using multiple classes to define one object
- Preventing a class from being instantiated
Correct answer: Hiding internal state and requiring access through public methods
Encapsulation bundles data (fields) with methods that operate on it and restricts direct access using access modifiers.
Which of the following correctly describes an abstract class in Java?