☕ Java MCQ Questions – Page 171

Questions 3401–3420 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3401 hard code error
Does this Java code compile? If not, what is the compile-time error?
java
import java.io.IOException;
public class UnreachableThrow {
    public String execute() throws IOException {
        System.out.println("Executing...");
        if (true) {
            return "Success";
        }
        throw new IOException("Should not reach here");
    }
    public static void main(String[] args) {
        // new UnreachableThrow().execute();
    }
}
Q3402 medium code output
What is the content of 'output.txt' after this code executes?
java
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterTest {
    public static void main(String[] args) {
        try (FileWriter writer = new FileWriter("output.txt")) {
            writer.write("Hello, Java!");
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}
Q3403 easy
Every Java application has at least one thread of execution. What is this initial thread commonly called?
Q3404 easy
Can a Java class have more than one constructor?
Q3405 medium code error
What is the error in this Java code snippet?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Start");
        sb.insert("World");
        System.out.println(sb);
    }
}
Q3406 easy code output
What is the output of this Java code?
java
import java.util.function.Predicate;

public class Main {
    public static void main(String[] args) {
        Predicate<Integer> isEven = num -> num % 2 == 0;
        System.out.println(isEven.test(10));
    }
}
Q3407 hard
A thread is in the `TIMED_WAITING` state due to `Thread.sleep(long milliseconds)`. If another thread calls `interrupt()` on it, what is the primary consequence for the sleeping thread?
Q3408 hard code error
What is the compilation error in the following Java code snippet?
java
import java.util.function.Supplier;

public class Main {
    public static void main(String[] args) {
        Supplier<String> valueSupplier = () -> {
            if (System.currentTimeMillis() % 2 == 0) {
                return "Even time";
            } else {
                return 123; // This line will cause an error
            }
        };
        System.out.println(valueSupplier.get());
    }
}
Q3409 hard code error
What is the compilation or runtime error when executing this Java code snippet?
java
import java.util.TreeSet;

class Person {
    String name;
    Person(String name) { this.name = name; }
}

public class Main {
    public static void main(String[] args) {
        TreeSet<Person> people = new TreeSet<>();
        people.add(new Person("Alice"));
        people.add(new Person("Bob"));
        System.out.println(people.size());
    }
}
Q3410 hard
A `HashSet` contains a mutable object `M`. After `M` is added, a field within `M` that is used in its `hashCode()` calculation is modified. What is the most likely consequence when attempting to retrieve or remove `M` from the `HashSet` using `contains(M)` or `remove(M)`?
Q3411 hard code error
What error will this code produce at compile time?
java
public class MultiInitForLoopError {
    public static void main(String[] args) {
        int x = 10;
        for (int i = 0, x = 20; i < 5; i++) {
            System.out.println(i + x);
        }
    }
}
Q3412 hard
Which statement accurately describes the behavior of Java daemon threads upon JVM shutdown?
Q3413 medium code error
What error occurs when running this code?
java
import java.io.FileReader;
import java.io.IOException;

public class MyClass {
    public static void main(String[] args) {
        try (FileReader reader = new FileReader(null)) { // Passing null
            System.out.println("Reader created.");
        } catch (IOException e) {
            System.err.println("Caught an IOException: " + e.getMessage());
        }
    }
}
Q3414 easy
How many bytes does the `byte` primitive data type occupy in Java?
Q3415 hard code output
What does this code print?
java
public class Q3_InterruptSleep {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            try {
                Thread.sleep(500);
                System.out.println("Slept normally.");
            } catch (InterruptedException e) {
                System.out.println("Interrupted during sleep.");
                Thread.currentThread().interrupt();
            }
            System.out.println("Thread finished.");
        });
        t.start();
        Thread.sleep(50);
        t.interrupt();
        Thread.sleep(50);
        System.out.println("Main: " + t.getState());
    }
}
Q3416 hard
In a multi-threaded Java application, a `while` loop's condition depends on a boolean flag updated by another thread. What is the most robust mechanism to ensure visibility of the flag's updates to the `while` loop and prevent potential indefinite spinning due to memory visibility issues?
Q3417 easy
Which of the following best describes the nature of `StringBuffer` objects in Java?
Q3418 medium code output
What does this code print?
java
import java.util.LinkedList;

public class Test {
    public static void main(String[] args) {
        LinkedList<Integer> list = new LinkedList<>();
        list.add(10);
        list.addFirst(5);
        list.addLast(20);
        list.removeFirst();
        list.add(15);
        System.out.println(list);
    }
}
Q3419 medium code error
What is the outcome when this Java code is executed?
java
import java.util.LinkedList;
import java.util.NoSuchElementException;

public class Test {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        String element = list.remove();
        System.out.println(element);
    }
}
Q3420 hard
Given the following Java class: java class Solver { void solve(long l, Integer i) { System.out.println("long, Integer"); } void solve(Integer i, long l) { System.out.println("Integer, long"); } } What is the result of compiling and running the following code? `new Solver().solve(5, 5);`
← Prev 169170171172173 Next → Page 171 of 200 · 3994 questions