☕ Java MCQ Questions – Page 175
Questions 3481–3500 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat is the output of this code?
java
public class Test {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
int result = 0;
for (int i = 0; i < 3; i++) {
result += numbers[i];
}
System.out.println(result);
}
}
What compilation error will this Java code produce?
java
public class SwitchError {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
default:
System.out.println("Weekend");
break;
default:
System.out.println("Another default");
break;
}
}
}
What is the output of this code?
java
import java.util.LinkedList;
public class Test {
static class MutableInt {
int value;
MutableInt(int v) { this.value = v; }
public String toString() { return String.valueOf(value); }
}
public static void main(String[] args) {
LinkedList<MutableInt> original = new LinkedList<>();
original.add(new MutableInt(1));
original.add(new MutableInt(2));
LinkedList<MutableInt> clone = (LinkedList<MutableInt>) original.clone();
clone.get(0).value = 10; // Modifies content of shared MutableInt object
clone.add(new MutableInt(30)); // Adds a new object to clone
System.out.println("Original: " + original);
System.out.println("Clone: " + clone);
}
}
What is the output of this Java code?
java
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
public class QueueTest {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<Integer> sq = new SynchronousQueue<>();
new Thread(() -> {
try {
System.out.println("Producer trying to put 1");
sq.put(1);
System.out.println("Producer put 1");
} catch (InterruptedException e) { Thread.currentThread().interrupt(); }
}).start();
Thread.sleep(100);
new Thread(() -> {
try {
System.out.println("Consumer trying to take");
Integer val = sq.take();
System.out.println("Consumer took " + val);
} catch (InterruptedException e) { Thread.currentThread().interrupt(); }
}).start();
Thread.sleep(500);
}
}
Can two methods be overloaded if they only differ by their return type?
What is wrong with this Java code?
java
public class Test {
public static void main(String[] args) {
Object obj = () -> System.out.println("Hello from lambda");
}
}
What is the output of this code?
java
import java.util.LinkedList;
public class Test {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("apple");
list.add("banana");
list.add("apple");
list.add("cherry");
System.out.println(list.indexOf("apple") + " " + list.lastIndexOf("orange"));
}
}
What is the output of this code?
java
class A {
A() { System.out.print("A"); }
A(int x) { System.out.print("A" + x); }
}
class B extends A {
B() { this(10); System.out.print("B"); }
B(int x) { super(x); System.out.print("B" + x); }
}
public class Main {
public static void main(String[] args) {
new B();
}
}
Which exception is thrown when the `main` method of this Java code is executed?
java
public class CharArrayToStringNull {
public static void main(String[] args) {
char[] chars = null;
try {
String s = new String(chars);
System.out.println(s);
} catch (Exception e) {
System.out.println(e.getClass().getSimpleName());
}
}
}
Consider a `try-with-resources` block that attempts to close two custom resources, `ResourceA` and `ResourceB`, each of which might throw a distinct custom exception (`ResourceAException` and `ResourceBException` respectively) during their `close()` method. If both `ResourceA.close()` and the main `try` block body throw exceptions, what happens to `ResourceBException` (if thrown by `ResourceB.close()`)?
What does this Java code print?
java
public class WhileLoopDemo {
public static void main(String[] args) {
int i = 0;
while (i < 5) {
if (i == 2) {
break;
}
System.out.print(i);
i++;
}
}
}
What compile-time error will occur when the `Derived` class tries to override the `display()` method?
java
class Base {
public final void display() {
System.out.println("Base display method");
}
}
class Derived extends Base {
@Override
public void display() {
System.out.println("Derived display method");
}
}
public class Main {
public static void main(String[] args) {
new Derived().display();
}
}
Which exception is thrown when the `main` method of this Java code is executed?
java
public class StringRepeatNegativeCount {
public static void main(String[] args) {
String s = "abc";
try {
// String.repeat() is available from Java 11 onwards
String repeated = s.repeat(-1);
System.out.println(repeated);
} catch (Exception e) {
System.out.println(e.getClass().getSimpleName());
}
}
}
After executing `Arrays.sort(myArray);` on an array of primitive integers, what is the guaranteed state of `myArray`?
Which Java primitive data type is used to store a single 16-bit Unicode character?
What does this code print?
java
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<Character> letters = new LinkedList<>();
letters.offer('A');
letters.offer('B');
System.out.println(letters.poll());
letters.add('C');
System.out.println(letters.element());
}
}
Consider an object used as a key in a `HashMap`. If the state of this key object changes *after* it has been inserted into the map, such that its `hashCode()` value is altered, what is the most likely outcome when attempting to retrieve the associated value using the *modified* key?
What is the average time complexity for accessing an element by its index using `get(int index)` in `java.util.LinkedList`?
What is the output of this code?
java
import java.util.function.Predicate;
public class LambdaTest {
public static void printIf(int num, Predicate<Integer> condition) {
if (condition.test(num)) {
System.out.println(num);
}
}
public static void main(String[] args) {
printIf(7, n -> n > 5);
}
}
After calling the `start()` method on a new thread, what state does it enter?