☕ Java MCQ Questions – Page 159

Questions 3161–3180 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3161 medium
What is the underlying data structure used by `java.util.TreeMap` to store its entries and maintain sorted order?
Q3162 easy code output
What is the final output of the following code?
java
class Box {
    int length;
    public Box(int l) {
        this.length = l;
    }
}
public class Main {
    public static void main(String[] args) {
        Box box1 = new Box(10);
        Box box2 = box1; 
        box2.length = 20;
        System.out.print("Box1 Length: " + box1.length);
    }
}
Q3163 medium code error
What is the error in this Java code snippet?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Bug");
        sb.setCharAt(3, 's');
        System.out.println(sb);
    }
}
Q3164 medium
How can a method declare that it might throw multiple distinct checked exceptions?
Q3165 hard
What is the consequence if a *new* checked exception is thrown from within a `finally` block, especially if there was already a pending exception from the `try` or `catch` block?
Q3166 hard
Regarding thread safety, what is the default behavior of `BufferedWriter` instances when multiple threads attempt to write to the same instance concurrently?
Q3167 medium code output
What does this code print?
java
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;

public class BufferedReaderTest {
    public static void main(String[] args) {
        String data = "Line1\nLine2";
        try (BufferedReader br = new BufferedReader(new StringReader(data))) {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line.length());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Q3168 hard
Which types of methods in Java inherently cannot participate in runtime polymorphism through overriding?
Q3169 easy code output
What does this Java code print?
java
public class LoopTest {
    public static void main(String[] args) {
        int countdown = 3;
        String result = "";
        do {
            result += countdown;
            countdown--;
        } while (countdown > 1);
        System.out.print(result);
    }
}
Q3170 medium
What is the primary advantage of using the enhanced `for` loop (for-each loop) in Java compared to a traditional `for` loop?
Q3171 easy code output
What does this code print?
java
import java.util.LinkedList;
import java.util.Queue;

public class Main {
    public static void main(String[] args) {
        Queue<Boolean> flags = new LinkedList<>();
        flags.add(true);
        flags.add(false);
        flags.remove();
        System.out.println(flags.remove());
    }
}
Q3172 easy
If `int[][] grid = new int[5][7];` is declared, what will `grid.length` return?
Q3173 medium code output
What is the output of this Java code snippet?
java
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(10);
        list.add(20);
        System.out.println(list.get(0) + list.get(1));
    }
}
Q3174 easy code output
What does this Java code print?
java
import java.util.function.Function;

public class Main {
    public String toUpper(String input) {
        return input.toUpperCase();
    }

    public static void main(String[] args) {
        Main obj = new Main();
        Function<String, String> converter = obj::toUpper;
        System.out.println(converter.apply("hello"));
    }
}
Q3175 medium
Which type of method reference is represented by the syntax `ClassName::staticMethodName`?
Q3176 hard
Regarding the `throws` clause, which of these is generally considered an anti-pattern or bad practice?
Q3177 easy
Which class is primarily used to write a serialized object to an `OutputStream`?
Q3178 easy code output
What is the output of the following Java code?
java
public class WhileLoopDemo {
    public static void main(String[] args) {
        int i = 0;
        while (i < 4) {
            i++;
            if (i == 2) {
                continue;
            }
            System.out.print(i);
        }
    }
}
Q3179 easy code error
What error will occur when compiling and running the following Java code?
java
public class Main {
    public static void main(String[] args) {
        int x = 10;
        int y = 0;
        int result = x / y;
        System.out.println(result);
    }
}
Q3180 medium
Which of the following statements about method references is true?
← Prev 157158159160161 Next → Page 159 of 200 · 3994 questions