☕ Java MCQ Questions – Page 35

Questions 681–700 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q681 easy
What does the `final` keyword indicate when used with a variable in Java?
Q682 hard code output
What is the output of this code?
java
import java.util.TreeSet;
import java.util.Iterator;

public class App {
    public static void main(String[] args) {
        TreeSet<Integer> set = new TreeSet<>();
        set.add(1); set.add(2); set.add(3); set.add(4);

        Iterator<Integer> it = set.iterator();
        while (it.hasNext()) {
            Integer num = it.next();
            if (num == 2) {
                set.add(5); // Structural modification via set's method
            }
            if (num == 3) {
                it.remove(); // Allowed via iterator
            }
        }
        System.out.println(set.size());
    }
}
Q683 easy code output
What is the output of this code?
java
public class Main {
    public static void main(String[] args) {
        int[][] arr = new int[2][];
        arr[0] = new int[]{7, 8};
        // arr[1] is still null
        System.out.println(arr[0][1]);
    }
}
Q684 medium code output
What does this Java code print to the console?
java
public class SwitchTest {
    public static void main(String[] args) {
        String status = "PENDING";
        String action = "";
        switch (status) {
            case "APPROVED":
                action = "Process";
                break;
            case "PENDING":
            case "REVIEW":
                action = "Review";
                break;
            case "REJECTED":
                action = "Notify";
                break;
            default:
                action = "Log";
        }
        System.out.println(action);
    }
}
Q685 medium
Which of the following statements accurately describes the thread-safety of `java.util.HashMap`?
Q686 easy
What is the default value for an uninitialized instance variable of type `int` in Java?
Q687 hard code output
What does this code print?
java
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

class Animal { public String toString() { return "Animal"; } }
class Dog extends Animal { public String toString() { return "Dog"; } }
class Poodle extends Dog { public String toString() { return "Poodle"; } }

public class PECSWithFI {
    public static void consumeItems(List<Dog> dogs, Consumer<? super Dog> consumer) {
        dogs.forEach(consumer);
    }

    public static <T, R> List<R> transformItems(List<T> items, Function<? super T, ? extends R> mapper) {
        List<R> result = new ArrayList<>();
        for (T item : items) {
            result.add(mapper.apply(item));
        }
        return result;
    }

    public static void main(String[] args) {
        List<Dog> dogs = List.of(new Dog(), new Poodle());

        Consumer<Animal> animalPrinter = a -> System.out.println("Consumed: " + a);
        consumeItems(dogs, animalPrinter);

        Function<Animal, String> animalNamer = Animal::toString;
        List<String> transformed = transformItems(dogs, animalNamer);
        System.out.println("Transformed: " + transformed);
    }
}
Q688 medium code error
What is the result of attempting to compile and run the following Java code?
java
class SleeperThread extends Thread {
    public void run() {
        System.out.println("Sleeping...");
        Thread.sleep(1000); // This line
        System.out.println("Awake!");
    }
}
public class Main {
    public static void main(String[] args) {
        new SleeperThread().start();
    }
}
Q689 medium
When using nested `while` loops, what happens if a `break` statement is executed in the inner loop?
Q690 medium code error
What is the compilation error in the provided Java code?
java
interface Printable {
    static void printHeader() {
        System.out.println("--- Document ---");
    }
    void printContent();
}

public class Report implements Printable {
    public void printContent() {
        System.out.println("Report content");
    }

    public void printHeader() { // Attempting to override static method
        System.out.println("Header for Report");
    }
}
Q691 easy code error
If the commented line `System.out.println(loopVar);` were uncommented, what kind of error would occur?
java
public class LoopError8 {
    public static void main(String[] args) {
        while (true) {
            int loopVar = 10;
            break;
        }
        // System.out.println(loopVar); // Error here
    }
}
Q692 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("Developer");
        String sub = sb.substring(10);
        System.out.println(sub);
    }
}
Q693 easy
Which of the following is the preferred way to declare a single-dimensional integer array named `numbers` in Java?
Q694 medium
If a class has a private field that is a mutable object (e.g., `Date` or `List`), and its getter method returns a direct reference to that object, how does this practice affect encapsulation?
Q695 medium code error
What is the result of attempting to run the following Java code?
java
public class Main {
    public static void main(String[] args) {
        try {
            Thread.sleep(-1000); // This line
            System.out.println("Awake!");
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } catch (IllegalArgumentException e) {
            System.err.println("Caught an error: " + e.getMessage());
        }
    }
}
Q696 hard code error
What is the error in this Java code snippet?
java
import java.util.ArrayList;
import java.util.List;

public class LoopIndexError {
    public static void main(String[] args) {
        List<String> items = new ArrayList<>();
        items.add("One");
        items.add("Two");
        items.add("Three");

        int i = 0;
        while (i <= items.size()) { // Incorrect condition
            System.out.println(items.get(i));
            i++;
        }
    }
}
Q697 hard code output
What does this Java code snippet print regarding path encoding?
java
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;

public class URIEncodingComparison {
    public static void main(String[] args) throws IOException, URISyntaxException {
        Path tempDir = Files.createTempDirectory("uri_encoding_test");
        Path fileWithSpace = Files.createFile(tempDir.resolve("my file.txt"));
        Path fileWithHash = Files.createFile(tempDir.resolve("file#1.txt"));

        File fSpace = fileWithSpace.toFile();
        File fHash = fileWithHash.toFile();

        System.out.println("Space in URI path: " + fSpace.toURI().getPath().contains("%20"));
        System.out.println("Space in URL path: " + fSpace.toURL().getPath().contains(" "));
        System.out.println("Hash in URI path: " + fHash.toURI().getPath().contains("%23"));
        System.out.println("Hash in URL path: " + fHash.toURL().getPath().contains("#"));

        Files.delete(fileWithSpace);
        Files.delete(fileWithHash);
        Files.delete(tempDir);
    }
}
Q698 easy code output
What is the output of this code?
java
class Animal {
    public void makeSound() {
        System.out.print("Animal sound");
    }
}

class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.print("Woof");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myAnimal = new Dog();
        myAnimal.makeSound();
    }
}
Q699 easy
What does the term 'nested if' refer to in Java?
Q700 medium
If a `Serializable` class contains an instance variable that refers to an object of a class that does *not* implement `Serializable`, what will happen during serialization of the outer object?
← Prev 3334353637 Next → Page 35 of 200 · 3994 questions