☕ Java MCQ Questions – Page 145
Questions 2881–2900 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat is the output of this code?
java
import java.util.ArrayList;
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add(null);
list.add("B");
list.add(null);
list.add("C");
ArrayList<String> retainElements = new ArrayList<>();
retainElements.add("A");
retainElements.add(null);
boolean changed = list.retainAll(retainElements);
System.out.println(list + ":" + changed);
}
}
Given `String text = "JavaProgramming";` what is the correct way to get the substring `"Programming"`?
What error will occur when this code is executed?
java
public class Main {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("HelloJava");
sb.replace(5, 2, "World");
System.out.println(sb);
}
}
Which keyword is used to implement inheritance in Java?
What type of error will occur when running this Java code?
java
public class Syncher {
private Object syncMonitor = null; // Deliberately null
public void doSynchronizedWork() {
synchronized (syncMonitor) {
System.out.println("Work done.");
}
}
public static void main(String[] args) {
Syncher s = new Syncher();
s.doSynchronizedWork();
}
}
What is the compilation error in this code?
java
class Box {
int value = 10;
}
public class Main {
public static void main(String[] args) {
Box myBox = new Box();
int x = myBox;
System.out.println(x);
}
}
What is the output of this Java code?
java
import java.util.PriorityQueue;
import java.util.Comparator;
class Item {
int id;
String name;
Item(int id, String name) { this.id = id; this.name = name; }
@Override public String toString() { return name + ":" + id; }
}
public class QueueTest {
public static void main(String[] args) {
PriorityQueue<Item> pq = new PriorityQueue<>(Comparator.comparingInt(item -> item.id));
pq.offer(new Item(2, "Banana"));
pq.offer(new Item(1, "Apple"));
pq.offer(new Item(3, "Cherry"));
pq.offer(new Item(1, "Date"));
StringBuilder sb = new StringBuilder();
while (!pq.isEmpty()) {
sb.append(pq.poll().toString()).append(" ");
}
System.out.println(sb.toString().trim());
}
}
Given a 2D array `int[][] matrix = new int[5][8];`, what do `matrix.length` and `matrix[0].length` represent, respectively?
Which compilation error will be thrown by the following code?
java
interface ParentProcessor {
void process();
}
@FunctionalInterface
interface ChildProcessor extends ParentProcessor {
void childProcess();
}
public class Main {
public static void main(String[] args) {
// Attempt to use ChildProcessor
}
}
What is the most significant difference in program flow control between using a `break` statement and a `return` statement inside a `while` loop when a specific condition is met?
What is the main purpose of the `newLine()` method in `BufferedWriter`?
What is the primary purpose of the `finally` block in Java's exception handling mechanism?
What is the output of this Java code snippet involving nested switch statements?
java
public class SwitchTest {
public static void main(String[] args) {
int x = 1;
char y = 'B';
String result = "";
switch (x) {
case 1:
switch (y) {
case 'A': result = "1A"; break;
case 'B': result = "1B"; break;
default: result = "1X";
}
break;
case 2:
result = "2Y";
break;
default:
result = "ZZ";
}
System.out.println(result);
}
}
What error will this Java code produce when executed?
java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> items = new ArrayList<>();
items.add("Item1");
Iterator<String> it = items.iterator();
it.remove(); // Calling remove() before next()
}
}
What does this Java code print?
java
public class Main {
public static void main(String[] args) {
int a = 20;
int b = 5;
if (a > 10) {
if (b > 10) {
System.out.println("Inner true");
} else {
System.out.println("Inner false");
}
} else {
System.out.println("Outer false");
}
}
}
What is the output of this code?
java
class Base {
protected Base() { System.out.print("Base ctor "); }
protected Base(int x) { System.out.print("Base ctor " + x + " "); }
}
class Derived extends Base {
public Derived() {
super();
System.out.print("Derived ctor ");
}
public Derived(String s) {
super(s.length());
System.out.print("Derived ctor " + s + " ");
}
}
public class Main {
public static void main(String[] args) {
new Derived("Hello");
new Derived();
}
}
What kind of error will occur when executing this Java code?
java
import java.util.TreeSet;
import java.util.SortedSet;
public class Main {
public static void main(String[] args) {
TreeSet<Integer> originalSet = new TreeSet<>();
originalSet.add(10);
originalSet.add(20);
originalSet.add(30);
originalSet.add(40);
SortedSet<Integer> subSet = originalSet.subSet(15, true, 35, false);
subSet.add(5);
System.out.println(subSet.size());
}
}
You insert two distinct objects, `obj1` and `obj2`, into a `TreeMap` as keys. `obj1.equals(obj2)` returns `true`, but `obj1.compareTo(obj2)` returns a non-zero value. What will be the state of the `TreeMap`?
What compile-time error will occur in the `MyClass()` constructor?
java
import java.io.IOException;
class MyClass {
MyClass() {
throw new IOException("Error!");
}
}
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
}
}
Where can the `default` case be placed within a Java `switch` statement or expression?