☕ Java MCQ Questions – Page 161

Questions 3201–3220 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3201 easy code error
What error will occur when compiling the following Java code?
java
public class Main {
    public static void main(String[] args) {
        int x = 5;
        if (x = 10) {
            System.out.println("X is 10");
        } else {
            System.out.println("X is not 10");
        }
    }
}
Q3202 easy
Which type of polymorphism is achieved through method overloading in Java?
Q3203 hard code error
What is the result of compiling and running the following Java code snippet?
java
public class StringError {
    public static void main(String[] args) {
        String greeting = "Hi";
        System.out.println(greeting.substring(3));
    }
}
Q3204 medium code output
What will the following Java code print to the console?
java
import java.util.HashSet;
import java.util.Set;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        Set<String> set1 = new HashSet<>(Arrays.asList("A", "B", "C"));
        Set<String> set2 = new HashSet<>(Arrays.asList("B", "D", "E"));
        set1.addAll(set2);
        System.out.println(set1.size());
    }
}
Q3205 easy
Which interface does `java.util.ArrayList` primarily implement?
Q3206 medium code output
What is the output of this code?
java
class Building {
    {
        System.out.println("Instance Initializer Block 1");
    }
    public Building() {
        System.out.println("Constructor called");
    }
    {
        System.out.println("Instance Initializer Block 2");
    }
}

public class Main {
    public static void main(String[] args) {
        Building b = new Building();
    }
}
Q3207 hard
Consider a `Parent` class with a method `doWork()` and a `Child` class extending `Parent` that overrides `doWork()`. If `Parent p = new Child();` and then `((Child) p).doWork();` is called, which `doWork()` method is executed?
Q3208 medium code error
What error will this Java code produce when executed?
java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>(Arrays.asList("One", "Two", "Three"));
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            String current = it.next();
            if (current.equals("Two")) {
                list.add("Four"); // Direct modification of the list during iteration
            }
        }
    }
}
Q3209 easy code error
What is the error in the following Java code?
java
public class Main {
    public static void main(String[] args) {
        Integer num = 50;
        String str = (String) num;
        System.out.println(str);
    }
}
Q3210 easy code error
What is the compile-time error in this Java code snippet?
java
public class ArrayTest {
    public static void main(String[] args) {
        int[][] matrix = new int[][3];
    }
}
Q3211 easy
What is the primary purpose of the `Iterator` interface in Java?
Q3212 hard code error
What error occurs when running this Java code?
java
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class ListIteratorSetAfterAdd {
    public static void main(String[] args) {
        List<String> list = new LinkedList<>(Arrays.asList("A", "B"));
        ListIterator<String> listIterator = list.listIterator();
        listIterator.next(); // Moves past A
        listIterator.add("X"); // Adds X after A
        listIterator.set("Y"); // Attempts to set the element
        System.out.println(list);
    }
}
Q3213 easy
In method overloading, what defines the method signature?
Q3214 hard code error
What is the compilation error, if any, for the provided Java code snippet?
java
class Overloader {
    public int calculate(String s) { return 1; }
    public String calculate(String s) { return "1"; } // Line X

    public static void main(String[] args) {
        Overloader o = new Overloader();
    }
}
Q3215 hard
Which statement accurately describes the memory layout of a `int[][]` array in Java?
Q3216 easy code error
What compile-time error will occur in the `main` method?
java
class SpecificException extends Exception {
    public SpecificException(String message) { super(message); }
}

public class Main {
    public static void operation() throws SpecificException {
        throw new SpecificException("Detail issue");
    }

    public static void main(String[] args) {
        try {
            operation();
        } catch (Exception e) { 
            System.out.println("Caught generic: " + e.getMessage());
        } catch (SpecificException e) { 
            System.out.println("Caught specific: " + e.getMessage());
        }
    }
}
Q3217 hard
What happens if you iterate over a `java.util.List` declared as a raw type (e.g., `List rawList = new ArrayList<String>();`) using an enhanced `for` loop, attempting to assign elements to a specific type (e.g., `for (String s : rawList)`)?
Q3218 medium code error
What compile-time error will occur in this Java code snippet?
java
public class LoopError {
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++); {
            System.out.println(i);
        }
    }
}
Q3219 medium
If an exception is thrown in the `try` block, handled by a `catch` block, and then a *new* exception is thrown within the `finally` block, which exception will be propagated out of the method?
Q3220 medium
When using a method reference, what is the primary factor the Java compiler uses to infer which specific method is being referenced, especially in cases of method overloading?
← Prev 159160161162163 Next → Page 161 of 200 · 3994 questions