☕ Java MCQ Questions – Page 114

Questions 2261–2280 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2261 easy code error
What error will occur when compiling and running the provided Java code?
java
class Person {
    private int age;
}

public class Main {
    public static void main(String[] args) {
        Person p = new Person();
        p.age = 30;
    }
}
Q2262 hard code output
What is the output of this code?
java
public class SwitchTest {
    enum Status { PENDING, PROCESSING, COMPLETED }
    public static void main(String[] args) {
        Status status = Status.PENDING;
        String desc = "";
        switch (status) {
            case PENDING:
                desc += "Waiting...";
            case PROCESSING:
                desc += "In progress.";
                break;
            case COMPLETED:
                desc += "Done!";
                break;
            default:
                desc += "Unknown.";
        }
        System.out.println(desc + " (End)");
    }
}
Q2263 medium code error
What error occurs when compiling this Java code?
java
import java.io.IOException;

public class ExceptionHandling {
    public static void readFile() {
        // Simulate an operation that might throw IOException
        throw new IOException("File not found");
    }

    public static void main(String[] args) {
        // Main method calls readFile
    }
}
Q2264 medium code error
What exception will be thrown when executing this Java code snippet?
java
import java.util.LinkedList;
import java.util.Queue;
import java.util.NoSuchElementException;

public class QueueError {
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<>();
        queue.remove(); // Attempt to remove from an empty queue
    }
}
Q2265 hard
Consider a scenario where a variable is declared exclusively within the `do` block of a `do-while` loop. If this variable is then referenced in the `while` condition, what is the outcome?
Q2266 hard code output
What does this code print?
java
abstract class AbstractProcessor {
    public void process() {
        System.out.println("Processing started.");
        doProcess();
    }
    protected abstract void doProcess();
}

public class Main {
    public static void main(String[] args) {
        AbstractProcessor processor = new AbstractProcessor() {
            @Override
            protected void doProcess() {
                System.out.println("Executing specific task.");
            }
        };
        processor.process();
    }
}
Q2267 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)) {
            writer.write("Initial data.");
            writer.close(); // Explicitly closing the writer
            writer.write("Attempt to write after close.");
        } catch (IOException e) {
            System.out.println("Caught error: " + e.getMessage());
        }
        System.out.print("Final content: " + stringWriter.toString());
    }
}
Q2268 hard
A developer iterates over a `LinkedList` using its `iterator()` and simultaneously modifies the list's structure (e.g., by calling `list.add()` or `list.remove()`) outside the iterator's own `remove()` method. What is the most likely outcome?
Q2269 easy code output
What is the output of this Java code?
java
class MyClass {
    MyClass() {
        System.out.println("Constructor called!");
    }
}
public class Main {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
    }
}
Q2270 easy code error
What compile-time error will occur in the `main` method?
java
abstract class AbstractCustomException extends Exception {
    public AbstractCustomException(String message) { super(message); }
}

public class Main {
    public static void main(String[] args) {
        try {
            throw new AbstractCustomException("Cannot instantiate"); 
        } catch (AbstractCustomException e) {
            System.out.println(e.getMessage());
        }
    }
}
Q2271 easy
Which of the following wrapper classes is NOT traditionally supported as an expression in a standard Java `switch` statement?
Q2272 hard
How do non-static inner classes impact the encapsulation of their outer class?
Q2273 hard code output
What is the output of this code?
java
class Parent {
    protected void printInfo() {
        System.out.println("Parent Info");
    }
}

class Child extends Parent {
    @Override
    void printInfo() {
        System.out.println("Child Info");
    }
}

public class Main {
    public static void main(String[] args) {
        Parent p = new Child();
        p.printInfo();
    }
}
Q2274 hard code error
What is the error encountered when running this Java code?
java
public class ArrayCopyError {
    public static void main(String[] args) {
        int[] source = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int[] dest = source;
        System.arraycopy(source, 5, dest, 0, 8);
    }
}
Q2275 hard code output
What is the output of this code?
java
class Overload {
    Overload(Object o) { System.out.print("Object "); }
    Overload(String s) { System.out.print("String "); }
    Overload(Integer i) { System.out.print("Integer "); }
    Overload(Number n) { System.out.print("Number "); }
}
public class Main {
    public static void main(String[] args) {
        new Overload(null);
        new Overload(5);
        new Overload("hello");
    }
}
Q2276 easy code output
What is the output of this code?
java
public class Main {
    public static String testMethod() {
        try {
            System.out.print("Try ");
            return "Return from try";
        } finally {
            System.out.print("Finally ");
        }
    }
    public static void main(String[] args) {
        System.out.println(testMethod());
    }
}
Q2277 hard code error
What exception is thrown when the `oos.writeObject(child)` line is executed in the `main` method?
java
import java.io.*;

class NonSerializableParent {
    private String parentData;
    public NonSerializableParent(String parentData) { this.parentData = parentData; }
    public String getParentData() { return parentData; }
}

class SerializableChild extends NonSerializableParent implements Serializable {
    private static final long serialVersionUID = 1L;
    private String childData;
    public SerializableChild(String parentData, String childData) {
        super(parentData);
        this.childData = childData;
    }
    public String getChildData() { return childData; }
}

public class SerializationError2 {
    public static void main(String[] args) throws IOException {
        SerializableChild child = new SerializableChild("Parent", "Child");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(child); // Error occurs here
        oos.close();
    }
}
Q2278 medium code error
What is the error in this Java code related to method overloading?
java
public class OverloadChecker {
    public void printValue(int a) {
        System.out.println("Int: " + a);
    }

    public int printValue(int a) { // This line causes the error
        return a;
    }

    public static void main(String[] args) {
        OverloadChecker oc = new OverloadChecker();
        oc.printValue(10);
    }
}
Q2279 easy code output
What does this code print?
java
public class MyClass {
    public static void callerMethod() {
        try {
            System.out.println("Caller: In try");
            throw new IllegalStateException("State issue");
        } catch (IllegalStateException e) {
            System.out.println("Caller: Caught " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        System.out.println("Main: Before call");
        callerMethod();
        System.out.println("Main: After call");
    }
}
Q2280 easy
When creating a `FileReader` object, which specific Java class instance can be passed to its constructor, besides a `String` path?
← Prev 112113114115116 Next → Page 114 of 200 · 3994 questions