☕ Java MCQ Questions – Page 103

Questions 2041–2060 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2041 hard code error
What kind of error will occur when compiling the following Java code?
java
import java.util.Comparator;
import java.util.PriorityQueue;

class MyData {}

public class QError10 {
    public static void main(String[] args) {
        Comparator<String> stringComparator = (s1, s2) -> s1.compareTo(s2);
        PriorityQueue<MyData> pq = new PriorityQueue<>(stringComparator);
        pq.add(new MyData());
        System.out.println(pq.poll());
    }
}
Q2042 easy code error
What kind of error will occur when compiling this Java code?
java
import java.io.BufferedWriter;
import java.io.FileWriter;

public class Main {
    public static void main(String[] args) {
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter("output.txt"));
            bw.write("Data to write.");
        } catch (Exception e) {
            e.printStackTrace();
        }
        // close() method can throw IOException and is outside a try-catch block
        bw.close();
    }
}
Q2043 medium code error
What will be the compilation error in the following Java code snippet?
java
public class ImmutableStringExample {
    public static void main(String[] args) {
        final String message = "Hello";
        message = "World"; // Attempt to reassign final variable
        System.out.println(message);
    }
}
Q2044 easy code error
Identify the compile-time error in the given Java code.
java
public class DataTypeBooleanError {
    public static void main(String[] args) {
        boolean isValid = 1;
        System.out.println(isValid);
    }
}
Q2045 medium code output
What does this Java code print?
java
import java.util.function.BiFunction;

class MathUtils {
    public static int multiply(int a, int b) {
        return a * b;
    }
}

public class MethodRefTest {
    public static void main(String[] args) {
        BiFunction<Integer, Integer, Integer> multiplier = MathUtils::multiply;
        int result = multiplier.apply(5, 3);
        System.out.println("Result: " + result);
    }
}
Q2046 easy
Which exception is typically thrown by the `FileReader` constructor if the specified file does not exist or cannot be opened for reading?
Q2047 easy
What is the primary purpose of the `try` block in Java exception handling?
Q2048 medium code output
What is the output of this Java program?
java
public class TypeOrder {
    void process(int num, String text) {
        System.out.println("Int-String: " + num + " " + text);
    }
    void process(String text, int num) {
        System.out.println("String-Int: " + text + " " + num);
    }
    public static void main(String[] args) {
        TypeOrder to = new TypeOrder();
        to.process(10, "Hello");
        to.process("World", 20);
    }
}
Q2049 easy
What is the typical average time complexity for adding an element to a `HashSet` (assuming a good hash function)?
Q2050 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.append("First");
            writer.append(' ');
            writer.append("Second");
            writer.flush();
            System.out.print(stringWriter.toString());
        } catch (IOException e) {
            System.out.print("Error");
        }
    }
}
Q2051 easy code output
What is the output of this code?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Start");
        sb.append("End").insert(5, "Middle");
        System.out.print(sb);
    }
}
Q2052 medium code error
What is the outcome of running this code?
java
public class LoopTest {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30};
        int i = 0;
        while (i <= numbers.length) {
            System.out.println(numbers[i]);
            i++;
        }
    }
}
Q2053 easy code output
What is the output of this code?
java
class Person {
    protected String name = "Default";
}

class Student extends Person {
    void displayName() {
        System.out.println("Name: " + name);
    }
}

public class Main {
    public static void main(String[] args) {
        Student s = new Student();
        s.displayName();
    }
}
Q2054 hard code error
What is the error in the following Java code?
java
abstract class Gadget {
    public abstract void activate();
    public void status() { System.out.println("Gadget OK"); }
}

class Smartwatch extends Gadget { // Missing implementation of activate()
    @Override
    public void status() { System.out.println("Smartwatch Ready"); }
}
Q2055 easy code output
What is the output of this Java program?
java
public class WhileLoopDemo {
    public static void main(String[] args) {
        int k = 3;
        while (k > 0) {
            System.out.print(k);
            k--;
        }
    }
}
Q2056 medium code output
What is the output of this Java code?
java
import java.util.TreeSet;
import java.util.Set;

public class Test {
    public static void main(String[] args) {
        Set<String> set = new TreeSet<>();
        set.add("hello");
        set.add("world");
        try {
            set.add(null);
            System.out.println(set);
        } catch (NullPointerException e) {
            System.out.println("NullPointerException caught!");
        }
    }
}
Q2057 hard code error
What error occurs when attempting to compile the following Java code?
java
public class ConditionalCompileError {
    public static void main(String[] args) {
        final boolean FLAG = true;
        if (FLAG) {
            System.out.println("Condition is true.");
        } else { // This 'else' branch is unreachable.
            System.out.println("Condition is false.");
        }
    }
}
Q2058 easy code error
Which error will occur when compiling this Java code?
java
import java.io.FileReader;
import java.io.FileNotFoundException;

public class FileReaderIssue {
    public static void main(String[] args) throws FileNotFoundException {
        FileReader reader = new FileReader("existing.txt"); // Assume existing.txt exists
        // ... some operations ...
        reader.close(); // Potential IOException, not declared or caught
    }
}
Q2059 hard
An `ImmutableObject` class is `Serializable` and correctly enforces immutability through private final fields and no setters. When deserializing an `ImmutableObject`, which approach is most effective for ensuring that the deserialized instance remains truly immutable and adheres to its creation invariants?
Q2060 easy code error
What is the error in the following Java code?
java
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        TreeSet<String> names = new TreeSet<>();
        names.add("Alice");
        names.add("Bob");
        names.add(null);
    }
}
← Prev 101102103104105 Next → Page 103 of 200 · 3994 questions