☕ Java MCQ Questions – Page 34

Questions 661–680 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q661 medium
What is the primary advantage of using `String.format()` over simple `+` concatenation for constructing complex formatted strings?
Q662 medium
When `BufferedReader` is chained with `InputStreamReader`, what is the specific role of `InputStreamReader`?
Q663 hard
Which of the following elements cannot be overridden in Java?
Q664 hard code error
What compile-time error occurs when attempting to compile this Java code?
java
class Base {
    private Base() {
        System.out.println("Base constructor");
    }
}

class Derived extends Base {
    public Derived() {
        super();
        System.out.println("Derived constructor");
    }
}
Q665 medium
Which pair of methods in the Java `Queue` interface is designed to insert an element, with one throwing an exception on failure and the other returning a special value?
Q666 medium
What is the scope of a variable declared in the initialization section of a traditional Java `for` loop (e.g., `for (int i = 0; ...)`)?
Q667 hard
What is the primary difference in how `String.chars()` and `String.codePoints()` represent supplementary Unicode characters (those requiring surrogate pairs)?
Q668 easy code output
What is the output of this code?
java
class Worker {
    private int taskCount = 0;
    public void doWork() {
        synchronized (this) {
            taskCount++;
            System.out.print("Worker " + taskCount + ": ");
        }
        System.out.print("Finished. ");
    }
}

public class MyProgram {
    public static void main(String[] args) throws InterruptedException {
        Worker worker = new Worker();
        Thread t1 = new Thread(() -> worker.doWork());
        Thread t2 = new Thread(() -> worker.doWork());
        t1.start();
        t2.start();
        t1.join();
        t2.join();
    }
}
Q669 hard
When is `java.util.LinkedList` a more suitable choice than `java.util.ArrayList` for a `List` implementation?
Q670 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 = "Hello\nWorld\nJava";
        try (BufferedReader br = new BufferedReader(new StringReader(data))) {
            System.out.println(br.readLine());
            System.out.println(br.readLine());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Q671 easy code output
What does this Java code print?
java
public class Main {
    public static void main(String[] args) {
        int value = 7;
        if (value % 2 == 0) {
            System.out.println("Even");
        } else {
            System.out.println("Odd");
        }
    }
}
Q672 medium code error
What is the compile-time error in this Java code?
java
interface Calculator {
    int calculate(int a, int b);
}

public class LambdaError {
    public static void performCalculation(Calculator calc) {
        System.out.println(calc.calculate(10, 5));
    }

    public static void main(String[] args) {
        performCalculation((a, b) -> System.out.println(a + b)); // Missing return
    }
}
Q673 medium code error
What is the compile-time error in the following Java code?
java
class MyClass {
    private int data;

    public void MyClass(int data) { // This looks like a constructor but has a return type
        this.data = data;
        System.out.println("Constructor-like method called");
    }

    public static void main(String[] args) {
        MyClass obj = new MyClass(); // Attempting to call default constructor
    }
}
Q674 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(3, "C");
        treeMap.put(1, "A");
        treeMap.put(2, "B");
        System.out.println(treeMap.size());
    }
}
Q675 easy
What is the primary advantage of using `StringBuffer` over `String` when performing many string modifications (e.g., concatenations) in a loop?
Q676 easy code error
What will happen when the following Java code is executed?
java
import java.util.Queue;
import java.util.LinkedList;

public class QueueError {
    public static void main(String[] args) {
        Queue<String> messages = new LinkedList<>();
        messages.add("Start");
        messages.remove();
        System.out.println(messages.remove());
    }
}
Q677 easy code error
What kind of error will occur when compiling this Java code?
java
public class Main {
    public static void main(String[] args) {
        int num = 1;
        if (num == 1) {
            System.out.println("One")
            System.out.println("Value is 1");
        }
    }
}
Q678 medium code error
What kind of error will occur when executing this Java code?
java
import java.util.TreeSet;

class MyObject {
    int id;
    public MyObject(int id) {
        this.id = id;
    }
}

public class Main {
    public static void main(String[] args) {
        TreeSet<MyObject> set = new TreeSet<>();
        set.add(new MyObject(1));
        set.add(new MyObject(2));
        System.out.println(set.size());
    }
}
Q679 easy
Where are local variables typically declared in Java?
Q680 hard code error
What compilation error will occur when compiling the following Java code?
java
import java.util.function.Consumer;
import java.util.function.Supplier;

public class LambdaReturnTypeMismatch {
    public static void main(String[] args) {
        Consumer<String> consumer = () -> "Hello";
    }
}
← Prev 3233343536 Next → Page 34 of 200 · 3994 questions