☕ Java MCQ Questions – Page 106

Questions 2101–2120 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2101 hard code output
What is the output of this code?
java
import java.util.LinkedList;

public class Test {
    public static void main(String[] args) {
        LinkedList<Integer> list = new LinkedList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(2);
        list.add(4);
        list.removeFirstOccurrence(2); // Removes the first '2'
        list.add(5);
        list.removeLastOccurrence(2); // Removes the remaining '2'
        System.out.println(list);
    }
}
Q2102 hard code output
What is the output of the following Java program?
java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ExecutorShutdown {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService executor = Executors.newFixedThreadPool(1);
        executor.submit(() -> {
            System.out.println("Task 1 Started");
            try { Thread.sleep(100); } catch (InterruptedException e) {}
            System.out.println("Task 1 Finished");
        });
        executor.submit(() -> {
            System.out.println("Task 2 Started");
            try { Thread.sleep(100); } catch (InterruptedException e) {}
            System.out.println("Task 2 Finished");
        });

        Thread.sleep(50); // Give Task 1 a chance to start
        executor.shutdownNow();
        System.out.println("Executor Shutdown Called");
    }
}
Q2103 hard
Which of the following is a key difference or characteristic when comparing `System.arraycopy()` and `Arrays.copyOf()`?
Q2104 hard
In the context of `FileWriter`, what is the primary difference in effect between calling `flush()` and `close()` regarding data persistence to the file system?
Q2105 hard
Consider a scenario where `Object[] objArr = new String[5];`. Which of the following statements about casting this array is true?
Q2106 hard code output
What does this Java code snippet print?
java
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class NonExistentFilePermissions {
    public static void main(String[] args) throws IOException {
        Path tempDir = Files.createTempDirectory("perm_test");
        File nonExistentFile = new File(tempDir.toFile(), "nonexistent.txt");
        
        System.out.println("exists: " + nonExistentFile.exists());
        System.out.println("canRead: " + nonExistentFile.canRead());
        System.out.println("canWrite: " + nonExistentFile.canWrite());
        System.out.println("canExecute: " + nonExistentFile.canExecute());
        
        Files.delete(tempDir);
    }
}
Q2107 easy
What is the effect of using the `transient` keyword on a field during serialization?
Q2108 medium code error
What error will occur when compiling and running this Java code?
java
import java.io.*;
import java.util.HashSet;
import java.util.Set;

class NonSerializableItem { // Does not implement Serializable
    String data;
    public NonSerializableItem(String data) {
        this.data = data;
    }
}

public class HashSetError10 {
    public static void main(String[] args) {
        Set<NonSerializableItem> items = new HashSet<>();
        items.add(new NonSerializableItem("Test"));

        try {
            FileOutputStream fos = new FileOutputStream("set.ser");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(items); // ERROR line
            oos.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Q2109 hard
A `TreeSet` is initialized with a custom `Comparator<String>` that does not explicitly handle null inputs. If an attempt is made to add `null` to this `TreeSet`, what is the most likely outcome?
Q2110 medium code error
What will be the compilation error in this code?
java
class Logger {
    public static void logMessage(String msg) {
        System.out.println("Log: " + msg);
    }
}

class FileLogger extends Logger {
    @Override
    public void logMessage(String msg) {
        System.out.println("File Log: " + msg);
    }
}
Q2111 easy
What is the primary purpose of method overriding in Java?
Q2112 easy code output
What is the output of this code?
java
class ReentrantLockTest {
    public synchronized void methodA() {
        System.out.print("A");
        methodB();
    }
    public synchronized void methodB() {
        System.out.print("B");
    }
}

public class MyProgram {
    public static void main(String[] args) throws InterruptedException {
        ReentrantLockTest obj = new ReentrantLockTest();
        Thread t1 = new Thread(() -> obj.methodA());
        Thread t2 = new Thread(() -> obj.methodB());
        t1.start();
        t2.start();
        t1.join();
        t2.join();
    }
}
Q2113 hard code error
What kind of runtime error or unexpected behavior will occur when running this Java code, primarily related to thread lifecycle?
java
public class ThreadBomb {
    public static void main(String[] args) {
        int i = 0;
        while (true) {
            new Thread(() -> {
                try {
                    Thread.sleep(Long.MAX_VALUE); // Keep threads alive indefinitely
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }, "Thread-" + i++).start();
            System.out.println("Created thread: " + (i-1));
        }
    }
}
Q2114 hard code error
What will be the result of compiling this Java code?
java
class Parent {
    public static void greet() {
        System.out.println("Hello from Parent");
    }
}
class Child extends Parent {
    public static void greet() {
        super.greet(); // Problematic line
        System.out.println("Hello from Child");
    }
    public static void main(String[] args) {
        Child.greet();
    }
}
Q2115 medium
What is the primary mechanism that enables runtime polymorphism in Java?
Q2116 hard
Given `class Animal {} class Dog extends Animal {} class Poodle extends Dog {}`. If you have `Animal a = new Dog();` and attempt `Poodle p = (Poodle) a;`, what is the most likely outcome at runtime?
Q2117 easy
What is necessary to ensure a `while` loop eventually terminates?
Q2118 medium
What is the primary requirement for an object to be eligible for standard Java serialization?
Q2119 medium code output
What is the output of this Java program?
java
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        boolean removed = numbers.remove(Integer.valueOf(2));
        System.out.println(removed + " " + numbers);
    }
}
Q2120 hard code output
What is the output of this code?
java
public class Q8_DaemonThreadTermination {
    public static void main(String[] args) throws InterruptedException {
        Thread daemonThread = new Thread(() -> {
            try {
                int count = 0;
                while (true) {
                    System.out.println("Daemon running: " + count++);
                    Thread.sleep(100);
                }
            } catch (InterruptedException e) {
                System.out.println("Daemon interrupted.");
            } finally {
                System.out.println("Daemon finally block.");
            }
        });
        daemonThread.setDaemon(true);
        daemonThread.start();
        
        Thread.sleep(250);
        System.out.println("Main thread exiting.");
    }
}
← Prev 104105106107108 Next → Page 106 of 200 · 3994 questions