LearnFrenzy
Sign in Take a free test

Constructors and the this Keyword · Exercise 2 of 4

Constructors and the this Keyword Predict the Output

  • 20 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Read each Java program and choose what it prints. Every program was compiled and run on Java 21, so the answers are the real output.

Question 1 Easy

What is the output of the following Java program?

Main.java
class Visitor {
    static int count = 0;
    final int id;
    Visitor() { count++; id = count * 100; }
}

public class Main {
    public static void main(String[] args) {
        Visitor a = new Visitor();
        Visitor b = new Visitor();
        new Visitor();
        System.out.println(a.id + " " + b.id + " " + Visitor.count);
    }
}
A 100 200 3
B 300 300 3
C 100 200 2
D 0 0 3
Question page & discussion
Question 2 Medium

What is the output of the following Java program?

Main.java
class Book {
    String title = "untitled";
    void Book() { title = "Java"; }        // looks like a constructor, but it is a method
}

public class Main {
    public static void main(String[] args) {
        Book b = new Book();
        System.out.print(b.title + " ");
        b.Book();
        System.out.println(b.title);
    }
}
A untitled Java
B Java Java
C untitled untitled
D Compilation error
Question page & discussion
Question 3 Medium

What is the output of the following Java program?

Main.java
class Box {
    Box() { this(1); System.out.print("A "); }
    Box(int w) { this(w, 2); System.out.print("B "); }
    Box(int w, int h) { System.out.print("C(" + w + "," + h + ") "); }
}

public class Main {
    public static void main(String[] args) {
        new Box();
        System.out.println();
        new Box(5);
    }
}
A C(1,2) B A C(5,2) B
B A B C(1,2) B C(5,2)
C C(1,2) C(5,2)
D A B
Question page & discussion
Question 4 Medium

What is the output of the following Java program?

Main.java
class Config {
    int retries = 3;
    String mode = "safe";

    Config() {
        System.out.print(retries + " " + mode + " | ");
        retries = 5;
    }
}

public class Main {
    public static void main(String[] args) {
        Config c = new Config();
        System.out.println(c.retries + " " + c.mode);
    }
}
A 3 safe | 5 safe
B 0 null | 5 safe
C 3 safe | 3 safe
D 5 safe | 5 safe
Question page & discussion
Question 5 Hard

What is the output of the following Java program?

Main.java
class Order {
    { System.out.print("block1 "); }
    int x = init();
    Order() { System.out.print("ctor "); }
    { System.out.print("block2 "); }

    int init() { System.out.print("field "); return 1; }
}

public class Main {
    public static void main(String[] args) {
        new Order();
        new Order();
    }
}
A block1 field block2 ctor block1 field block2 ctor
B ctor block1 field block2 ctor block1 field block2
C block1 block2 field ctor block1 block2 field ctor
D field block1 block2 ctor field block1 block2 ctor
Question page & discussion
Question 6 Medium

What is the output of the following Java program?

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

public class Main {
    public static void main(String[] args) {
        System.out.print("M ");
        new Engine();
        new Engine();
        System.out.println();
    }
}
A M S I C I C
B S M I C I C
C M S I C S I C
D M I C I C S
Question page & discussion
Question 7 Easy

What is the output of the following Java program?

Main.java
class Laptop {
    String brand;
    int ram;

    Laptop(String brand, int ram) {
        brand = brand;           // the parameter, not the field
        this.ram = ram;
    }
}

public class Main {
    public static void main(String[] args) {
        Laptop l = new Laptop("Dell", 16);
        System.out.println(l.brand + " " + l.ram);
    }
}
A null 16
B Dell 16
C null 0
D Compilation error
Question page & discussion
Question 8 Hard

What is the output of the following Java program?

Main.java
import java.util.Arrays;

class Scores {
    int[] values;
    Scores(int... v) { values = v; }
    Scores(Scores other, boolean deep) {
        values = deep ? other.values.clone() : other.values;
    }
}

public class Main {
    public static void main(String[] args) {
        Scores a = new Scores(10, 20);
        Scores shallow = new Scores(a, false), deep = new Scores(a, true);
        a.values[0] = 99;
        System.out.println(Arrays.toString(shallow.values) + " " + Arrays.toString(deep.values));
    }
}
A [99, 20] [10, 20]
B [10, 20] [10, 20]
C [99, 20] [99, 20]
D [10, 20] [99, 20]
Question page & discussion
Question 9 Medium

What is the output of the following Java program?

Main.java
class Value {
    String kind;
    Value(int x) { kind = "int"; }
    Value(long x) { kind = "long"; }
    Value(double x) { kind = "double"; }
    Value(Object x) { kind = "Object"; }
}

public class Main {
    public static void main(String[] args) {
        byte b = 1;
        System.out.println(new Value(b).kind + " " + new Value(5L).kind + " " + new Value(2.5f).kind + " " + new Value("s").kind + " " + new Value('A').kind);
    }
}
A int long double Object int
B Object long double Object Object
C int long Object Object int
D int int double Object Object
Question page & discussion
Question 10 Medium

What is the output of the following Java program?

Main.java
class Account {
    private final String id;
    private final String type;

    Account(String id) { this(id, "SAVINGS"); }
    Account(String id, String type) { this.id = id.toUpperCase(); this.type = type; }

    @Override public String toString() { return id + ":" + type; }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(new Account("ac101") + " " + new Account("ac102", "CURRENT"));
    }
}
A AC101:SAVINGS AC102:CURRENT
B ac101:SAVINGS ac102:CURRENT
C AC101:null AC102:CURRENT
D Compilation error
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links