☕ Java MCQ Questions – Page 130

Questions 2581–2600 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2581 medium code error
What compilation error will this Java code produce? (Assume Java 11 environment)
java
public class SwitchError {
    public static void main(String[] args) {
        String status = "ACTIVE";
        String result = switch (status) {
            case "ACTIVE" -> yield "User Active";
            case "INACTIVE" -> yield "User Inactive";
            default -> yield "Unknown Status";
        };
        System.out.println(result);
    }
}
Q2582 medium code error
What error will occur when compiling this Java code?
java
public class ConditionCheck {
    public static void main(String[] args) {
        int a = 5;
        if a > 0 {
            System.out.println("Positive");
        } else {
            System.out.println("Non-positive");
        }
    }
}
Q2583 hard code error
Which type of error will this Java code produce when executed?
java
public class NullInLoop {
    public static void main(String[] args) {
        String[] values = new String[3];
        values[0] = "Alpha";
        values[1] = null; // Deliberate null
        values[2] = "Gamma";

        int i = 0;
        while (i < values.length) {
            if (values[i].length() > 0) {
                System.out.println(values[i]);
            }
            i++;
        }
    }
}
Q2584 hard code output
What is the output of this code?
java
class Parent {
    String pField = "ParentField";
    Parent() {
        System.out.print("Parent ctor: " + pField + " ");
        init();
    }
    void init() { System.out.print("Parent init. "); }
}
class Child extends Parent {
    String cField = "ChildField";
    Child() {
        System.out.print("Child ctor: " + cField + " ");
    }
    @Override
    void init() {
        System.out.print("Child init (" + cField + "). "); // cField is null here
    }
}
public class Main {
    public static void main(String[] args) { new Child(); }
}
Q2585 hard code output
What is the output of this code?
java
public class VarCompileError {
    public static void main(String[] args) {
        // 'var' requires an explicit initializer from which its type can be inferred.
        // It cannot be used for declaration without initialization or with 'null'.
        var data; // Line A
        // var info = null; // Line B (would also cause error)
        data = "Hello";
        System.out.println(data);
    }
}
Q2586 hard code error
What does this code print?
java
import java.io.*;

public class BufferResetWithoutMark {
    public static void main(String[] args) {
        String data = "Test Data";
        try (StringReader sr = new StringReader(data);
             BufferedReader br = new BufferedReader(sr)) {
            br.readLine(); // Read some data, no mark set
            br.reset(); // Attempt to reset without a mark
            System.out.println((char)br.read());
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}
Q2587 easy code error
What is the error in the following Java code snippet?
java
public class ArrayError {
    public static void main(String[] args) {
        int[] data = new int[5];
        for (int i = 0; i <= data.length; i++) {
            data[i] = i * 2;
        }
    }
}
Q2588 hard
When designing an immutable class in Java that includes a mutable object (e.g., `java.util.Date` or a `List`) as a field, what is the most critical step to ensure true immutability?
Q2589 easy code output
What is the output of this code?
java
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        ArrayList<String> animals = new ArrayList<>();
        animals.add("Dog");
        animals.add("Cat");
        animals.add("Bird");
        animals.remove("Cat");
        System.out.println(animals.size());
    }
}
Q2590 hard
When would `Arrays.deepEquals(Object[] a1, Object[] a2)` be preferred over `Arrays.equals(Object[] a1, Object[] a2)` for comparing two arrays?
Q2591 medium code error
What error will this Java code produce?
java
public class ArrayError {
    public static void main(String[] args) {
        int[][] matrix = new int[2][];
        matrix[0] = {1, 2, 3}; 
        System.out.println(matrix[0][0]);
    }
}
Q2592 easy
Can a `TreeMap` store duplicate keys?
Q2593 medium code error
What will be the compilation error in this code?
java
class Processor {
    public void process(int data) {
        System.out.println("Processing int: " + data);
    }
}

class AdvancedProcessor extends Processor {
    @Override
    public void process(String data) {
        System.out.println("Processing String: " + data);
    }
}
Q2594 easy code error
What kind of error will occur when compiling this Java code?
java
class Animal {
    public final void makeSound() {
        System.out.println("Generic sound");
    }
}

class Dog extends Animal {
    public void makeSound() {
        System.out.println("Woof");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.makeSound();
    }
}
Q2595 medium code output
What is the result of executing this Java code snippet using a switch expression (Java 14+)?
java
public class SwitchTest {
    public static void main(String[] args) {
        int score = 75;
        String grade = switch (score / 10) {
            case 10, 9 -> "A";
            case 8 -> "B";
            case 7 -> "C";
            case 6 -> "D";
            default -> "F";
        };
        System.out.println(grade);
    }
}
Q2596 medium code error
What type of error will occur when compiling the following Java code snippet?
java
public class StaticRunnable implements Runnable {
    @Override
    public static void run() {
        System.out.println("Running statically.");
    }
}

public class Main {
    public static void main(String[] args) {
        StaticRunnable task = new StaticRunnable();
    }
}
Q2597 easy code output
What is the output of this code?
java
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        ArrayList<String> items = new ArrayList<>();
        items.add("Pen");
        items.add("Book");
        items.add("Eraser");
        items.set(1, "Notebook");
        System.out.println(items.get(1));
    }
}
Q2598 hard code output
Assume a file named 'large.txt' exists and contains the text "abcdefghijklmnopqrstuvwxyz". What does this code print?
java
import java.io.FileReader;
import java.io.IOException;
import java.io.File;

public class FileReaderPatternRead {
    public static void main(String[] args) throws IOException {
        // Assume 'large.txt' has content: "abcdefghijklmnopqrstuvwxyz"
        StringBuilder sb = new StringBuilder();
        int count = 0;
        try (FileReader reader = new FileReader(new File("large.txt"))) {
            int c;
            while ((c = reader.read()) != -1) {
                if (count % 3 == 0) { 
                    sb.append((char) c);
                }
                count++;
            }
        }
        System.out.println(sb.toString());
    }
}
Q2599 medium code error
What compilation error will occur in this Java code snippet attempting to create an immutable class?
java
public class DateHolder {
    private final java.util.Date date;

    public DateHolder(java.util.Date date) {
        this.date = date; // No defensive copy
    }

    public void setDate(java.util.Date newDate) {
        this.date = newDate; // This line will cause a compilation error
    }
}
Q2600 medium
Consider a Java method with a `try-catch-finally` block. If no exception occurs within the `try` block, what is the correct sequence of execution?
← Prev 128129130131132 Next → Page 130 of 200 · 3994 questions