☕ Java MCQ Questions – Page 10

Questions 181–200 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q181 hard code error
What is the compile-time error when attempting to create an instance of the Singleton class?
java
class Singleton {
    private Singleton() {
        System.out.println("Singleton instance created.");
    }
    // Assume a static factory method exists for proper instantiation, but we're ignoring it.
}

class Main {
    public static void main(String[] args) {
        Singleton s = new Singleton();
    }
}
Q182 medium
What is the implication of declaring a method as `final` in Java?
Q183 easy code error
What kind of error will occur when executing the following Java code?
java
public class DualLock {
    private final Object lockA = new Object();
    private final Object lockB = new Object();

    public void performWait() {
        synchronized (lockA) { // Synchronizing on lockA
            try {
                lockB.wait(); // Calling wait() on lockB
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }

    public static void main(String[] args) {
        DualLock dualLock = new DualLock();
        new Thread(dualLock::performWait).start();
    }
}
Q184 medium
Consider a scenario where multiple `Thread` objects are created using the *same single instance* of a `Runnable` implementation. What is a consequence of this design?
Q185 easy code output
What does this code print?
java
import java.io.FileReader;
import java.io.IOException;

public class FileReaderTest {
    public static void main(String[] args) {
        // Assume test.txt exists and contains a single character '1'
        FileReader reader = null;
        try {
            reader = new FileReader("test.txt");
            System.out.print((char) reader.read());
        } catch (IOException e) {
            System.out.print("Error");
        } finally {
            try {
                if (reader != null) reader.close();
            } catch (IOException e) {
                System.out.print("Close Error");
            }
        }
    }
}
Q186 hard
Consider a `for` loop where a complex computation `result = expensiveFunction(constantValue)` is performed repeatedly, but `constantValue` does not change across loop iterations. What is the most likely behavior of a modern Java JIT compiler regarding this computation?
Q187 easy
What happens if the condition expression in a `for` loop evaluates to `false` from the very beginning?
Q188 hard code error
What error will occur when the following Java code snippet is run?
java
import java.io.*;

public class FileWriterError4 {
    public static void main(String[] args) {
        String filename = null;
        try {
            FileWriter fw = new FileWriter(filename); // filename is null
            fw.write("Data");
            fw.close();
        } catch (IOException e) {
            System.out.println("Caught IOException: " + e.getMessage());
        } catch (NullPointerException e) {
            System.out.println("Caught NullPointerException: " + e.getMessage());
        }
    }
}
Q189 hard
Consider the Java snippet: `short s = 5; s += 10.5;`. Which statement accurately describes the compilation outcome and the final value of `s`?
Q190 easy
`StringBuffer` in Java is designed to be thread-safe. What mechanism primarily contributes to its thread-safety?
Q191 medium
Which method of `BufferedReader` is commonly used to read an entire line of text, including the line terminator, and returns `null` at the end of the stream?
Q192 easy
Which Java primitive data type can only hold the values `true` or `false`?
Q193 hard code error
What is the error in this Java code?
java
public class LoopScopeError {
    public static void main(String[] args) {
        int i = 0;
        while (i < 3) {
            String message = "Current iteration: " + i;
            i++;
        }
        System.out.println(message);
    }
}
Q194 hard
Consider a method `void outerMethod()` which has a local variable `int x = 10;`. Inside `outerMethod()`, you define both an anonymous inner class and a lambda expression. If you declare another local variable `int x = 20;` inside the anonymous inner class, and attempt to do the same inside the lambda, what would be the outcome?
Q195 easy code error
What kind of error will occur when executing the following Java code?
java
public class MyThreadPriority {
    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            System.out.println("Child thread running with priority: " + Thread.currentThread().getPriority());
        });
        t.setPriority(Thread.MAX_PRIORITY); // Set priority before start
        t.start();
        t.setPriority(Thread.MIN_PRIORITY); // Attempt to change priority after starting
        System.out.println("Main thread finished.");
    }
}
Q196 hard
In a scenario with deeply nested loops, what is the precise impact of using `continue label;` compared to a simple `continue;` statement if `label` refers to one of the outer loops?
Q197 medium code error
What kind of error will occur when running this Java code?
java
public class ArrayError {
    public static void main(String[] args) {
        Number[] nums = new Integer[5];
        nums[0] = 3.14;
        System.out.println(nums[0]);
    }
}
Q198 medium
What is the purpose of the `Thread.yield()` method?
Q199 medium code output
What does this Java code print to the console?
java
import java.util.function.Function;

public class Test {
    public static void main(String[] args) {
        Function<String, Integer> toInt = Integer::parseInt;
        Function<Integer, String> toString = String::valueOf;
        Function<Integer, Integer> timesTwo = x -> x * 2;

        Function<String, String> f1 = toInt.andThen(timesTwo).andThen(toString);
        Function<String, String> f2 = toString.compose(timesTwo).compose(toInt);

        System.out.println(f1.apply("5"));
        System.out.println(f2.apply("5"));
    }
}
Q200 easy code error
Identify the compile-time error in the provided Java code.
java
public class DataTypeFloatError {
    public static void main(String[] args) {
        float pi = 3.14;
        System.out.println(pi);
    }
}
← Prev 89101112 Next → Page 10 of 200 · 3994 questions