1Z0-811 Java Language Fundamentals 3 — Questions and Answers
Question 1: What is the range of a Java 'byte' data type?
- -128 to 127 (Correct answer)
- 0 to 255
- -256 to 255
- -32768 to 32767
Correct answer: -128 to 127
The byte type is an 8-bit signed integer with a range of -128 to 127.
Question 2: What is the output of the following? int a = 10; int b = 3; System.out.println(a % b);
- 3
- 1 (Correct answer)
- 0
- 3.33
Correct answer: 1
The % operator returns the remainder; 10 divided by 3 is 3 with a remainder of 1.
Question 3: Which statement about 'var' in Java 11 is correct?
- var can be used for method parameters
- var can be used for local variable type inference (Correct answer)
- var is a reserved keyword in Java 11
- var can be used for instance variable declarations
Correct answer: var can be used for local variable type inference
In Java 10+, 'var' enables local variable type inference, allowing the compiler to infer the type from the initializer.
Question 4: What happens when you compare two String objects using == in Java?
- It compares their content
- It compares their memory references (Correct answer)
- It always returns true if content is the same
- It throws a NullPointerException
Correct answer: It compares their memory references
The == operator on objects compares references, not content; use .equals() to compare String content.
Question 5: What is the result of the expression: (int) 9.99?
- 10
- 9 (Correct answer)
- 9.99
- Compilation error
Correct answer: 9
Casting a double to int truncates (not rounds) the decimal part, so (int)9.99 yields 9.
Question 6: Which access modifier allows access only within the same class?
- protected
- default (no modifier)
- public
- private (Correct answer)
Correct answer: private
The 'private' access modifier restricts access to only the class in which the member is declared.
Question 7: What is printed by this code? for (int i = 0; i < 3; i++) { if (i == 1) continue; System.out.print(i + " "); }
- 0 1 2
- 0 2 (Correct answer)
- 1 2
- 0 1
Correct answer: 0 2
The 'continue' statement skips the current iteration when i==1, so only 0 and 2 are printed.
What is the range of a Java 'byte' data type?