1Z0-803 · Question #145
Given: class Cake { int model; String flavor; Cake() { model = 0; flavor = "Unknown"; } } public class Test { public static void main(String[] args) { Cake c = new Cake(); bake1(c); System.out.println
The correct answer is C. 1200 Strawberry. This code demonstrates object modification via methods, where changes made to the object through a passed reference persist and are reflected in subsequent print statements.
Question
Options
- A0 Unknown
- B1200 Strawberry
- C1200 Strawberry
- DCompilation fails
How the community answered
(26 responses)- A4% (1)
- B8% (2)
- C77% (20)
- D12% (3)
Why each option
This code demonstrates object modification via methods, where changes made to the object through a passed reference persist and are reflected in subsequent print statements.
The constructor initializes `model` to 0 and `flavor` to 'Unknown', but these values are modified by the `bake1` method before the first print statement.
This choice is textually identical to the correct answer C, but C is specified as the correct option.
After `bake1(c)` is called, the `model` and `flavor` of the `Cake` object `c` are updated to 1200 and 'Strawberry' respectively, making the first print statement output '1200 Strawberry'.
The code is syntactically correct and will compile without errors as `bake2` correctly declares `void` for no return value.
Concept tested: Java object pass by reference, mutability
Source: https://docs.oracle.com/javase/tutorial/java/javaOO/passing.html
Topics
Community Discussion
No community discussion yet for this question.