☕ Java MCQ Questions – Page 157

Questions 3121–3140 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3121 hard code output
What is the output of this code?
java
public class Test {
    public static void main(String[] args) {
        char[] data = {'1', '2', '3', '4', '5'};
        StringBuilder sb = new StringBuilder("Start Middle End");
        String replacement = new String(data, 1, 3);
        sb.replace(6, 12, replacement);
        System.out.println(sb);
    }
}
Q3122 hard code error
What error occurs when running this Java code?
java
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

class FaultyIterable<T> implements Iterable<T> {
    private List<T> data;
    public FaultyIterable(List<T> data) { this.data = new ArrayList<>(data); }

    @Override
    public Iterator<T> iterator() {
        return new Iterator<T>() {
            private int currentIndex = 0;
            @Override
            public boolean hasNext() { return currentIndex <= data.size(); } // Incorrect condition
            @Override
            public T next() {
                if (currentIndex >= data.size()) {
                    throw new java.util.NoSuchElementException();
                }
                return data.get(currentIndex++);
            }
        };
    }
}
public class IteratorFaultyHasNext {
    public static void main(String[] args) {
        FaultyIterable<Integer> myIterable = new FaultyIterable<>(Arrays.asList(10, 20));
        Iterator<Integer> it = myIterable.iterator();
        it.next(); // 10
        it.next(); // 20
        it.next(); // Error here
    }
}
Q3123 easy
What is the primary purpose of a `for` loop in Java?
Q3124 medium code error
What error occurs when this Java code attempts to use `headMap`?
java
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        TreeMap<Integer, String> map = new TreeMap<>();
        map.put(10, "A");
        map.put(20, "B");
        map.put(30, "C");
        String nonComparableKey = "Five";
        map.headMap(nonComparableKey); // Intentional error point
    }
}
Q3125 hard
What is a key characteristic of constructors in Java enums?
Q3126 medium code error
What compile-time error will occur in this Java code snippet?
java
public class LoopError {
    public static void main(String[] args) {
        int data = 100;
        for (int val : data) {
            System.out.println(val);
        }
    }
}
Q3127 easy
Given `int[][] numbers = new int[2][3];`, how would you assign the value `10` to the element in the first row and second column?
Q3128 medium code error
What error will occur when running the following Java code?
java
public class NullThreadStart {
    public static void main(String[] args) {
        Thread myThread = null;
        myThread.start(); // Calling method on a null reference
    }
}
Q3129 medium
Which of the following is the correct way to rethrow an exception that has been caught in a `catch` block?
Q3130 medium code output
What does this code print?
java
class Message {
    String content;

    public Message(String content) {
        this.content = content;
    }

    public String getContent() {
        return "Message: " + content;
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(new Message("Hello World").getContent());
    }
}
Q3131 easy
What method is used to retrieve the next element in the iteration and advance the iterator's position?
Q3132 hard code output
What is the output of this code?
java
public class Test {
    public static void main(String[] args) {
        String result = "";
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                if (i == 0 && j == 1) {
                    break;
                }
                if (i == 1 && j == 0) {
                    continue;
                }
                result += "(" + i + "," + j + ")";
            }
        }
        System.out.print(result);
    }
}
Q3133 hard code error
What is the output of this code?
java
import java.util.LinkedList;

public class MyClass {
    public static void main(String[] args) {
        LinkedList<String> names = new LinkedList<>();
        names.add("Alice");
        names.add("Bob");
        names.add(3, "Charlie"); // Invalid index
        System.out.println(names);
    }
}
Q3134 hard code output
What is the output of this Java code?
java
import java.util.TreeMap;
import java.util.NavigableMap;

public class Test {
    public static void main(String[] args) {
        TreeMap<Integer, String> map = new TreeMap<>();
        map.put(1, "A");
        map.put(2, "B");
        map.put(3, "C");
        map.put(4, "D");
        map.put(5, "E");

        NavigableMap<Integer, String> subMap = map.subMap(2, true, 4, false);
        subMap.put(2, "X"); // Modifies original map
        subMap.remove(3);   // Modifies original map
        System.out.println(map.keySet());
    }
}
Q3135 medium code output
What is the output of this code?
java
import java.util.ArrayList;
import java.util.List;

public class GenericsExample {
    public static void main(String[] args) {
        List<? extends Number> list1 = new ArrayList<Integer>();
        List<Integer> intList = new ArrayList<>();
        intList.add(1);
        intList.add(2);
        List<? extends Number> list2 = intList;
        System.out.println(list2.get(0));
    }
}
Q3136 hard code output
What is the output of this code?
java
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;

public class Test {
    public static void main(String[] args) {
        StringWriter sw = new StringWriter();
        try (BufferedWriter bw = new BufferedWriter(sw)) {
            bw.write("Data");
            if (true) throw new IOException("Simulated error");
        } catch (IOException e) {
            System.out.println("Caught exception: " + e.getMessage());
        }
        System.out.println("Content: " + sw.toString());
    }
}
Q3137 medium code output
What is the output of this code?
java
String message = "  Hello World  ";
String processed = message.trim().replace("o", "X").toUpperCase();
System.out.println(processed + " " + message.contains("World"));
Q3138 medium code error
What error will occur when compiling or running this Java code?
java
public class ArrayError {
    public static void main(String[] args) {
        String[] names = new String[5];
        System.out.println(names[-1]);
    }
}
Q3139 easy code error
What kind of error will occur when compiling this Java code?
java
class A {}
class B {}

class C extends A, B {
    // Class C attempting to extend multiple classes
}

public class Main {
    public static void main(String[] args) {
        C obj = new C();
    }
}
Q3140 easy
What is the typical purpose of using `break` within a `switch` statement in Java?
← Prev 155156157158159 Next → Page 157 of 200 · 3994 questions