☕ Java MCQ Questions – Page 151
Questions 3001–3020 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhich statement about the `@FunctionalInterface` annotation is FALSE?
What is the primary cause of a `ConcurrentModificationException` when iterating over a `HashMap`?
What does this code print?
java
public class MyCallableRunnable implements Runnable {
public static void sayHello() {
System.out.println("Hello from Runnable!");
}
@Override
public void run() {
sayHello();
}
public static void main(String[] args) {
Thread thread = new Thread(new MyCallableRunnable());
thread.start();
}
}
Consider the following Java code snippet:
java
int[][] matrix = new int[3][];
System.out.println(matrix[0][0]);
What will be the output or error when this code is executed?
What compile-time error will this Java code produce?
java
public class OperatorError2 {
public static void main(String[] args) {
boolean flag = true;
boolean result = ~flag; // Error line
System.out.println(result);
}
}
What is the consequence of running this Java code snippet?
java
import java.util.TreeMap;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
Comparator<String> caseInsensitive = String::compareToIgnoreCase;
TreeMap<String, Integer> map = new TreeMap<>(caseInsensitive);
map.put("apple", 1);
map.put("Banana", 2);
map.put(null, 3); // Intentional error point
System.out.println(map.size());
}
}
Which of the following is the correct way to iterate through all elements of an `int` array named `data` using a traditional `for` loop?
What happens when a thread attempts to enter a `synchronized` block or method, but the required lock is already held by another thread?
Given the following Java class:
java
class Overload {
void process(Integer i) { System.out.println("Integer"); }
void process(int... args) { System.out.println("Varargs"); }
}
What will be printed when the following code is executed?
`new Overload().process(10);`
Java Records aim to simplify boilerplate for 'data carrier' classes. How do they handle encapsulation for their components compared to traditional classes with `private` fields and `public` getters?
In a scenario where data is frequently read but rarely modified, which synchronization mechanism is typically preferred for improved concurrency and why?
What exception is thrown when the `ois.readObject()` line is executed during deserialization?
java
import java.io.*;
class CustomFieldsClass implements Serializable {
private static final long serialVersionUID = 1L;
private String fieldA;
private int fieldB;
private String fieldC;
// Custom serialization fields declaration
private static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("fieldA", String.class),
new ObjectStreamField("fieldC", String.class) // fieldC is declared here
};
public CustomFieldsClass(String a, int b, String c) {
this.fieldA = a;
this.fieldB = b;
this.fieldC = c;
}
private void writeObject(ObjectOutputStream out) throws IOException {
ObjectOutputStream.PutField fields = out.putFields();
fields.put("fieldA", fieldA); // Only fieldA is actually written
// fieldC is NOT explicitly put into the stream
out.writeFields();
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
ObjectInputStream.GetField fields = in.readFields();
fieldA = (String) fields.get("fieldA", null);
// Attempting to read fieldC which was declared in serialPersistentFields but not written
fieldC = (String) fields.get("fieldC", "defaultC"); // Error occurs here if default value not provided or type mismatch
fieldB = (int) fields.get("fieldB", 0); // This field is not in serialPersistentFields, nor explicitly written
}
public String getFieldA() { return fieldA; }
public int getFieldB() { return fieldB; }
public String getFieldC() { return fieldC; }
}
public class SerializationError8 {
public static void main(String[] args) throws Exception {
CustomFieldsClass obj = new CustomFieldsClass("ValueA", 123, "ValueC");
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);
CustomFieldsClass deserialized = (CustomFieldsClass) ois.readObject(); // Error occurs during readObject
ois.close();
System.out.println(deserialized.getFieldA() + ", " + deserialized.getFieldB() + ", " + deserialized.getFieldC());
}
}
What is the compilation error in the following Java code snippet?
java
public class LoopError {
public static void main(String[] args) {
int x = 0;
do {
System.out.println(x++);
if (x == 2) continue;
} while (x < 5);
System.out.println("Loop finished.");
}
}
What is the output of this code?
java
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
HashSet<Object> set = new HashSet<>();
char[] array1 = {'a', 'b'};
char[] array2 = {'a', 'b'};
String str1 = new String(array1);
String str2 = new String(array2);
set.add(array1);
set.add(array2);
set.add(str1);
set.add(str2);
System.out.println(set.size());
}
}
What is the error encountered when running this Java code?
java
import java.io.FileReader;
import java.io.IOException;
import java.io.File;
public class FileReaderError9 {
public static void main(String[] args) {
File file = new File("test_fr9.txt");
try {
file.createNewFile();
try (FileReader fr = new FileReader(file)) {
char[] buffer = new char[10];
fr.read(buffer, 0, -5); // Negative length
System.out.println("Read successfully");
}
} catch (IOException e) {
System.err.println(e.getClass().getSimpleName() + ": " + e.getMessage());
} catch (IndexOutOfBoundsException e) {
System.err.println(e.getClass().getSimpleName());
} finally {
file.delete();
}
}
}
To enable an instance of a custom class `MyCustomCollection` to be directly used with the enhanced `for` (for-each) loop syntax in Java, which specific interface *must* `MyCustomCollection` implement?
Consider an `int[][] data`. To iterate through all elements using enhanced for loops, which structure is correct?
What is the output or error of the following Java code?
java
public class ExceptionTest {
public static void main(String[] args) {
try {
String s = null;
System.out.println(s.length());
} catch (java.io.FileNotFoundException e) {
System.out.println("Caught FileNotFoundException");
}
System.out.println("End of program");
}
}
What is wrong with this Java code?
java
import java.util.function.Function;
public class Test {
public static void main(String[] args) {
Function<Integer, Integer> doubler = x -> return x * 2;
}
}
What does this Java code print?
java
public class WhileLoopDemo {
public static void main(String[] args) {
int num = 0;
while (num < 5) {
if (num % 2 == 0) {
System.out.print(num);
}
num++;
}
}
}