☕ Java MCQ Questions – Page 5
Questions 81–100 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat error will occur when running the following Java code?
java
public class StaticWaitError {
public static void staticWaitMethod() throws InterruptedException {
// Attempting to wait() on the class object without holding its monitor
StaticWaitError.class.wait();
}
public static void main(String[] args) {
try {
staticWaitMethod();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
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(30);
set.remove("20");
System.out.println(set.size());
}
}
What error will prevent the compilation of this Java code snippet?
java
public class DataTypeCharError {
public static void main(String[] args) {
char letter = 'AB';
System.out.println(letter);
}
}
What compilation error will occur in the `AccessController` class?
java
public class RestrictedAccessException extends Exception {
private RestrictedAccessException(String message) {
super(message);
}
}
public class AccessController {
public void checkAccess(String user) throws RestrictedAccessException {
if (!user.equals("admin")) {
throw new RestrictedAccessException("Access denied for user: " + user);
}
System.out.println("Access granted for " + user);
}
public static void main(String[] args) {
// Won't compile
}
}
What error occurs when executing the following Java code?
java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Character> charList = new ArrayList<>();
charList.add('a');
charList.remove(0);
charList.remove(0); // Attempt to remove from an empty list
System.out.println(charList);
}
}
What kind of error will occur when executing the following Java code?
java
public class DaemonThreadConfig {
public static void main(String[] args) {
Thread t = new Thread(() -> {
System.out.println("Child thread running. Is Daemon: " + Thread.currentThread().isDaemon());
});
t.start();
t.setDaemon(true); // Attempt to set daemon status after starting
}
}
What error does this Java code produce when executed?
java
public class Main {
public static void main(String[] args) {
String valueStr = "hello";
Integer numberObj = Integer.valueOf(valueStr);
System.out.println(numberObj);
}
}
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 void readFile() {
BufferedReader br = new BufferedReader(new StringReader("data"));
String line = br.readLine();
}
public static void main(String[] args) {
// Code here, if any, is not relevant to the error in readFile()
}
}
Consider an array `int[] data = new int[3];`. Which of the following statements would cause an `ArrayIndexOutOfBoundsException`?
What is the output of this Java code snippet?
java
import java.io.File;
import java.io.IOException;
public class FileTest {
public static void main(String[] args) {
File file = new File("temp_file_q5.txt");
try {
boolean created = file.createNewFile();
System.out.println(created + " " + file.exists());
} catch (IOException e) {
System.out.println("Error");
} finally {
file.delete(); // Clean up
}
}
}
When a `java.io.File` object is instantiated using a path string, what is the initial state regarding the actual file or directory on the file system?
Given two overloaded methods: `void process(Function<Integer, Integer> f)` and `void process(Predicate<String> p)`. If you call `process(s -> s.length() > 5);`, what will be the compile-time outcome?
On a Unix-like system, what would be the result of calling `new File("/dev/null").isDirectory()` and `new File("/dev/null").isFile()`?
Examine the Java code below. What is the expected outcome upon compilation?
java
public class Test {
public static void main(String[] args) {
myIfStatement: if (true) {
System.out.println("Inside the if block.");
break myIfStatement;
}
System.out.println("After the if block.");
}
}
Which method is used to retrieve the character at a specific index within a String?
What does this code print?
java
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
public class LambdaCapture {
public static void main(String[] args) {
AtomicInteger counter = new AtomicInteger(10);
Supplier<Integer> supplier = () -> {
counter.incrementAndGet();
return counter.get();
};
counter.set(5);
System.out.println(supplier.get());
}
}
What value will be printed by this Java code?
java
public class LoopTest {
public static void main(String[] args) {
int k = 0;
do {
k++;
} while (k < 3);
System.out.print(k);
}
}
What is the output or error when compiling this Java program?
java
class Product {
private String name = "Laptop";
// No public getter method for 'name'
}
public class Shop {
public static void main(String[] args) {
Product item = new Product();
String productName = item.getName();
System.out.println(productName);
}
}
What is the output of this code?
java
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class ReadWriteLockTest {
private final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
private String data = "initial";
public String read() {
rwLock.readLock().lock();
try { Thread.sleep(10); return data; }
catch (InterruptedException e) { Thread.currentThread().interrupt(); return null; }
finally { rwLock.readLock().unlock(); }
}
public void write(String newData) {
rwLock.writeLock().lock();
try { Thread.sleep(50); this.data = newData; }
catch (InterruptedException e) { Thread.currentThread().interrupt(); }
finally { rwLock.writeLock().unlock(); }
}
public static void main(String[] args) throws InterruptedException {
ReadWriteLockTest test = new ReadWriteLockTest();
Thread r1 = new Thread(() -> System.out.println("R1 got: " + test.read()), "Reader-1");
Thread r2 = new Thread(() -> System.out.println("R2 got: " + test.read()), "Reader-2");
Thread w1 = new Thread(() -> test.write("updated"), "Writer-1");
r1.start(); r2.start();
Thread.sleep(5); // Give readers a slight head start
w1.start();
r1.join(); r2.join(); w1.join();
System.out.println("Final data: " + test.data);
}
}
What does this Java code print?
java
import java.util.TreeSet;
import java.util.Set;
public class Test {
public static void main(String[] args) {
TreeSet<Integer> set = new TreeSet<>();
set.add(10); set.add(20); set.add(30); set.add(40); set.add(50);
Set<Integer> head = set.headSet(30);
Set<Integer> tail = set.tailSet(40, false);
System.out.println(head + " " + tail);
}
}