☕ Java MCQ Questions – Page 177

Questions 3521–3540 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3521 medium code error
What 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);
    }
}
Q3522 easy code error
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()
    }
}
Q3523 medium
Which two guarantees does the `synchronized` keyword provide simultaneously?
Q3524 medium
Is it permissible for a Java `if` statement's condition to include a method call, provided the method returns a `boolean` value?
Q3525 easy code output
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());
    }
}
Q3526 medium
Consider a `BufferedWriter` wrapping a `FileWriter`. When the `BufferedWriter` is closed, what happens to the underlying `FileWriter`?
Q3527 easy code error
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 */ }
}
Q3528 easy code output
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();
    }
}
Q3529 medium code error
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());
        }
    }
}
Q3530 hard code output
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);
    }
}
Q3531 easy code output
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();
    }
}
Q3532 easy
Which statement correctly declares and initializes a `double` array named `prices` with the values 10.5, 20.0, and 5.2?
Q3533 easy code error
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
    }
}
Q3534 easy code output
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");
        }
    }
}
Q3535 easy code output
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();
    }
}
Q3536 medium code output
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");
        }
    }
}
Q3537 medium
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?
Q3538 medium
In Java, how is a 2D array `int[][] grid;` fundamentally represented in memory?
Q3539 easy code error
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);
        }
    }
}
Q3540 hard code error
What is the compile-time error in this Java code?
java
class MyClass {
    MyClass() {
        this(0);
    }

    MyClass(int x) {
        this();
    }
}
← Prev 175176177178179 Next → Page 177 of 200 · 3994 questions