☕ Java MCQ Questions – Page 111

Questions 2201–2220 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2201 hard code error
Which line causes a compilation error regarding exception handling?
java
import java.io.IOException;

class Service {
    public void performAction() throws IOException {
        System.out.println("Performing action...");
    }
}
class SpecialService extends Service {
    @Override
    public void performAction() throws Exception { // Line 10
        System.out.println("Performing special action...");
    }
}
public class Runner {
    public static void main(String[] args) {
        Service s = new SpecialService();
        // try-catch block for IOException
    }
}
Q2202 easy
Which access modifier allows members to be accessed within the same package and by subclasses in any package?
Q2203 easy
What is the underlying data structure that `ArrayList` uses to store its elements?
Q2204 easy
Which statement must be the first statement in a constructor if you want to explicitly call a constructor of its superclass?
Q2205 hard
Given the following Java class: java import java.io.Serializable; import java.util.Comparator; class Resolver { void resolve(Serializable s) { System.out.println("Serializable"); } void resolve(Comparator c) { System.out.println("Comparator"); } } What is the result of compiling and running the following code? `new Resolver().resolve(null);`
Q2206 easy code output
What is the output of this code?
java
class Base { }
class Derived extends Base { }

public class Main {
    public static void main(String[] args) {
        Base b = new Derived();
        System.out.println(b instanceof Base);
        System.out.println(b instanceof Derived);
    }
}
Q2207 hard
From an internal JVM perspective, how are generic functional interfaces like `Function<T, R>` often implemented with lambdas, especially considering type erasure and method dispatch?
Q2208 hard code error
What compile-time error will this Java code produce?
java
public class OperatorError5 {
    public static void main(String[] args) {
        int x = 5;
        int y = 10;
        if (x = y) { // Error line
            System.out.println("x is now " + x);
        } else {
            System.out.println("Condition false");
        }
    }
}
Q2209 easy
What is the primary purpose of an `if` statement in Java?
Q2210 medium code error
What is the result of attempting to compile and run the following Java code?
java
import java.io.IOException;

class MyFaultyTask implements Runnable {
    @Override
    public void run() throws IOException { // This line
        if (true) {
            throw new IOException("Simulated I/O error");
        }
        System.out.println("Task complete.");
    }
}
public class Main {
    public static void main(String[] args) {
        MyFaultyTask task = new MyFaultyTask();
        Thread thread = new Thread(task);
        thread.start();
    }
}
Q2211 easy
What is the main purpose of abstraction in Java?
Q2212 medium
Can a `TreeSet` store `null` elements?
Q2213 hard
You have a `TreeMap<MyKey, MyValue>` initialized with `new TreeMap<>(new MyNonSerializableComparator())`, where `MyNonSerializableComparator` does not implement `Serializable`. If you attempt to serialize this `TreeMap` instance using `ObjectOutputStream`, what will happen?
Q2214 hard
Which statement accurately describes the `char` data type in Java?
Q2215 hard code output
What is the output of this code?
java
import java.io.*;

public class Test {
    public static void main(String[] args) throws IOException {
        String data = "HelloWorld";
        BufferedReader br = new BufferedReader(new StringReader(data));
        br.mark(5); // Mark at 'o' (index 5)
        br.read(); // H
        br.read(); // e
        br.reset(); // Should reset to 'o'
        System.out.print((char) br.read()); // Read 'o'
        System.out.print((char) br.read()); // Read 'W'
        br.close();
    }
}
Q2216 easy code output
Given the Java code, what will be printed?
java
public class Main {
    public static void main(String[] args) {
        int num = 0;
        do {
            if (num == 2) {
                num++;
                continue;
            }
            System.out.print(num + "-");
            num++;
        } while (num < 5);
    }
}
Q2217 medium code error
What is the compile-time or runtime error in the following Java code snippet when `main` method is executed?
java
import java.io.*;

class Book {
    String title = "Java Programming";
}

public class SerializationQuestion2 {
    public static void main(String[] args) throws IOException {
        Book myBook = new Book();
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("book.ser"))) {
            oos.writeObject(myBook);
        }
    }
}
Q2218 easy code error
What is the outcome when compiling and running this Java code?
java
public class LoopError4 {
    public static void main(String[] args) {
        while (true) {
            System.out.println("Looping forever...");
        }
        System.out.println("Done."); // This code is unreachable
    }
}
Q2219 medium code output
What is the output of the child thread's state at each observation point?
java
public class ThreadStateDemo10 {
    public static void main(String[] args) throws InterruptedException {
        Thread childThread = new Thread(() -> {
            try {
                System.out.println("Child: Starting work.");
                Thread.sleep(300); // Child is TIMED_WAITING
                System.out.println("Child: Work done.");
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        });

        System.out.println("Main: Child state (initial): " + childThread.getState());
        childThread.start();
        System.out.println("Main: Child state (after start): " + childThread.getState());
        Thread.sleep(100); // Give child time to enter sleep
        System.out.println("Main: Child state (during sleep): " + childThread.getState());
        childThread.join(); // Main waits for child to finish
        System.out.println("Main: Child state (after join): " + childThread.getState());
    }
}
Q2220 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<String> stringList = new LinkedList<>();
        stringList.add("Hello");
        stringList.set(0, 123);
        System.out.println(stringList.get(0));
    }
}
← Prev 109110111112113 Next → Page 111 of 200 · 3994 questions