☕ Java MCQ Questions – Page 72

Questions 1421–1440 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1421 hard code output
What is the output of this code, demonstrating a 'this' reference escape during constructor execution?
java
import java.util.ArrayList; import java.util.List;

class EventLogger {
    private List<String> logs = new ArrayList<>();
    public void logEvent(String event) { logs.add(event); System.out.println("Logged: " + event); }
}

class Notifier {
    private final String id;
    public Notifier(EventLogger logger, String id) {
        logger.logEvent("Notifier created (ID: " + this.id + ")");
        this.id = id;
    }
}

public class Main {
    public static void main(String[] args) {
        new Notifier(new EventLogger(), "N_001");
    }
}
Q1422 hard code output
What is the output of this code?
java
import java.util.HashSet;
import java.util.Objects;

class MyKey {
    int value;
    public MyKey(int value) { this.value = value; }
    @Override public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MyKey myKey = (MyKey) o;
        return value == myKey.value;
    }
    @Override public int hashCode() { return Objects.hash(value); }
}

public class Main {
    public static void main(String[] args) {
        HashSet<MyKey> set = new HashSet<>();
        set.add(new MyKey(10));
        System.out.println(set.contains(new MyKey(10)));
        System.out.println(set.remove(new MyKey(10)));
        System.out.println(set.size());
    }
}
Q1423 easy
What is the ultimate superclass for all exception and error types in Java, including custom exceptions?
Q1424 medium code error
What error will occur when compiling or running this Java code?
java
public class ArrayError {
    public static void main(String[] args) {
        int[] numbers = new int[5];
        numbers[0] = 3.14;
    }
}
Q1425 easy
Which method is used to add an element to an `ArrayList`?
Q1426 easy
What is the primary reason for creating a custom exception in Java?
Q1427 easy
Can an abstract class be instantiated?
Q1428 easy code error
What is the error in this Java code?
java
import java.util.HashMap;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        Map<String, Integer> myMap = new HashMap<>();
        myMap.put("score", null);
        int finalScore = myMap.get("score");
        System.out.println(finalScore);
    }
}
Q1429 medium code output
What does this code print?
java
class Company {
    static {
        System.out.println("Static Block 1");
    }
    public Company() {
        System.out.println("Constructor called");
    }
    static {
        System.out.println("Static Block 2");
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println("Main method started");
        Company c1 = new Company();
        Company c2 = new Company();
    }
}
Q1430 medium code output
What does this Java code snippet print?
java
public class StringBufferTest {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("ABCDE");
        sb.setCharAt(2, 'X');
        sb.insert(1, sb.charAt(3));
        System.out.print(sb);
    }
}
Q1431 easy code error
What is the error in the following Java code snippet?
java
public class ArrayError {
    public static void main(String[] args) {
        int[] numbers;
        numbers[0] = 10;
        System.out.println(numbers[0]);
    }
}
Q1432 medium code error
What error does this Java code produce when executed?
java
public class Main {
    public static void main(String[] args) {
        String country = "Canada";
        System.out.println(country.substring(7));
    }
}
Q1433 medium
What is the primary purpose of the `java.util.Iterator` interface in Java?
Q1434 easy code output
What does this code print?
java
public class User {
    private String username;

    public User(String username) {
        this.username = username;
    }

    public String getUsername() {
        return username;
    }

    public static void main(String[] args) {
        User user = new User("alice");
        System.out.println(user.getUsername());
    }
}
Q1435 medium
When is a `ClassCastException` most likely to occur in the context of polymorphism?
Q1436 easy code output
What is the output of this code?
java
public class DataTypeTest {
    public static void main(String[] args) {
        byte b1 = 10;
        byte b2 = 20;
        int sum = b1 + b2;
        System.out.println(sum);
    }
}
Q1437 easy code error
What is the output or error of the following Java code?
java
public class ExceptionTest {
    public static void main(String[] args) {
        try {
            int result = 10 / 0;
        } catch (ArithmeticException e) {
            System.out.println("Caught ArithmeticException");
        } catch (Exception e) {
            System.out.println("Caught general Exception");
        }
    }
}
Q1438 hard code output
What is the output of this Java code?
java
import java.util.PriorityQueue;
import java.util.Comparator;

class MutableInt {
    int value;
    MutableInt(int v) { this.value = v; }
    void setValue(int v) { this.value = v; }
    @Override public String toString() { return String.valueOf(value); }
}

public class QueueTest {
    public static void main(String[] args) {
        PriorityQueue<MutableInt> pq = new PriorityQueue<>(Comparator.comparingInt(mi -> mi.value));

        MutableInt m1 = new MutableInt(5);
        MutableInt m2 = new MutableInt(10);
        MutableInt m3 = new MutableInt(2);

        pq.offer(m1);
        pq.offer(m2);
        pq.offer(m3);

        m3.setValue(15);

        String result = "";
        result += pq.poll().value + " ";
        result += pq.poll().value + " ";
        result += pq.poll().value;

        System.out.println(result);
    }
}
Q1439 easy code output
What is the output of this Java code?
java
class Product {
    String name;
    Product(String name) {
        this.name = name;
        System.out.println("Product: " + this.name);
    }
}
public class Main {
    public static void main(String[] args) {
        Product p = new Product("Laptop");
    }
}
Q1440 medium code output
What is the output of this code?
java
class Counter {
    int count = 0;
    static int totalCount = 0;

    public Counter() {
        count++;
        totalCount++;
    }
}

public class Main {
    public static void main(String[] args) {
        Counter c1 = new Counter();
        Counter c2 = new Counter();
        c1.count++;
        Counter.totalCount++;
        System.out.println("c1.count: " + c1.count);
        System.out.println("c2.count: " + c2.count);
        System.out.println("totalCount: " + Counter.totalCount);
    }
}
← Prev 7071727374 Next → Page 72 of 200 · 3994 questions