☕ Java MCQ Questions – Page 53
Questions 1041–1060 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat is the error in the following Java code snippet?
java
public class ArrayError {
public static void main(String[] args) {
int myArray = new int[5];
}
}
What is the output of this Java code?
java
import java.util.function.Predicate;
public class Test {
public static void main(String[] args) {
Predicate<Integer> isEven = num -> num % 2 == 0;
Predicate<Integer> isGreaterThanTen = num -> num > 10;
System.out.println(isEven.test(12));
System.out.println(isGreaterThanTen.test(9));
}
}
What kind of error will the following Java code snippet produce?
java
import java.io.*;
public class FileWriterError2 {
public static void main(String[] args) {
try (FileWriter fw = new FileWriter("testfile.txt")) {
char[] data = {'H', 'e', 'l', 'l', 'o'};
fw.write(data, 0, 10); // Attempt to write more characters than available
} catch (IOException | IndexOutOfBoundsException e) {
System.out.println("Error: " + e.getClass().getSimpleName() + " - " + e.getMessage());
}
}
}
Which compilation error will occur in the following Java code snippet?
java
import java.util.function.Predicate;
@FunctionalInterface
interface MyValidator {
boolean validate(String input);
default void logValidationAttempt();
}
public class Main {
public static void main(String[] args) {
// Attempt to use MyValidator
}
}
What common exception might be thrown when working with `BufferedReader` or `BufferedWriter` operations?
What is the output of this Java code?
java
import java.io.*;
class NonSerializableField {
String value;
public NonSerializableField(String value) { this.value = value; }
}
class Container implements Serializable {
String id;
NonSerializableField field;
public Container(String id, String value) {
this.id = id;
this.field = new NonSerializableField(value);
}
}
public class DeserializationError2 {
public static void main(String[] args) {
byte[] data = null;
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
Container container = new Container("C1", "Data");
oos.writeObject(container);
data = bos.toByteArray();
} catch (IOException e) {
System.out.println("Error: " + e.getClass().getName() + ": " + e.getMessage());
}
}
}
What does this code print?
java
public class Main {
public static void main(String[] args) {
int[][] matrix = {{1, 2}, {3, 4, 5}};
System.out.println(matrix[1].length);
}
}
What is printed when this code is executed?
java
public class MultiArrayQ10 {
public static void main(String[] args) {
int[][][] cube = new int[2][][];
cube[0] = new int[2][3];
cube[1] = new int[1][1];
cube[0][0][0] = 10;
try {
System.out.println(cube[1][0][1]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("AIOOBE caught");
} catch (Exception e) {
System.out.println(e.getClass().getSimpleName());
}
System.out.println(cube[0][0][0]);
}
}
Consider two methods: `doWork(int a, String b)` and `doWork(String b, int a)`. Are these valid overloaded methods?
What is the outcome when compiling and running this Java code?
java
public class FinalPrimitiveReassignment {
public static void main(String[] args) {
final int count = 5;
count = 10;
System.out.println(count);
}
}
When using multiple `catch` blocks for a single `try` block, what is the recommended and required order for the `catch` blocks?
What is the output of this code?
java
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Comparator;
public class PriorityQueueCustomOrdering {
public static void main(String[] args) {
Queue<String> pq = new PriorityQueue<>(Comparator.reverseOrder());
pq.offer("apple");
pq.offer("banana");
pq.offer("cherry");
System.out.println(pq.poll());
System.out.println(pq.peek());
}
}
What error will occur when deserializing the `Student` object, given that `Address` does not implement `Serializable`?
java
import java.io.*;
class Address {
String street;
Address(String street) { this.street = street; }
}
class Student implements Serializable {
String name;
Address address;
public Student(String name, String street) {
this.name = name;
this.address = new Address(street);
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student("Alice", "123 Main St");
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student.ser"))) {
oos.writeObject(s);
} catch (IOException e) {
System.err.println(e.getClass().getSimpleName() + ": " + e.getMessage());
}
}
}
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 FileReaderError2 {
public static void main(String[] args) {
File file = new File("test_fr2.txt");
try {
file.createNewFile();
try (FileReader fr = new FileReader(file)) {
fr.close(); // Close the stream
int data = fr.read(); // Attempt to read after closing
System.out.println("Read: " + (char)data);
}
} catch (IOException e) {
System.err.println(e.getClass().getSimpleName() + ": " + e.getMessage());
} finally {
file.delete();
}
}
}
What is the compilation error in the `Counter` class?
java
class Counter {
int count = 0;
public static void increment() {
count++;
}
}
public class Main {
public static void main(String[] args) {
Counter.increment();
}
}
What compile-time error will occur in the `Child` class?
java
class Parent {
public void startProcess() {
System.out.println("Parent process started.");
}
}
class Child extends Parent {
@Override
public void startProcass() {
System.out.println("Child process started.");
}
}
What is the primary difference between `String` and `StringBuffer` in Java regarding their object state?
Is `java.lang.StringBuilder` an immutable class?
What is the output of this Java code?
java
import java.util.TreeMap;
public class Test {
public static void main(String[] args) {
TreeMap<String, Integer> map = new TreeMap<>();
map.put("A", 1);
map.put("B", 2);
Integer oldVal1 = map.replace("A", 10); // Replaces 1 with 10
boolean replaced2 = map.replace("B", 20, 200); // Fails, old value is 2, not 20
boolean replaced3 = map.replace("C", 3, 30); // Fails, key C does not exist
Integer oldVal4 = map.replace("D", 4); // Returns null, key D does not exist
System.out.println(oldVal1 + ", " + replaced2 + ", " + replaced3 + ", " + oldVal4 + ", " + map.get("B"));
}
}
Which of the following Stream API operations is considered a terminal operation?