☕ Java MCQ Questions – Page 169

Questions 3361–3380 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3361 medium code output
What does this Java code snippet print?
java
import java.io.BufferedWriter;
import java.io.StringWriter;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        StringWriter sw = new StringWriter();
        BufferedWriter bw = new BufferedWriter(sw);
        bw.write('X');
        bw.write(89); // ASCII value for 'Y'
        bw.write("Z");
        bw.close();
        System.out.print(sw.toString());
    }
}
Q3362 hard code output
What is the output of this code?
java
import java.util.TreeSet;

public class App {
    public static void main(String[] args) {
        TreeSet<Integer> set = new TreeSet<>();
        set.add(5);
        set.add(10);
        set.add(null); // Adding null after non-null elements
        System.out.println(set.size());
    }
}
Q3363 easy
Which of the following is NOT a characteristic of Java Lambda Expressions?
Q3364 easy code output
What is the output of this Java code?
java
interface Calculator {
    int calculate(int a, int b);
}

public class Main {
    public static void main(String[] args) {
        Calculator multiply = (x, y) -> x * y;
        System.out.println(multiply.calculate(6, 7));
    }
}
Q3365 hard code error
What is the expected outcome when compiling the following Java code snippet?
java
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterError6 {
    public void createFile() { // Method does not declare throws IOException
        FileWriter fw = new FileWriter("another_test.txt"); // IOException is a checked exception
        fw.write("Some text");
        fw.close();
    }

    public static void main(String[] args) {
        new FileWriterError6().createFile();
    }
}
Q3366 easy code output
What is the output of this code?
java
class Device {
    void start() {
        System.out.println("Device starting");
    }
}

class Computer extends Device {
    void start() {
        System.out.println("Computer starting");
    }
}

public class Main {
    public static void main(String[] args) {
        Device myDevice = new Computer();
        myDevice.start();
    }
}
Q3367 easy code output
What is the output of this Java code?
java
public class MyClass {
    public static void main(String[] args) {
        System.out.println("Step 1");
        try {
            System.out.println("Step 2");
            throw new UnsupportedOperationException("Operation not supported");
        } catch (UnsupportedOperationException e) {
            System.out.println("Step 3: " + e.getMessage());
        }
        System.out.println("Step 4");
    }
}
Q3368 hard
Prior to Java 7 (specifically Java 6), a call to `String.substring()` could lead to a memory leak in certain scenarios. Which of the following best describes the underlying reason for this issue?
Q3369 medium code error
What error will occur when compiling this Java code snippet?
java
import java.util.HashMap;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("age", 30);
        map.put("name", "Alice"); // Type mismatch
        
        System.out.println(map.get("age"));
    }
}
Q3370 hard
What is the behavior if a `continue` statement uses a label that identifies a simple code block (i.e., not a loop)?
Q3371 medium code error
What is the outcome when this Java code is executed?
java
import java.util.LinkedList;

public class Test {
    public static void main(String[] args) {
        LinkedList<int> primitiveList = new LinkedList<>();
        primitiveList.add(100);
        System.out.println(primitiveList.get(0));
    }
}
Q3372 hard
Which statement most accurately describes the difference in the behavior of `flush()` and `close()` methods for a `BufferedWriter` instance, specifically concerning its interaction with the underlying `Writer`?
Q3373 medium code error
What is the output of this Java code?
java
public class ExceptionFlow6 {
    public static void main(String[] args) {
        testMethod();
    }

    public static void testMethod() {
        try {
            System.out.println("Try block");
            throw new IllegalArgumentException("Invalid argument");
        } catch (NullPointerException e) {
            System.out.println("Catch block (NullPointerException)");
        } finally {
            System.out.println("Finally block");
        }
    }
}
Q3374 hard
Which statement accurately describes the thread-safety of `ArrayList` and its implications in a multi-threaded environment?
Q3375 hard
A common strategy to prevent deadlocks when acquiring multiple independent locks is to:
Q3376 medium
What happens if a subclass attempts to override a `final` method defined in its superclass?
Q3377 hard code error
What compile-time error will this Java code produce?
java
public class OperatorError8 {
    public static void main(String[] args) {
        int x = 10;
        String s = "2";
        int result = x << s; // Error line
        System.out.println(result);
    }
}
Q3378 hard code error
What is the result of executing this Java code snippet?
java
public class WrapperCasting {
    public static void main(String[] args) {
        Integer i = 100;
        Object obj = i;
        Long l = (Long) obj;
        System.out.println(l);
    }
}
Q3379 medium
When is `flatMap()` preferred over `map()` when working with `Optional`?
Q3380 easy code output
What does this Java code print?
java
public class ArrayTest {
    public static void main(String[] args) {
        int[] data = {10, 20, 30};
        for (int i = 0; i < data.length; i++) {
            System.out.print(data[i] + " ");
        }
    }
}
← Prev 167168169170171 Next → Page 169 of 200 · 3994 questions