☕ Java MCQ Questions – Page 73

Questions 1441–1460 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1441 easy code error
Which error will occur when running this Java code?
java
import java.io.FileReader;
import java.io.IOException;
import java.io.File;
import java.io.FileWriter;

public class FileReaderIssue {
    public static void main(String[] args) throws IOException {
        File tempFile = File.createTempFile("test", ".txt");
        FileWriter writer = new FileWriter(tempFile);
        writer.write("Data");
        writer.close();
        
        FileReader reader = new FileReader(tempFile);
        reader.close(); // Stream is closed here
        int data = reader.read(); // Attempt to read from closed stream
        tempFile.deleteOnExit();
    }
}
Q1442 medium code output
What is a possible output of this Java code?
java
public class Main {
    public static void main(String[] args) {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Anon Runnable thread: " + Thread.currentThread().getName());
            }
        });
        t.start();
        System.out.println("Main execution continues.");
    }
}
Q1443 hard code error
What will be the output of running this Java code?
java
class Animal {}
class Dog extends Animal {}
class Cat extends Animal {}

public class Zoo {
    public static void main(String[] args) {
        Animal a = new Cat();
        Dog d = (Dog) a; 
        System.out.println("Casted successfully");
    }
}
Q1444 easy code output
What is the output of this Java code?
java
import java.io.IOException;

public class MyClass {
    public static void readFile() throws IOException {
        System.out.println("Reading file...");
        throw new IOException("File not found");
    }

    public static void main(String[] args) {
        try {
            readFile();
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
        System.out.println("End of main.");
    }
}
Q1445 hard code error
What error will this code produce at compile time?
java
import java.util.ArrayList;
import java.util.List;

public class EnhancedForError {
    public static void main(String[] args) {
        List<String> words = new ArrayList<>();
        words.add("Java");
        words.add("ForLoop");

        for (Integer num : words) {
            System.out.println(num);
        }
    }
}
Q1446 hard code output
What is the output of this code?
java
import java.io.*;

class NonSerializableDependency {
    String value;
    public NonSerializableDependency(String v) { this.value = v; }
    public String toString() { return "Dep: " + value; }
}

class ParentObject implements Serializable {
    private static final long serialVersionUID = 1L;
    int id;
    NonSerializableDependency dependency;

    public ParentObject(int id, String depValue) {
        this.id = id;
        this.dependency = new NonSerializableDependency(depValue);
    }

    public String toString() {
        return "ID: " + id + ", Dependency: " + dependency;
    }
}

public class SerializationTest {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ParentObject obj = new ParentObject(1, "Data");
        
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
             ObjectOutputStream oos = new ObjectOutputStream(bos)) {
            oos.writeObject(obj);
            oos.close();
            
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            ParentObject deserializedObj = (ParentObject) ois.readObject();
            ois.close();
            System.out.println(deserializedObj);
        } catch (NotSerializableException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}
Q1447 medium
If a `break` statement is encountered inside a `do-while` loop in Java, what is its immediate effect?
Q1448 hard
Consider a long-running `while` loop that periodically creates new, large objects (e.g., `byte[]` arrays) within its body. If these objects are not stored in collections that persist beyond the loop iteration or explicitly nulled, what is the most likely impact on Garbage Collection during the loop's execution?
Q1449 medium
What is the scope of the `this` keyword inside a lambda expression compared to an anonymous inner class?
Q1450 hard
What happens if a class attempts to override a `final` method inherited from its superclass?
Q1451 medium code error
What is the compile-time error in this Java code?
java
import java.util.function.Function;

public class LambdaError {
    public static void main(String[] args) {
        Function<String, Integer> stringLength = (str) -> {
            return str.length();
        };
        
        // Incorrect usage
        Function<Integer, String> intToString = (num) -> num;
    }
}
Q1452 medium code output
What does this Java code print to the console?
java
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");
        names.remove(1);
        System.out.println(names);
    }
}
Q1453 medium code error
What error will occur when compiling or running this Java code?
java
public class ArrayError {
    public static void main(String[] args) {
        int size = -10;
        int[] data = new int[size];
        System.out.println(data.length);
    }
}
Q1454 easy code output
What is the output of this code?
java
interface Calculator {
    int operate(int a, int b);
}

public class LambdaTest {
    public static void main(String[] args) {
        Calculator adder = (x, y) -> x + y;
        System.out.println(adder.operate(5, 3));
    }
}
Q1455 easy
Which of the following is NOT a standard state in the Java Thread lifecycle (java.lang.Thread.State enum)?
Q1456 easy code error
What will happen when this Java code is compiled and executed?
java
class MyClass {
    public void emptyThrow() throws Exception {
        throw;
    }
}
Q1457 medium
Can a `private` method be overloaded in Java?
Q1458 easy code error
What is the error in this Java code?
java
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterError8 {
    public static void main(String[] args) throws IOException {
        FileWriter writer = new FileWriter(123);
        writer.write("Number as file.");
        writer.close();
    }
}
Q1459 easy
How can you obtain a reference to the currently executing `Thread` object?
Q1460 easy code output
What does this code print to the console?
java
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterTest {
    public static void main(String[] args) {
        try (FileWriter writer = new FileWriter("/nonexistent/path/output.txt")) {
            writer.write("Hello");
        } catch (IOException e) {
            System.out.println("Failed to write: " + e.getMessage());
        }
    }
}
← Prev 7172737475 Next → Page 73 of 200 · 3994 questions