☕ Java MCQ Questions – Page 73
Questions 1441–1460 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhich 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();
}
}
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.");
}
}
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");
}
}
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.");
}
}
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);
}
}
}
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());
}
}
}
If a `break` statement is encountered inside a `do-while` loop in Java, what is its immediate effect?
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?
What is the scope of the `this` keyword inside a lambda expression compared to an anonymous inner class?
What happens if a class attempts to override a `final` method inherited from its superclass?
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;
}
}
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);
}
}
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);
}
}
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));
}
}
Which of the following is NOT a standard state in the Java Thread lifecycle (java.lang.Thread.State enum)?
What will happen when this Java code is compiled and executed?
java
class MyClass {
public void emptyThrow() throws Exception {
throw;
}
}
Can a `private` method be overloaded in Java?
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();
}
}
How can you obtain a reference to the currently executing `Thread` object?
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());
}
}
}