1Z0-819 Java Fundamentals & Object-Oriented Programming 4 β Questions and Answers
Question 1: Which of the following correctly describes polymorphism in Java?
- A class can extend multiple classes
- A reference type can behave differently depending on the actual object it points to at runtime (Correct answer)
- A method can return different types in different invocations
- A field can hold multiple values simultaneously
Correct answer: A reference type can behave differently depending on the actual object it points to at runtime
Polymorphism allows a supertype reference to invoke overridden methods on subtype objects, with the actual behavior determined at runtime.
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 when a class is instantiated.
Question 3: Given `List<String> list = new ArrayList<>(); list.add("x");`, which statement causes a compilation error?
- list.get(0)
- list.size()
- list.add(1) (Correct answer)
- list.remove("x")
Correct answer: list.add(1)
`list.add(1)` attempts to add an Integer to a List<String>, violating the generic type constraint and causing a compilation error.
Question 4: Which statement about constructors in Java is correct?
- Constructors can have a return type of void
- Constructors are inherited by subclasses
- Constructors can be overloaded (Correct answer)
- Constructors cannot call other constructors
Correct answer: Constructors can be overloaded
Multiple constructors with different parameter lists (overloading) are allowed and commonly used to provide flexible object initialization.
Question 5: What does the `instanceof` operator return when the left operand's type is a subtype of the right operand?
- false
- true (Correct answer)
- Compilation error
- ClassCastException
Correct answer: true
`instanceof` returns true whenever the object on the left is an instance of the class or any of its subtypes on the right.
Question 6: A Java enum implicitly extends which class?
- java.lang.Object
- java.lang.Enum (Correct answer)
- java.lang.Comparable
- java.io.Serializable
Correct answer: java.lang.Enum
All Java enums implicitly extend `java.lang.Enum`, which is why they cannot extend any other class.
Question 7: What is the effect of declaring a method as `static` in an interface (Java 8+)?
- Implementing classes inherit the static method
- The method can only be called via the interface name (Correct answer)
- It becomes an abstract method with a default body
- It is automatically synchronized
Correct answer: The method can only be called via the interface name
Static interface methods are not inherited by implementing classes or sub-interfaces and must be called through the interface type itself.
Which of the following correctly describes polymorphism in Java?