nerdexam
Exams1Z0-803Questions#83
Oracle

1Z0-803 · Question #83

1Z0-803 Question #83: Real Exam Question with Answer & Explanation

The correct answer is A. 10 10. Static variables in Java are class-level variables, meaning there is only one copy shared by all instances of the class and accessible via both class and object references.

Java Basics

Question

What is the result? Class StaticField { static int i = 7; public static void main(String[] args) { StaticFied obj = new StaticField(); obj.i++; StaticField.i++; obj.i++; System.out.println(StaticField.i + " "+ obj.i); } }

Options

  • A10 10
  • B8 9
  • C9 8
  • D7 10

Explanation

Static variables in Java are class-level variables, meaning there is only one copy shared by all instances of the class and accessible via both class and object references.

Common mistakes.

  • B. This answer incorrectly implies that obj.i and StaticField.i would hold different values after increments, failing to recognize that a static field is shared.
  • C. This answer incorrectly assumes that obj.i and StaticField.i are distinct variables after operations, which is false for a static field.
  • D. This answer incorrectly suggests that StaticField.i would remain 7 or that the values would diverge in a way inconsistent with a shared static variable.

Concept tested. Java static variables, class vs instance fields

Reference. https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

Topics

#static members#class variables#object state

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice