☕ Java MCQ Questions – Page 143

Questions 2841–2860 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2841 hard
In a traditional Java `for` loop (`for (initializer; condition; update)`), which combination of the three parts can be *legally omitted* without necessarily causing a compile-time error, potentially leading to an infinite loop?
Q2842 easy code output
Consider the following Java code. What will be printed to the console?
java
public class Employee {
    private String name;
    private int id;

    public Employee(String name, int id) {
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name.toUpperCase(); // Returns name in uppercase
    }

    public static void main(String[] args) {
        Employee emp = new Employee("john doe", 101);
        System.out.println(emp.getName());
    }
}
Q2843 medium
Consider the following declarations: `String s1 = "hello";` `String s2 = new String("hello");` `String s3 = "hello";` Which statement correctly describes the outcome of `s1 == s3` and `s1 == s2`?
Q2844 hard code error
What is the result of executing this Java code snippet?
java
import java.util.ArrayList;
import java.util.List;

public class GenericsCasting {
    public static void main(String[] args) {
        List<Integer> intList = new ArrayList<>();
        intList.add(10);
        
        List rawList = intList; 
        rawList.add("Hello"); 
        
        List<String> stringList = (List<String>) rawList; 
        String s = stringList.get(0); 
        System.out.println(s);
    }
}
Q2845 medium code output
What is the output of this code?
java
class ProcessingException extends RuntimeException {
    private int errorCode;
    public ProcessingException(String message, int code) {
        super(message);
        this.errorCode = code;
    }
    public int getErrorCode() {
        return errorCode;
    }
}

public class Main {
    public static void processData(boolean fail) {
        if (fail) {
            throw new ProcessingException("Failed to process!", 101);
        }
        System.out.println("Data processed successfully.");
    }

    public static void main(String[] args) {
        try {
            processData(true);
        } catch (ProcessingException e) {
            System.out.println("Error: " + e.getMessage() + " [Code: " + e.getErrorCode() + "]");
        }
    }
}
Q2846 hard code error
What runtime error will this Java code produce?
java
public class OperatorError7 {
    public static void main(String[] args) {
        Integer i = null;
        int j = 10;
        int result = i + j; // Error line
        System.out.println(result);
    }
}
Q2847 medium code error
What error will this Java `TreeMap` code snippet generate?
java
import java.util.TreeMap;

class Key implements Comparable<Key> {
    int value;
    public Key(int v) { this.value = v; }
    @Override
    public int compareTo(Key other) {
        if (this.value > other.value) return 1;
        return 0; // This is problematic
    }
}

public class Main {
    public static void main(String[] args) {
        TreeMap<Key, String> map = new TreeMap<>();
        map.put(new Key(1), "A");
        map.put(new Key(2), "B");
        map.put(new Key(3), "C"); // This might cause an issue
    }
}
Q2848 easy
What is a key advantage of using immutable objects, especially in multi-threaded environments?
Q2849 easy code output
What is the output of this code?
java
abstract class LivingBeing {
    public LivingBeing() {
        System.out.println("A living being is created.");
    }
    abstract void breath();
}

class Human extends LivingBeing {
    public Human() {
        // super() is implicitly called here
        System.out.println("A human is created.");
    }
    @Override
    void breath() {
        System.out.println("Breathing air.");
    }
}

public class Test {
    public static void main(String[] args) {
        Human h = new Human();
    }
}
Q2850 medium code output
What is the output of this Java code snippet?
java
import java.io.BufferedWriter;
import java.io.StringWriter;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        StringWriter sw = new StringWriter();
        BufferedWriter bw = new BufferedWriter(sw);
        bw.write("Hello");
        bw.flush();
        bw.write("World");
        bw.close();
        System.out.print(sw.toString());
    }
}
Q2851 medium
Which of the following is the correct way to declare and initialize a 3x4 two-dimensional integer array in Java?
Q2852 hard
Why are `break` and `continue` statements not directly applicable or typically used within the functional operations of the Java 8 Streams API (e.g., `filter`, `map`, `forEach`)?
Q2853 medium code error
What will be the compilation error in this code?
java
class Vehicle {
    public final void start() {
        System.out.println("Vehicle started.");
    }
}

class Car extends Vehicle {
    @Override
    public void start() {
        System.out.println("Car started.");
    }
}
Q2854 easy code error
What is the compilation error in the following Java code snippet?
java
public class LoopError {
    public static void main(String[] args) {
        int i = 0;
        do {
            System.out.println("Iteration " + i);
            i++;
        } while (i < 3)
    }
}
Q2855 hard code output
What does this code print?
java
abstract class Logger {
    public static abstract void log(String message);
}

class ConsoleLogger extends Logger {
    // Cannot override static method
    // public static void log(String message) { System.out.println("LOG: " + message); }
}

public class StaticAbstractTest {
    public static void main(String[] args) {
        // Code would be here if no error
    }
}
Q2856 hard
In Java, arrays are covariant. Which of the following statements correctly describes a potential runtime issue stemming from array covariance?
Q2857 easy code output
What is the output of this Java code?
java
public class LoopTest {
    public static void main(String[] args) {
        for (int i = 5; i > 2; i--) {
            System.out.print(i);
        }
    }
}
Q2858 easy
Which of the following classes is a common concrete implementation of the `Queue` interface in Java?
Q2859 easy code error
What is the compilation or runtime error in the following Java code?
java
public class Main {
    public static void main(String[] args) {
        String text = "Hello World";
        String sub = text.substring(0, 12);
        System.out.println(sub);
    }
}
Q2860 hard code output
What is the output of this code?
java
class ValidationException extends Exception {
    private final int errorCode;
    public ValidationException(String message, int errorCode) {
        super(message + " [Code: " + errorCode + "]");
        this.errorCode = errorCode;
    }
    public int getErrorCode() { return errorCode; }
}
public class Main {
    public static void validateInput(int value) throws ValidationException {
        if (value < 0 || value > 100) { throw new ValidationException("Input value out of range", 101); }
        System.out.println("Input valid: " + value);
    }
    public static void main(String[] args) {
        try { validateInput(150); }
        catch (ValidationException e) {
            System.out.println("Error: " + e.getMessage());
            System.out.println("Details: Code " + e.getErrorCode());
        }
    }
}
← Prev 141142143144145 Next → Page 143 of 200 · 3994 questions