☕ Java MCQ Questions – Page 138

Questions 2741–2760 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2741 hard code error
What is the output of this code?
java
public class Main {
    public static void main(String[] args) {
        Thread t = new Thread(() -> System.out.println("Worker thread."));
        try {
            t.setPriority(Thread.MAX_PRIORITY + 1); // Invalid priority
        } catch (SecurityException e) {
            System.out.println("Security Exception");
        }
        t.start();
    }
}
Q2742 medium
Which of the following statements about `java.util.Arrays.copyOf(original, newLength)` is true?
Q2743 easy code output
What is the output of this code?
java
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        ArrayList<String> colors = new ArrayList<>();
        colors.add("Red");
        colors.add("Green");
        colors.add("Blue");
        colors.remove(0);
        System.out.println(colors.get(0));
    }
}
Q2744 medium
Which Java String method is primarily used to compare the actual character sequence content of two String objects for equality, regardless of their memory addresses?
Q2745 hard
Which statement about constructors in abstract classes is *true*?
Q2746 medium
What is the primary purpose of the `java.util.concurrent.BlockingQueue` interface?
Q2747 hard
Which of the following assignments is *not* legal in Java (resulting in a compile-time error)?
Q2748 medium code output
Consider the following Java code. What will be printed to the console?
java
public class SwitchTest {
    public static void main(String[] args) {
        char code = 'C';
        String description = "";
        switch (code) {
            case 'A':
            case 'E':
            case 'I':
                description = "Vowel";
                break;
            case 'O':
                description = "Round Vowel";
                break;
            case 'U':
                description = "Long Vowel";
                break;
            default:
                description = "Consonant";
        }
        System.out.println(description);
    }
}
Q2749 medium
What happens when a `continue` statement is executed within a `do-while` loop?
Q2750 easy code output
What is the output of this code?
java
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class FileReaderTest {
    public static void main(String[] args) {
        // Assume test.txt exists and contains a single character 'Z'
        File file = new File("test.txt");
        try (FileReader reader = new FileReader(file)) {
            int charCode = reader.read();
            System.out.print((char) charCode);
        } catch (IOException e) {
            System.out.print("Error");
        }
    }
}
Q2751 medium code error
What kind of error will occur when compiling the following Java code, assuming 'input.txt' exists?
java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class TestClass {
    public static void main(String[] args) {
        BufferedReader br; 
        try (br = new BufferedReader(new FileReader("input.txt"))) {
            String line = br.readLine();
            System.out.println(line);
        } catch (IOException e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}
Q2752 hard
Which statement highlights a key distinction in how abstract classes and interfaces (post-Java 8 with default methods) interact with inheritance hierarchies?
Q2753 hard code output
What is the output of this code?
java
import java.util.ArrayList;
import java.util.List;

final class MyImmutableList {
    private final List<String> data;
    public MyImmutableList(List<String> data) {
        this.data = new ArrayList<>(data); // Defensive copy
    }
    public List<String> getData() {
        return new ArrayList<>(data); // Defensive copy
    }
}

public class Main {
    public static void main(String[] args) {
        List<String> mutableList = new ArrayList<>();
        mutableList.add("A");
        MyImmutableList immutableList = new MyImmutableList(mutableList);
        mutableList.add("B"); // Modify original list
        immutableList.getData().add("C"); // Modify returned copy
        System.out.println(immutableList.getData().size());
    }
}
Q2754 easy
Which keyword is used to define a block of code that should be executed if no other `case` label matches the `switch` expression?
Q2755 easy code output
What is the output of this code?
java
import java.util.HashSet;

public class Test {
    public static void main(String[] args) {
        HashSet<Character> chars = new HashSet<>();
        chars.add('A');
        chars.add('B');
        chars.add('C');
        chars.remove('B');
        System.out.println(chars.size());
    }
}
Q2756 hard code output
What is the output of this Java code?
java
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class FileWriterToDirectory {
    public static void main(String[] args) {
        String dirname = "my_directory";
        File dir = new File(dirname);
        try {
            Files.createDirectory(Path.of(dirname));
            try (FileWriter fw = new FileWriter(dir)) {
                fw.write("This should fail.");
            }
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        } finally {
            try { Files.deleteIfExists(Path.of(dirname)); } catch (IOException ignored) {}
        }
    }
}
Q2757 easy code error
What error occurs when the `start()` method is called multiple times on the same `Thread` object?
java
class MyRunnable implements Runnable {
    public void run() {
        System.out.println("Thread running.");
    }
}

public class Main {
    public static void main(String[] args) {
        MyRunnable r = new MyRunnable();
        Thread t = new Thread(r);
        t.start();
        t.start(); // Second call to start()
    }
}
Q2758 medium code error
What error will occur when running this Java code snippet?
java
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;

public class Test {
    public static void main(String[] args) {
        Map<String, Integer> counts = new HashMap<>();
        counts.put("apple", 10);
        
        // Attempt to merge with a null remapping function
        // when both old and new values are non-null
        counts.merge("apple", 5, null); 
        System.out.println(counts.get("apple"));
    }
}
Q2759 medium code output
What will be the output of this Java code?
java
import java.util.function.Supplier;

class Product {
    String name;
    public Product() { this.name = "Default Product"; }
    public Product(String name) { this.name = name; }
}

public class ConstructorReference {
    public static void main(String[] args) {
        Supplier<Product> productSupplier = Product::new; // References no-arg constructor
        Product p = productSupplier.get();
        System.out.println(p.name);
    }
}
Q2760 hard code error
What error occurs when executing the following Java code?
java
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> immutableColors = List.of("Cyan", "Magenta", "Yellow");
        immutableColors.add("Black"); // Attempt to add to an immutable list
        System.out.println(immutableColors);
    }
}
← Prev 136137138139140 Next → Page 138 of 200 · 3994 questions