☕ Java MCQ Questions – Page 191

Questions 3801–3820 of 3994 total — Java interview practice

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

public class Main {
    public static void main(String[] args) {
        Consumer<String> greet = name -> System.out.print("Greetings, " + name + "!");
        greet.accept("Alice");
    }
}
Q3802 easy
Given `int[][] data = new int[5][10];`, how would you access the element at the 2nd row and 3rd column (0-indexed)?
Q3803 hard code error
What compilation error will occur when compiling this Java code?
java
public class MethodContinue {
    public static void processValue(int val) {
        if (val % 2 == 0) {
            continue;
        }
        System.out.println("Processed odd: " + val);
    }

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            System.out.println("Loop iteration: " + i);
            processValue(i);
        }
    }
}
Q3804 easy
Which of the following statements is true about a Java do-while loop?
Q3805 hard code output
What is the output of this code?
java
import java.util.LinkedList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        LinkedList<String> original = new LinkedList<>();
        original.add("One");
        original.add("Two");
        original.add("Three");
        original.add("Four");
        List<String> sub = original.subList(1, 3); // 'Two', 'Three'
        sub.clear(); // Clears 'Two' and 'Three' from original
        original.add("Five");
        System.out.println(original);
    }
}
Q3806 hard code error
Identify the compile-time error in the following Java code snippet.
java
class MyClass {
    static MyClass() {
        System.out.println("Static constructor?");
    }
    public MyClass(String s) {}
}
Q3807 easy
If you want to iterate through a block of code as long as a condition is true, and you're unsure if the loop needs to run even once, which loop structure is most appropriate?
Q3808 hard
What occurs if a non-positive integer (zero or negative) is passed as the `sz` parameter to the `BufferedWriter(Writer out, int sz)` constructor?
Q3809 hard
If an unchecked `RuntimeException` is thrown within the `do` block of a `do-while` loop and is not caught, what is the immediate consequence regarding the loop's execution and the `while` condition?
Q3810 easy code error
What is the error in this Java code?
java
class Parent {
    public static void greet() {
        System.out.println("Parent greets");
    }
}

class Child extends Parent {
    @Override // Line 7
    public static void greet() {
        System.out.println("Child greets");
    }
}

public class Main {
    public static void main(String[] args) {
        Child.greet();
    }
}
Q3811 easy code output
What is the output of this code?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
        sb.append("Result: ");
        sb.append(100);
        sb.append(true);
        System.out.print(sb);
    }
}
Q3812 easy
What does the `getAbsolutePath()` method of the `java.io.File` class return?
Q3813 hard
Which of the following is a primary reason why `java.lang.String` is designed to be immutable in Java?
Q3814 hard
How does a computationally expensive custom `Comparator` (e.g., one that performs complex string manipulations or database lookups) impact the performance of `TreeSet` operations like `add()`, `remove()`, and `contains()`?
Q3815 hard code error
What exception is thrown when the `oos.writeObject(container)` line is executed in the `main` method?
java
import java.io.*;

class NotSerializableClass {
    private String name;
    public NotSerializableClass(String name) { this.name = name; }
    public String getName() { return name; }
}

class SerializableContainer implements Serializable {
    private static final long serialVersionUID = 1L;
    private int id;
    private NotSerializableClass nonSerializableField; // This field is not Serializable

    public SerializableContainer(int id, String name) {
        this.id = id;
        this.nonSerializableField = new NotSerializableClass(name);
    }
}

public class SerializationError1 {
    public static void main(String[] args) throws IOException {
        SerializableContainer container = new SerializableContainer(1, "Test");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(container); // Error occurs here
        oos.close();
    }
}
Q3816 medium
Which of the following logical operators in Java exhibits short-circuiting behavior, potentially preventing the evaluation of its right-hand operand?
Q3817 easy code error
What error will this Java code produce during compilation?
java
public class ArrayError {
    public static void main(String[] args) {
        int[][] matrix = new int[2][];
        matrix[0] = new int[3][];
    }
}
Q3818 easy code output
What is the output of this code?
java
class LoginFailedException extends Exception {
    public LoginFailedException(String reason) {
        super("Login failed: " + reason);
    }
}

public class Main {
    public static void authenticate(String username, String password) throws LoginFailedException {
        if (!username.equals("user") || !password.equals("pass")) {
            throw new LoginFailedException("Invalid credentials.");
        }
    }

    public static void main(String[] args) {
        try {
            authenticate("admin", "pass");
        } catch (LoginFailedException e) {
            System.out.println(e.getMessage());
        }
        System.out.println("Program continues.");
    }
}
Q3819 hard
Which statement about Java functional interfaces is TRUE?
Q3820 easy
What type of exception is typically thrown by `FileWriter` methods like `write()` or `close()` if an I/O error occurs?
← Prev 189190191192193 Next → Page 191 of 200 · 3994 questions