☕ Java MCQ Questions – Page 72
Questions 1421–1440 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat is the output of this code, demonstrating a 'this' reference escape during constructor execution?
java
import java.util.ArrayList; import java.util.List;
class EventLogger {
private List<String> logs = new ArrayList<>();
public void logEvent(String event) { logs.add(event); System.out.println("Logged: " + event); }
}
class Notifier {
private final String id;
public Notifier(EventLogger logger, String id) {
logger.logEvent("Notifier created (ID: " + this.id + ")");
this.id = id;
}
}
public class Main {
public static void main(String[] args) {
new Notifier(new EventLogger(), "N_001");
}
}
What is the output of this code?
java
import java.util.HashSet;
import java.util.Objects;
class MyKey {
int value;
public MyKey(int value) { this.value = value; }
@Override public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyKey myKey = (MyKey) o;
return value == myKey.value;
}
@Override public int hashCode() { return Objects.hash(value); }
}
public class Main {
public static void main(String[] args) {
HashSet<MyKey> set = new HashSet<>();
set.add(new MyKey(10));
System.out.println(set.contains(new MyKey(10)));
System.out.println(set.remove(new MyKey(10)));
System.out.println(set.size());
}
}
What is the ultimate superclass for all exception and error types in Java, including custom exceptions?
What error will occur when compiling or running this Java code?
java
public class ArrayError {
public static void main(String[] args) {
int[] numbers = new int[5];
numbers[0] = 3.14;
}
}
Which method is used to add an element to an `ArrayList`?
What is the primary reason for creating a custom exception in Java?
Can an abstract class be instantiated?
What is the error in this Java code?
java
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map<String, Integer> myMap = new HashMap<>();
myMap.put("score", null);
int finalScore = myMap.get("score");
System.out.println(finalScore);
}
}
What does this code print?
java
class Company {
static {
System.out.println("Static Block 1");
}
public Company() {
System.out.println("Constructor called");
}
static {
System.out.println("Static Block 2");
}
}
public class Main {
public static void main(String[] args) {
System.out.println("Main method started");
Company c1 = new Company();
Company c2 = new Company();
}
}
What does this Java code snippet print?
java
public class StringBufferTest {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("ABCDE");
sb.setCharAt(2, 'X');
sb.insert(1, sb.charAt(3));
System.out.print(sb);
}
}
What is the error in the following Java code snippet?
java
public class ArrayError {
public static void main(String[] args) {
int[] numbers;
numbers[0] = 10;
System.out.println(numbers[0]);
}
}
What error does this Java code produce when executed?
java
public class Main {
public static void main(String[] args) {
String country = "Canada";
System.out.println(country.substring(7));
}
}
What is the primary purpose of the `java.util.Iterator` interface in Java?
What does this code print?
java
public class User {
private String username;
public User(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public static void main(String[] args) {
User user = new User("alice");
System.out.println(user.getUsername());
}
}
When is a `ClassCastException` most likely to occur in the context of polymorphism?
What is the output of this code?
java
public class DataTypeTest {
public static void main(String[] args) {
byte b1 = 10;
byte b2 = 20;
int sum = b1 + b2;
System.out.println(sum);
}
}
What is the output or error of the following Java code?
java
public class ExceptionTest {
public static void main(String[] args) {
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Caught ArithmeticException");
} catch (Exception e) {
System.out.println("Caught general Exception");
}
}
}
What is the output of this Java code?
java
import java.util.PriorityQueue;
import java.util.Comparator;
class MutableInt {
int value;
MutableInt(int v) { this.value = v; }
void setValue(int v) { this.value = v; }
@Override public String toString() { return String.valueOf(value); }
}
public class QueueTest {
public static void main(String[] args) {
PriorityQueue<MutableInt> pq = new PriorityQueue<>(Comparator.comparingInt(mi -> mi.value));
MutableInt m1 = new MutableInt(5);
MutableInt m2 = new MutableInt(10);
MutableInt m3 = new MutableInt(2);
pq.offer(m1);
pq.offer(m2);
pq.offer(m3);
m3.setValue(15);
String result = "";
result += pq.poll().value + " ";
result += pq.poll().value + " ";
result += pq.poll().value;
System.out.println(result);
}
}
What is the output of this Java code?
java
class Product {
String name;
Product(String name) {
this.name = name;
System.out.println("Product: " + this.name);
}
}
public class Main {
public static void main(String[] args) {
Product p = new Product("Laptop");
}
}
What is the output of this code?
java
class Counter {
int count = 0;
static int totalCount = 0;
public Counter() {
count++;
totalCount++;
}
}
public class Main {
public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
c1.count++;
Counter.totalCount++;
System.out.println("c1.count: " + c1.count);
System.out.println("c2.count: " + c2.count);
System.out.println("totalCount: " + Counter.totalCount);
}
}