Bird
0
0

What will be the output of the following Java code?

medium📝 Predict Output Q4 of 15
Java - Packages and Access Control
What will be the output of the following Java code?
public class Demo {
    public int value = 10;
}

public class Test {
    public static void main(String[] args) {
        Demo d = new Demo();
        System.out.println(d.value);
    }
}
ARuntime error
B0
C10
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Check class declarations

    Both Demo and Test are public top-level classes in the same file.
  2. Step 2: Recall Java source file rules

    Java permits only one public top-level class per .java file, which must match the filename.
  3. Final Answer:

    Compilation error -> Option D
  4. Quick Check:

    Two public classes in one file = compilation error [OK]
Quick Trick: Only one public top-level class per source file [OK]
Common Mistakes:
  • Ignoring the one public class per file rule
  • Focusing only on member access modifiers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes