☕ Java MCQ Questions – Page 62
Questions 1221–1240 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat does this code print to the console?
java
import java.util.Arrays;
import java.util.List;
public class StreamTest {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 2, 4, 3, 5, 1);
long count = numbers.stream()
.distinct()
.filter(n -> n % 2 == 1)
.count();
System.out.println(count);
}
}
What is the output of this code?
java
abstract class Processor {
abstract void processData();
void setup() {
System.out.println("Processor setup.");
}
}
class SimpleProcessor extends Processor {
// Missing implementation of processData()
}
public class Main {
public static void main(String[] args) {
SimpleProcessor sp = new SimpleProcessor();
sp.setup();
}
}
What is the output of this code?
java
import java.util.ArrayList;
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("X");
list.add("Y");
list.add("Z");
String[] smallArray = new String[2];
String[] result1 = list.toArray(smallArray);
String[] exactArray = new String[3];
String[] result2 = list.toArray(exactArray);
System.out.println(result1 == smallArray);
System.out.println(result2 == exactArray);
}
}
What is the output of this code?
java
public class Main {
public static void main(String[] args) {
String s1 = "hello";
String s2 = s1.concat(" world");
String s3 = "hello world";
System.out.println(s1 == s3);
System.out.println(s2 == s3);
System.out.println(s1.equals("hello"));
}
}
What does this code print to the console?
java
import java.io.*;
class MyData implements Serializable {
private String value1;
private transient String value2;
public MyData(String v1, String v2) {
this.value1 = v1;
this.value2 = v2;
}
public String toString() {
return "Value1: " + value1 + ", Value2: " + value2;
}
}
public class Main {
public static void main(String[] args) throws IOException, ClassNotFoundException {
MyData data = new MyData("Hello", "World");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(data);
oos.close();
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
MyData deserializedData = (MyData) ois.readObject();
ois.close();
System.out.println(deserializedData);
}
}
What is the output of this code?
java
public class DataTypeChallenge {
public static void main(String[] args) {
System.out.println("Hello " + 'A' + 10);
System.out.println('A' + 10 + " World");
}
}
What is the specific compilation error in this Java code example involving method overloading and `throws` clauses?
java
import java.io.IOException;
public class OverloadChecker {
public void executeTask() {
System.out.println("Task without exception");
}
public void executeTask() throws IOException { // This line causes the error
System.out.println("Task with IOException");
}
public static void main(String[] args) {
OverloadChecker oc = new OverloadChecker();
oc.executeTask();
}
}
Which of the following statements accurately describes a critical limitation of `BufferedReader`'s `mark()` and `reset()` methods when dealing with large input streams?
When creating a `FileWriter` directly, it is often recommended to wrap it with a `BufferedWriter` (e.g., `new BufferedWriter(new FileWriter("file.txt"))`). What is the primary reason for this common practice?
What type of error will occur when compiling the following Java code?
java
public class Main {
public static void main(String[] args) {
byte b = 130;
System.out.println(b);
}
}
What does this code print?
java
public class StringImmutability {
public static void main(String[] args) {
String s1 = "Hello";
String s2 = s1;
s1 = s1.concat(" World");
System.out.println(s2);
}
}
What is the error in this Java code?
java
class Parent {
public void process(int x) {
System.out.println("Parent int: " + x);
}
}
class Child extends Parent {
@Override // Line 7
public void process(double x) {
System.out.println("Child double: " + x);
}
}
public class Main {
public static void main(String[] args) {
Child c = new Child();
c.process(10.5);
}
}
What is the primary purpose of the `trimToSize()` method in `ArrayList`?
In a situation of classic deadlock where Thread A holds Lock X and waits for Lock Y, and Thread B holds Lock Y and waits for Lock X, what specific Java Thread state would both Thread A and Thread B be in?
What is the output of this Java code?
java
public class LoopTest {
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
System.out.print(i);
}
}
}
What is the output of this Java code?
java
import java.io.*;
public class DeserializationError6 {
public static void main(String[] args) {
byte[] data = null;
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
oos.writeObject("Hello"); // Writes an object
oos.writeInt(123); // Writes a primitive
data = bos.toByteArray();
} catch (IOException e) { e.printStackTrace(); }
try (ByteArrayInputStream bis = new ByteArrayInputStream(data);
ObjectInputStream ois = new ObjectInputStream(bis)) {
int number = ois.readInt(); // Attempts to read a primitive first
String text = (String) ois.readObject();
System.out.println("Number: " + number + ", Text: " + text);
} catch (Exception e) {
System.out.println("Error: " + e.getClass().getName() + ": " + e.getMessage());
}
}
}
What does this code print?
java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<String> items = new ArrayList<>();
items.add("Pen");
items.add("Book");
items.add("Pencil");
Iterator<String> it = items.iterator();
while (it.hasNext()) {
String item = it.next();
if (item.startsWith("P")) {
it.remove();
}
}
System.out.println(items);
}
}
Which of the following scenarios is NOT a valid use case for the `var` keyword (local variable type inference) in Java?
What is the output of this code?
java
public class Test {
public static void main(String[] args) {
String result = "";
for (int i = 0; i < 3; i++) {
switch (i) {
case 0: result += "A"; break;
case 1: result += "B"; break;
case 2: result += "C"; break;
}
result += i;
if (i == 1) {
break;
}
}
System.out.print(result);
}
}
What kind of error will occur when compiling and running the following Java code, assuming 'input.txt' exists?
java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class TestClass {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new FileReader("input.txt"));
String line = br.readLine();
System.out.println(line);
br.close();
}
}