☕ Java MCQ Questions – Page 195
Questions 3881–3900 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhich of the following statements about method overloading is true?
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());
}
}
What is the primary purpose of synchronization in Java?
How does increasing the buffer size of a `BufferedWriter` generally impact performance for writing very large amounts of data?
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());
}
}
}
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?
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));
}
}
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);
}
}
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);
}
}
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");
}
}
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);
}
}
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.");
}
}
When using the `concat()` method on a String, what happens if you attempt to concatenate a `null` String reference?
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();
}
}
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;
}
});
}
}
}
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.");
}
}
}
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";
}
}
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");
}
}
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);
}
}
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.");
}
}