☕ Java MCQ Questions – Page 63

Questions 1241–1260 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1241 easy
In a standard Queue, elements are typically added to which end and removed from which end?
Q1242 medium code error
What error will this code produce at runtime?
java
public class LoopTest {
    public static void main(String[] args) {
        String[] names = new String[3];
        names[0] = "Alice";
        names[1] = "Bob";
        // names[2] remains null
        int i = 0;
        while (i < names.length) {
            System.out.println(names[i].toUpperCase());
            i++;
        }
    }
}
Q1243 hard code error
Consider the following Java code snippet. What kind of error will occur when this code is executed?
java
import java.io.*;

public class FileWriterError1 {
    public static void main(String[] args) {
        File dir = new File("my_test_directory");
        dir.mkdir(); // Ensure directory exists
        try {
            FileWriter fw = new FileWriter(dir); // Attempt to create FileWriter with a directory
            fw.write("Some data");
            fw.close();
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        } finally {
            dir.delete(); // Clean up
        }
    }
}
Q1244 medium
A thread calls `Thread.yield()`. What is the immediate effect on the calling thread's state?
Q1245 easy
Is Java's `java.util.HashMap` inherently thread-safe?
Q1246 hard
You have a hierarchy of custom exceptions: `BaseAppException` -> `DataAccessException` -> `ConstraintViolationException`. If a method throws `ConstraintViolationException`, and another method `handleException(BaseAppException e)` is called with this exception, what is the most specific type of catch block that will *also* catch `ConstraintViolationException` without catching `BaseAppException` directly?
Q1247 easy
Which of the following operations CANNOT be performed using the `java.io.File` class directly?
Q1248 easy
Which operator represents the logical AND operation in Java?
Q1249 hard code output
What is the output of this code?
java
interface SpeakerA {
    default void greet() { System.out.println("Hello from A"); }
}

interface SpeakerB {
    default void greet() { System.out.println("Hello from B"); }
}

class HybridSpeaker implements SpeakerA, SpeakerB {
    // No override for greet()
}

public class DiamondProblemTest {
    public static void main(String[] args) {
        // new HybridSpeaker().greet(); // This line would cause the error
    }
}
Q1250 hard code error
What kind of error will occur when executing the following Java code?
java
import java.util.concurrent.LinkedBlockingQueue;

public class QError8 {
    public static void main(String[] args) {
        LinkedBlockingQueue<String> lbq = new LinkedBlockingQueue<>(5);
        try {
            lbq.put("Item A");
            lbq.put(null);
            System.out.println(lbq.take());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Q1251 easy code error
What kind of error will occur when compiling this Java code?
java
class SecretAgent {
    private SecretAgent() {
        System.out.println("Agent created");
    }
}

class JamesBond extends SecretAgent {
    public JamesBond() {
        super(); // Attempting to call private constructor
        System.out.println("007 created");
    }
}

public class Main {
    public static void main(String[] args) {
        JamesBond jb = new JamesBond();
    }
}
Q1252 easy
What is it called when a subclass provides a specific implementation for a method that is already defined in its superclass with the exact same signature?
Q1253 hard
When `BufferedReader` wraps a `FileReader` (e.g., `new BufferedReader(new FileReader("file.txt"))`), how does this differ fundamentally in character encoding handling compared to wrapping an `InputStreamReader` (e.g., `new BufferedReader(new InputStreamReader(new FileInputStream("file.txt")))`)?
Q1254 hard code output
What does this code print?
java
@FunctionalInterface
interface Calculator {
    int calculate(int a, int b);

    default int add(int a, int b) {
        return a + b;
    }

    static int multiply(int a, int b) {
        return a * b;
    }
}

public class CustomFIWithMethods {
    public static void main(String[] args) {
        Calculator subtractor = (a, b) -> a - b;
        Calculator divider = (a, b) -> a / b;

        System.out.println("Subtract: " + subtractor.calculate(10, 5));
        System.out.println("Add using subtractor: " + subtractor.add(10, 5));
        System.out.println("Add using divider: " + divider.add(10, 5));
        System.out.println("Multiply static: " + Calculator.multiply(10, 5));
    }
}
Q1255 hard
When `clone()` is invoked on an array of `String` objects (e.g., `String[] original = {"A", "B"}; String[] cloned = original.clone();`), what is the relationship between `original` and `cloned`?
Q1256 medium code error
Which exception is expected to be printed to the console by the given Java code?
java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter("log.txt"))) {
            writer.write("Message 1");
            writer.flush();
            writer.close();
            writer.flush(); // Attempt to flush after close
        } catch (IOException e) {
            System.out.println(e.getClass().getSimpleName() + ": " + e.getMessage());
        }
    }
}
Q1257 medium
Consider `Optional<String> optional = Optional.empty();`. Which statement correctly describes the key difference between `optional.orElse("Default")` and `optional.orElseGet(() -> "Default")`?
Q1258 hard code error
What is the error encountered when running this Java code?
java
public class JaggedArrayError {
    public static void main(String[] args) {
        int[][] matrix = new int[3][];
        matrix[0] = new int[2];
        matrix[1] = new int[3];
        // matrix[2] is not initialized

        System.out.println(matrix[1][2]); // Valid access
        System.out.println(matrix[2][0]); // Problematic access
    }
}
Q1259 hard
If an abstract class `A` has two abstract methods `method1()` and `method2()`, and a non-abstract class `B` extends `A` but only provides an implementation for `method1()`, what are the implications for class `B`?
Q1260 medium code output
What is the state of `t2` at point Y in the output?
java
public class ThreadStateDemo6 {
    private static final Object lock = new Object();
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            synchronized (lock) {
                try { Thread.sleep(200); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
            }
        });
        Thread t2 = new Thread(() -> {
            synchronized (lock) {
                System.out.println("T2 acquired lock and is RUNNABLE.");
                try { Thread.sleep(100); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
            }
            System.out.println("T2 finished.");
        });
        t1.start();
        Thread.sleep(50); t2.start(); Thread.sleep(50); 
        System.out.println("State of t2 at point X: " + t2.getState());
        Thread.sleep(200); // Allow t1 to finish and t2 to acquire lock
        System.out.println("State of t2 at point Y: " + t2.getState());
    }
}
← Prev 6162636465 Next → Page 63 of 200 · 3994 questions