☕ Java MCQ Questions – Page 49

Questions 961–980 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q961 easy code output
What does this Java code print?
java
import java.util.function.Predicate;

public class Main {
    public static void main(String[] args) {
        int limit = 20;
        Predicate<Integer> isUnderLimit = num -> num < limit;
        System.out.println(isUnderLimit.test(15));
    }
}
Q962 easy
What is the initial state of a thread immediately after it has been created using `new Thread()` but before its `start()` method is invoked?
Q963 hard
Consider a class `MyObject` that overrides `equals()` to compare objects based on their `id` field but does *not* override `hashCode()`. If two `MyObject` instances, `obj1` and `obj2`, have the same `id` (meaning `obj1.equals(obj2)` is true) but are distinct objects, what would be the most common outcome after adding both `obj1` and `obj2` to a `HashSet`?
Q964 medium
What is the underlying data structure primarily used by `HashSet` in Java?
Q965 medium code error
What will be the compilation error in this code?
java
class Parent {
    public void action() {
        System.out.println("Parent's action.");
    }
}

class Child extends Parent {
    @Override
    private void action() {
        System.out.println("Child's action.");
    }
}
Q966 medium
Is `FileWriter` inherently buffered for performance optimization?
Q967 medium code error
What is the output of this Java code?
java
public class ExceptionFlow9 {
    public static void main(String[] args) {
        testMethod();
    }

    public static void testMethod() {
        try {
            System.out.println("In try");
            throw new IllegalArgumentException("From try");
        } catch (IllegalArgumentException e) {
            System.out.println("In catch: " + e.getMessage());
        } finally {
            System.out.println("In finally");
            throw new IllegalStateException("From finally");
        }
    }
}
Q968 hard code error
What type of error will occur when compiling this Java code?
java
public class StaticForwardRef {
    static int sum = num1 + num2; // Error line
    static int num1 = 5;
    static int num2 = 10;

    public static void main(String[] args) {
        System.out.println(sum);
    }
}
Q969 easy code error
What type of error will occur when running this Java code?
java
public class Broadcaster {
    private final Object eventLock = new Object();

    public void broadcastEvent() {
        // Missing synchronized(eventLock)
        eventLock.notifyAll();
        System.out.println("All notified!");
    }

    public static void main(String[] args) {
        Broadcaster b = new Broadcaster();
        b.broadcastEvent();
    }
}
Q970 easy code output
What does this Java code print?
java
public class LoopTest {
    public static void main(String[] args) {
        int num = 0;
        String s = "";
        do {
            s += num;
            num += 2;
        } while (num < 6);
        System.out.print(s);
    }
}
Q971 medium
Which type of object can `BufferedReader` directly wrap in its constructor to provide buffering capabilities?
Q972 medium code output
What is the output of this code?
java
import java.util.ArrayDeque;
import java.util.Queue;

public class ArrayDequeAsQueue {
    public static void main(String[] args) {
        Queue<Character> queue = new ArrayDeque<>();
        queue.add('X');
        queue.offer('Y');
        queue.add('Z');
        System.out.print(queue.size());
        System.out.print(queue.poll());
        System.out.print(queue.peek());
    }
}
Q973 hard code output
What is the output of this code?
java
public class DataTypeChallenge {
    public static void main(String[] args) {
        byte b1 = (byte) 200;
        byte b2 = 100;
        int sum = b1 + b2;
        byte b3 = (byte) (b1 + b2);
        System.out.println(sum + ", " + b3);
    }
}
Q974 hard
You have a `List<List<Integer>> listOfLists;`. Which approach is the most efficient and idiomatic way to create a single `Iterator<Integer>` that provides sequential access to all integers from all inner lists, effectively "flattening" the structure, without explicitly copying all elements into a new single list beforehand?
Q975 hard
What is the return value of `new String("abc").indexOf("")`?
Q976 easy code output
What is the output of this Java code?
java
import java.io.FileNotFoundException;

public class MyClass {
    public static void processFile() throws FileNotFoundException {
        System.out.println("Processing...");
        if (true) {
            throw new FileNotFoundException("File not found exception");
        }
    }

    public static void main(String[] args) {
        try {
            processFile();
        } catch (Exception e) { // Catching a broader exception type
            System.out.println("Caught exception: " + e.getClass().getName());
        }
    }
}
Q977 medium
Which `StringBuffer` method allows reversing the order of the characters in the sequence?
Q978 medium
What is the primary purpose of the `Runnable` interface in Java?
Q979 medium code error
What is the compile-time error in this Java code?
java
interface MyTransformer<T, R> {
    R transform(T t);
}

public class LambdaError {
    public static void main(String[] args) {
        MyTransformer<String, Integer> stringToInt = str -> {
            return str.length();
        };

        MyTransformer<String, String> identity = (String s) -> s;
        
        // Incorrect assignment
        MyTransformer<String, Integer> incompatible = identity;
    }
}
Q980 easy code error
What happens when you compile the following Java code?
java
public class Main {
    public static void main(String[] args) {
        int i = 0;
        do {
            System.out.println(i);
            i++;
            if (i == 2) {
                continue;
            }
        } while (false); // Loop only runs once
        System.out.println("End of program");
    }
}
← Prev 4748495051 Next → Page 49 of 200 · 3994 questions