☕ Java MCQ Questions – Page 149
Questions 2961–2980 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat is the output of this code?
java
class SuperClass {
String message = "Hello from Super";
}
class SubClass extends SuperClass {
String message = "Hello from Sub";
}
public class Main {
public static void main(String[] args) {
SuperClass ref = new SubClass();
System.out.print(ref.message);
}
}
What is the literal meaning of 'Polymorphism' in the context of Java?
What does this code print?
java
import java.util.LinkedList;
public class MyClass {
public static void main(String[] args) {
LinkedList<String> items = new LinkedList<>();
items.add("Pen");
items.add("Pencil");
items.add("Eraser");
items.remove("Pencil");
System.out.println(items.size());
}
}
What is the error in the execution of this Java code snippet?
java
public class BuilderError {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Java");
sb.setCharAt(4, '!');
System.out.println(sb);
}
}
What is the compile-time or runtime error in the following Java code snippet after deserialization?
java
import java.io.*;
class Configuration implements Serializable {
private String setting1;
private transient String setting2; // Will not be serialized
public Configuration(String s1, String s2) {
this.setting1 = s1;
this.setting2 = s2;
}
public String getInfo() {
return "Setting 1: " + setting1 + ", Setting 2 length: " + setting2.length();
}
}
public class SerializationQuestion8 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Configuration config = new Configuration("ValueA", "ValueB");
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("config.ser"))) {
oos.writeObject(config);
}
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("config.ser"))) {
Configuration loadedConfig = (Configuration) ois.readObject();
System.out.println(loadedConfig.getInfo()); // This line will cause an error
}
}
}
Which compile-time error occurs in this Java class definition?
java
class Item {
final String name;
final int id;
Item() {
this.name = "Default";
}
}
What is the error in the following Java code?
java
public class Main {
public static void main(String[] args) {
long l = 20000000000L; // 20 billion
int i = l;
System.out.println(i);
}
}
What is the result of compiling and running the following Java code snippet?
java
public class StringError {
public static void main(String[] args) {
String data = "item1";
String[] parts = data.split(",");
System.out.println(parts[1]);
}
}
Regarding the combination of `abstract` and `static` modifiers in Java, which statement is accurate?
What is the output of this code?
java
public class Q7_JoinTimeoutState {
public static void main(String[] args) throws InterruptedException {
Thread worker = new Thread(() -> {
try {
Thread.sleep(500); // Takes long time
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
worker.start();
Thread.sleep(10); // Ensure worker started
System.out.println("Worker state before join: " + worker.getState());
worker.join(100); // Main thread waits for max 100ms
System.out.println("Worker state after join with timeout: " + worker.getState());
System.out.println("Main thread continues.");
worker.join(); // Main thread waits indefinitely now for worker to finish
System.out.println("Worker state after final join: " + worker.getState());
}
}
How can an instance of a `Runnable` implementation receive initial data or parameters that it needs to perform its task?
What exception is thrown when the `oos.writeObject(container)` line is executed in the `main` method?
java
import java.io.*;
class ClassContainer implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private Class<?> type; // Field of type Class, which is not Serializable
public ClassContainer(String name, Class<?> type) {
this.name = name;
this.type = type;
}
public Class<?> getType() { return type; }
}
public class SerializationError7 {
public static void main(String[] args) throws IOException {
ClassContainer container = new ClassContainer("String Type", String.class);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(container); // Error occurs here
oos.close();
}
}
Given the declaration `final int[] numbers = {1, 2, 3};`. Which of the following operations is permissible without causing a compile-time error?
What does this Java code print, considering the `BufferedWriter`'s internal buffer?
java
import java.io.BufferedWriter;
import java.io.StringWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
StringWriter sw = new StringWriter();
BufferedWriter bw = new BufferedWriter(sw, 4); // Small buffer for demonstration
bw.write('1');
bw.write('2');
bw.write('3');
bw.write('4');
bw.write('5');
bw.close();
System.out.print(sw.toString());
}
}
What happens when you try to compile and run this Java code?
java
public class Main {
public static void main(String[] args) {
int x = 10;
if (x > 5) {
continue;
}
System.out.println("Hello");
}
}
Which statement correctly describes the conversion of a `double` value to an `int` variable in Java?
Unlike the `synchronized` keyword, what advanced capability does `java.util.concurrent.locks.ReentrantLock` provide?
What does this Java code print?
java
import java.io.BufferedWriter;
import java.io.StringWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
StringWriter sw = new StringWriter();
BufferedWriter bw = new BufferedWriter(sw);
char[] chars = {'A', 'B', 'C', 'D', 'E'};
bw.write(chars, 1, 3); // Writes 'BCD'
bw.write("Testing", 0, 2); // Writes 'Te'
bw.close();
System.out.print(sw.toString());
}
}
Assume a file named 'protected.txt' exists but the application does not have read permissions for it. What is the output of this code?
java
import java.io.FileReader;
import java.io.IOException;
import java.io.File;
public class FileReaderPermission {
public static void main(String[] args) {
// Assume 'protected.txt' exists but has no read permissions
String fileName = "protected.txt";
try (FileReader reader = new FileReader(new File(fileName))) {
System.out.println("File opened successfully.");
reader.read();
} catch (IOException e) {
String msg = e.getMessage();
if (msg != null && (msg.contains("Permission denied") || msg.contains("Access is denied"))) {
System.out.println("Error: Permission denied.");
} else if (msg != null && msg.contains("No such file or directory")) {
System.out.println("Error: File not found.");
} else {
System.out.println("Error: An unexpected I/O error occurred: " + msg);
}
}
}
}
What is the runtime error encountered when executing this Java code snippet?
java
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<String> set = Collections.singleton("initial");
set.add("another"); // Attempt to modify singleton set
}
}