1Z0-819 Java Fundamentals & Object-Oriented Programming 3 — Questions and Answers
Question 1: What happens if a class implements two interfaces that both declare the same default method with identical signatures?
- The first interface's method is silently chosen
- The class inherits both methods simultaneously
- A compilation error occurs unless the class overrides the method (Correct answer)
- A runtime exception is thrown
Correct answer: A compilation error occurs unless the class overrides the method
When two interfaces provide a default method with the same signature, the implementing class must override the method to resolve the conflict, or a compilation error results.
Question 2: Which statement about abstract methods is correct?
- An abstract method can have a body
- A class with one abstract method must be declared abstract (Correct answer)
- An interface cannot have abstract methods since Java 8
- Abstract methods are implicitly static
Correct answer: A class with one abstract method must be declared abstract
If a class contains even one abstract method it must itself be declared abstract, otherwise a compilation error occurs.
Question 3: What is the result of `'A' + 1` in Java?
- 'B'
- "A1"
- 66 (Correct answer)
- Compilation error
Correct answer: 66
char is promoted to int during arithmetic; 'A' has the value 65, so 65 + 1 = 66.
Question 4: In Java, which of the following correctly describes method overloading?
- A subclass provides a new implementation of an inherited method
- Two methods in the same class share a name but differ in parameter list (Correct answer)
- A method is hidden by a static method in a subclass
- A method changes its return type in a subclass
Correct answer: Two methods in the same class share a name but differ in parameter list
Overloading occurs when multiple methods in the same class have the same name but different parameter types or counts.
Question 5: What is the scope of a variable declared inside a `for` loop initializer?
- The entire method
- The enclosing block only
- The for loop block and its update expression (Correct answer)
- Only the loop condition
Correct answer: The for loop block and its update expression
A variable declared in the `for` initializer is scoped to the entire for statement including its condition, body, and update expressions.
Question 6: Which of the following is true about Java's `this` reference?
- It can be used inside static methods
- It refers to the class object of the current instance
- It is implicitly passed to every instance method (Correct answer)
- It cannot be passed as a method argument
Correct answer: It is implicitly passed to every instance method
`this` is an implicit reference to the current object and is silently available in every instance (non-static) method.
Question 7: What modifier must a field have to be accessible from a static method in the same class?
- final
- static (Correct answer)
- public
- transient
Correct answer: static
Static methods belong to the class rather than an instance, so they can only directly access fields that are also declared static.
What happens if a class implements two interfaces that both declare the same default method with identical signatures?