☕ Java MCQ Questions – Page 122

Questions 2421–2440 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2421 hard code error
What error will occur when the `decrementCount` method is called without holding the lock?
java
import java.util.concurrent.locks.StampedLock;

public class StampedLockError {
    private StampedLock lock = new StampedLock();
    private int count = 0;

    public void decrementCount() {
        // This method does not acquire any lock before calling unlockWrite
        long stamp = 0; // Invalid stamp
        lock.unlockWrite(stamp); // Attempt to unlock with an invalid stamp
        count--;
    }

    public static void main(String[] args) {
        StampedLockError example = new StampedLockError();
        example.decrementCount();
    }
}
Q2422 hard code output
What is the output of this code?
java
public class EnumVars {
    public enum Status {
        ACTIVE("A"), INACTIVE("I");
        private final String code;
        Status(String code) { this.code = code; }
        public String getCode() { return code; }
    }
    public static void main(String[] args) {
        Status s1 = Status.ACTIVE;
        String c1 = s1.getCode();
        s1 = Status.INACTIVE;
        String c2 = Status.ACTIVE.getCode();
        System.out.println(c1 + c2);
    }
}
Q2423 easy code error
What compile-time error will occur when creating a `MyClass` object?
java
class MyClass {
    public My_Class() { // Note the underscore
        System.out.println("Init");
    }
}
public class Main {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
    }
}
Q2424 easy
Which of the following is the most common and efficient way to create a String literal in Java?
Q2425 easy
What is a class in Java?
Q2426 medium code error
What error will prevent this Java code from compiling, specifically concerning the `calculate` method call?
java
public class OverloadChecker {
    public void calculate(long l) {
        System.out.println("Long: " + l);
    }

    public void calculate(float f) {
        System.out.println("Float: " + f);
    }

    public static void main(String[] args) {
        OverloadChecker oc = new OverloadChecker();
        oc.calculate(10); // This line causes the error
    }
}
Q2427 hard code output
What is the output of this Java code snippet, demonstrating lambda expression closure?
java
public class LambdaRunnableClosure {
    private static int staticCounter = 0;

    public static void main(String[] args) throws InterruptedException {
        int localId = 100;

        Runnable lambdaTask = () -> {
            staticCounter++;
            // localId++; // This would be a compilation error
            System.out.println("Runnable Id: " + localId + ", Static Counter: " + staticCounter + ", Thread: " + Thread.currentThread().getName());
        };

        Thread t1 = new Thread(lambdaTask, "Thread-A");
        Thread t2 = new Thread(lambdaTask, "Thread-B");

        t1.start();
        t2.start();

        t1.join();
        t2.join();
    }
}
Q2428 medium code output
What is the output of this code?
java
import java.util.HashMap;

public class Test {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("Alpha", 10);
        map.put("Beta", 20);
        System.out.println(map.get("Alpha"));
    }
}
Q2429 medium
In Java, without explicitly using curly braces `{}` for block statements, to which `if` statement does an `else` statement always associate?
Q2430 easy code output
What is the output of this code?
java
public class Main {
    public static void main(String[] args) {
        boolean x = true;
        boolean y = false;
        System.out.println(x || y);
    }
}
Q2431 easy code output
What does this code print?
java
import java.util.LinkedList;

public class MyClass {
    public static void main(String[] args) {
        LinkedList<Double> values = new LinkedList<>();
        values.add(1.1);
        values.add(2.2);
        values.add(3.3);
        for (Double val : values) {
            System.out.print(val + " ");
        }
    }
}
Q2432 hard code error
What is the error in the following Java code?
java
class DataProcessor<T> {
    T process() { return null; }
}

class IntegerProcessor extends DataProcessor<Integer> {
    @Override
    int process() { // Covariant return type mismatch: Integer vs int
        return 0;
    }
}
Q2433 hard code error
What is the compile-time error in this Java code?
java
interface Logger {
    public static abstract void log(String message);
    default void info(String message) {
        System.out.println("INFO: " + message);
    }
}
Q2434 easy
What is the effect of the `continue` keyword when encountered inside a `for` loop?
Q2435 easy code error
What is the runtime error in the following Java code snippet?
java
import java.util.ArrayList;
import java.util.Iterator;

public class Question10 {
    public static void main(String[] args) {
        ArrayList<String> items = new ArrayList<>();
        items.add("Apple");
        items.add("Banana");
        for (String item : items) {
            if (item.equals("Apple")) {
                items.remove(item);
            }
        }
        System.out.println(items);
    }
}
Q2436 hard
What is the outcome if a `break` statement uses a label that identifies a simple code block (i.e., not a loop or a `switch` statement)?
Q2437 hard
In Java, what specific object is associated with a `synchronized` block or method and used by the JVM to manage exclusive access?
Q2438 hard
If a labeled `continue` statement targets an *outer* `do-while` loop from within an *inner* `do-while` loop, what is the exact flow of execution?
Q2439 hard code output
What is the output of this code?
java
abstract class Vehicle {
    String type;
    public Vehicle(String type) {
        this.type = type;
        System.out.println("Vehicle constructor: " + type);
    }
}

class Car extends Vehicle {
    public Car() {
        super("Car");
        System.out.println("Car constructor.");
    }
}

public class ConstructorTest {
    public static void main(String[] args) {
        new Car();
    }
}
Q2440 hard code error
Examine the following Java code. What is the specific compilation error?
java
public class NarrowingConversion {
    public static void main(String[] args) {
        int largeNum = 200;
        byte smallNum = largeNum; // Illegal without explicit cast
        System.out.println(smallNum);
    }
}
← Prev 120121122123124 Next → Page 122 of 200 · 3994 questions