☕ Java MCQ Questions – Page 166

Questions 3301–3320 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3301 hard
Which statement regarding constructor invocation during object creation and inheritance is TRUE?
Q3302 hard code error
What is the runtime error encountered when executing this Java code snippet?
java
import java.util.Set;
import java.util.HashSet;

public class Main {
    public static void main(String[] args) {
        Set<String> immutableSet = Set.of("apple", "banana"); // Java 9+ immutable set
        immutableSet.remove("apple"); // Attempt to modify immutable set
    }
}
Q3303 medium code output
What is the output of this code?
java
class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("Runnable task executed.");
    }
}

public class Main {
    public static void main(String[] args) {
        MyRunnable r = new MyRunnable();
        Thread t = new Thread(r);
        t.start();
        System.out.println("Main thread finished.");
    }
}
Q3304 hard
A thread is currently executing within a `synchronized` method. Another thread attempts to call a different `synchronized` method on the *same* object. What state does the second thread transition to?
Q3305 medium code error
What error will occur when compiling the following Java code?
java
public class SleepUnhandledException {
    public void methodWithSleep() {
        Thread.sleep(100); // InterruptedException is a checked exception
    }
    public static void main(String[] args) {
        new SleepUnhandledException().methodWithSleep();
    }
}
Q3306 hard code output
What is the output of this code?
java
public class StringEqualsIgnoreCaseTest {
    public static void main(String[] args) {
        String s1 = "Straße"; // German sharp s (ß)
        String s2 = "STRASSE";
        System.out.println(s1.equalsIgnoreCase(s2));
    }
}
Q3307 easy code output
What is the output of this code?
java
import java.io.BufferedWriter;
import java.io.StringWriter;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        StringWriter stringWriter = new StringWriter();
        try (BufferedWriter writer = new BufferedWriter(stringWriter)) {
            String data = "Programming Language";
            writer.write(data, 0, 11);
            writer.flush();
            System.out.print(stringWriter.toString());
        } catch (IOException e) {
            System.out.print("Error");
        }
    }
}
Q3308 hard code error
What is the output of this code?
java
import java.util.LinkedList;
import java.util.Arrays;

public class MyClass {
    public static void main(String[] args) {
        LinkedList<Object> mixedList = new LinkedList<>(Arrays.asList(100, "Hello", true));
        String s = (String) mixedList.get(0); // Attempt to cast Integer to String
        System.out.println(s);
    }
}
Q3309 easy
What is the primary purpose of the `finally` block in Java exception handling?
Q3310 easy code error
What is the error in the following Java code?
java
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        TreeSet<Integer> numbers = new TreeSet<>();
        numbers.add(10);
        numbers.add(20);
        int first = numbers.get(0);
    }
}
Q3311 medium
Which Java String method would return `true` for a string containing only space characters (e.g., `" "`), but `false` for an empty string (e.g., `""`)?
Q3312 easy code output
What is the output of this code?
java
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<String, String> dictionary = new HashMap<>();
        System.out.println(dictionary.isEmpty());
        dictionary.put("Hello", "Greeting");
        System.out.println(dictionary.isEmpty());
    }
}
Q3313 medium code error
What error will this Java code produce when executed?
java
public class ArrayError {
    public static void main(String[] args) {
        int[][] matrix = new int[3][];
        System.out.println(matrix[0][0]);
    }
}
Q3314 hard
Which of the following is the primary functional difference between `String.trim()` and `String.strip()`?
Q3315 hard
If a `switch` expression (Java 14+) has different branches that `yield` values of distinct but compatible types (e.g., one `yield`s an `Integer` and another `yield`s a `Double`), what will be the resulting compile-time type of the `switch` expression?
Q3316 hard
`FileWriter` constructors do not directly accept a `Charset` object to specify the encoding. If a specific encoding (e.g., UTF-16) is required for a file, how would you best achieve this using the `FileWriter`'s underlying mechanisms?
Q3317 easy code output
What does this code print?
java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>();
        nums.add(1);
        nums.add(4);
        nums.add(2);
        nums.add(5);
        Iterator<Integer> it = nums.iterator();
        while (it.hasNext()) {
            Integer num = it.next();
            if (num > 2) {
                System.out.print(num);
            }
        }
    }
}
Q3318 hard code output
Assume a file named 'test.txt' exists with the content "Hello". What is the output of this code?
java
import java.io.FileReader;
import java.io.IOException;
import java.io.File;

public class FileReaderMark {
    public static void main(String[] args) throws IOException {
        // Assume 'test.txt' exists and contains "Hello"
        try (FileReader reader = new FileReader(new File("test.txt"))) {
            System.out.println("Mark Supported: " + reader.markSupported());
            reader.mark(10); // Attempt to mark
            reader.read(); // Read 'H'
            reader.reset(); // Attempt to reset
            int c = reader.read(); 
            System.out.println("After reset, read: " + (char) c);
        }
    }
}
Q3319 easy code output
What is the output of this Java program?
java
public class MyClass {
    public static void main(String[] args) {
        int x = 1;
        switch (x) {
            case 1: System.out.print("One");
            case 2: System.out.print("Two");
            case 3: System.out.print("Three"); break;
            default: System.out.print("Other");
        }
    }
}
Q3320 easy code output
What does this code print when executed?
java
public class Wallet {
    private int balance;

    public Wallet(int initialBalance) {
        this.balance = initialBalance;
    }

    public void deposit(int amount) {
        if (amount > 0) {
            balance += amount;
        }
    }

    public int getBalance() {
        return balance;
    }

    public static void main(String[] args) {
        Wallet myWallet = new Wallet(100);
        myWallet.deposit(50);
        myWallet.deposit(-20);
        System.out.println(myWallet.getBalance());
    }
}
← Prev 164165166167168 Next → Page 166 of 200 · 3994 questions