☕ Java MCQ Questions – Page 88

Questions 1741–1760 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1741 medium
Why are `IntStream`, `LongStream`, and `DoubleStream` provided in the Java Stream API, in addition to `Stream<T>`?
Q1742 easy code error
What compile-time error will occur in the `Child` class?
java
class Parent {
    public void executeTask() {
        System.out.println("Executing Parent task");
    }
}

class Child extends Parent {
    @Override
    public static void executeTask() {
        System.out.println("Executing Child task");
    }
}
Q1743 hard code output
What is the output of this Java code?
java
class Parent {
    public static void show() { System.out.println("Parent's Static"); }
    public void display() { System.out.println("Parent's Instance"); }
}
class Child extends Parent {
    public static void show() { System.out.println("Child's Static"); }
    @Override
    public void display() { System.out.println("Child's Instance"); }
}
public class Main {
    public static void main(String[] args) {
        Parent obj = new Child();
        obj.show();
        obj.display();
    }
}
Q1744 easy code error
What compile-time error will occur when compiling this Java code?
java
public class MyClass {
    public static void main(String[] args) {
        int choice = 1;
        switch (choice) {
            case 1:
                System.out.println("Option One");
                break;
            case "Two":
                System.out.println("Option Two");
                break;
            default:
                System.out.println("Invalid option");
        }
    }
}
Q1745 medium code error
What is the output of this Java code?
java
import java.io.IOException;

public class ExceptionFlow8 {
    static class MyResource implements AutoCloseable {
        public void close() throws IOException {
            System.out.println("Resource Closed");
        }
        public void use() {
            System.out.println("Resource Used");
        }
    }

    public static void main(String[] args) {
        testMethod();
    }

    public static void testMethod() {
        try (MyResource res = new MyResource()) {
            System.out.println("In try-with-resources");
            res.use();
            throw new RuntimeException("TWR Exception");
        } catch (RuntimeException e) {
            System.out.println("Caught: " + e.getMessage());
        } finally {
            System.out.println("In finally block");
        }
    }
}
Q1746 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;

public class FileReaderIssue {
    public static void main(String[] args) throws IOException {
        File dir = new File("test_directory");
        dir.mkdir(); // Creates a directory
        FileReader reader = new FileReader(dir); // Attempt to open a directory as a file
        reader.close(); // Ensure closure for good practice if no error
        dir.delete(); // Cleanup
    }
}
Q1747 medium
A constructor can be declared with `private` access. What is a common design pattern or use case that leverages a private constructor?
Q1748 medium code error
What exception will be thrown when running this Java code snippet?
java
import java.util.concurrent.ArrayBlockingQueue;
import java.util.Queue;

public class QueueError {
    public static void main(String[] args) {
        Queue<String> queue = new ArrayBlockingQueue<>(1);
        queue.add("First");
        queue.add("Second");
    }
}
Q1749 easy
Which method is essential to call on a `FileWriter` object after finishing writing to ensure all data is written and resources are released?
Q1750 medium
Which set of constructors is most commonly recommended for a custom exception class to provide flexibility and proper error context?
Q1751 hard code output
What is the output of this code?
java
public class PassByValueTest {
    static class MyObject {
        int value;
        MyObject(int v) { this.value = v; }
    }

    public static void changeObject(MyObject obj) {
        obj.value = 20; // Modifies the object pointed to by 'obj'
        obj = new MyObject(30); // Reassigns the local parameter 'obj'
    }

    public static void main(String[] args) {
        MyObject o = new MyObject(10);
        changeObject(o);
        System.out.println(o.value);
    }
}
Q1752 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 RenameToTest {
    public static void main(String[] args) throws IOException {
        Path tempDir = Files.createTempDirectory("rename_test");
        Path sourcePath = Files.createFile(tempDir.resolve("source.txt"));
        Files.writeString(sourcePath, "Hello");
        
        Path existingTargetPath = Files.createFile(tempDir.resolve("existing_target.txt"));
        Files.writeString(existingTargetPath, "World");

        File sourceFile = sourcePath.toFile();
        File existingTargetFile = existingTargetPath.toFile();

        boolean renamed = sourceFile.renameTo(existingTargetFile);

        System.out.println("Rename successful: " + renamed);
        System.out.println("Source exists: " + sourceFile.exists());
        System.out.println("Target exists: " + existingTargetFile.exists());
        System.out.println("Target content: " + Files.readString(existingTargetPath));
        
        Files.deleteIfExists(sourcePath); // Clean up if rename failed
        Files.deleteIfExists(existingTargetPath);
        Files.delete(tempDir);
    }
}
Q1753 medium code output
What is the output of this code?
java
public class Main {
    public static void main(String[] args) {
        Runnable errorTask = () -> {
            System.out.println("Runnable started.");
            throw new RuntimeException("Error in Runnable task!");
        };
        new Thread(errorTask).start();
        System.out.println("Main thread unaffected.");
    }
}
Q1754 easy code error
What will happen when this Java code is compiled?
java
public class MyClass {
    public static void main(String[] args) {
        String name = "Alice";
        String name = "Bob";
        System.out.println(name);
    }
}
Q1755 easy code output
What is the output of this Java code?
java
import java.util.TreeMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        TreeMap<String, Integer> treeMap = new TreeMap<>();
        treeMap.put("One", 1);
        treeMap.put("Two", 2);
        treeMap.put("Three", 3);
        for (Map.Entry<String, Integer> entry : treeMap.entrySet()) {
            System.out.print(entry.getKey() + ":" + entry.getValue() + " ");
        }
    }
}
Q1756 hard code error
What does this code print?
java
import java.io.*;

public class InvalidMarkLimit {
    public static void main(String[] args) {
        try (StringReader sr = new StringReader("data");
             BufferedReader br = new BufferedReader(sr)) {
            br.mark(-1); // Invalid readlimit
            System.out.println((char)br.read());
        } catch (IllegalArgumentException e) {
            System.out.println("IllegalArgument: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("IOException: " + e.getMessage());
        }
    }
}
Q1757 hard
What is the result of calling `BufferedReader.readLine()` repeatedly on a `BufferedReader` instance that wraps an *empty* file?
Q1758 easy code output
What is the output of this code?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Hi");
        sb.append("There");
        System.out.print(sb.length());
    }
}
Q1759 easy code error
What compile-time error will occur when compiling this Java code?
java
public class MyClass {
    public static void main(String[] args) {
        int code = 1;
        switch (code) {
            case 1:
                System.out.println("Code 1");
                break;
            case 2.0f: 
                System.out.println("Code 2");
                break;
            default:
                System.out.println("Unknown Code");
        }
    }
}
Q1760 medium
What is the primary purpose of a method reference in Java?
← Prev 8687888990 Next → Page 88 of 200 · 3994 questions