SCJP Basic 2 — Questions and Answers
Question 1: Which of the following is a valid declaration of a char in Java?
- char c = 'ab';
- char c = 65; (Correct answer)
- char c = "A";
- char c = 3.14;
Correct answer: char c = 65;
A char can be assigned an integer literal within the valid Unicode range (0–65535).
Question 2: What is the default value of an instance variable of type boolean in Java?
- true
- null
- false (Correct answer)
- 0
Correct answer: false
Instance variables of type boolean are initialized to false by default.
Question 3: Which keyword is used to prevent a class from being subclassed?
- static
- abstract
- final (Correct answer)
- private
Correct answer: final
The final keyword on a class prevents any other class from extending it.
Question 4: What will happen if you try to compile a Java file that contains two public classes?
- Only the first class compiles
- Compile error (Correct answer)
- Both classes compile
- Runtime exception
Correct answer: Compile error
A Java source file may contain only one public class, and its name must match the filename.
Question 5: Which of the following correctly declares a two-dimensional array in Java?
- int[2][3] arr;
- int arr[2][3];
- int[][] arr; (Correct answer)
- int arr = new int[2,3];
Correct answer: int[][] arr;
The correct syntax for declaring a 2D array reference is int[][] arr;
Question 6: What is the result of 5 % 3 in Java?
- 1
- 2 (Correct answer)
- 0
- 1.67
Correct answer: 2
The modulus operator returns the remainder; 5 divided by 3 gives remainder 2.
Question 7: Which statement about Java's garbage collector is true?
- It can be forced to run with System.gc() guarantee
- It runs on a fixed schedule
- It reclaims memory from unreachable objects (Correct answer)
- It deletes all local variables after each method call
Correct answer: It reclaims memory from unreachable objects
The garbage collector identifies and reclaims heap memory occupied by objects that are no longer reachable.
Which of the following is a valid declaration of a char in Java?