1Z0-819 Java Fundamentals & Object-Oriented Programming 5 — Questions and Answers
Question 1: Which of the following is an example of a widening primitive conversion?
- double to int
- long to short
- int to long (Correct answer)
- float to byte
Correct answer: int to long
Converting int to long is widening because long can represent all values an int can hold without data loss.
Question 2: What is the output of: `System.out.println("5" + 2 + 3);`?
- 10
- 523 (Correct answer)
- 55
- 5 + 2 + 3
Correct answer: 523
String concatenation is left-associative: `"5" + 2` yields `"52"`, then `"52" + 3` yields `"523"`.
Question 3: Which OOP principle does encapsulation primarily support?
- Code reuse through inheritance
- Hiding internal implementation details behind a public interface (Correct answer)
- Allowing a class to take many forms
- Deferring method implementation to subclasses
Correct answer: Hiding internal implementation details behind a public interface
Encapsulation bundles data and methods together and restricts direct access to internal state, exposing only a controlled public API.
Question 4: In Java, what is a 'has-a' relationship implemented through?
- Inheritance (extends)
- Interface implementation (implements)
- Composition (field reference) (Correct answer)
- Method overriding
Correct answer: Composition (field reference)
A 'has-a' relationship is modeled by composition: one class holds a reference to another class as a field.
Question 5: What happens when you try to instantiate an abstract class directly with `new AbstractClass()`?
- A runtime exception is thrown
- An object is created with null fields
- A compilation error occurs (Correct answer)
- The first concrete subclass is instantiated instead
Correct answer: A compilation error occurs
The Java compiler rejects any attempt to instantiate an abstract class, producing a compilation error.
Question 6: Which of the following best describes the difference between `==` and `.equals()` for String objects?
- `==` compares content; `.equals()` compares references
- `==` compares references; `.equals()` compares content (Correct answer)
- Both compare references by default
- Both compare content by default
Correct answer: `==` compares references; `.equals()` compares content
`==` tests reference equality (same object in memory), while `String.equals()` tests value equality (same character sequence).
Question 7: What is the purpose of a no-argument (no-arg) constructor?
- It is required for all Java classes
- It enables object creation without providing initial field values (Correct answer)
- It prevents subclasses from adding constructors
- It automatically initializes all fields to their maximum values
Correct answer: It enables object creation without providing initial field values
A no-arg constructor allows a class to be instantiated without any arguments, which is also required by many frameworks for reflection-based object creation.
Which of the following is an example of a widening primitive conversion?