LearnFrenzy
Sign in Take a free test

Static and Final Keywords · Exercise 2 of 4

Static and Final Keywords 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 Clicker {
    static int all = 0;
    int mine = 0;
    void click() { all++; mine++; }
}

public class Main {
    public static void main(String[] args) {
        Clicker a = new Clicker(), b = new Clicker();
        a.click(); b.click(); b.click(); a.click(); a.click();
        System.out.println(a.mine + " " + b.mine + " " + Clicker.all);
    }
}
A 3 2 5
B 5 5 5
C 3 2 3
D 2 3 5
Question page & discussion
Question 2 Medium

What is the output of the following Java program?

Main.java
class Theme {
    static String color = "light";
}

public class Main {
    public static void main(String[] args) {
        Theme t1 = new Theme(), t2 = new Theme();
        t1.color = "dark";
        System.out.println(t2.color + " " + Theme.color);
    }
}
A dark dark
B light dark
C light light
D dark light
Question page & discussion
Question 3 Medium

What is the output of the following Java program?

Main.java
class Db {
    static int connections;
    static {
        connections = 5;
        System.out.print("init ");
    }
    Db() { connections--; }
}

public class Main {
    public static void main(String[] args) {
        new Db(); new Db(); new Db();
        System.out.println(Db.connections);
    }
}
A init 2
B init init init 2
C init 5
D 2
Question page & discussion
Question 4 Hard

What is the output of the following Java program?

Main.java
public class Main {
    static int x = 10;
    static { x += 5; System.out.print("A" + x + " "); }
    static int y = x * 2;
    static { y++; System.out.print("B" + y + " "); }

    public static void main(String[] args) {
        System.out.println("main " + x + " " + y);
    }
}
A A15 B31 main 15 31
B main 15 31
C A15 B30 main 15 30
D B31 A15 main 15 31
Question page & discussion
Question 5 Hard

What is the output of the following Java program?

Main.java
class Animal {
    static String kind() { return "animal"; }
    String sound() { return "..."; }
}

class Dog extends Animal {
    static String kind() { return "dog"; }
    @Override String sound() { return "woof"; }
}

public class Main {
    public static void main(String[] args) {
        Animal a = new Dog();
        System.out.println(Animal.kind() + " " + Dog.kind() + " " + a.sound());
    }
}
A animal dog woof
B dog dog woof
C animal animal ...
D animal dog ...
Question page & discussion
Question 6 Easy

What is the output of the following Java program?

Main.java
import java.util.ArrayList;
import java.util.List;

public class Main {
    static final List<Integer> SCORES = new ArrayList<>();

    public static void main(String[] args) {
        SCORES.add(90);
        SCORES.add(75);
        SCORES.remove(Integer.valueOf(90));
        SCORES.add(0, 60);
        System.out.println(SCORES + " " + SCORES.size());
    }
}
A [60, 75] 2
B [] 0
C Compilation error
D [90, 75, 60] 3
Question page & discussion
Question 7 Hard

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        String full = "JavaPro";
        final String a = "Java";
        String b = "Java";
        String c = a + "Pro";
        String d = b + "Pro";
        System.out.println((c == full) + " " + (d == full) + " " + d.equals(full) + " " + (d.intern() == full));
    }
}
A true false true true
B false false true true
C true true true true
D false false true false
Question page & discussion
Question 8 Medium

What is the output of the following Java program?

Main.java
class Session implements AutoCloseable {
    static int open = 0;
    final String user;
    Session(String user) { this.user = user; open++; }
    @Override public void close() { open--; }
}

public class Main {
    public static void main(String[] args) {
        try (Session a = new Session("asha"); Session b = new Session("ravi")) {
            System.out.print(Session.open + " " + a.user.length() + b.user.length() + " ");
        }
        System.out.println(Session.open);
    }
}
A 2 44 0
B 2 8 0
C 2 44 2
D 0 44 0
Question page & discussion
Question 9 Medium

What is the output of the following Java program?

Main.java
public class Main {
    static class Ticket {
        private static int next = 1;
        final int no = next++;
    }

    public static void main(String[] args) {
        Ticket a = new Ticket(), b = new Ticket();
        Main.Ticket c = new Main.Ticket();
        System.out.println(a.no + " " + b.no + " " + c.no + " " + Ticket.next);
    }
}
A 1 2 3 4
B 1 1 1 2
C 0 1 2 3
D 1 2 3 3
Question page & discussion
Question 10 Medium

What is the output of the following Java program?

Main.java
public class Main {
    static void update(final StringBuilder sb, final int n) {
        sb.append("-").append(n);       // allowed: changes the object
        // sb = new StringBuilder();    // not allowed: reassigns the parameter
        // n++;                          // not allowed
    }

    public static void main(String[] args) {
        StringBuilder log = new StringBuilder("start");
        for (int i = 1; i <= 3; i++) update(log, i * i);
        System.out.println(log);
    }
}
A start-1-4-9
B start
C start-1-2-3
D Compilation error
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links