☕ Java MCQ Questions – Page 81
Questions 1601–1620 of 3994 total — Java interview practice
▶ Practice All Java QuestionsCan a `java.util.LinkedList` store duplicate elements?
What does this code print?
java
import java.io.FileReader;
import java.io.IOException;
public class FileReaderTest {
public static void main(String[] args) {
// Assume test.txt exists and contains a single character 'A'
char[] buffer = new char[3];
try (FileReader reader = new FileReader("test.txt")) {
int charsRead = reader.read(buffer);
int nextCharsRead = reader.read(buffer);
System.out.print(charsRead + ", " + nextCharsRead);
} catch (IOException e) {
System.out.print("Error");
}
}
}
When a class implements the `Runnable` interface, which method must it define to specify the code that a new thread will execute?
What is the output of this code?
java
abstract class Device {
void powerOn() {
System.out.println("Device is ON.");
}
abstract void operate();
}
class Phone extends Device {
@Override
void operate() {
System.out.println("Dialing number.");
}
}
public class Test {
public static void main(String[] args) {
Phone myPhone = new Phone();
myPhone.powerOn();
}
}
What is the output of this code?
java
import java.util.LinkedList;
import java.util.Queue;
public class QueueOperations {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
queue.offer("Apple");
queue.offer("Banana");
System.out.println(queue.poll());
queue.offer("Cherry");
System.out.println(queue.peek());
}
}
Which statement accurately describes a key difference between Java interfaces and abstract classes?
What does this code print?
java
import java.io.*;
class NonSerializableClass {
private String data = "secret data";
public String getData() { return data; }
}
class Container implements Serializable {
private String id;
private NonSerializableClass nonSerializableField;
public Container(String id, NonSerializableClass nonSerializableField) {
this.id = id;
this.nonSerializableField = nonSerializableField;
}
}
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
try {
Container container = new Container("C1", new NonSerializableClass());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(container);
oos.close();
} catch (Exception e) {
System.out.println(e.getClass().getSimpleName());
}
}
}
What error will this code produce?
java
public class LoopError {
public static void main(String[] args) {
for (int i = 0; i < 3; int j = 0) {
System.out.println(i);
}
}
}
What error will this Java code produce when executed?
java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> words = new ArrayList<>();
words.add("hello");
words.add("world");
Iterator<String> it1 = words.iterator();
it1.next(); // "hello"
words.add("java"); // Modifies the list directly, outside the iterator's control
it1.next(); // Attempts to iterate further after modification
}
}
What is the primary error that will occur when running this Java code, assuming it's executed in an environment with limited memory?
java
import java.util.ArrayList;
import java.util.List;
public class InfiniteLoopMemory {
public static void main(String[] args) {
List<byte[]> data = new ArrayList<>();
int i = 0;
while (true) {
data.add(new byte[1024 * 1024]); // Allocate 1MB
i++;
System.out.println("Allocated " + i + "MB");
}
}
}
What does this code print?
java
import java.io.*;
class MyObject implements Serializable {
private String name;
public MyObject(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
MyObject obj = new MyObject("Hello Serialization");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.close();
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
MyObject deserializedObj = (MyObject) ois.readObject();
ois.close();
System.out.println(deserializedObj.getName());
}
}
Given a `File` object `myFile = new File("/home/user/document.txt");`, what would `myFile.getName()` return?
What does this code print?
java
public class MyClass {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("abcdefg");
sb.delete(2, 5);
System.out.print(sb);
}
}
Which common concurrency problem does synchronization primarily help to prevent?
What is the error in the following Java code?
java
import java.io.IOException;
class Reader {
public void readData() throws IOException {
System.out.println("Reading data...");
}
}
class FileReader extends Reader {
@Override
public void readData() throws Exception { // Broadening checked exception
System.out.println("Reading file data...");
}
}
What is the output of this code?
java
public class ThreadStateDemo8 {
private static final Object monitor = new Object();
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
synchronized (monitor) {
try {
System.out.println("Thread is waiting indefinitely.");
monitor.wait(); // Stays in WAITING state
System.out.println("Thread resumed.");
} catch (InterruptedException e) {
System.out.println("Thread interrupted while waiting.");
}
}
});
t.start();
Thread.sleep(100); // Give thread time to enter wait()
System.out.println("State of t: " + t.getState());
Thread.sleep(200); // Wait longer, no notify() is called
System.out.println("State of t after more time: " + t.getState());
}
}
What is the output of this code?
java
abstract class Shape {
public abstract void draw();
}
class Circle extends Shape {
@Override
public void draw() {
System.out.print("Drawing a circle");
}
}
public class Main {
public static void main(String[] args) {
Shape s = new Circle();
s.draw();
}
}
What is the purpose of the `addFirst(E e)` method in `java.util.LinkedList`?
How does making a class immutable primarily enhance its encapsulation?
What compile-time error will occur in the `Child` class?
java
class Parent {
public void init() {
System.out.println("Initializing Parent");
}
}
class Child extends Parent {
@Override
public void init(String config) {
System.out.println("Initializing Child with config: " + config);
}
}