☕ Java MCQ Questions – Page 147
Questions 2921–2940 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat does this code print?
java
public class Test {
public static void main(String[] args) {
String[] names = new String[3];
names[0] = "Alice";
names[1] = "Bob";
System.out.println(names[2]);
}
}
What is the outcome when this Java code is executed?
java
import java.util.LinkedList;
public class Test {
public static void main(String[] args) {
LinkedList<String> names = new LinkedList<>();
names.add("Alice");
names.add("Bob");
names.trimToSize();
System.out.println(names.size());
}
}
What is the primary difference between method overloading and method overriding in Java?
What is the expected error when compiling and running the following Java code?
java
import java.util.LinkedList;
public class MyClass {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
String element = list.peek(); // Returns null if list is empty
System.out.println(element.length());
}
}
What is the compilation error in the following Java code snippet?
java
import java.util.ArrayList;
public class Question1 {
public static void main(String[] args) {
ArrayList<int> numbers = new ArrayList<>();
numbers.add(10);
System.out.println(numbers);
}
}
What is the compile-time error in the provided Java code?
java
class A {
A(int x) {}
}
class B extends A {
B() {
System.out.println("Hello from B constructor");
super(5);
}
}
What is the output of this code when run on a Windows operating system?
java
import java.io.File;
import java.io.IOException;
public class InvalidFileNameCanonical {
public static void main(String[] args) {
// On Windows, characters like ':', '<', '>', '"', '|', '?', '*' are invalid in filenames.
// Using ':' within a filename component usually leads to an IOException.
File invalidFile = new File("C:\\temp\\invalid:filename.txt");
try {
System.out.println("Canonical path: " + invalidFile.getCanonicalPath());
} catch (IOException e) {
System.out.println("Error: " + e.getClass().getSimpleName() + ": " + e.getMessage());
}
}
}
Can two methods with the same name and parameter list but different return types be overloaded in Java?
What is the output of this Java code?
java
import java.util.LinkedList;
public class MyClass {
public static void main(String[] args) {
LinkedList<Boolean> flags = new LinkedList<>();
flags.add(true);
flags.add(false);
flags.clear();
System.out.println(flags.isEmpty());
}
}
What kind of error will occur when executing the following Java code?
java
public class MyThread implements Runnable {
@Override
public void run() {
System.out.println("Thread is running...");
}
public static void main(String[] args) {
MyThread myRunnable = new MyThread();
Thread t = new Thread(myRunnable);
t.start();
t.start(); // Attempt to start the same thread again
}
}
What compilation error will occur when compiling this Java code?
java
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;
public class MyClass {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new StringReader("A"));
char c = br.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Which statement about variable capturing by Java lambda expressions is FALSE?
What kind of error will occur when executing this Java code?
java
import java.util.TreeSet;
class Item implements Comparable<Item> {
String name;
public Item(String name) { this.name = name; }
@Override
public int compareTo(Item other) {
return this.name.compareTo(other.name);
}
}
public class Main {
public static void main(String[] args) {
TreeSet<Item> items = new TreeSet<>();
items.add(new Item("Apple"));
items.add(new Item(null));
System.out.println(items.size());
}
}
What is the output of this Java program?
java
public class TypeCast {
public static void main(String[] args) {
byte b = 75;
short s = b;
System.out.println(s);
}
}
What kind of error will occur when executing the following Java code?
java
public class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("Runnable object trying to wait on itself.");
try {
// 'this' refers to the MyRunnable instance, not the Thread
this.wait();
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
}
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
new Thread(myRunnable).start();
}
}
What error will be thrown when the `main` method of the following code is executed?
java
import java.util.Date;
class MutableData {
private Date value;
public MutableData(Date value) { this.value = value; }
public Date getValue() { return value; }
public void setValue(Date value) { this.value = value; }
// No implements Cloneable
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone(); // Calling Object's clone without implementing Cloneable
}
}
public class CloneErrorExample {
public static void main(String[] args) {
MutableData data = new MutableData(new Date());
try {
MutableData clonedData = (MutableData) data.clone();
} catch (CloneNotSupportedException e) {
System.out.println("Caught: " + e.getMessage());
}
}
}
What is the compilation error in the following Java code snippet?
java
public class LoopError {
public static void main(String[] args) {
int count;
do {
count = 1;
System.out.println(count);
} while (count < 3);
}
}
What is a possible output of this code?
java
public class Main {
public static void main(String[] args) {
Runnable task = () -> {
try {
Thread.sleep(50); // Simulate some work
System.out.println("Task completed after delay.");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
};
new Thread(task).start();
System.out.println("Main thread continues immediately.");
}
}
What is the output of this Java code snippet?
java
public class Test {
public static void main(String[] args) {
char[] letters = {'a', 'b', 'c'};
System.out.println(letters.length);
}
}
What does this code print?
java
import java.util.HashSet;
public class Test {
public static void main(String[] args) {
HashSet<String> fruits = new HashSet<>();
fruits.add("Apple");
fruits.add("Banana");
System.out.println(fruits.contains("Apple"));
System.out.println(fruits.contains("Orange"));
}
}