LearnFrenzy
Sign in Take a free test

Encapsulation · Exercise 2 of 4

Encapsulation 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
class Window {
    private int start = 9, end = 17;

    boolean reschedule(int s, int e) {
        if (s < 0 || e > 24 || s >= e) return false;
        start = s; end = e;
        return true;
    }

    int hours() { return end - start; }
}

public class Main {
    public static void main(String[] args) {
        Window w = new Window();
        System.out.println(w.reschedule(18, 22) + " " + w.hours() + " " + w.reschedule(20, 19) + " " + w.hours() + " " + w.reschedule(0, 25) + " " + w.hours());
    }
}
A true 4 false 4 false 4
B true 4 false -1 false 25
C true 4 true -1 false 4
D false 8 false 8 false 8
Question page & discussion
Question 12 Hard

What is the output of the following Java program?

Main.java
class Address {
    String city;
    Address(String c) { city = c; }
    Address(Address o) { this(o.city); }
}

class Customer {
    private final Address address;
    Customer(Address a) { address = new Address(a); }
    Address getAddress() { return new Address(address); }
    void moveTo(String city) { address.city = city; }
}

public class Main {
    public static void main(String[] args) {
        Address a = new Address("Pune");
        Customer c = new Customer(a);
        a.city = "Delhi";
        c.getAddress().city = "Goa";
        String before = c.getAddress().city;
        c.moveTo("Mumbai");
        System.out.println(before + " " + c.getAddress().city + " " + a.city);
    }
}
A Pune Mumbai Delhi
B Goa Mumbai Delhi
C Delhi Mumbai Delhi
D Pune Pune Delhi
Question page & discussion
Question 13 Easy

What is the output of the following Java program?

Main.java
class PageViews {
    private long views;
    private long uniqueVisitors;
    private final java.util.Set<String> seen = new java.util.HashSet<>();

    void visit(String visitorId) {
        views++;
        if (seen.add(visitorId)) uniqueVisitors++;
    }

    long views() { return views; }
    long unique() { return uniqueVisitors; }
}

public class Main {
    public static void main(String[] args) {
        PageViews pv = new PageViews();
        for (String v : "a b a c b a d".split(" ")) pv.visit(v);
        System.out.println(pv.views() + " " + pv.unique());
    }
}
A 7 4
B 7 7
C 4 4
D 7 3
Question page & discussion
Question 14 Medium

What is the output of the following Java program?

Main.java
class Ticket {
    private String state = "OPEN";

    boolean start()   { return move("OPEN", "IN_PROGRESS"); }
    boolean resolve() { return move("IN_PROGRESS", "RESOLVED"); }
    boolean reopen()  { return move("RESOLVED", "OPEN"); }

    private boolean move(String from, String to) {
        if (!state.equals(from)) return false;
        state = to;
        return true;
    }

    String state() { return state; }
}

public class Main {
    public static void main(String[] args) {
        Ticket t = new Ticket();
        System.out.println(t.resolve() + " " + t.start() + " " + t.start() + " " + t.resolve() + " " + t.reopen() + " " + t.state());
    }
}
A false true false true true OPEN
B true true true true true OPEN
C false true false true false RESOLVED
D false false false false false OPEN
Question page & discussion
Question 15 Easy

What is the output of the following Java program?

Main.java
class Contact {
    private String phone = "";

    void setPhone(String raw) {
        String digits = raw.replaceAll("\\D", "");
        if (digits.length() == 12 && digits.startsWith("91")) digits = digits.substring(2);
        if (digits.length() == 10) phone = digits;
    }

    String getPhone() { return phone.isEmpty() ? "(none)" : "+91 " + phone.substring(0, 5) + " " + phone.substring(5); }
}

public class Main {
    public static void main(String[] args) {
        Contact c = new Contact();
        String a = c.getPhone();
        c.setPhone("+91 (987) 654-3210");
        String b = c.getPhone();
        c.setPhone("12345");
        System.out.println(a + " | " + b + " | " + c.getPhone());
    }
}
A (none) | +91 98765 43210 | +91 98765 43210
B (none) | +91 (987) 654-3210 | 12345
C (none) | +91 98765 43210 | (none)
D | +91 98765 43210 | +91 12345
Question page & discussion
Question 16 Medium

What is the output of the following Java program?

Main.java
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

final class Email {
    private final String value;
    Email(String v) { value = v.strip().toLowerCase(); }
    @Override public boolean equals(Object o) { return o instanceof Email e && value.equals(e.value); }
    @Override public int hashCode() { return Objects.hash(value); }
    @Override public String toString() { return value; }
}

public class Main {
    public static void main(String[] args) {
        Set<Email> set = new HashSet<>();
        set.add(new Email("Asha@Mail.com"));
        set.add(new Email(" asha@mail.com "));
        set.add(new Email("ravi@mail.com"));
        System.out.println(set.size() + " " + set.contains(new Email("ASHA@MAIL.COM")));
    }
}
A 2 true
B 3 false
C 3 true
D 2 false
Question page & discussion
Question 17 Medium

What is the output of the following Java program?

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

class Wallet {
    private int balance;
    private final List<Integer> history = new ArrayList<>();

    boolean apply(int change) {
        if (balance + change < 0) return false;
        balance += change;
        history.add(change);
        return true;
    }

    int balance() { return balance; }
    int sumOfHistory() { return history.stream().mapToInt(Integer::intValue).sum(); }
}

public class Main {
    public static void main(String[] args) {
        Wallet w = new Wallet();
        int[] changes = {500, -200, -400, 100, -400};
        int ok = 0;
        for (int c : changes) if (w.apply(c)) ok++;
        System.out.println(ok + " " + w.balance() + " " + (w.balance() == w.sumOfHistory()));
    }
}
A 4 0 true
B 5 -400 true
C 3 400 true
D 4 0 false
Question page & discussion
Question 18 Medium

What is the output of the following Java program?

Main.java
class Thermometer {
    private double celsius;

    private void setCelsius(double c) { celsius = Math.max(-273.15, c); }

    void readCelsius(double c) { setCelsius(c); }
    void readFahrenheit(double f) { setCelsius((f - 32) * 5 / 9); }

    String show() { return String.format("%.1f C", celsius); }
}

public class Main {
    public static void main(String[] args) {
        Thermometer t = new Thermometer();
        t.readFahrenheit(98.6);
        String a = t.show();
        t.readCelsius(-500);
        System.out.println(a + " " + t.show());
    }
}
A 37.0 C -273.2 C
B 37.0 C -500.0 C
C 98.6 C -273.2 C
D 37.0 C -273.1 C
Question page & discussion
Question 19 Medium

What is the output of the following Java program?

Main.java
record Marks(String subject, int score) {
    Marks {                                           // compact constructor: validation
        if (score < 0 || score > 100) throw new IllegalArgumentException("score " + score);
        subject = subject.toUpperCase();
    }
}

public class Main {
    public static void main(String[] args) {
        Marks m = new Marks("maths", 92);
        String r;
        try { new Marks("art", 120); r = "created"; } catch (IllegalArgumentException e) { r = e.getMessage(); }
        System.out.println(m.subject() + " " + m.score() + " " + m + " " + r);
    }
}
A MATHS 92 Marks[subject=MATHS, score=92] score 120
B maths 92 Marks[subject=maths, score=92] created
C MATHS 92 Marks@1b6d score 120
D Compilation error
Question page & discussion
Question 20 Medium

What is the output of the following Java program?

Main.java
class Stock {
    private int available;
    private int reserved;

    Stock(int available) { this.available = available; }

    boolean reserve(int qty) {
        if (qty <= 0 || qty > available) return false;
        available -= qty;
        reserved += qty;
        return true;
    }

    void release(int qty) {
        int r = Math.min(qty, reserved);
        reserved -= r;
        available += r;
    }

    String show() { return available + "/" + reserved; }
}

public class Main {
    public static void main(String[] args) {
        Stock s = new Stock(10);
        System.out.print(s.reserve(4) + " " + s.reserve(7) + " " + s.reserve(6) + " ");
        s.release(15);
        System.out.println(s.show());
    }
}
A true false true 10/0
B true true false 0/10
C true false true 5/5
D true false true 25/0
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