☕ Java MCQ Questions – Page 164

Questions 3261–3280 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3261 hard code error
What compilation error will occur when compiling this Java code?
java
public class LabeledBlockContinue {
    public static void main(String[] args) {
        outer: {
            for (int i = 0; i < 2; i++) {
                innerBlock: {
                    if (i == 1) {
                        continue innerBlock;
                    }
                    System.out.println("i: " + i);
                }
            }
        }
    }
}
Q3262 easy code output
What is the output of this Java code?
java
import java.util.LinkedList;

public class MyClass {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");
        System.out.println(list);
    }
}
Q3263 hard code output
What is the output of this Java code?
java
import java.util.ArrayDeque;
import java.util.Deque;

public class QueueTest {
    public static void main(String[] args) {
        Deque<Integer> deque = new ArrayDeque<>();
        deque.addFirst(1);
        deque.addLast(2);
        deque.addFirst(0);
        deque.addLast(3);

        System.out.println(deque.removeFirst());
        System.out.println(deque.removeLast());
        System.out.println(deque.peekFirst());
        System.out.println(deque.size());
    }
}
Q3264 medium
When resolving overloaded method calls, if both primitive widening and autoboxing could apply to an argument, which one does the Java compiler prefer?
Q3265 medium code error
What compilation error will this Java code produce? (Assume Java 17 environment)
java
public class SwitchError {
    enum Color { RED, GREEN, BLUE }
    public static void main(String[] args) {
        Color c = Color.RED;
        String colorName = switch (c) {
            case RED -> "Red";
            case GREEN -> "Green";
        };
        System.out.println(colorName);
    }
}
Q3266 hard code error
Does this Java code compile? If not, what is the compile-time error?
java
import java.io.IOException;
import java.sql.SQLException;
class Parent {
    public void process() throws IOException {}
}
class Child extends Parent {
    @Override
    public void process() throws SQLException {}
}
public class Main {
    public static void main(String[] args) {}
}
Q3267 easy
When `continue` is executed inside a nested loop without any labels, which loop does it affect?
Q3268 easy code error
What is the compilation or runtime error in the following Java code?
java
public class Main {
    public static void main(String[] args) {
        String message = "hello";
        String upperMessage = message.upperCase();
        System.out.println(upperMessage);
    }
}
Q3269 hard code output
What is the output of this code?
java
class TestCtor {
    String message;
    TestCtor(String msg) throws IllegalArgumentException {
        System.out.print("Ctor started: " + msg + ". ");
        if (msg.equals("error")) { throw new IllegalArgumentException("Error in ctor"); }
        this.message = msg;
        System.out.print("Ctor finished: " + msg + ". ");
    }
}
public class Main {
    public static void main(String[] args) {
        TestCtor t1 = null;
        try { t1 = new TestCtor("success"); new TestCtor("error"); }
        catch (IllegalArgumentException e) { System.out.print("Caught: " + e.getMessage() + ". "); }
        finally { if (t1 != null) { System.out.print("t1.msg:" + t1.message + ". "); }}
        System.out.print("End.");
    }
}
Q3270 easy
Can a private method in a superclass be overridden in a subclass in Java?
Q3271 medium
What happens if you try to declare a `private abstract` method within an abstract class in Java?
Q3272 easy code output
What does this code print?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Java");
        sb.append("World");
        System.out.print(sb);
    }
}
Q3273 hard code error
What is the error in this Java code, specifically regarding the last statement?
java
public class UnreachableCodeLoop {
    public static void main(String[] args) {
        int count = 0;
        while (true) {
            System.out.println("Looping...");
            count++;
            if (count > 2) {
                // break; // Intentionally missing break
            }
        }
        System.out.println("Loop finished.");
    }
}
Q3274 hard
Which statement accurately describes an 'effectively immutable' object?
Q3275 easy code output
What does this Java code print?
java
public class LoopTest {
    public static void main(String[] args) {
        int count = 1;
        do {
            System.out.println(count);
            count++;
        } while (count <= 3);
    }
}
Q3276 medium code error
What compile-time error will occur in this Java code snippet?
java
public class LoopError {
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            if (i == 1) {
                System.out.println("Skipping 1");
            }
        }
        continue;
    }
}
Q3277 easy
What is the primary advantage of using an 'enhanced for loop' (or 'for-each loop') in Java?
Q3278 easy
Which method must be called to transition a thread from the NEW state to the RUNNABLE state?
Q3279 medium code output
What does this code print?
java
import java.util.LinkedList;

public class Test {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("X");
        list.add("Y");
        System.out.println(list.isEmpty());
        list.clear();
        System.out.println(list.size());
    }
}
Q3280 medium
Which of the following is generally considered a good practice when working with `Optional` in modern Java?
← Prev 162163164165166 Next → Page 164 of 200 · 3994 questions