☕ Java MCQ Questions – Page 195

Questions 3881–3900 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3881 medium
Which of the following statements about method overloading is true?
Q3882 medium code output
What does this code print?
java
import java.io.File;

public class PathComponents {
    public static void main(String[] args) {
        File file1 = new File("/users/documents/report.doc");
        File file2 = new File("image.png");
        System.out.println(file1.getName() + " | " + file1.getParent() + " | " + file2.getName() + " | " + file2.getParent());
    }
}
Q3883 easy
What is the primary purpose of synchronization in Java?
Q3884 medium
How does increasing the buffer size of a `BufferedWriter` generally impact performance for writing very large amounts of data?
Q3885 hard code output
What will be printed when the `main` method is executed?
java
class CustomException extends Exception {}
class Parent {
    void method() throws Exception { System.out.println("Parent method"); }
}
class Child extends Parent {
    @Override
    void method() throws CustomException { System.out.println("Child method"); }
}
public class Main {
    public static void main(String[] args) {
        Parent p = new Child();
        try {
            p.method();
        } catch (Exception e) {
            System.out.println("Caught: " + e.getClass().getSimpleName());
        }
    }
}
Q3886 hard
In a performance-critical application, certain custom exceptions are thrown very frequently but are rarely logged or their stack traces inspected. Generating a full stack trace for each instance could introduce significant overhead. Which `Throwable` method can be strategically overridden in a custom exception to mitigate this performance impact?
Q3887 hard code output
What is the output of this Java code?
java
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.Queue;

public class QueueTest {
    public static void main(String[] args) throws InterruptedException {
        Queue<String> queue = new ArrayBlockingQueue<>(2);

        System.out.println(queue.offer("A"));
        System.out.println(queue.offer("B"));
        System.out.println(queue.offer("C", 10, TimeUnit.MILLISECONDS));

        System.out.println(queue.poll());
        System.out.println(queue.poll(10, TimeUnit.MILLISECONDS));
        System.out.println(queue.poll(10, TimeUnit.MILLISECONDS));
    }
}
Q3888 medium code error
What is the eventual outcome of running this Java code?
java
import java.util.ArrayList;
import java.util.List;

public class LoopTest {
    public static void main(String[] args) {
        List<Object> storage = new ArrayList<>();
        do {
            storage.add(new Object());
        } while (true);
    }
}
Q3889 hard code error
What error occurs when executing the following Java code?
java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> fixedSizeList = Arrays.asList("Red", "Green", "Blue");
        fixedSizeList.add("Yellow");
        System.out.println(fixedSizeList);
    }
}
Q3890 medium code error
Identify the compile-time error in the provided Java code snippet.
java
public class Test {
    public static void main(String[] args) {
        int value = 10;
        if (value > 5) {
            System.out.println("Condition met");
            continue;
        }
        System.out.println("End of program");
    }
}
Q3891 easy code error
What is the compile-time error in the following Java code?
java
class MyClass {
    private String data = "Hello";
}

public class Demo {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        System.out.println(obj.data);
    }
}
Q3892 hard code output
Consider the following Java code. What is the output of this code?
java
public class DirectRunCall {
    public static void main(String[] args) throws InterruptedException {
        Runnable myRunnable = () -> {
            System.out.println("Runnable running on thread: " + Thread.currentThread().getName());
        };

        System.out.println("Main thread starting...");
        myRunnable.run(); // Direct call
        new Thread(myRunnable).start(); // New thread call
        System.out.println("Main thread finished.");
    }
}
Q3893 medium
When using the `concat()` method on a String, what happens if you attempt to concatenate a `null` String reference?
Q3894 medium code error
What is the compile-time or runtime error in the following Java code snippet?
java
import java.io.*;

class Message implements Serializable {
    String content;
    public Message(String c) { this.content = c; }
}

public class SerializationQuestion9 {
    public static void main(String[] args) throws IOException {
        Message msg = new Message("Test message");
        FileOutputStream fos = new FileOutputStream("message.ser");
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        fos.close(); // Closing underlying stream prematurely

        oos.writeObject(msg); // Attempting to write after stream closed
        oos.close();
    }
}
Q3895 medium code error
Consider the following Java code. What will happen when you try to compile it?
java
import java.util.Arrays;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3);
        outerLoop: for (int i = 0; i < 5; i++) {
            numbers.forEach(num -> {
                if (num == 2) {
                    break outerLoop;
                }
            });
        }
    }
}
Q3896 easy code error
What error will occur when compiling the following Java code?
java
public class Main {
    public static void main(String[] args) {
        int val = 10;
        if (!val) {
            System.out.println("Value is zero.");
        } else {
            System.out.println("Value is not zero.");
        }
    }
}
Q3897 hard code error
What is the compilation error in the following Java code?
java
interface MyConfig {
    private int TIMEOUT = 5000; // Attempt to declare a private field in an interface
    public String getSetting();
}

public class DefaultConfig implements MyConfig {
    @Override
    public String getSetting() {
        // return "Timeout: " + TIMEOUT; // This line would also fail if TIMEOUT was public
        return "Default";
    }
}
Q3898 medium code output
What does this Java code print?
java
class Dog {
    String name;
    Dog(String name) {
        this.name = name;
        System.out.println("Dog created: " + name);
    }
}
public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog("Buddy");
    }
}
Q3899 hard code error
What error will this code produce at runtime?
java
import java.util.ArrayList;
import java.util.List;

public class AutounboxingNPE {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(null);
        numbers.add(3);

        int total = 0;
        for (Integer num : numbers) {
            total += num;
        }
        System.out.println(total);
    }
}
Q3900 medium code error
What error occurs when compiling this code?
java
import java.io.IOException;

public class MyClass {
    public static void main(String[] args) {
        java.io.FileReader reader = new java.io.FileReader("nonexistent.txt");
        System.out.println("FileReader created.");
    }
}
← Prev 193194195196197 Next → Page 195 of 200 · 3994 questions