☕ Java MCQ Questions – Page 128

Questions 2541–2560 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2541 easy code error
What is wrong with this Java code?
java
import java.util.function.Function;

public class Test {
    public static void main(String[] args) {
        String greeting = "Hello";
        Function<String, String> appender = greeting::concat(" World");
    }
}
Q2542 medium code error
Which exception will be thrown when executing this Java code?
java
public class StringError4 {
    public static void main(String[] args) {
        String s1 = null;
        String s2 = "test";
        boolean result = s1.equals(s2);
        System.out.println(result);
    }
}
Q2543 easy code error
What is the result of compiling this Java code?
java
public class Main {
    public static void someMethod() {
        continue;
    }
    public static void main(String[] args) {
        someMethod();
    }
}
Q2544 hard code error
What is the most likely error when executing this Java code snippet?
java
import java.io.BufferedWriter;
import java.io.StringWriter;
import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        try {
            BufferedWriter bw = new BufferedWriter(new StringWriter(), 0);
            bw.write("Hello");
            bw.flush();
        } catch (IOException e) {
            System.err.println("Error: " + e.getMessage());
        } catch (IllegalArgumentException e) {
            System.err.println("Configuration Error: " + e.getMessage());
        }
    }
}
Q2545 easy code output
What does this Java code print?
java
public class OverloadDemo4 {
    void process(long l) {
        System.out.println("Processing long: " + l);
    }
    void process(double d) {
        System.out.println("Processing double: " + d);
    }
    public static void main(String[] args) {
        OverloadDemo4 obj = new OverloadDemo4();
        obj.process(15);
    }
}
Q2546 medium code error
Predict the runtime error for the given Java program.
java
import java.util.TreeMap;
import java.util.Comparator;

public class Main {
    public static void main(String[] args) {
        TreeMap<String, Integer> map = new TreeMap<>();
        map.put("one", 1);
        map.put("two", 2);
        TreeMap<Integer, String> anotherMap = new TreeMap<>();
        anotherMap.putAll(map); // Intentional error point
    }
}
Q2547 easy code error
What compile-time error will occur in the `faultyMethod`?
java
class MyCheckedException extends Exception {
    public MyCheckedException(String message) {
        super(message);
    }
}

public class Main {
    public void faultyMethod() { 
        // This method declares no 'throws' clause
        throw new MyCheckedException("Error in method"); 
    }

    public static void main(String[] args) {
        Main obj = new Main();
        try {
            obj.faultyMethod();
        } catch (MyCheckedException e) {
            System.out.println(e.getMessage());
        }
    }
}
Q2548 easy code output
What does this code print?
java
public class DataTypeTest {
    public static void main(String[] args) {
        int value1 = 7;
        double value2 = 3.0;
        double sum = value1 + value2;
        System.out.println(sum);
    }
}
Q2549 easy code output
What is the output of this code?
java
import java.io.BufferedWriter;
import java.io.StringWriter;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        StringWriter stringWriter = new StringWriter();
        try (BufferedWriter writer = new BufferedWriter(stringWriter)) {
            writer.write("Data one.");
            System.out.println("Before close: " + stringWriter.toString().replace("\n", "\\n"));
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
        System.out.println("After close: " + stringWriter.toString().replace("\n", "\\n"));
    }
}
Q2550 easy code error
What is the error in this Java code?
java
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterError6 {
    public static void main(String[] args) {
        FileWriter writer = null;
        try {
            writer = new FileWriter("somefile.txt");
            writer.write('A'); // Writing a single character
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            writer.close(); // Potential NullPointerException here
        }
    }
}
Q2551 easy code error
What is wrong with this Java code?
java
import java.util.function.Supplier;

public class Test {
    public static void main(String[] args) {
        Supplier<String> printer = () -> System.out.println("Hello");
    }
}
Q2552 medium
Which of the following primitive data types CANNOT be used as the argument for a Java `switch` statement or expression?
Q2553 hard code error
What is the result of compiling and running the following Java code snippet?
java
public class StringError {
    public static void main(String[] args) {
        String s = "developer";
        System.out.println(s.substring(5, 2));
    }
}
Q2554 medium
What is the main purpose of the `serialVersionUID` field in a `Serializable` class?
Q2555 medium code error
What exception will be thrown when running this Java code snippet?
java
import java.util.PriorityQueue;

class CustomObject { /* No Comparable implementation */ }

public class QueueError {
    public static void main(String[] args) {
        PriorityQueue<CustomObject> pq = new PriorityQueue<>();
        pq.add(new CustomObject());
        pq.add(new CustomObject()); // This will cause an error
    }
}
Q2556 medium code output
What is the output of this Java code?
java
import java.util.function.Supplier;

class MyClass {
    String message = "Default";
    MyClass() {
        System.out.println("MyClass constructor called.");
    }
    public String getMessage() { return message; }
}

public class MethodRefTest {
    public static void main(String[] args) {
        Supplier<MyClass> factory = MyClass::new;
        MyClass obj = factory.get();
        System.out.println(obj.getMessage());
    }
}
Q2557 medium code output
What does the following Java code print to the console?
java
public class SwitchTest {
    public static void main(String[] args) {
        int value = 0;
        String message = "Start";
        switch (value) {
            case 0:
                message += "-Zero";
            case 1:
                message += "-One";
                break;
            case 2:
                message += "-Two";
                break;
            default:
                message += "-Default";
        }
        System.out.println(message);
    }
}
Q2558 medium code error
What is the result of attempting to run the following Java code?
java
public class Main {
    public static void main(String[] args) {
        Object lock = new Object();
        try {
            lock.wait(); // This line
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        System.out.println("Finished waiting.");
    }
}
Q2559 hard code error
What error will this code throw when `thread1` and `thread2` try to execute their run methods?
java
import java.util.concurrent.locks.ReentrantLock;

public class DeadlockExample {
    private static ReentrantLock lock1 = new ReentrantLock();
    private static ReentrantLock lock2 = new ReentrantLock();

    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> {
            lock1.lock();
            try { Thread.sleep(100); } catch (InterruptedException e) {}
            lock2.lock(); // Tries to acquire lock2
        });

        Thread thread2 = new Thread(() -> {
            lock2.lock();
            try { Thread.sleep(100); } catch (InterruptedException e) {}
            lock1.lock(); // Tries to acquire lock1
        });

        thread1.start();
        thread2.start();
    }
}
Q2560 medium
What is the significance of the semicolon at the end of a `do-while` loop's `while` condition (e.g., `do { /* ... */ } while (condition);`)?
← Prev 126127128129130 Next → Page 128 of 200 · 3994 questions