☕ Java MCQ Questions – Page 93

Questions 1841–1860 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1841 medium code output
What is the output of this code?
java
class MyProcessor {
    public static void process(int value) throws Exception {
        if (value < 0) {
            throw new Exception("Negative value not allowed");
        }
        System.out.println("Processing: " + value);
    }

    public static void main(String[] args) {
        try {
            process(-10);
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        }
        System.out.println("Program finished.");
    }
}
Q1842 medium code error
What is the compilation error in the provided Java code?
java
abstract class ServiceConfig {
    public abstract static void setup(); // ERROR: abstract static method
    
    public void load() {
        System.out.println("Loading configuration.");
    }
}

class MyService extends ServiceConfig {
    // Must implement setup(), but it's static in parent
    // public static void setup() { ... } 
}

public class Main {
    public static void main(String[] args) {
        // ServiceConfig.setup();
    }
}
Q1843 easy code output
What is the output of this code?
java
public class StringTest {
    public static void main(String[] args) {
        String s = "Hello";
        System.out.println(s.length());
    }
}
Q1844 hard
Which of the following is a *valid* way to create an instance of a class `MyClass` that has *only* a private constructor, *without* adding a public static factory method within `MyClass` itself?
Q1845 medium
The `exists()` method of the `File` class can return `true` for which of the following file system entities?
Q1846 medium code error
What error does this Java code snippet produce when executed?
java
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        TreeMap<String, Integer> map = new TreeMap<>();
        map.put("Apple", 10);
        map.put(null, 5);
        System.out.println(map.size());
    }
}
Q1847 hard
What is the consequence if a constructor explicitly throws a *checked exception* without declaring it in its `throws` clause?
Q1848 easy code output
What does this code print?
java
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        TreeSet<Double> ts = new TreeSet<>();
        boolean added1 = ts.add(1.1);
        boolean added2 = ts.add(2.2);
        boolean added3 = ts.add(1.1);
        System.out.println(added1 + ", " + added2 + ", " + added3);
    }
}
Q1849 easy
Does the `run()` method defined in the `Runnable` interface return any value?
Q1850 easy code output
What is the output of this code?
java
interface First {
    void methodOne();
}

interface Second {
    void methodTwo();
}

class MyClass implements First, Second {
    @Override
    public void methodOne() {
        System.out.println("Method One executed.");
    }

    @Override
    public void methodTwo() {
        System.out.println("Method Two executed.");
    }
}

public class Main {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        obj.methodOne();
        obj.methodTwo();
    }
}
Q1851 hard
Beyond automatic resource closure, what significant advantage does `try-with-resources` offer over using a traditional `try-finally` block for managing `AutoCloseable` resources, particularly concerning multiple exceptions?
Q1852 hard
A method `A.method()` declares `throws IOException`. A subclass method `B.method()` overrides it. Which exception declaration is *invalid* for `B.method()`?
Q1853 easy code error
What is the error in this code?
java
import java.util.TreeMap;

class MyKey { /* No Comparable implementation */ }

public class Test {
    public static void main(String[] args) {
        TreeMap<MyKey, String> map = new TreeMap<>();
        map.put(new MyKey(), "Value1");
        map.put(new MyKey(), "Value2");
    }
}
Q1854 easy
If the condition in a do-while loop is initially false, what happens?
Q1855 hard code output
What is the output of this Java code?
java
String a = new String("abc");
String b = "abc";
String c = "a" + "bc";
String d = a.intern();

System.out.println(a == b);
System.out.println(b == c);
System.out.println(d == b);
Q1856 medium
Consider two object reference variables, `obj1` and `obj2`. If `obj2 = obj1;` is executed, what is the outcome?
Q1857 easy
What is the primary characteristic of method overloading in Java?
Q1858 hard code error
What is the runtime error encountered when executing this Java code snippet?
java
import java.util.HashSet;
import java.util.Objects;

class DisposableKey {
    String id;
    boolean disposed = false;
    public DisposableKey(String id) { this.id = id; }
    public void dispose() { this.disposed = true; }
    @Override
    public int hashCode() {
        if (disposed) { throw new IllegalStateException("Hashed a disposed key!"); }
        return Objects.hashCode(id);
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        DisposableKey that = (DisposableKey) o;
        if (this.disposed || that.disposed) { throw new IllegalStateException("Compared disposed keys!"); }
        return Objects.equals(id, that.id);
    }
}

public class Main {
    public static void main(String[] args) {
        HashSet<DisposableKey> set = new HashSet<>();
        DisposableKey key1 = new DisposableKey("data1");
        set.add(key1);
        key1.dispose(); // Key state changes after being added
        set.contains(new DisposableKey("data1")); // Triggering hashCode/equals on potentially disposed key
    }
}
Q1859 easy code output
What is the output of the following Java program?
java
public class Test {
    public static void main(String[] args) {
        int[] arr1 = {10, 20};
        int[] arr2 = arr1;
        arr2[0] = 5;
        System.out.println(arr1[0]);
    }
}
Q1860 easy
Does `java.util.HashSet` allow `null` elements?
← Prev 9192939495 Next → Page 93 of 200 · 3994 questions