nerdexam
Oracle

1Z0-809 · Question #137

Given: ``java public class Test { public static void main(String[] args) { Test test = new Test(); System.out.print(isAvailable + " "); test.isAvailable = true; System.out.println(isAvailable); }…

The correct answer is A. false true. Option A is correct because isAvailable is a static field initialized to false, so the first System.out.print outputs false. The line test.isAvailable = true uses an instance reference to modify a static field - which Java permits - meaning it changes the single class-level…

Question

Given:
public class Test {
 public static void main(String[] args) {
 Test test = new Test();
 System.out.print(isAvailable + " ");
 test.isAvailable = true;
 System.out.println(isAvailable);
 }
 public static boolean doStuff() {
 return isAvailable;
 }
 static boolean isAvailable = false;
}
What is the result?

Options

  • Afalse true
  • Btrue false
  • CCompilation fails.
  • Dfalse false
  • Etrue true

How the community answered

(29 responses)
  • A
    72% (21)
  • B
    3% (1)
  • C
    3% (1)
  • D
    7% (2)
  • E
    14% (4)

Explanation

Option A is correct because isAvailable is a static field initialized to false, so the first System.out.print outputs false. The line test.isAvailable = true uses an instance reference to modify a static field - which Java permits - meaning it changes the single class-level value, not some instance-specific copy. The second System.out.println(isAvailable) then reads that updated static value and prints true, giving the output false true.

Why the distractors fail:

  • B (true false): Reverses the order - isAvailable starts false, not true.
  • C (Compilation fails): The code compiles successfully; accessing a static field via an instance reference (test.isAvailable) is legal Java, though it generates an IDE warning.
  • D (false false): Assumes the assignment has no effect, but test.isAvailable = true genuinely mutates the static field.
  • E (true true): Requires isAvailable to already be true on the first print, which it isn't - default is false.

Memory tip: Think of static as "shared by the whole class." Whenever you see a static field accessed or set through an instance (obj.staticField = x), remember it's just syntactic sugar - it modifies the class-level variable, and every subsequent access (via class or instance) will see the new value.

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice