☕ Java MCQ Questions – Page 153

Questions 3041–3060 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3041 hard code error
What 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);
    }
}
Q3042 medium code error
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]);
    }
}
Q3043 medium code error
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();
    }
}
Q3044 medium
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?
Q3045 easy code output
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]);
    }
}
Q3046 easy code error
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);
    }
}
Q3047 hard
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?
Q3048 easy
Is `java.util.HashSet` synchronized by default?
Q3049 easy code error
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 */ }
            }
        }
    }
}
Q3050 easy code error
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
    }
}
Q3051 easy code output
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);
    }
}
Q3052 hard code error
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);
        });
    }
}
Q3053 easy
When `thread.join()` is invoked on the current thread, causing it to wait for `thread` to die, what state does the current thread enter?
Q3054 medium
For a method to successfully override another method in Java, which components of the method signature must be identical?
Q3055 easy code error
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);
    }
}
Q3056 easy code output
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);
    }
}
Q3057 medium
What is a fundamental distinction between a Java constructor and a regular method?
Q3058 easy
What are the default values for elements in a newly created `boolean` array in Java?
Q3059 medium code output
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();
        }
    }
}
Q3060 hard code error
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);
    }
}
← Prev 151152153154155 Next → Page 153 of 200 · 3994 questions