☕ Java MCQ Questions – Page 76
Questions 1501–1520 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat is the output of this code?
java
public class StringTest {
public static void main(String[] args) {
String val = null;
System.out.println(String.valueOf(val) + ".isEmpty:" + (val != null && val.isEmpty()));
}
}
What compilation error will occur in the following Java code?
java
abstract class Shape {
abstract double getArea();
abstract double getPerimeter();
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
double getArea() {
return Math.PI * radius * radius;
}
}
What is the output of this Java code?
java
import java.util.function.Consumer;
class Printer {
public void print(String s) {
System.out.println("String: " + s);
}
public void print(Object o) {
System.out.println("Object: " + o.getClass().getName());
}
}
public class MethodRefTest {
public static void main(String[] args) {
Printer p = new Printer();
Consumer<String> consumer = p::print;
consumer.accept("Test");
}
}
What error will occur when compiling or running this Java code?
java
public class ArrayError {
public static void main(String[] args) {
int[] numbers;
// The array reference 'numbers' is not explicitly initialized here
System.out.println(numbers.length);
}
}
What compilation error will occur in this Java code, focusing on variable access within an inner class?
java
public class EffectivelyFinal {
public static void main(String[] args) {
int count = 0;
Runnable r = new Runnable() {
@Override
public void run() {
System.out.println(count); // Accesses count
}
};
count = 1; // This line causes the error
r.run();
}
}
What is the result of executing this Java code?
java
import java.util.function.Function;
public class Test {
public static void main(String[] args) {
Function<String, Integer> stringLength = String::length;
Function<Integer, String> intToString = String::valueOf;
int length = stringLength.apply("Java");
String result = intToString.apply(length);
System.out.println(result + result.length());
}
}
Which of the following statements about global variables in Java is true?
What error occurs when running this Java code?
java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class UnmodifiableListRemove {
public static void main(String[] args) {
List<String> originalList = new ArrayList<>(Arrays.asList("P", "Q", "R"));
List<String> unmodifiableList = Collections.unmodifiableList(originalList);
Iterator<String> iterator = unmodifiableList.iterator();
iterator.next(); // Moves past P
iterator.remove(); // Attempts to remove P
System.out.println(unmodifiableList);
}
}
What is the error in this Java code?
java
class Super {
Number getObject() {
return Integer.valueOf(10);
}
}
class Sub extends Super {
String getObject() { // Line 8
return "Hello";
}
}
public class Main {
public static void main(String[] args) {
Super s = new Sub();
System.out.println(s.getObject());
}
}
What does this code print?
java
import java.io.*;
class Item implements Serializable {
String itemName;
int itemId;
public Item(String itemName, int itemId) {
this.itemName = itemName;
this.itemId = itemId;
}
@Override
public String toString() {
return itemName + ":" + itemId;
}
}
class Box implements Serializable {
Item item1;
Item item2; // This will be null
public Box(Item item1, Item item2) {
this.item1 = item1;
this.item2 = item2;
}
@Override
public String toString() {
return "Box[" + item1 + "," + item2 + "]";
}
}
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Item i1 = new Item("Laptop", 101);
Box box = new Box(i1, null); // item2 is null
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(box);
oos.close();
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
Box deserializedBox = (Box) ois.readObject();
ois.close();
System.out.println(deserializedBox.toString());
}
}
A `final` instance variable that is not initialized at the point of declaration is known as a 'blank final' variable. Which of the following is true about its initialization?
`BufferedReader` typically wraps which type of stream?
By default, which character encoding does `java.io.FileReader` use when reading characters from a file?
What is the output of this code?
java
import java.util.ArrayList;
import java.util.Collection;
class Item {
int id;
String name;
public Item(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Item item = (Item) o;
return id == item.id && name.equals(item.name);
}
@Override
public int hashCode() {
return java.util.Objects.hash(id, name);
}
@Override
public String toString() {
return "Item{" + id + "}";
}
}
public class Test {
public static void main(String[] args) {
ArrayList<Item> list1 = new ArrayList<>();
list1.add(new Item(1, "Apple"));
list1.add(new Item(2, "Banana"));
list1.add(new Item(3, "Cherry"));
ArrayList<Item> list2 = new ArrayList<>();
list2.add(new Item(2, "Banana"));
list2.add(new Item(4, "Date"));
list1.removeAll(list2);
System.out.println(list1);
}
}
Which of the following is the correct way to declare and initialize an array of 10 integers in Java?
A key distinction between `FileWriter` and `FileOutputStream` when writing character data is how they handle multi-byte characters (e.g., Unicode characters outside the ASCII range). Which statement correctly describes this difference?
What compile-time error occurs when attempting to compile this Java code?
java
class Base {
private void secretMethod() {
System.out.println("Base's secret");
}
}
class Derived extends Base {
@Override
public void secretMethod() {
System.out.println("Derived's secret");
}
}
What is the output of this Java code?
java
import java.util.LinkedList;
import java.util.Queue;
import java.util.NoSuchElementException;
public class QueueTest {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
String result = "";
try {
result += queue.poll() + ",";
result += queue.peek() + ",";
result += queue.remove() + ",";
} catch (Exception e) {
result += e.getClass().getSimpleName();
}
System.out.println(result);
}
}
In the context of designing an immutable class, when is 'defensive copying' most critically applied?
When is the boolean condition of a `while` loop evaluated?