1Z0-819 Java Fundamentals & Object-Oriented Programming 2 — Questions and Answers
Question 1: What is the output of: `System.out.println(10 / 3);`?
- 3.333
- 3 (Correct answer)
- 3.0
- Compilation error
Correct answer: 3
Integer division truncates the decimal part, so 10 / 3 yields 3.
Question 2: Which keyword prevents a class from being subclassed in Java?
- static
- abstract
- final (Correct answer)
- sealed
Correct answer: final
The `final` keyword on a class declaration prevents any other class from extending it.
Question 3: Given `String s = null; System.out.println(s instanceof String);`, what is printed?
- true
- false (Correct answer)
- NullPointerException
- Compilation error
Correct answer: false
`instanceof` always returns false when the left operand is null, without throwing an exception.
Question 4: Which access modifier makes a member visible only within its own class?
- protected
- package-private (no modifier)
- private (Correct answer)
- public
Correct answer: private
`private` restricts visibility strictly to the declaring class itself.
Question 5: What does the `super()` call inside a constructor do?
- Calls the superclass's static initializer
- Invokes the superclass constructor (Correct answer)
- Creates a new superclass instance
- Calls the current class's no-arg constructor
Correct answer: Invokes the superclass constructor
`super()` must be the first statement in a constructor and delegates to the matching superclass constructor.
Question 6: Which of the following is a valid declaration of a two-dimensional int array in Java?
- int[2][3] arr;
- int arr[2][3];
- int[][] arr; (Correct answer)
- int arr[][];
Correct answer: int[][] arr;
The canonical style places both bracket pairs after the type: `int[][] arr;`.
Question 7: What is autoboxing in Java?
- Automatic conversion of a String to an int
- Automatic conversion of a primitive to its wrapper type (Correct answer)
- Automatic upcasting of a subtype to a supertype
- Automatic generation of getter and setter methods
Correct answer: Automatic conversion of a primitive to its wrapper type
Autoboxing is the automatic conversion the compiler performs from a primitive (e.g., int) to its corresponding wrapper class (e.g., Integer).
What is the output of: `System.out.println(10 / 3);`?