LearnFrenzy
Sign in Take a free test

OOPs · Topic 7 of 10

static, final, this and super: questions with answers

Class-level vs instance-level members, static initialisation, final/const variables, methods and classes, this for disambiguation and chaining, super for parent members, classmethod and staticmethod in Python.

Questions
45
Timed sets
3
Answers run or computed
36
Your accuracy
–

What this topic covers

8 subtopics
  • static members14
  • final and const11
  • Static initialisation6
  • this5
  • classmethod and staticmethod4
  • super3
  • Class vs instance attributes1
  • Class-level hooks1
  • Beginner 15
  • Intermediate 15
  • Advanced 15

About this topic

Four small keywords cause a large share of OOPs output puzzles. static members belong to the class (one copy shared by all objects); instance members belong to each object. final (Java) and const (C++) stop reassignment – of a variable, an override or a subclass – but not mutation of the object a final reference points to. this is the current object; super reaches the parent's version of a member. Python spells these ideas as class attributes, @classmethod, @staticmethod, self and super().

Free preview

Sample questions

Try each one first, then open the answer for the explanation and why the other options are wrong.

Question 1 static members Beginner Practice Code executed

What does this print?

Java Main.java
public class Main {
    static class Visitor {
        static int total = 0;
        final int id;
        Visitor() { total++; id = total; }
    }

    public static void main(String[] args) {
        Visitor a = new Visitor();
        Visitor b = new Visitor();
        Visitor c = new Visitor();
        System.out.println(Visitor.total + " " + b.id + " " + a.id);
    }
}
3 3 3
3 1 1
1 1 1
3 2 1
Show answer and explanation
Correct answer D

Explanation

total is static, so all Visitors share one counter; id is an instance field, so each object keeps the value it had when it was created.

Why the other options are wrong

  • Aid is copied at construction time.
  • BEach visitor gets the next number.
  • CThe static counter is shared, not per object.
Question 2 Static initialisation Intermediate Practice Code executed

Static fields are initialised in the order they appear. What does this print?

Java Main.java
public class Main {
    static int a = Main.b + 1;
    static int b = 5;
    static int c = b + 1;

    public static void main(String[] args) {
        System.out.println(a + " " + b + " " + c);
    }
}
Compile error
6 5 6
0 5 6
1 5 6
Show answer and explanation
Correct answer D

Explanation

When a is initialised, b still holds its default 0, so a = 1. (Writing just b there is an "illegal forward reference" compile error; qualifying it as Main.b sidesteps the check and exposes the default value.)

Why the other options are wrong

  • AThe qualified name compiles.
  • Bb is still 0 when a is computed.
  • Ca adds 1.
Question 3 Static initialisation Advanced Practice Code executed

What goes wrong when this runs?

Java Main.java
public class Main {
    static class Rates {
        static final int DIVISOR = load();
        static final int RATE = 100 / DIVISOR;
        static int load() { return 0; }
    }

    public static void main(String[] args) {
        System.out.println(Rates.RATE);
    }
}
It prints 0
Division by zero inside static initialisation becomes ExceptionInInitializerError, and the class is unusable afterwards
It prints Infinity
It fails to compile
Show answer and explanation
Correct answer B

Explanation

An exception thrown while initialising a class is wrapped in ExceptionInInitializerError; later uses of the class throw NoClassDefFoundError. Keep static initialisers simple.

Why the other options are wrong

  • AInteger division by zero throws.
  • COnly floating-point division gives Infinity.
  • DIt compiles.
42 more questions in the timed setsSame format as the samples, with a timer, a question palette and a full review at the end.
Start practising

OOPs

All 10 topics

Topics, levels and full mocks
  1. 01 Classes, Objects and Constructors45 questions · 3 levels
  2. 02 Encapsulation and Access Modifiers45 questions · 3 levels
  3. 03 Inheritance45 questions · 3 levels
  4. 04 Polymorphism45 questions · 3 levels
  5. 05 Abstract Classes and Interfaces45 questions · 3 levels
  6. 06 Object Lifecycle and Copying45 questions · 3 levels
  7. 07 static, final, this and super45 questions · 3 levels
  8. 08 Exception Handling45 questions · 3 levels
  9. 09 SOLID and Class Relationships45 questions · 3 levels
  10. 10 Design Patterns45 questions · 3 levels

Search LearnFrenzy

Esc

Quick links