☕ Java MCQ Questions – Page 21

Questions 401–420 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q401 medium code error
What error will occur when running the following Java code?
java
public class ResumeWithoutSuspend {
    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            System.out.println("Thread running");
            try { Thread.sleep(100); } catch (InterruptedException e) {}
        });
        t.start();
        // The thread was never suspended
        t.resume(); // Deprecated, but still behaves this way
    }
}
Q402 medium
What is the approximate time complexity for `add()`, `remove()`, and `contains()` operations in a `TreeSet`?
Q403 hard code error
What is the compilation error in the following Java code?
java
public class Configuration {
    private final String API_KEY = "key_123";
    private final String BASE_URL = getBaseUrl();

    private String getBaseUrl() {
        // This method depends on API_KEY but is called before API_KEY is initialized.
        return "https://api.example.com/" + API_KEY.substring(0, 3);
    }

    public String getFullUrl() {
        return BASE_URL;
    }
}
Q404 hard code error
What is the error in the execution of this Java code snippet?
java
public class BuilderError {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Data");
        sb.ensureCapacity(-5);
        System.out.println(sb.capacity());
    }
}
Q405 medium
Which of the following primitive type casting scenarios would result in a compile-time error?
Q406 hard
Given `int a = 5; int b = a;` and `MyObject obj1 = new MyObject(); MyObject obj2 = obj1;`, what is the fundamental difference in how `b` gets its value from `a` compared to `obj2` getting its value from `obj1`?
Q407 easy
What is the primary difference between `FileReader` and `FileInputStream`?
Q408 hard code output
What is the output of this code?
java
import java.util.LinkedList;

public class Test {
    public static void main(String[] args) {
        LinkedList<Integer> list = new LinkedList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        for (Integer num : list) {
            if (num == 2) {
                list.add(4); // Modifying list during enhanced for-loop
            }
        }
        System.out.println(list);
    }
}
Q409 easy
Which of the following is NOT a direct advantage of encapsulation?
Q410 medium code error
What error will prevent the following Java code from compiling?
java
public class Main {
    public static void main(String[] args) {
        int counter = 0;
        Runnable r = () -> {
            counter++; // Attempt to modify 'counter'
            System.out.println(counter);
        };
        Thread t = new Thread(r);
        t.start();
    }
}
Q411 hard code output
What is the output of this code?
java
public class DataTypeChallenge {
    public static void main(String[] args) {
        float f = 0.7f;
        double d = 0.7;
        System.out.println(f == d);
    }
}
Q412 hard
Under what specific circumstance might a `SecurityException` be thrown during the construction of a `FileReader` instance, even if the file exists and the path is valid?
Q413 hard code output
What is the output of this code?
java
public class WhileTrueBreak {
    public static void main(String[] args) {
        int x = 0;
        int y = 0;
        while (true) {
            x++;
            if (x % 2 == 0) {
                y++;
            }
            if (x > 5) {
                if (y >= 2) {
                    break;
                }
                y = 0; 
            }
        }
        System.out.println(x + ", " + y);
    }
}
Q414 easy code error
What compile-time error will this code produce?
java
class MyTask implements Runnable {
    // No run() method implemented
}

public class Main {
    public static void main(String[] args) {
        MyTask task = new MyTask();
        Thread thread = new Thread(task);
        thread.start();
    }
}
Q415 medium code error
What is the primary error in the following Java code when attempting to compile it?
java
public class MyTask implements Runnable {
    // No run() method implemented
}

public class Main {
    public static void main(String[] args) {
        MyTask task = new MyTask();
        System.out.println("Task created.");
    }
}
Q416 medium code output
What is the output of this Java code?
java
import java.util.TreeSet;
import java.util.Set;

public class Test {
    public static void main(String[] args) {
        Set<Integer> uniqueNumbers = new TreeSet<>();
        uniqueNumbers.add(10);
        uniqueNumbers.add(20);
        uniqueNumbers.add(10);
        uniqueNumbers.add(30);
        uniqueNumbers.add(20);
        System.out.println(uniqueNumbers.size());
    }
}
Q417 hard code output
What is the output of this code?
java
enum MySingleton {
    INSTANCE;
    private MySingleton() {
        System.out.println("MySingleton ctor.");
    }
    public void doSomething() {
        System.out.println("Singleton action.");
    }
}
class Utility {
    private Utility() { throw new AssertionError("No instances!"); }
    public static void info() { System.out.println("Utility info."); }
}
public class Main {
    public static void main(String[] args) {
        Utility.info();
        MySingleton s1 = MySingleton.INSTANCE;
        s1.doSomething();
        MySingleton s2 = MySingleton.INSTANCE;
        System.out.println(s1 == s2);
    }
}
Q418 easy code error
What compile-time error will occur when compiling this Java code?
java
public class MyClass {
    public static void main(String[] args) {
        int value; // Uninitialized local variable
        switch (value) {
            case 1:
                System.out.println("One");
                break;
            default:
                System.out.println("Other");
        }
    }
}
Q419 easy code error
What compile-time error will occur in the `main` method?
java
class MyCustomException extends Exception {
    public MyCustomException(String message) { super(message); }
}

public class Main {
    public static void main(String[] args) {
        try {
            throw MyCustomException; // Attempting to throw a class name, not an instance
        } catch (MyCustomException e) {
            System.out.println(e.getMessage());
        }
    }
}
Q420 medium code output
What is the output of the following Java code snippet?
java
import java.io.BufferedWriter;
import java.io.StringWriter;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        StringWriter sw = new StringWriter();
        BufferedWriter bw = new BufferedWriter(sw);
        bw.write("First");
        bw.newLine(); // Uses default system separator
        System.setProperty("line.separator", "---");
        bw.write("Second");
        bw.newLine(); // Uses new system separator
        bw.write("Third");
        bw.close();
        System.out.print(sw.toString());
    }
}
← Prev 1920212223 Next → Page 21 of 200 · 3994 questions