LearnFrenzy
Sign in Take a free test

OOPs · Topic 1 of 10

Classes, Objects and Constructors: questions with answers

Classes and objects, references, constructors and overloading, this() chaining, initialisation order, copy constructors, instance vs class attributes and object equality.

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

What this topic covers

18 subtopics
  • Constructors9
  • Initialisation order4
  • Methods3
  • Object equality3
  • References3
  • Value objects3
  • Attributes2
  • Basics2
  • Class vs instance attributes2
  • Constructor chaining2
  • Copy constructor2
  • Object methods2
Show all 18 subtopics
  • Objects in memory2
  • Overloading2
  • Classes at runtime1
  • Design1
  • Nested classes1
  • State and behaviour1
  • Beginner 15
  • Intermediate 15
  • Advanced 15

About this topic

A class is a blueprint; an object is one instance of it with its own state. Constructors set that state up, and most output puzzles test when each piece of initialisation code runs: field initialisers, initialiser blocks, this(…) chaining, member initialiser lists. Remember that in Java and Python a variable holds a reference to an object, so two variables can point to the same object. Every program here was run to produce its answer.

Free preview

Sample questions

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

Question 1 Constructors Beginner Practice Code executed

The class declares no constructor. What does this print?

Java Main.java
public class Main {
    static class Point {
        int x;
        boolean visible;
        String label;
    }

    public static void main(String[] args) {
        Point p = new Point();
        System.out.println(p.x + " " + p.visible + " " + p.label);
    }
}
garbage false null
0 false ""
Compile error: fields not initialised
0 false null
Show answer and explanation
Correct answer D

Explanation

Without any constructor, Java supplies a default no-argument one, and fields get default values: 0, false and null. (Local variables, by contrast, must be assigned before use.)

Why the other options are wrong

  • AJava never exposes uninitialised memory.
  • BReference fields default to null.
  • CFields are initialised automatically.
Question 2 Initialisation order Intermediate Practice Code executed

A field initialiser, an instance initialiser block and a constructor all print. What does this print?

Java Main.java
public class Main {
    static int log(String s) { System.out.print(s); return 0; }

    static class Widget {
        Widget() { log("C"); }
        { log("I"); }
        int size = log("F");
    }

    public static void main(String[] args) {
        new Widget();
    }
}
FIC
IFC
CFI
CIF
Show answer and explanation
Correct answer B

Explanation

Field initialisers and instance initialiser blocks run in the order they appear in the source, before the constructor body – so I (written first) then F, then C. The position of the constructor in the source does not matter.

Why the other options are wrong

  • AOrder follows the source text: the block comes before the field.
  • CInitialisers run before the constructor body.
  • DThe constructor body runs last.
Question 3 Initialisation order Advanced Practice Code executed

Two objects are created. What does this print?

Java Main.java
public class Main {
    static class Engine {
        static { System.out.print("S "); }
        { System.out.print("I "); }
        Engine() { System.out.print("C "); }
    }

    public static void main(String[] args) {
        System.out.print("main ");
        new Engine();
        new Engine();
    }
}
main I S C I C
main S I C I C
main S I C S I C
S main I C I C
Show answer and explanation
Correct answer B

Explanation

The static block runs once, when the class is first initialised – here on the first new Engine(), after "main" has printed. Instance blocks and the constructor run for every object.

Why the other options are wrong

  • AStatic initialisation comes before any instance code.
  • CStatic blocks run only once.
  • DEngine is initialised lazily, on first use.
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