☕ Java MCQ Questions – Page 153
Questions 3041–3060 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat error occurs when executing the following Java code?
java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
ArrayList rawList = new ArrayList<Integer>();
rawList.add("This is a string"); // Adding a String to a raw ArrayList
Integer value = (Integer) rawList.get(0);
System.out.println(value);
}
}
What error will this Java code produce when executed?
java
public class ArrayError {
public static void main(String[] args) {
int[][] matrix = new int[2][3];
matrix[0][0] = 1;
matrix[1][0] = 2;
System.out.println(matrix[2][0]);
}
}
What is the compilation error in the provided Java code?
java
abstract class Shape {
public abstract void draw();
public void display() {
System.out.println("Displaying shape");
}
}
public class TestShape {
public static void main(String[] args) {
Shape s = new Shape(); // ERROR: cannot instantiate abstract class
s.display();
}
}
What typically happens if a `HashSet` is structurally modified (e.g., adding or removing elements) while it is being iterated over using its default iterator?
What is the output of this Java code snippet?
java
public class ArrayTest {
public static void main(String[] args) {
int[] numbers = {1, 2, 3};
numbers[0] = 5;
System.out.println(numbers[0]);
}
}
What compilation error will occur when compiling this Java code?
java
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;
public class MyClass {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new StringReader("data"));
String line = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(br);
}
}
In Java's `AbstractList` implementation, the `modCount` field tracks structural modifications. Which of the following modifications to an `ArrayList` instance, performed *after* its `Iterator` has been obtained but *before* the `Iterator` completes, would *not* reliably lead to a `ConcurrentModificationException` upon a subsequent `next()` or `hasNext()` call, assuming it's a single-threaded environment and the modification happens externally to the iterator's own `remove()` method?
Is `java.util.HashSet` synchronized by default?
What is the error in this Java code?
java
import java.io.FileWriter;
import java.io.IOException;
import java.io.File;
public class FileWriterError5 {
public static void main(String[] args) {
FileWriter writer = null;
try {
// Trying to create a file in a non-existent directory
writer = new FileWriter("nonexistent_dir/output.txt");
writer.write("Directory issues.");
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
} finally {
if (writer != null) {
try { writer.close(); } catch (IOException e) { /* ignore */ }
}
}
}
}
What is the error in this Java code?
java
final class BaseClass {
public void method() {
System.out.println("Base method");
}
}
class DerivedClass extends BaseClass { // Line 7
public void anotherMethod() {
System.out.println("Derived method");
}
}
public class Main {
public static void main(String[] args) {
// DerivedClass d = new DerivedClass(); // If uncommented, would show the error
}
}
What is the output of this Java code snippet?
java
public class LoopTest {
public static void main(String[] args) {
int start = 10;
do {
System.out.print("Execute ");
start++;
} while (start < 10);
}
}
What compilation error will occur when compiling this Java code?
java
import java.util.Arrays;
import java.util.List;
public class LambdaBreak {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3);
numbers.forEach(n -> {
if (n == 2) {
break;
}
System.out.println(n);
});
}
}
When `thread.join()` is invoked on the current thread, causing it to wait for `thread` to die, what state does the current thread enter?
For a method to successfully override another method in Java, which components of the method signature must be identical?
What will happen when this Java code is compiled?
java
public class MyClass {
public static void main(String[] args) {
final int FIXED_VALUE = 25;
FIXED_VALUE = 30;
System.out.println(FIXED_VALUE);
}
}
What is the output of this Java code?
java
class ValidationException extends Exception {
public ValidationException(String message) {
super(message);
}
}
public class Main {
public static void validateAge(int age) {
if (age < 0) {
throw new ValidationException("Age cannot be negative"); // Compiler error here
}
System.out.println("Age is valid: " + age);
}
public static void main(String[] args) {
validateAge(-5);
}
}
What is a fundamental distinction between a Java constructor and a regular method?
What are the default values for elements in a newly created `boolean` array in Java?
What does this code print?
java
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;
public class BufferedReaderTest {
public static void main(String[] args) {
String fileContent = "Line A\nLine B\nLine C";
try (BufferedReader br = new BufferedReader(new StringReader(fileContent))) {
String line = br.readLine();
System.out.println(line.charAt(line.length() - 1));
System.out.println(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
}
What is the compile-time error in the provided Java code snippet using record patterns in a switch expression?
java
record Point(int x, int y) {}
public class RecordPatternMismatchError {
public static void main(String[] args) {
Object obj = new Point(1, 2);
String description = switch (obj) {
case Point(int a, double b) -> "Point with a: " + a + ", b: " + b; // Error expected here
default -> "Unknown";
};
System.out.println(description);
}
}