☕ Java MCQ Questions – Page 131

Questions 2601–2620 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2601 easy code error
What kind of error will occur when compiling this Java code?
java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter("output.txt"));
            // The write() method can throw IOException, but it's not caught by NullPointerException
            bw.write("Test data.");
        } catch (NullPointerException e) {
            System.err.println("Caught NPE: " + e.getMessage());
        }
    }
}
Q2602 medium
Under what circumstance would a `do-while` loop in Java result in an infinite loop?
Q2603 medium code error
Which compile-time error will occur when compiling the `Child` class?
java
class Parent {
    Parent(int x) {
        System.out.println("Parent: " + x);
    }
}

class Child extends Parent {
    Child() {
        System.out.println("Child default constructor");
        super(10); 
    }
}

public class Main {
    public static void main(String[] args) {
        new Child();
    }
}
Q2604 hard code output
What is the most likely output of this code?
java
public class JoinWithoutSyncTest {
    private int counter = 0; // Not volatile, not synchronized

    public void increment() {
        for (int i = 0; i < 1000; i++) {
            counter++;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        JoinWithoutSyncTest test = new JoinWithoutSyncTest();
        Thread t1 = new Thread(test::increment);
        Thread t2 = new Thread(test::increment);

        t1.start();
        t2.start();

        t1.join(); // Main waits for t1 to finish
        t2.join(); // Main waits for t2 to finish

        System.out.println("Final counter: " + test.counter);
    }
}
Q2605 hard code error
What is the compile-time error in the `Human` class?
java
abstract class LivingBeing {
    abstract void breath();
    public void eat() {
        System.out.println("Eating...");
    }
}
class Human extends LivingBeing { // Line 7
    public void talk() {
        System.out.println("Talking...");
    }
}
public class World {
    public static void main(String[] args) {
        LivingBeing h = new Human();
        h.eat();
    }
}
Q2606 easy
What is the minimum number of times the code inside a do-while loop will execute?
Q2607 hard
You are designing an API for a critical financial system. A specific operation might fail due to an invalid input parameter provided by the *caller*, indicating a programming error in the calling code, rather than a transient system issue. Which type of custom exception is most appropriate for this scenario, and why?
Q2608 medium
Which of the following methods must be called from within a `synchronized` block or method?
Q2609 medium
Which of the following methods, when called on a `Thread` object, *can* throw an `InterruptedException`?
Q2610 medium
What is the primary advantage of using an `if-else if-else` ladder compared to a series of independent `if` statements (without `else`) for handling multiple mutually exclusive conditions?
Q2611 easy
`FileReader` is designed to read data as what type of stream?
Q2612 medium code output
What does the following Java code print?
java
import java.io.IOException;
import java.io.FileNotFoundException;

class Processor {
    void processFile() throws IOException {
        System.out.println("Processor handling generic file");
    }
}

class ImageProcessor extends Processor {
    @Override
    void processFile() throws FileNotFoundException { // Valid: FileNotFoundException is a subclass of IOException
        System.out.println("ImageProcessor handling image file");
    }
}

public class Main {
    public static void main(String[] args) {
        Processor p = new ImageProcessor();
        try {
            p.processFile();
        } catch (IOException e) {
            System.out.println("Caught exception");
        }
    }
}
Q2613 easy code output
What does this Java code print?
java
import java.io.File;
import java.io.IOException;

public class FileTest {
    public static void main(String[] args) {
        File file = new File("temp_file_q10.txt");
        try {
            file.createNewFile();
            System.out.println(file.isFile() + " " + file.isDirectory());
        } catch (IOException e) {
            System.out.println("Error");
        } finally {
            file.delete(); // Clean up
        }
    }
}
Q2614 easy code error
What is the error in this code?
java
import java.util.TreeMap;

public class Test {
    public static void main(String[] args) {
        TreeMap<Object, String> map = new TreeMap<>();
        map.put("StringKey", "Value1");
        map.put(100, "Value2"); // Integer key
        System.out.println(map.size());
    }
}
Q2615 easy
What happens if you directly call the `run()` method of an object that implements `Runnable` instead of passing it to a `Thread` and calling `start()`?
Q2616 medium code output
What is the final state of thread `t` printed in the output?
java
public class ThreadStateDemo9 {
    private static final Object monitor = new Object();
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            synchronized (monitor) {
                try {
                    System.out.println("Thread is timed waiting.");
                    monitor.wait(100); // Wait for 100ms
                    System.out.println("Thread resumed after timeout.");
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        });
        t.start();
        Thread.sleep(50); // Ensure thread enters wait()
        System.out.println("State of t (mid-wait): " + t.getState());
        Thread.sleep(100); // Allow timeout
        System.out.println("State of t (final): " + t.getState());
    }
}
Q2617 medium code output
What does this code print?
java
import java.util.LinkedList;

public class Test {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("One");
        list.add("Two");
        String[] arr = list.toArray(new String[0]);
        System.out.println(arr.length + " " + arr[0]);
    }
}
Q2618 easy code error
What kind of error will occur when compiling this Java code?
java
public class Main {
    public static void main(String[] args) {
        int score = 85;
        else {
            System.out.println("Invalid score");
        }
    }
}
Q2619 medium code output
What is the output of this code snippet regarding StringBuilder equality?
java
public class StringBuilderTest {
    public static void main(String[] args) {
        StringBuilder sb1 = new StringBuilder("test");
        StringBuilder sb2 = new StringBuilder("test");
        String s1 = "test";
        System.out.println(sb1.equals(sb2) + ", " + sb1.toString().equals(s1));
    }
}
Q2620 easy
What is the return type of a constructor in Java?
← Prev 129130131132133 Next → Page 131 of 200 · 3994 questions