☕ Java MCQ Questions – Page 89
Questions 1761–1780 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat 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 = "123456789";
try (BufferedReader br = new BufferedReader(new StringReader(data))) {
System.out.print((char) br.read());
br.mark(5);
System.out.print((char) br.read());
System.out.print((char) br.read());
br.reset();
System.out.print((char) br.read());
System.out.print((char) br.read());
} catch (IOException e) {
e.printStackTrace();
}
}
}
When using `BufferedWriter`, why is it important to call the `flush()` method or `close()` the writer?
What is the compilation error in the provided Java code?
java
class Base {
public int getId() {
return 1;
}
}
class Derived extends Base {
@Override
public Integer getId() { // ERROR: incompatible return type
return 2;
}
}
public class Main {
public static void main(String[] args) {
Derived d = new Derived();
System.out.println(d.getId());
}
}
What error occurs when running this Java code?
java
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class CustomAbstractListUOE {
public static void main(String[] args) {
List<String> myCustomList = new AbstractList<String>() {
private List<String> internal = new ArrayList<>(Arrays.asList("X", "Y", "Z"));
@Override
public String get(int index) { return internal.get(index); }
@Override
public int size() { return internal.size(); }
// remove(int index) is NOT overridden here, so AbstractList's default is used
};
Iterator<String> it = myCustomList.iterator();
it.next(); // "X"
it.remove(); // Expect UOE
}
}
What does this code print?
java
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
private static AtomicInteger counter = new AtomicInteger(0);
public static void main(String[] args) throws InterruptedException {
Runnable task = () -> {
for (int i = 0; i < 5000; i++) {
counter.incrementAndGet();
}
};
Thread t1 = new Thread(task);
Thread t2 = new Thread(task);
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(counter.get());
}
}
What is the output of this Java code snippet?
java
public class Main {
public static void main(String[] args) {
String prefix = "Value: ";
int number = 100;
System.out.println(prefix + number);
}
}
Considering Java 17, how does a `switch` *expression* handle a `null` selector expression if there is no explicit `case null` label?
What is the compile-time error in this Java code snippet?
java
public class ArrayTest {
public static void main(String[] args) {
int[] numbers = {10, 20, 30};
double index = 1.0;
System.out.println(numbers[index]);
}
}
What does this code print?
java
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
public class LambdaCaptureHard {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3);
AtomicInteger sum = new AtomicInteger(0);
int factor = 10;
Consumer<Integer> processor = n -> {
sum.getAndAdd(n * factor);
System.out.println("Processing " + n + ", current sum: " + sum.get());
};
numbers.forEach(processor);
System.out.println("Final sum: " + sum.get());
}
}
Which class must a custom checked exception in Java directly or indirectly extend?
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> colors = new HashSet<>();
String[] colorArray = {"Red", "Green", "Blue"};
colors.addAll(colorArray); // This line will cause an error
System.out.println(colors.size());
}
}
Which of the following data types is NOT directly supported in a Java `switch` statement's expression (before Java 7)?
What will happen when this Java code is compiled?
java
public class MyClass {
public static void main(String[] args) {
int count;
System.out.println(count);
}
}
What error occurs during compilation for the following code snippet?
java
class MyThread extends Thread {
public void run() {
System.out.println("Child thread running.");
}
}
public class Main {
public static void main(String[] args) {
MyThread t = new MyThread();
t.start();
t.join(); // Attempting to join without handling InterruptedException
}
}
If `file.isDirectory()` returns `true` for a `File` object named `file`, what can be definitively concluded about `file.isFile()`?
Identify the compilation error in the following Java code using the `var` keyword.
java
public class VarNullInit {
public static void main(String[] args) {
var myVar = null; // Illegal: 'var' cannot infer type from null literal
System.out.println(myVar);
}
}
What type of error will occur when compiling the following Java code?
java
public class Main {
public static void main(String[] args) {
double d = 100.0;
int i = d;
System.out.println(i);
}
}
What will be the content of the file 'output.txt' after this code executes?
java
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterTest {
public static void main(String[] args) {
FileWriter writer = null;
try {
writer = new FileWriter("output.txt");
writer.write("Final Content");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
What is the output of this code?
java
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<String> myQueue = new LinkedList<>();
myQueue.offer("First");
myQueue.poll();
System.out.println(myQueue.peek());
}
}
What is the compile-time error in this Java code?
java
public class LambdaReturnError {
public static void main(String[] args) {
Runnable myTask = () -> { // Line 3
return "Hello";
};
new Thread(myTask).start();
}
}