☕ Java MCQ Questions – Page 177
Questions 3521–3540 of 3994 total — Java interview practice
▶ Practice All Java QuestionsWhat is the error in this Java code?
java
import java.util.ArrayList;
import java.util.List;
public class MyClass {
public static void main(String[] args) {
// Attempt to use a primitive type with ArrayList generics
List<int> primeNumbers = new ArrayList<int>();
primeNumbers.add(2);
primeNumbers.add(3);
System.out.println(primeNumbers);
}
}
What error will this Java code produce?
java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
public class IteratorError {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3));
Iterator<Integer> it = numbers.iterator();
it.next(); // Moves iterator past 1
it.remove(); // Removes 1
it.remove(); // Calling remove() a second time without an intervening next()
}
}
Which two guarantees does the `synchronized` keyword provide simultaneously?
Is it permissible for a Java `if` statement's condition to include a method call, provided the method returns a `boolean` value?
What does this code print?
java
import java.util.LinkedList;
public class MyClass {
public static void main(String[] args) {
LinkedList<Character> letters = new LinkedList<>();
letters.add('A');
letters.add('B');
letters.add('C');
System.out.println(letters.getFirst() + ", " + letters.getLast());
}
}
Consider a `BufferedWriter` wrapping a `FileWriter`. When the `BufferedWriter` is closed, what happens to the underlying `FileWriter`?
What compile-time error will occur in the `AnotherCustomException` class?
java
class CustomException extends Exception {
public CustomException() { super(); }
public CustomException(String message) { super(message); }
}
class AnotherCustomException extends CustomException {
public AnotherCustomException(int code) {
super(code); // CustomException does not have a constructor that takes an int
}
}
public class Main {
public static void main(String[] args) { /* No code here directly causes error */ }
}
What is the output of this code?
java
class Vehicle {
public static void identify() {
System.out.print("I am a vehicle");
}
}
class Car extends Vehicle {
public static void identify() {
System.out.print("I am a car");
}
}
public class Main {
public static void main(String[] args) {
Vehicle myVehicle = new Car();
myVehicle.identify();
}
}
What is the output of this Java code?
java
import java.io.*;
public class DeserializationError7 {
public static void main(String[] args) {
byte[] data = null;
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
oos.writeObject(new Integer(100)); // Serializes an Integer
data = bos.toByteArray();
} catch (IOException e) { e.printStackTrace(); }
try (ByteArrayInputStream bis = new ByteArrayInputStream(data);
ObjectInputStream ois = new ObjectInputStream(bis)) {
String value = (String) ois.readObject(); // Attempts to cast to String
System.out.println("Value: " + value);
} catch (Exception e) {
System.out.println("Error: " + e.getClass().getName() + ": " + e.getMessage());
}
}
}
What is the result of executing this code?
java
public class MultiArrayQ5 {
public static void main(String[] args) {
int[][] board = {{1,2}, {3}, {4,5,6}};
int sum = 0;
for (int[] row : board) {
for (int cell : row) {
sum += cell;
}
}
System.out.println(sum);
}
}
What is the output of this code?
java
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.makeSound();
}
}
Which statement correctly declares and initializes a `double` array named `prices` with the values 10.5, 20.0, and 5.2?
Which error will occur when the following Java code attempts to execute?
java
import java.util.ArrayList;
import java.util.Iterator;
public class IteratorError {
public static void main(String[] args) {
ArrayList<String> emptyList = new ArrayList<>();
Iterator<String> it = emptyList.iterator();
it.next(); // Calling next() on an empty iterator
}
}
What is the output of this Java code?
java
import java.io.FileReader;
import java.io.IOException;
public class FileReaderTest {
public static void main(String[] args) {
// Assume empty.txt exists and is an empty file
try (FileReader reader = new FileReader("empty.txt")) {
int charCode = reader.read();
System.out.print(charCode);
} catch (IOException e) {
System.out.print("Error");
}
}
}
What is the output of this code?
java
class Shape {
Shape() {
System.out.println("Shape constructor");
}
}
class Circle extends Shape {
Circle() {
super(); // Explicitly calling superclass constructor
System.out.println("Circle constructor");
}
}
public class Main {
public static void main(String[] args) {
Circle c = new Circle();
}
}
What is the output of this code?
java
class Car {
String model = "Sedan";
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car();
myCar = null;
try {
System.out.println(myCar.model);
} catch (NullPointerException e) {
System.out.println("Caught NullPointerException");
}
}
}
Consider an interface `Shape` with an abstract method `getArea()`. If a class `Circle` implements `Shape` but does not provide an implementation for `getArea()`, what will happen?
In Java, how is a 2D array `int[][] grid;` fundamentally represented in memory?
What error will this code produce?
java
public class LoopError {
public static void main(String[] args) {
int val = 1;
for (int i = 0; val; i++) {
System.out.println(i);
}
}
}
What is the compile-time error in this Java code?
java
class MyClass {
MyClass() {
this(0);
}
MyClass(int x) {
this();
}
}