☕ Java MCQ Questions – Page 196

Questions 3901–3920 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3901 easy code output
What is the output of this Java code?
java
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        TreeMap<String, Integer> treeMap = new TreeMap<>();
        treeMap.put("banana", 2);
        treeMap.put("apple", 1);
        treeMap.put("cherry", 3);
        System.out.println(treeMap.firstKey());
    }
}
Q3902 hard
Which statement accurately describes the typical use case for a `LinkedBlockingDeque` when compared to a `LinkedBlockingQueue`?
Q3903 easy
Which of the following syntax is correct for an explicit type cast in Java?
Q3904 medium code error
What error will occur when compiling or running this Java code?
java
public class ArrayError {
    public static void main(String[] args) {
        int[] data = null;
        System.out.println(data[0]);
    }
}
Q3905 medium code error
What error will occur when compiling or running this Java code?
java
public class ArrayError {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30};
        System.out.println(numbers[3]);
    }
}
Q3906 hard code error
What is the runtime error when executing this Java code snippet?
java
import java.util.TreeSet;
import java.util.Objects;

class Item implements Comparable<Item> {
    String name;
    Item(String name) { this.name = name; }

    @Override
    public int compareTo(Item other) {
        return Objects.requireNonNull(other.name).compareTo(this.name);
    }
}

public class Main {
    public static void main(String[] args) {
        TreeSet<Item> items = new TreeSet<>();
        items.add(new Item("Banana"));
        items.add(new Item(null));
        System.out.println(items.size());
    }
}
Q3907 medium
Given a String `s = "Java Programming"`, what would be the result of calling `s.substring(5, 12)`?
Q3908 easy code error
What type of error will occur when running this Java code?
java
public class MixedLocks {
    private final Object lockA = new Object();
    private final Object lockB = new Object();

    public void doWork() throws InterruptedException {
        synchronized (lockA) {
            System.out.println("Lock A acquired.");
            lockB.wait(); // Calling wait on lockB, while holding lockA
            System.out.println("Work finished.");
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MixedLocks m = new MixedLocks();
        m.doWork();
    }
}
Q3909 hard
Why can an `abstract` class define a constructor in Java, even though it cannot be instantiated directly?
Q3910 easy
What is the primary purpose of the `catch` block in Java exception handling?
Q3911 easy code error
What will be the compilation error in the following Java code?
java
import java.util.function.Function;

@FunctionalInterface
interface MyProcessor {
    void processData(String data);
    int calculateHash(String data);
}

public class Main {
    public static void main(String[] args) {
        // Attempt to use MyProcessor
    }
}
Q3912 medium
For a functional interface `Function<String, Integer>`, which method reference type correctly maps to a method that takes a `String` and returns its length?
Q3913 medium
In terms of when the condition is checked, how is a `do-while` loop classified?
Q3914 easy code output
What does this Java code print?
java
import java.io.File;

public class FileTest {
    public static void main(String[] args) {
        File dir = new File("temp_dir_q6");
        boolean created = dir.mkdir();
        System.out.println(created + " " + dir.isDirectory());
        dir.delete(); // Clean up
    }
}
Q3915 medium code output
What is the output of this Java code?
java
public class Main {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // ignore
            }
        });
        System.out.println(t.getState());
        t.start();
        Thread.sleep(10);
        System.out.println(t.getState());
        Thread.sleep(150);
        System.out.println(t.getState());
    }
}
Q3916 hard code output
What is the output of this code?
java
import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        try {
            performOperation();
        } catch (IOException e) {
            System.out.print("Caught IOException: " + e.getMessage());
        } catch (RuntimeException e) {
            System.out.print("Caught RuntimeException: " + e.getMessage());
        } finally {
            System.out.print(" Outer Finally");
        }
    }

    public static void performOperation() throws IOException {
        try {
            System.out.print("Inner Try ");
            throw new IOException("IO Issue");
        } catch (IOException e) {
            System.out.print("Inner Catch IO ");
        } finally {
            System.out.print("Inner Finally ");
            throw new RuntimeException("Finally Runtime Issue");
        }
    }
}
Q3917 medium code error
Which statement correctly identifies the error in this Java code that defines a static and an instance method with the same signature?
java
public class OverloadChecker {
    public static void performAction(int value) {
        System.out.println("Static action: " + value);
    }

    public void performAction(int value) { // This line causes the error
        System.out.println("Instance action: " + value);
    }

    public static void main(String[] args) {
        OverloadChecker.performAction(100);
    }
}
Q3918 medium code output
What does this chained StringBuilder operation print?
java
public class StringBuilderTest {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        sb.append("A").insert(1, "B").delete(0, 1).append("C");
        System.out.println(sb);
    }
}
Q3919 hard
When a `BufferedWriter` wraps an `OutputStreamWriter` (e.g., `new BufferedWriter(new OutputStreamWriter(new FileOutputStream("file.txt"), "UTF-8"))`), which component in this chain is primarily responsible for converting characters to bytes using the specified character encoding?
Q3920 hard code error
What is the compilation outcome of the following Java code snippet?
java
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterError10 {
    public static void main(String[] args) {
        FileWriter fw;
        try (fw = new FileWriter("res_test.txt")) { // 'fw' must be effectively final or declared inside try-with-resources
            fw.write("Hello");
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}
← Prev 194195196197198 Next → Page 196 of 200 · 3994 questions