☕ Java MCQ Questions – Page 54

Questions 1061–1080 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1061 hard code output
What is the output of this code?
java
public class Test {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Start");
        sb.setLength(10);
        sb.setCharAt(7, 'X');
        sb.setCharAt(9, 'Y');
        System.out.println(sb.length() + " [" + sb.toString() + "]");
    }
}
Q1062 medium code error
Consider the following Java code. What type of error will it produce at compile time?
java
public class CustomRunnable implements Runnable {
    @Override
    public void run(String message) {
        System.out.println("Running with: " + message);
    }
}

public class Main {
    public static void main(String[] args) {
        CustomRunnable task = new CustomRunnable();
    }
}
Q1063 easy
If no `case` label matches the `switch` expression and there is no `default` label, what happens?
Q1064 easy code output
What is the output of this Java program?
java
public class Main {
    public static void main(String[] args) {
        int m = 5;
        int n = ++m;
        System.out.println("m=" + m + ", n=" + n);
    }
}
Q1065 easy code error
What compile-time error will this code produce?
java
class Worker implements Runnable {
    public void run(String message) {
        System.out.println(message);
    }
}

public class Main {
    public static void main(String[] args) {
        Worker worker = new Worker();
        Thread thread = new Thread(worker);
        thread.start();
    }
}
Q1066 hard code error
What is the runtime error when `main` method is executed?
java
class MyChainedException extends Exception {
    public MyChainedException(String message) {
        super(message);
    }
}

public class Main {
    public static void main(String[] args) {
        MyChainedException exc = new MyChainedException("Primary error");
        exc.initCause(new IllegalArgumentException("First cause"));
        // Attempting to overwrite the cause after it has been set
        exc.initCause(new NullPointerException("Second cause")); // This line causes a runtime error
    }
}
Q1067 hard code error
What compilation error will occur when compiling the following Java code?
java
import java.util.function.IntFunction;

public class RecursiveLambdaError {
    public static void main(String[] args) {
        IntFunction<Integer> factorial = n -> n == 0 ? 1 : n * factorial.apply(n - 1);
        System.out.println(factorial.apply(5));
    }
}
Q1068 medium
When an `ArrayList` is initialized with a specific initial capacity (e.g., `new ArrayList<>(10)`) but no elements are added, what is its `size()` and internal array capacity?
Q1069 easy code output
What does this Java code print?
java
public class StringBuilding {
    public static void main(String[] args) {
        String result = "";
        for (int i = 0; i < 2; i++) {
            result += i;
        }
        System.out.println(result);
    }
}
Q1070 medium code error
Analyze the given Java code and determine the type of error that occurs when `myMethod(null)` is called.
java
public class OverloadChecker {
    public void myMethod(Integer i) {
        System.out.println("Integer: " + i);
    }

    public void myMethod(String s) {
        System.out.println("String: " + s);
    }

    public static void main(String[] args) {
        OverloadChecker oc = new OverloadChecker();
        oc.myMethod(null); // This line causes the error
    }
}
Q1071 easy
If a lambda expression requires multiple statements in its body, how must the body be structured?
Q1072 hard code output
What is the output of this Java code?
java
import java.util.TreeMap;
import java.util.NoSuchElementException;

public class Test {
    public static void main(String[] args) {
        TreeMap<Integer, String> map = new TreeMap<>();
        try {
            map.firstKey(); // Throws NoSuchElementException on empty map
            System.out.println("Should not reach here");
        } catch (NoSuchElementException e) {
            map.put(1, "One");
            map.put(2, "Two");
            System.out.println(map.lastKey());
        } catch (Exception e) {
            System.out.println("Other Exception");
        }
    }
}
Q1073 easy code error
What error will occur when compiling the following Java code?
java
public class Main {
    public static void main(String[] args) {
        int a = 5;
        int result = a + ++10;
        System.out.println(result);
    }
}
Q1074 easy
What kind of type casting occurs when you assign an `int` variable to a `byte` variable?
Q1075 easy code error
What is the primary issue with this Java code snippet?
java
public class LoopError1 {
    public static void main(String[] args) {
        int i = 0;
        while (i < 3); { // Semicolon here
            System.out.println("i: " + i);
            i++;
        }
    }
}
Q1076 easy code output
What is the output of this code?
java
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        TreeSet<Integer> ts = new TreeSet<>();
        ts.add(1);
        ts.add(2);
        ts.add(3);
        System.out.println(ts.contains(2) + " " + ts.contains(4));
    }
}
Q1077 hard code output
Consider the `User` class is immutable. What is the output of this code snippet, assuming `getName()` and `getAge()` return the values set in the constructor?
java
final class User {
    private final String name;
    private final int age;
    public User(String name, int age) {
        this.name = name; // String is immutable
        this.age = age;
    }
    public String getName() { return name; }
    public int getAge() { return age; }
}

public class Main {
    public static void main(String[] args) {
        User user1 = new User("Alice", 30);
        User user2 = user1; // Reference assignment
        user1 = new User("Bob", 35);
        System.out.println(user2.getName() + "," + user2.getAge());
    }
}
Q1078 medium
What is the primary benefit of using a `try-with-resources` statement in Java?
Q1079 easy
Is `java.util.LinkedList` inherently synchronized?
Q1080 hard code output
What is the output of this code? (Assuming Java 17+)
java
public class SwitchTest {
    public static void main(String[] args) {
        Object data = 12.3; // Double value
        String output = switch (data) {
            case Integer i -> "Integer: " + i;
            case var v when v instanceof Double d -> "Double value: " + d;
            case String s -> "String: " + s;
            case var v -> "Catch all: " + v.getClass().getSimpleName();
        };
        System.out.println(output);
    }
}
← Prev 5253545556 Next → Page 54 of 200 · 3994 questions