Bird
0
0

Consider this code:

hard📝 Application Q9 of 15
Java - Static Keyword
Consider this code:
class Sample {
    static int a = 5;
    int b = 10;

    static void modify(Sample s) {
        s.b = 20;
        a = 15;
    }

    public static void main(String[] args) {
        Sample obj = new Sample();
        modify(obj);
        System.out.println(obj.b + ", " + Sample.a);
    }
}
What is the output?
A20, 15
B20, 5
C10, 5
D10, 15
Step-by-Step Solution
Solution:
  1. Step 1: Analyze static and instance variable changes

    Static variable a is changed to 15; instance variable b of object s is changed to 20.
  2. Step 2: Trace output statement

    Printing obj.b and Sample.a outputs "20, 15".
  3. Final Answer:

    20, 15 -> Option A
  4. Quick Check:

    Static and instance variables updated correctly [OK]
Quick Trick: Static accessed by class; instance via object reference [OK]
Common Mistakes:
  • Assuming static variable a remains unchanged
  • Confusing instance variable changes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes