☕ Java MCQ Questions – Page 47

Questions 921–940 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q921 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 SuspendResumeDemo {
    private static Object lock = new Object();
    private static Thread suspendedThread;

    public static void main(String[] args) throws InterruptedException {
        suspendedThread = new Thread(() -> {
            synchronized (lock) { // Acquires lock
                System.out.println("Suspended Thread acquired lock.");
                Thread.currentThread().suspend(); // Deprecated: doesn't release lock
                System.out.println("Suspended Thread resumed.");
            }
        }, "SuspendedThread");

        suspendedThread.start();
        Thread.sleep(100); // Give time for the thread to acquire lock and suspend
        System.out.println("Main thread attempting to acquire lock...");
        synchronized (lock) { // Problem line: main thread tries to acquire the same lock
            System.out.println("Main thread acquired lock.");
            suspendedThread.resume(); // This line might never be reached
        }
    }
}
Q922 easy code error
What is the compilation error in the following Java code snippet?
java
public class LoopError {
    public static void main(String[] args) {
        int a = 0;
        do (
            System.out.println(a);
            a++;
        ) while (a < 1);
    }
}
Q923 medium code error
What is the compilation error in the following code?
java
final class Vehicle {
    void honk() {
        System.out.println("Vehicle honks!");
    }
}

class Car extends Vehicle {
    void drive() {
        System.out.println("Car drives!");
    }
}
Q924 medium code output
What is the output of this code?
java
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;

public class BufferedReaderTest {
    public static void main(String[] args) {
        String data = "ABCDEFG";
        try (BufferedReader br = new BufferedReader(new StringReader(data))) {
            System.out.print((char) br.read());
            br.skip(2);
            System.out.print((char) br.read());
            br.skip(Long.MAX_VALUE);
            System.out.print(br.readLine());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Q925 easy code error
What will be the output or error when running this code?
java
class Car {
    String model;
    void start() {
        System.out.println(model + " starting.");
    }
}

public class Main {
    public static void main(String[] args) {
        Car myCar;
        myCar.start();
    }
}
Q926 hard code output
What is the output of this Java code?
java
String s1 = "Java";
String s2 = "Interview";
String s3 = "JavaInterview";
String s4 = s1 + s2;
String s5 = (s1 + s2).intern();

System.out.println(s3 == s4);
System.out.println(s3 == s5);
Q927 easy
Which method must a class implement when it implements the `Runnable` interface?
Q928 medium code error
What compilation error will occur in the `Child` class?
java
public class BaseException extends Exception {
    public BaseException(String msg) { super(msg); }
}

public class SpecificException extends BaseException {
    public SpecificException(String msg) { super(msg); }
}

class Parent {
    public void execute() throws SpecificException {
        System.out.println("Parent executing.");
    }
}

class Child extends Parent {
    @Override
    public void execute() throws BaseException {
        System.out.println("Child executing.");
    }
}

public class TestExceptions {
    public static void main(String[] args) {
        // Won't compile
    }
}
Q929 medium code error
What is the compile-time error in the following Java code?
java
class MyClass {
    int a;
    boolean b;

    public MyClass() {
        this(5); // Calls constructor with int
    }

    public MyClass(int a) {
        this(); // Calls no-arg constructor, creating a cycle
        this.a = a;
    }

    public static void main(String[] args) {
        new MyClass();
    }
}
Q930 medium code error
What error occurs when running this code, assuming '.' is the current directory?
java
import java.io.FileReader;
import java.io.IOException;
import java.io.File;

public class MyClass {
    public static void main(String[] args) {
        File directory = new File("."); // Represents the current directory
        try (FileReader reader = new FileReader(directory)) {
            System.out.println("Reader created for directory.");
        } catch (IOException e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}
Q931 easy code error
What error will this Java code produce during compilation?
java
public class ArrayError {
    public static void main(String[] args) {
        int[][] matrix = int[2][3];
    }
}
Q932 medium code output
What does this Java code print?
java
import java.util.TreeSet;
import java.util.Set;

public class Test {
    public static void main(String[] args) {
        Set<String> words = new TreeSet<>();
        words.add("Banana");
        words.add("Apple");
        words.add("Cherry");
        words.add("apple");
        System.out.println(words);
    }
}
Q933 easy
What happens if you try to overload a method by only changing its access modifier (e.g., from `public` to `private`)?
Q934 medium
`java.io.FileReader` is a subclass of which abstract class, indicating it operates on character streams?
Q935 hard code output
What is the output of this code?
java
public class Test {
    public static void main(String[] args) {
        String result = "";
        outer: for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (i == 1 && j == 1) {
                    continue outer;
                }
                result += "(" + i + "," + j + ")";
            }
        }
        System.out.print(result);
    }
}
Q936 medium
When is an explicit type cast NOT required for object references in Java?
Q937 hard code output
What is the output of this code?
java
public class DataTypeChallenge {
    public static void main(String[] args) {
        Integer a = 127;
        Integer b = 127;
        Integer c = 128;
        Integer d = 128;
        System.out.println(a == b);
        System.out.println(c == d);
    }
}
Q938 easy code output
What is the output of this Java code snippet?
java
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        TreeMap<Integer, String> treeMap = new TreeMap<>();
        treeMap.put(1, "One");
        treeMap.remove(1);
        System.out.println(treeMap.isEmpty());
    }
}
Q939 medium
What is the average time complexity for the `put`, `get`, and `remove` operations in a `TreeMap`?
Q940 medium
Which characteristic primarily differentiates `Runnable` from `Callable`?
← Prev 4546474849 Next → Page 47 of 200 · 3994 questions