☕ Java MCQ Questions – Page 31

Questions 601–620 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q601 hard code output
What is the output of this code?
java
public class ArrayInstanceofCheck {
    public static void main(String[] args) {
        Object obj1 = new int[5];
        Object obj2 = new Integer[5];
        Object obj3 = new String[5];

        System.out.println(obj1 instanceof int[]);
        System.out.println(obj2 instanceof Integer[]);
        System.out.println(obj3 instanceof Object[]);
        System.out.println(obj1 instanceof Object[]);
    }
}
Q602 hard code error
What is the compilation error, if any, for the provided Java code snippet?
java
import java.util.List;
import java.util.Set;

class Overloader {
    public void process(List l) {}
    public void process(Set s) {}

    public static void main(String[] args) {
        Overloader o = new Overloader();
        o.process(null); // Line X
    }
}
Q603 easy code error
What kind of error will occur when compiling this Java code?
java
final class Base {
    public void show() {
        System.out.println("Base");
    }
}

class Derived extends Base {
    public void show() {
        System.out.println("Derived");
    }
}

public class Main {
    public static void main(String[] args) {
        Derived d = new Derived();
        d.show();
    }
}
Q604 easy
Which method should be used to compare the content of two String objects for equality in Java?
Q605 easy
If an exception occurs within a `try` block and there is a matching `catch` block, what is the typical order of execution?
Q606 easy
In which Java package is the `Iterator` interface located?
Q607 medium
What is the primary purpose of the `java.io.FileReader` class?
Q608 easy code output
What will be printed to the console when this code runs?
java
class Counter {
    int count = 0;
    public void increment() {
        count = count + 1;
    }
}
public class Main {
    public static void main(String[] args) {
        Counter c = new Counter();
        c.increment();
        c.increment();
        System.out.print("Count: " + c.count);
    }
}
Q609 medium code output
What is the output of this code?
java
import java.util.LinkedList;

public class Test {
    public static void main(String[] args) {
        LinkedList<String> fruits = new LinkedList<>();
        fruits.add("Mango");
        fruits.add("Orange");
        fruits.addFirst("Apple");
        fruits.remove("Orange");
        fruits.add(1, "Grape"); 
        System.out.println(fruits);
    }
}
Q610 hard code error
What is the compile-time error in this Java code?
java
abstract class Outer {
    abstract class Inner {
        public abstract void process();
    }
    public void start() {
        System.out.println("Outer started.");
    }
}

public class Main {
    public static void main(String[] args) {
        Outer.Inner obj = new Outer().new Inner();
    }
}
Q611 hard code error
What compilation error will occur when compiling the following Java code?
java
interface MyPredicate<T> { boolean test(T t); }
interface MyFunction<T, R> { R apply(T t); }

class OverloadResolver {
    public static void process(MyPredicate<String> p) { System.out.println("Predicate"); }
    public static void process(MyFunction<String, Boolean> f) { System.out.println("Function"); }
}

public class Main {
    public static void main(String[] args) {
        OverloadResolver.process(s -> s.isEmpty());
    }
}
Q612 easy code error
What is the primary error that will occur when executing the following Java code?
java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

public class IteratorError {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>(Arrays.asList("A", "B"));
        Iterator<String> it = list.iterator();
        it.next(); // A
        it.next(); // B
        it.next(); // This line causes an error
    }
}
Q613 medium code error
What kind of error will occur when running this Java code?
java
public class ArrayError {
    public static void main(String[] args) {
        int[] data = null;
        System.out.println(data[0]);
    }
}
Q614 hard
Suppose a `HashSet<Person>` contains a `Person` object `p1`. If a *new* `Person` object `p2` is created such that `p1.equals(p2)` returns `true` (and `p1.hashCode() == p2.hashCode()`), what will be the state of the `HashSet` after `set.add(p2)` is called?
Q615 easy code output
What does this Java code print?
java
import java.util.function.Supplier;

public class Main {
    public static void main(String[] args) {
        Supplier<String> welcome = () -> "Welcome to Java!";
        System.out.println(welcome.get());
    }
}
Q616 hard
Consider `class Base { public void print(Object o) {} }` and `class Derived extends Base { public void print(String s) {} }`. If `Base b = new Derived();` is executed, and then `b.print("hello");` is called, which method will be invoked?
Q617 hard
What is the output of the following Java expression: `"Hello" + null + 10 * 20 + 30 / 3`?
Q618 hard code error
What compilation error will occur when compiling the following Java code?
java
import java.util.function.Function;

class MyParsers {
    public static int parse(String s) { return Integer.parseInt(s); }
    public static double parse(String s, int radix) { return Double.parseDouble(s); }
}

public class AmbiguousMethodReference {
    public static void main(String[] args) {
        Function<String, Integer> parser = MyParsers::parse;
    }
}
Q619 hard code output
What is the output of this code?
java
class Outer {
    private String outerName;
    Outer(String name) {
        this.outerName = name;
        System.out.println("Outer ctor: " + outerName);
    }
    class Inner {
        private String innerName;
        Inner(String name) {
            this.innerName = name;
            System.out.println("Inner ctor: " + innerName + " within " + outerName);
        }
    }
    public Inner createInner(String name) { return new Inner(name); }
}
public class Main {
    public static void main(String[] args) {
        Outer o1 = new Outer("O1");
        Outer o2 = new Outer("O2");
        Outer.Inner i1 = o1.new Inner("I1");
        Outer.Inner i2 = o2.createInner("I2");
    }
}
Q620 hard code output
What is the output of this code?
java
interface MyInterfaceA {
    default void show() {
        System.out.println("MyInterfaceA show");
    }
}

interface MyInterfaceB {
    default void show() {
        System.out.println("MyInterfaceB show");
    }
}

class MyClass implements MyInterfaceA, MyInterfaceB {
    // No explicit override of show()
}

public class Main {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        obj.show();
    }
}
← Prev 2930313233 Next → Page 31 of 200 · 3994 questions