☕ Java MCQ Questions – Page 45
Questions 881–900 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat kind of error will occur when compiling this Java code?
java
abstract class Shape {
abstract void draw();
public void display() {
System.out.println("Displaying shape");
}
}
public class Main {
public static void main(String[] args) {
Shape s = new Shape(); // Attempting to instantiate an abstract class
s.display();
}
}
What is the error in this Java code?
java
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map<String, String> myMap = new HashMap<>();
myMap.put("A", "1");
myMap.put("B", "2");
for (String key : myMap.keySet()) {
myMap.remove(key);
}
System.out.println(myMap.size());
}
}
What does this code print?
java
public class OperatorChallenge {
public static void main(String[] args) {
int x = 0;
boolean result = (x++ > 0 && x++ < 2);
System.out.println(x + ", " + result);
}
}
What error will occur when running this Java code snippet?
java
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
public class Test {
public static void main(String[] args) {
Map<String, Integer> scores = new HashMap<>();
scores.put("Alice", 90);
scores.put("Bob", 85);
scores.put("Charlie", 92);
for (String name : scores.keySet()) {
if (name.equals("Bob")) {
scores.remove(name);
}
}
System.out.println(scores);
}
}
What is the output of this code?
java
import java.util.PriorityQueue;
import java.util.Queue;
public class PriorityQueueNaturalOrdering {
public static void main(String[] args) {
Queue<Integer> pq = new PriorityQueue<>();
pq.offer(5);
pq.offer(1);
pq.offer(3);
System.out.print(pq.poll());
System.out.print(pq.poll());
System.out.print(pq.poll());
}
}
What is the compilation error if `doSomething()` is invoked from `main`?
java
class MyCustomThrowable extends Throwable {
public MyCustomThrowable(String message) {
super(message);
}
}
public class Main {
public static void doSomething() {
throw new MyCustomThrowable("Something failed!");
}
public static void main(String[] args) {
doSomething(); // This line causes the compilation error
}
}
What does this code print?
java
import java.io.File;
import java.io.IOException;
public class FileDeletion {
public static void main(String[] args) throws IOException {
File file = new File("temp_delete.txt");
file.createNewFile();
boolean existsBefore = file.exists();
boolean deleted = file.delete();
boolean existsAfter = file.exists();
System.out.println(existsBefore + ", " + deleted + ", " + existsAfter);
}
}
What is the output of this code?
java
import java.util.LinkedList;
import java.util.Arrays;
public class MyClass {
public static void main(String[] args) {
LinkedList<String> letters = new LinkedList<>(Arrays.asList("X", "Y", "Z"));
System.out.println(letters.get(-1)); // Invalid index
}
}
What is the immediate effect of executing an unlabeled `break` statement inside a `for` loop?
When using `try-with-resources` with a `BufferedWriter`, if an `IOException` occurs within the `try` block and subsequently another `IOException` occurs during the implicit `close()` method call of the `BufferedWriter` (e.g., while flushing remaining data), how are these exceptions handled?
What is the output of the following Java code?
java
class ChainedExceptionDemo {
static void methodA() throws java.io.IOException {
throw new java.io.IOException("Error in A");
}
static void methodB() throws Exception { // Can throw broader exception for checked exceptions
try {
methodA();
} catch (java.io.IOException e) {
throw new Exception("Error in B wrapping A", e);
}
}
public static void main(String[] args) {
try {
methodB();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
What is the output of this code?
java
public class DoWhileBreak {
public static void main(String[] args) {
int a = 0;
int b = 1;
String res = "";
do {
res += a + "" + b;
if (a == 2) {
break;
}
a++;
if (b % 2 == 0) {
b += a;
} else {
b *= a;
}
} while (a < 3);
System.out.println(res);
}
}
What does this code print?
java
public class StringUnicodeTest {
public static void main(String[] args) {
String emoji = "Hello \uD83D\uDE00 World"; // U+1F600 GRINNING FACE
System.out.println(emoji.length() + ", " + emoji.codePointCount(0, emoji.length()));
}
}
Consider the following `try-with-resources` block:
java
try (BufferedReader reader = new BufferedReader(new FileReader("data.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
// Process line
}
} catch (IOException e) {
// Handle exception
}
If an `IOException` occurs *during* a call to `reader.readLine()` within the `while` loop, what is guaranteed regarding the `reader` resource?
What exception is thrown when the `ois.readObject()` line is executed during deserialization?
java
import java.io.*;
class MismatchedSerialization implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int id;
public MismatchedSerialization(String name, int id) {
this.name = name;
this.id = id;
}
private void writeObject(ObjectOutputStream oos) throws IOException {
oos.writeUTF(name); // Writes String first
oos.writeInt(id); // Writes int second
}
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
this.id = ois.readInt(); // Tries to read int first
this.name = ois.readUTF(); // Tries to read String second
}
@Override
public String toString() {
return "Name: " + name + ", ID: " + id;
}
}
public class SerializationError5 {
public static void main(String[] args) throws Exception {
MismatchedSerialization obj = new MismatchedSerialization("Alice", 42);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
byte[] serializedData = bos.toByteArray();
oos.close();
ByteArrayInputStream bis = new ByteArrayInputStream(serializedData);
ObjectInputStream ois = new ObjectInputStream(bis);
MismatchedSerialization deserialized = (MismatchedSerialization) ois.readObject(); // Error occurs here
ois.close();
System.out.println(deserialized);
}
}
What is wrong with the `run()` method implementation in the following `Runnable` class definition?
java
public class RestrictedRunnable implements Runnable {
@Override
private void run() {
System.out.println("This should be running...");
}
}
public class Main {
public static void main(String[] args) {
RestrictedRunnable task = new RestrictedRunnable();
}
}
What is the error in this Java code?
java
import java.io.FileWriter;
public class FileWriterError2 {
public static void main(String[] args) throws java.io.IOException {
FileWriter writer = new FileWriter("test.txt");
int[] numbers = {1, 2, 3};
writer.write(numbers); // Attempting to write an array of integers directly
writer.close();
}
}
What is the compilation error in the following Java snippet?
java
import java.util.function.Function;
@FunctionalInterface
interface Converter {
int convert(String s);
}
public class Main {
public static void main(String[] args) {
Converter c = s -> "Result: " + s;
System.out.println(c.convert("123"));
}
}
What is the output of this code?
java
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, String> capitals = new HashMap<>();
capitals.put("USA", "Washington DC");
capitals.put("France", "Paris");
capitals.put("USA", "New York"); // Overwrites previous value
System.out.println(capitals.get("USA"));
}
}
A `StringBuffer` initially holds 100 characters and has an internal capacity of 200. If `sb.delete(0, 90)` is called, removing 90 characters, what is the most accurate statement regarding the `StringBuffer`'s internal capacity afterwards?