☕ Java MCQ Questions – Page 30

Questions 581–600 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q581 medium code error
What compile-time error will be produced when accessing `account.balance` in the `main` method?
java
class BankAccount {
    private double balance;

    public BankAccount(double initialBalance) {
        this.balance = initialBalance;
    }
}

public class Main {
    public static void main(String[] args) {
        BankAccount account = new BankAccount(1000.0);
        System.out.println(account.balance); 
    }
}
Q582 easy
How can you specify a custom sorting order for a `TreeMap`?
Q583 medium code output
What is the output of this Java code?
java
import java.util.function.Consumer;

public class LambdaBasic {
    public static void main(String[] args) {
        Consumer<String> printer = s -> System.out.println("Hello " + s);
        printer.accept("World");
    }
}
Q584 medium code output
What does this code print?
java
public class StringBuilderTest {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Java");
        sb.insert(2, "va");
        System.out.println(sb);
    }
}
Q585 hard
How does the `final` keyword for a field contribute to immutability, and what is its limitation in guaranteeing immutability?
Q586 easy code output
What does this Java code print?
java
public class ArrayTest {
    public static void main(String[] args) {
        String[] names = new String[3];
        System.out.println(names.length);
    }
}
Q587 easy
Which package contains the `java.util.HashSet` class?
Q588 easy
Which of the following is a correct usage of the `throw` keyword in Java?
Q589 hard
Consider a `do-while` loop where the `while` condition uses a post-increment or post-decrement operator on a variable (e.g., `while (x++ < 10)`). When is the modification to the variable `x` performed relative to the condition's evaluation?
Q590 hard
What is the typical direct consequence of attempting to modify a `java.util.Collection` (e.g., `ArrayList`) by adding or removing elements *directly* within an enhanced `for` (for-each) loop that is iterating over it?
Q591 easy code error
What kind of error will occur when compiling this Java code?
java
class Vehicle {
    public Vehicle() {
        System.out.println("Vehicle constructor");
    }
}

class Bike extends Vehicle {
    public Bike() {
        System.out.println("Bike constructor");
        super(); // Incorrect position
    }
}

public class Main {
    public static void main(String[] args) {
        Bike myBike = new Bike();
    }
}
Q592 medium code error
What is the result when compiling and running the following Java code?
java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter("data.txt"))) {
            writer.write("Line 1");
            writer.newLine();
            writer.close();
            writer.newLine(); // Call newLine() after close
        } catch (IOException e) {
            System.out.println(e.getClass().getSimpleName() + ": " + e.getMessage());
        }
    }
}
Q593 easy code output
What will be the content of the file 'output.txt' after this code executes?
java
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterTest {
    public static void main(String[] args) {
        try (FileWriter writer = new FileWriter("output.txt")) {
            writer.write("Initial");
        } catch (IOException e) {}

        try (FileWriter writer = new FileWriter("output.txt")) {
            // No explicit write operation here
        } catch (IOException e) {}
    }
}
Q594 easy code output
What does this code print?
java
public class Counter implements Runnable {
    private int count;

    public Counter(int initialCount) {
        this.count = initialCount;
    }

    @Override
    public void run() {
        System.out.println("Count: " + count);
    }

    public static void main(String[] args) {
        Counter c1 = new Counter(5);
        Thread t1 = new Thread(c1);
        t1.start();
    }
}
Q595 medium code error
What is the compile-time error in the following Java code?
java
class SingletonClass {
    private SingletonClass() {
        System.out.println("Singleton instance created");
    }

    // Assume there's a getInstance() method here for actual singleton pattern
}

public class ConstructorError5 {
    public static void main(String[] args) {
        SingletonClass instance = new SingletonClass(); // Error here
    }
}
Q596 hard code output
What is the output of this code?
java
public class DataTypeChallenge {
    public static void main(String[] args) {
        double d1 = Double.NaN;
        double d2 = Double.NaN;
        System.out.println(d1 == d2);
        System.out.println(d1 != d1);
        System.out.println(Double.isNaN(d1));
    }
}
Q597 hard code output
What is the output of this code?
java
public class Test {
    public static void main(String[] args) {
        String result = "";
        outer: for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                if (i == 0) {
                    if (j == 1) { result += "X"; continue outer; }
                    result += "A" + j;
                } else {
                    result += "B" + j;
                }
            }
        }
        System.out.print(result);
    }
}
Q598 easy
How does an `if-else` statement differ from a simple `if` statement in Java?
Q599 easy code error
What is the error in this Java code?
java
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterError4 {
    public static void main(String[] args) {
        FileWriter writer;
        try {
            writer = new FileWriter("data.txt");
            writer.write("Some data.");
        } catch (IOException e) {
            e.printStackTrace();
        }
        writer.close(); // Attempting to close outside the try block
    }
}
Q600 medium code error
What compilation error will this Java code produce?
java
public class SwitchError {
    public static void main(String[] args) {
        int limit = 5;
        switch (2) {
            case limit:
                System.out.println("Two");
                break;
            default:
                System.out.println("Other");
        }
    }
}
← Prev 2829303132 Next → Page 30 of 200 · 3994 questions