☕ Java MCQ Questions – Page 66

Questions 1301–1320 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1301 medium code output
What does this code print?
java
class MyResource {
    public MyResource(String name) throws IllegalArgumentException {
        if (name == null || name.isEmpty()) {
            throw new IllegalArgumentException("Resource name cannot be empty");
        }
        System.out.println("Resource '" + name + "' created.");
    }

    public static void main(String[] args) {
        try {
            MyResource res = new MyResource(null);
            System.out.println("After creation");
        } catch (IllegalArgumentException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}
Q1302 easy code error
What will happen when this Java code is compiled and executed?
java
class Base { public void methodA() {} }
class Derived extends Base { public void methodA() throws java.io.IOException {} }
Q1303 easy code output
What is the output of this Java code?
java
import java.io.FileReader;
import java.io.IOException;

public class FileReaderTest {
    public static void main(String[] args) {
        // Assume test.txt exists and contains "JAVA"
        char[] buffer = new char[2];
        try (FileReader reader = new FileReader("test.txt")) {
            int charsRead = reader.read(buffer);
            System.out.print(new String(buffer, 0, charsRead));
        } catch (IOException e) {
            System.out.print("Error");
        }
    }
}
Q1304 medium code error
What will be the ultimate outcome of running this Java program?
java
public class LoopTest {
    public static void main(String[] args) {
        String s = "a";
        while (true) {
            s = s + "a";
        }
    }
}
Q1305 easy code error
What is wrong with this Java code?
java
import java.util.function.Consumer;

public class Test {
    public static void main(String[] args) {
        int counter = 0;
        Consumer<String> printer = s -> {
            System.out.println(s + counter);
            counter++;
        };
        printer.accept("Count: ");
    }
}
Q1306 medium
Which of the following data types is NOT directly supported as the controlling expression for a Java `switch` statement or expression?
Q1307 medium
Which statement correctly describes the return type rule for method overriding in Java?
Q1308 hard code output
What is the output of this code?
java
public class Q9_NotifyAllVsNotify {
    private static final Object lock = new Object();
    private static int counter = 0;
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> { synchronized (lock) { try { lock.wait(); counter++; } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } });
        Thread t2 = new Thread(() -> { synchronized (lock) { try { lock.wait(); counter++; } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } });
        Thread t3 = new Thread(() -> { synchronized (lock) { try { lock.wait(); counter++; } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } });

        t1.start(); t2.start(); t3.start();
        Thread.sleep(100); // Ensure all threads are waiting
        System.out.println("Counter before notify: " + counter);
        
        synchronized (lock) {
            lock.notify(); // Only one thread will be woken up
        }
        Thread.sleep(100); // Give time for one thread to process
        System.out.println("Counter after 1st notify: " + counter);
        
        synchronized (lock) {
            lock.notifyAll(); // Wake up remaining threads
        }
        Thread.sleep(100); // Give time for others to process
        System.out.println("Counter after notifyAll: " + counter);
    }
}
Q1309 hard
If an array is declared as `volatile Object[] data;`, what does the `volatile` keyword guarantee with respect to the array's elements in a multithreaded environment?
Q1310 medium
Regarding the `remove()` and `poll()` methods of the Java `Queue` interface, what is the key difference in their behavior when the queue is empty?
Q1311 medium code error
What error will occur when running the following Java code?
java
public class NotifyWithoutMonitor {
    public static void main(String[] args) {
        Object lock = new Object();
        lock.notify(); // Calling notify() without owning the monitor
    }
}
Q1312 medium
What is the typical range for thread priorities in Java, and how does setting priority affect thread scheduling?
Q1313 easy code error
What compile-time error will occur in the `Child` class?
java
class Parent {
    public static void showStatic() {
        System.out.println("Parent Static");
    }
}

class Child extends Parent {
    @Override
    public void showStatic() {
        System.out.println("Child Static");
    }
}
Q1314 hard code error
What error occurs when executing the following Java code?
java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Integer> original = new ArrayList<>();
        original.add(10);
        original.add(20);

        List<Integer> unmodifiable = Collections.unmodifiableList(original);
        unmodifiable.add(30); // Attempt to modify the unmodifiable list
        System.out.println(unmodifiable);
    }
}
Q1315 medium
What is the requirement for objects stored in a `TreeSet` if no custom `Comparator` is provided during its instantiation?
Q1316 easy
What characteristic makes String objects unchangeable once created in Java?
Q1317 easy code error
What will happen when attempting to instantiate `ArrayDeque` with a negative capacity?
java
import java.util.ArrayDeque;
import java.util.Deque;

public class QueueError {
    public static void main(String[] args) {
        Deque<String> deque = new ArrayDeque<>(-5);
        deque.add("Item");
    }
}
Q1318 hard
If an `if` statement's condition calls a method with a side effect (e.g., `if (updateAndCheckStatus())`), and this `if` statement is inside a loop, how often is the `updateAndCheckStatus()` method guaranteed to be called per loop iteration?
Q1319 hard
Which of the following scenarios would lead to an `ExceptionInInitializerError` being thrown at runtime?
Q1320 hard
Under what specific circumstances might wrapping a `Writer` with a `BufferedWriter` lead to *reduced* performance or offer no significant advantage?
← Prev 6465666768 Next → Page 66 of 200 · 3994 questions