SCJP Basic 3 — Questions and Answers
Question 1: What is the range of a Java int data type?
- -2^31 to 2^31 - 1 (Correct answer)
- -2^15 to 2^15 - 1
- 0 to 2^32 - 1
- -2^63 to 2^63 - 1
Correct answer: -2^31 to 2^31 - 1
Java's int is a 32-bit signed integer, ranging from -2,147,483,648 to 2,147,483,647.
Question 2: Which of the following is NOT a valid Java identifier?
- _myVar
- $value
- 2ndValue (Correct answer)
- myValue2
Correct answer: 2ndValue
Java identifiers cannot begin with a digit; 2ndValue is invalid.
Question 3: 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 only accessible within the method
- The variable is stored on the stack
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 4: What is the output of: System.out.println(10 / 3); in Java?
- 3.33
- 3 (Correct answer)
- 3.0
- Compile error
Correct answer: 3
Integer division truncates the decimal; 10 / 3 = 3 in Java.
Question 5: Which access modifier makes a member accessible only within the same class?
- protected
- default
- private (Correct answer)
- public
Correct answer: private
The private modifier restricts access to within the declaring class only.
Question 6: What is autoboxing in Java?
- Converting a String to a primitive
- Automatically converting a primitive to its wrapper type (Correct answer)
- Casting a double to an int
- Automatically growing an array
Correct answer: Automatically converting a primitive to its wrapper type
Autoboxing is the automatic conversion from a primitive type (e.g., int) to its corresponding wrapper class (e.g., Integer).
Question 7: Which of the following is a correct way to create a String object in Java?
- String s = new String;
- String s = 'Hello';
- String s = "Hello"; (Correct answer)
- string s = "Hello";
Correct answer: String s = "Hello";
String literals are enclosed in double quotes, and String (capital S) is the correct class name.
What is the range of a Java int data type?