LearnFrenzy
Sign in Take a free test

Classes and Objects · Exercise 2 of 4

Classes and Objects Predict the Output

  • 20 questions
  • Page 2 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 11 Medium

What is the output of the following Java program?

Main.java
interface Discount { double apply(double price); }

public class Main {
    public static void main(String[] args) {
        Discount festival = new Discount() {
            @Override
            public double apply(double price) { return price * 0.8; }
        };
        Discount flat = price -> price - 50;      // the same idea as a lambda
        double p = 500;
        System.out.println(festival.apply(p) + " " + flat.apply(p) + " " + flat.apply(festival.apply(p)));
    }
}
A 400.0 450.0 350.0
B 400.0 450.0 360.0
C 100.0 50.0 50.0
D 400 450 350
Question page & discussion
Question 12 Medium

What is the output of the following Java program?

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

class Task {
    String title;
    boolean done;
    Task(String t) { title = t; }
}

public class Main {
    public static void main(String[] args) {
        Task t = new Task("Write code");
        List<Task> list = new ArrayList<>();
        list.add(t);
        list.add(t);                        // the same object added twice
        list.add(new Task("Test"));
        list.get(0).done = true;
        long done = list.stream().filter(x -> x.done).count();
        System.out.println(list.size() + " " + done + " " + t.done);
    }
}
A 3 2 true
B 3 1 true
C 2 1 true
D 3 2 false
Question page & discussion
Question 13 Easy

What is the output of the following Java program?

Main.java
class Money {
    final long paise;
    Money(long p) { paise = p; }
    Money plus(Money o) { return new Money(paise + o.paise); }
    @Override public String toString() { return "Rs " + paise / 100 + "." + String.format("%02d", paise % 100); }
}

public class Main {
    public static void main(String[] args) {
        Money a = new Money(1050), b = new Money(275);
        Money c = a.plus(b);
        System.out.println(a + " " + b + " " + c);
    }
}
A Rs 10.50 Rs 2.75 Rs 13.25
B Rs 13.25 Rs 2.75 Rs 13.25
C Rs 10.50 Rs 2.75 Rs 10.50
D Rs 1050 Rs 275 Rs 1325
Question page & discussion
Question 14 Medium

What is the output of the following Java program?

Main.java
public class Main {
    static class Pair {
        final String key;
        final int value;
        Pair(String k, int v) { key = k; value = v; }
        @Override public String toString() { return key + "=" + value; }
    }

    static Pair maxOf(Pair[] pairs) {
        Pair best = pairs[0];
        for (Pair p : pairs) if (p.value > best.value) best = p;
        return best;
    }

    public static void main(String[] args) {
        Pair[] ps = {new Pair("a", 3), new Pair("b", 9), new Pair("c", 5)};
        System.out.println(maxOf(ps) + " " + ps.length);
    }
}
A b=9 3
B a=3 3
C c=5 3
D Compilation error
Question page & discussion
Question 15 Medium

What is the output of the following Java program?

Main.java
import java.util.Objects;

public class Main {
    public static void main(String[] args) {
        String a = null, b = "x";
        System.out.println(Objects.equals(a, b) + " " + Objects.equals(a, null) + " " + "x".equals(b) + " " + Objects.toString(a, "none") + " " + Objects.hashCode(a));
    }
}
A false true true none 0
B false false true null 0
C NullPointerException
D false true true null -1
Question page & discussion
Question 16 Medium

What is the output of the following Java program?

Main.java
class Ticket {
    static int created = 0;
    final int no;
    Ticket() { no = ++created; }
}

public class Main {
    public static void main(String[] args) {
        Ticket last = null;
        int same = 0;
        for (int i = 0; i < 5; i++) {
            Ticket t = new Ticket();
            if (t == last) same++;
            last = t;
        }
        System.out.println(Ticket.created + " " + last.no + " " + same);
    }
}
A 5 5 0
B 5 1 4
C 1 1 4
D 5 5 4
Question page & discussion
Question 17 Hard

What is the output of the following Java program?

Main.java
public class Main {
    interface Greeting { String say(String name); }

    static Greeting make(String prefix) {
        int times = 2;                               // effectively final
        class Repeater implements Greeting {
            public String say(String name) { return (prefix + name + " ").repeat(times).trim(); }
        }
        return new Repeater();
    }

    public static void main(String[] args) {
        Greeting g = make("Hi ");
        System.out.println(g.say("Asha") + " | " + make("Bye ").say("Ravi"));
    }
}
A Hi Asha Hi Asha | Bye Ravi Bye Ravi
B Hi Asha | Bye Ravi
C Hi Asha Hi Asha | Hi Ravi Hi Ravi
D Compilation error
Question page & discussion
Question 18 Easy

What is the output of the following Java program?

Main.java
class Person {
    String name;
    int age;
    Person(String n, int a) { name = n; age = a; }
}

public class Main {
    public static void main(String[] args) {
        Person[] people = {new Person("Asha", 21), new Person("Ravi", 34), new Person("Meena", 29)};
        Person oldest = people[0];
        int totalAge = 0;
        for (Person p : people) {
            if (p.age > oldest.age) oldest = p;
            totalAge += p.age;
        }
        System.out.println(oldest.name + " " + totalAge / people.length);
    }
}
A Ravi 28
B Asha 28
C Ravi 84
D Meena 28
Question page & discussion
Question 19 Medium

What is the output of the following Java program?

Main.java
class Car {
    String model = "unknown";
    void setWrong(String model) { model = model; }        // assigns the parameter to itself
    void setRight(String model) { this.model = model; }   // this.model is the field
}

public class Main {
    public static void main(String[] args) {
        Car c = new Car();
        c.setWrong("Nexon");
        System.out.print(c.model + " ");
        c.setRight("Creta");
        System.out.println(c.model);
    }
}
A unknown Creta
B Nexon Creta
C unknown unknown
D Compilation error
Question page & discussion
Question 20 Hard

What is the output of the following Java program?

Main.java
class N {
    N next;
}

public class Main {
    static int count(N start) {
        int c = 0;
        for (N n = start; n != null; n = n.next) c++;
        return c;
    }

    public static void main(String[] args) {
        N a = new N();
        a.next = new N();
        a.next.next = new N();
        a.next.next.next = new N();
        N b = a.next.next;
        a.next = null;
        System.out.println(count(a) + " " + count(b));
    }
}
A 1 2
B 4 2
C 1 1
D 2 2
Question page & discussion
Up next · Exercise 3Find the Error10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links