☕ Java MCQ Questions – Page 36
Questions 701–720 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat is the typical growth factor for an `ArrayList`'s internal array when it needs to expand its capacity in Java?
What error will occur when compiling the following Java code?
java
import java.util.HashSet;
import java.util.Set;
public class MyClass {
public static void main(String[] args) {
Set<String> s = new HashSet<Integer>(); // This line will cause an error
s.add("test");
System.out.println(s.size());
}
}
What error will this Java code produce when compiled and run?
java
public class NegativeSizeArray {
public static void main(String[] args) {
int x = 10;
byte y = 20;
// Calculation results in a negative value for array size
int size = (byte)(x - y);
int[] arr = new int[size];
System.out.println("Array created successfully.");
}
}
What is the expected error when compiling and running the following Java code?
java
import java.util.List;
import java.util.LinkedList;
public class MyClass {
public static void main(String[] args) {
List<String> list = new LinkedList<>();
list.add("Item");
list.removeFirst(); // Method specific to LinkedList, not List interface
}
}
Which type of variable can be accessed directly from both instance methods and static methods, but requires an object reference when accessed from a static context?
What kind of error will occur when executing this Java code?
java
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
TreeSet<Integer> set = new TreeSet<>();
set.add(10);
set.add(20);
set.add(null);
System.out.println(set);
}
}
What happens when a thread calls `Thread.sleep(long millis)`?
What does this code print?
java
public class StringTest {
public static void main(String[] args) {
String s1 = "Hello";
String s2 = "World";
System.out.println(s1 + " " + s2);
}
}
What does this code print?
java
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;
public class BufferedReaderTest {
public static void main(String[] args) {
String data = "First\nSecond";
try (BufferedReader br = new BufferedReader(new StringReader(data))) {
System.out.println(br.ready());
br.readLine();
System.out.println(br.ready());
br.readLine();
System.out.println(br.ready());
} catch (IOException e) {
e.printStackTrace();
}
}
}
What is the output of this code?
java
class MyTask implements Runnable {
// This is not a valid override for Runnable.run() because of reduced visibility
void run() {
System.out.println("Task running.");
}
}
public class Main {
public static void main(String[] args) {
MyTask task = new MyTask();
Thread t = new Thread(task);
t.start();
}
}
Given the following Java class:
java
class Overload {
void method(long l) { System.out.println("long"); }
void method(Integer i) { System.out.println("Integer"); }
}
What will be printed when the following code is executed?
`new Overload().method(5);`
Which exception is thrown when the `main` method of this Java code is executed?
java
public class StringCompareToNull {
public static void main(String[] args) {
String s1 = "alpha";
String s2 = null;
try {
int result = s1.compareTo(s2);
System.out.println(result);
} catch (Exception e) {
System.out.println(e.getClass().getSimpleName());
}
}
}
What is the output of this Java code snippet?
java
interface A { default void foo() { System.out.println("Interface A"); } }
interface B { default void foo() { System.out.println("Interface B"); } }
class C implements A, B {
@Override public void foo() { System.out.println("Class C"); }
}
class D implements A, B {
@Override public void foo() { A.super.foo(); }
}
public class Main {
public static void main(String[] args) {
new C().foo();
new D().foo();
}
}
Which of the following is NOT a primitive type that can be directly type-cast in Java?
What is the output of the following Java program?
java
public class LoopTest {
public static void main(String[] args) {
int i = 1;
do {
System.out.println("Iteration: " + i);
i++;
} while (i < 3);
}
}
What is the result of attempting to run the following Java code?
java
public class Main {
public static void main(String[] args) {
Thread daemonThread = new Thread(() -> {
try {
System.out.println("Daemon thread running...");
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
daemonThread.start();
daemonThread.setDaemon(true); // This line
System.out.println("Main thread exiting.");
}
}
What is the output of this code?
java
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> inventory = new HashMap<>();
inventory.put("Apple", 10);
inventory.put("Banana", 5);
inventory.clear();
System.out.println(inventory.size());
}
}
What is the output of this Java code?
java
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.nio.file.Files;
import java.nio.file.Path;
public class FileWriterTruncation {
public static void main(String[] args) {
String filename = "test_truncation.txt";
try {
try (FileWriter fw = new FileWriter(filename)) { fw.write("Line A\nLine B\n"); }
try (FileWriter fw = new FileWriter(filename)) { fw.write("Line C"); }
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String line; while ((line = br.readLine()) != null) { System.out.println(line); }
}
} catch (IOException e) { System.out.println("Error: " + e.getMessage());
} finally { try { Files.deleteIfExists(Path.of(filename)); } catch (IOException ignored) {} }
}
}
What exception is thrown if you call `next()` on an `Iterator` when there are no more elements?
What is the output of this Java code?
java
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<String> items = new HashSet<>();
items.add("ItemA");
items.add("ItemB");
items.clear();
System.out.println(items.isEmpty());
}
}