nerdexam
Oracle

1Z0-811 · Question #23

1Z0-811 Question #23: Real Exam Question with Answer & Explanation

The correct answer is D. A compilation error occurs at line n1.. Option D is asserted as correct, but based on the code as presented, this question appears to have a label formatting issue - // line n1 is never shown in the visible source, making it impossible to identify where the stated compilation error occurs. What the code actually does:

Object-Oriented Programming Principles

Question

Given: public class Test { static int var2 = 200; public static void print () { System.out.println (var2); } public void print(int var1) { System.out.println(var1); var2 = var2 + var1; print (); // line n2 } public static void main (String[] args) { Test obj = new Test(); obj.print(100); } } What is the result?

Options

  • A100
  • B200
  • CA compilation error occurs at line n2.
  • DA compilation error occurs at line n1.

Explanation

Option D is asserted as correct, but based on the code as presented, this question appears to have a label formatting issue - // line n1 is never shown in the visible source, making it impossible to identify where the stated compilation error occurs.

What the code actually does: It compiles and runs successfully. obj.print(100) calls the instance method, which prints 100, sets var2 to 300, then calls the no-arg static print() at line n2 - which is perfectly legal (instance methods can invoke static methods by name). Output is 100 then 300.

Why the distractors are wrong:

  • A (100) and B (200) are wrong because the code prints two values (100 and 300), not one.
  • C is wrong because print() at line n2 is valid Java - calling a static method from within an instance method using its bare name is allowed.

Why D as stated is problematic: No // line n1 label appears in the source, and no line in the visible code causes a compilation error. If a hidden version of line n1 had something like print(100) called directly inside main (static context, no object reference), that would fail - you can't call an instance method without an object in a static context.

Memory tip: "Static methods belong to the class, instance methods belong to an object." From a static method (like main), you must use an object reference to reach instance methods - calling instanceMethod(args) directly without obj. is the classic compile error.

Topics

#Method Overloading#Static vs Instance Methods#Method Resolution#Static Context Access

Community Discussion

No community discussion yet for this question.

Full 1Z0-811 Practice