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[]);
}
}
✅ Correct Answer: A) true
true
true
false
Primitive arrays (e.g., `int[]`) are `Object`s but are not subclasses of `Object[]`. Therefore, `obj1 instanceof Object[]` is false. Object arrays (e.g., `Integer[]`, `String[]`) are `Object`s and also can be cast to `Object[]` if their component type is a subclass of `Object`, making `obj2 instanceof Integer[]` and `obj3 instanceof Object[]` true.
Q602hardcode 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
}
}
✅ Correct Answer: B) error: reference to process is ambiguous
When `null` is passed, the compiler looks for the most specific method. Both `List` and `Set` are interfaces, and neither is a sub-interface or super-interface of the other. Therefore, both `process(List l)` and `process(Set s)` are equally applicable for `null`, leading to an ambiguous method call error.
Q603easycode 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();
}
}
✅ Correct Answer: A) Compile-time error: cannot inherit from final Base
A `final` class cannot be extended by any other class in Java. Attempting to extend `Base` will result in a compile-time error.
Q604easy
Which method should be used to compare the content of two String objects for equality in Java?
✅ Correct Answer: C) The `equals()` method
The `equals()` method compares the actual sequence of characters (content) of two String objects, unlike `==` which compares their references.
Q605easy
If an exception occurs within a `try` block and there is a matching `catch` block, what is the typical order of execution?
✅ Correct Answer: C) try -> catch -> finally
When an exception occurs in the `try` block, execution immediately transfers to the matching `catch` block, and then the `finally` block is executed.
Q606easy
In which Java package is the `Iterator` interface located?
✅ Correct Answer: B) `java.util`
The `Iterator` interface, along with most other collection-related interfaces and classes, is part of the `java.util` package.
Q607medium
What is the primary purpose of the `java.io.FileReader` class?
✅ Correct Answer: B) To read characters from a file using the platform's default character encoding.
`FileReader` is a convenience class for reading character files. It automatically handles the conversion from bytes to characters using the default character encoding of the platform.
Q608easycode 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);
}
}
✅ Correct Answer: A) Count: 2
An instance of `Counter` is created, initializing `count` to 0. The `increment()` method is called twice, each time increasing `count` by 1. Therefore, the final value of `count` will be 2.
Q609mediumcode 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);
}
}
✅ Correct Answer: B) [Apple, Grape, Mango]
Initial list: []. After `add("Mango")`: [Mango]. After `add("Orange")`: [Mango, Orange]. After `addFirst("Apple")`: [Apple, Mango, Orange]. After `remove("Orange")`: [Apple, Mango]. After `add(1, "Grape")`: [Apple, Grape, Mango]. The final output is `[Apple, Grape, Mango]`.
Q610hardcode 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();
}
}
✅ Correct Answer: A) error: Outer.Inner is abstract; cannot be instantiated
Just like top-level abstract classes, an abstract inner class cannot be instantiated directly. To create an instance, you would need to define a concrete subclass (either named or anonymous) that implements its abstract methods.
Q611hardcode 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());
}
}
✅ Correct Answer: A) Error: ambiguous method call: both process(MyPredicate<String>) and process(MyFunction<String,Boolean>) match
The lambda `s -> s.isEmpty()` can be converted to both `MyPredicate<String>` (takes `String`, returns `boolean`) and `MyFunction<String, Boolean>` (takes `String`, returns `Boolean`). The compiler cannot determine which overloaded `process` method to invoke, leading to an ambiguous method call error.
Q612easycode 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
}
}
✅ Correct Answer: A) java.util.NoSuchElementException
Calling `next()` on an Iterator when there are no more elements available (i.e., `hasNext()` would return false) results in a `NoSuchElementException`.
Q613mediumcode 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]);
}
}
✅ Correct Answer: C) Runtime error: NullPointerException
The array reference `data` is explicitly set to `null`. Attempting to access an element of a `null` array (e.g., `data[0]`) will always result in a `NullPointerException` at runtime.
Q614hard
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?
✅ Correct Answer: C) The `HashSet` will still contain only `p1`, and `add(p2)` will return `false`.
`HashSet.add()` uses the `hashCode()` and `equals()` methods to check for existing elements. If an element `p2` is `equals()` to an existing element `p1` already in the set, `add(p2)` will return `false` and the set's contents will remain unchanged.
Q615easycode 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());
}
}
✅ Correct Answer: A) Welcome to Java!
The Supplier `welcome` provides the string 'Welcome to Java!'. Calling `get()` on it returns this string, which is then printed.
Q616hard
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?
✅ Correct Answer: B) `Base.print(Object o)` due to static method resolution based on the reference type and lack of overriding.
`Derived.print(String s)` overloads, but does not override, `Base.print(Object o)` because their parameter types differ. Method overriding requires the same method signature. The compiler resolves the call `b.print("hello")` to `Base.print(Object o)` based on the reference type `Base` and parameter matching.
Q617hard
What is the output of the following Java expression: `"Hello" + null + 10 * 20 + 30 / 3`?
✅ Correct Answer: A) Hellonull20010
Arithmetic operations have higher precedence, so `10 * 20` is `200` and `30 / 3` is `10`. String concatenation proceeds left-to-right, converting `null` to the string "null" before concatenating.
Q618hardcode 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;
}
}
✅ Correct Answer: A) Error: ambiguous method reference: both parse(String) in MyParsers and parse(String,int) in MyParsers match
The `MyParsers` class has two overloaded `parse` methods. When using a method reference `MyParsers::parse` for `Function<String, Integer>`, the compiler cannot unambiguously determine which `parse` method signature (taking one `String` or two arguments) is intended, as both could conceptually match after boxing/unboxing, leading to an ambiguous method reference error.
Q619hardcode 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");
}
}
✅ Correct Answer: A) Outer ctor: O1
Outer ctor: O2
Inner ctor: I1 within O1
Inner ctor: I2 within O2
Non-static inner class instances are implicitly tied to an instance of their outer class. `o1.new Inner("I1")` creates an inner object tied to `o1`, and `o2.createInner("I2")` creates one tied to `o2`, correctly reflecting their respective outerName fields.
Q620hardcode 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();
}
}
✅ Correct Answer: C) Compile-time error
When a class implements multiple interfaces that provide conflicting default methods (methods with the same signature), the compiler requires the implementing class to explicitly override the method to resolve the ambiguity. This leads to a compile-time error.