☕ Java MCQ Questions – Page 181

Questions 3601–3620 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3601 medium code error
What error occurs when running this code, assuming 'sample.txt' exists?
java
import java.io.FileReader;
import java.io.IOException;
import java.io.File;

public class MyClass {
    public static void main(String[] args) {
        File file = new File("sample.txt");
        try {
            file.createNewFile();
            try (FileReader reader = new FileReader(file)) {
                char[] buffer = new char[5];
                reader.read(buffer, 0, 10); // length (10) > buffer.length (5)
                System.out.println("Read attempt finished.");
            }
        } catch (IOException e) {
            System.err.println("Caught IOException: " + e.getMessage());
        } finally {
            file.delete();
        }
    }
}
Q3602 easy
In a `for` loop, which part is executed only once at the very beginning of the loop?
Q3603 medium code error
What error will occur when running the following Java code?
java
public class WaitWithoutMonitor {
    public static void main(String[] args) {
        Object lock = new Object();
        try {
            lock.wait(); // Calling wait() without owning the monitor
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
Q3604 hard
Consider a class `MyClass` with two overloaded constructors: `MyClass(double d)` and `MyClass(Integer i)`. Which of the following attempts to instantiate `MyClass` would result in a *compilation error* due to constructor ambiguity?
Q3605 easy
If a concrete class extends an abstract class that contains abstract methods, what must the concrete class do?
Q3606 medium
You want to define a custom functional interface `Calculator` that takes two integers and returns their sum. Which signature correctly defines its single abstract method?
Q3607 medium code error
Consider the following Java code snippet. What is the compilation error it will produce?
java
public class OperatorError {
    public static void main(String[] args) {
        double d = 10.5;
        int result = d >> 2;
        System.out.println(result);
    }
}
Q3608 medium
What is the recommended approach for ensuring that a `BufferedReader` (and its underlying stream) is properly closed after use?
Q3609 hard
What is the primary purpose of `Thread.setDefaultUncaughtExceptionHandler()` or `Thread.setUncaughtExceptionHandler()`?
Q3610 easy code error
What will happen when this Java code is compiled?
java
public class MyClass {
    public static void main(String[] args) {
        if (true) {
            int temp = 100;
        }
        System.out.println(temp);
    }
}
Q3611 hard
Regarding the `synchronized` keyword, which statement about method overriding is true?
Q3612 medium code output
What is the output of the following Java code snippet?
java
import java.util.Arrays;
import java.util.List;

public class StreamTest {
    public static void main(String[] args) {
        List<Integer> values = Arrays.asList(1, 2, 3, 4);
        Integer sum = values.stream()
                            .reduce(10, (a, b) -> a + b);
        System.out.println(sum);
    }
}
Q3613 hard
In the context of polymorphism, why are constructors not considered polymorphic in the same way as instance methods?
Q3614 easy code error
What will be the compile-time error in the following Java code snippet?
java
public class DataTypeError {
    public static void main(String[] args) {
        int myInt = 10.5;
        System.out.println(myInt);
    }
}
Q3615 medium code output
What is the most likely output order for the 'started' and 'finished' messages?
java
class SyncExample {
    public synchronized static void staticMethod() {
        System.out.println(Thread.currentThread().getName() + ": Static method started.");
        try { Thread.sleep(200); } catch (InterruptedException e) {}
        System.out.println(Thread.currentThread().getName() + ": Static method finished.");
    }

    public synchronized void instanceMethod() {
        System.out.println(Thread.currentThread().getName() + ": Instance method started.");
        try { Thread.sleep(200); } catch (InterruptedException e) {}
        System.out.println(Thread.currentThread().getName() + ": Instance method finished.");
    }
}

public class Main {
    public static void main(String[] args) throws InterruptedException {
        SyncExample obj = new SyncExample();
        Thread t1 = new Thread(() -> SyncExample.staticMethod(), "StaticThread");
        Thread t2 = new Thread(() -> obj.instanceMethod(), "InstanceThread");

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

        t1.join();
        t2.join();
    }
}
Q3616 hard code error
What is the result of running this Java code?
java
import java.util.TreeMap;

class NonComparableKey {}

public class TreeMapError9 {
    public static void main(String[] args) {
        TreeMap<NonComparableKey, String> map = new TreeMap<>(null); 
        map.put(new NonComparableKey(), "Value"); 
        System.out.println(map.size());
    }
}
Q3617 easy code error
What compilation error will occur in the following Java code?
java
interface RemoteControl {
    void turnOn();
    void turnOff();
}

class TV implements RemoteControl {
    @Override
    public void turnOn() {
        System.out.println("TV is ON");
    }
}
Q3618 medium
If a custom exception `MyUncheckedException` extends `java.lang.RuntimeException`, what is the implication for a method `bar()` that might throw it?
Q3619 easy code error
Which error will be reported by the Java compiler for the given code?
java
class Utility {
    static void processData(int value) {
        System.out.println("Static processing: " + value);
    }

    void processData(int value) {
        System.out.println("Instance processing: " + value);
    }
}
Q3620 hard code error
What kind of runtime error or unexpected behavior will occur when running this Java code, primarily related to thread lifecycle?
java
public class Notifier implements Runnable {
    private Object monitor = new Object();
    public void run() {
        try {
            Thread.sleep(50); // Ensure main thread starts first
            monitor.notifyAll(); // Problem line
        } catch (InterruptedException | IllegalMonitorStateException e) {
            System.out.println("Caught: " + e.getClass().getSimpleName());
        }
    }
    public static void main(String[] args) {
        Notifier notifier = new Notifier();
        Thread t = new Thread(notifier);
        t.start();
    }
}
← Prev 179180181182183 Next → Page 181 of 200 · 3994 questions