☕ Java MCQ Questions – Page 17

Questions 321–340 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q321 medium code output
What does this code print?
java
class Product {
    String name;
    double price;

    public Product(String name) {
        this(name, 0.0);
    }

    public Product(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String toString() {
        return name + " - " + price;
    }
}

public class Main {
    public static void main(String[] args) {
        Product p1 = new Product("Laptop");
        Product p2 = new Product("Mouse", 25.0);
        System.out.println(p1);
        System.out.println(p2);
    }
}
Q322 easy code error
What is the compilation error in the following Java code snippet?
java
import java.util.ArrayList;

public class Question8 {
    public static void main(String[] args) {
        ArrayList<Integer> intList = new ArrayList<>();
        intList.add(10);
        ArrayList<Number> numList = intList;
        System.out.println(numList);
    }
}
Q323 easy code error
What error will occur when this code is executed?
java
public class Main {
    public static void main(String[] args) {
        StringBuffer sb = null;
        sb.append("Hello");
        System.out.println(sb);
    }
}
Q324 easy code output
What is the output of this code?
java
class A {
    public void display() {
        System.out.print("Class A");
    }
}

class B extends A {
    @Override
    public void display() {
        super.display();
        System.out.print("Class B");
    }
}

public class Main {
    public static void main(String[] args) {
        B obj = new B();
        obj.display();
    }
}
Q325 easy code error
Identify the error produced by the following Java code:
java
class Calculator {
    private int add(int a, int b) {
        return a + b;
    }
}

public class App {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        int sum = calc.add(5, 3);
        System.out.println(sum);
    }
}
Q326 medium code output
What is the output of the following Java code?
java
class Example {
    static void generateError() {
        System.out.println("Generating error...");
        throw new OutOfMemoryError("Memory exhausted!"); // Error type
    }

    public static void main(String[] args) {
        try {
            generateError();
        } catch (Exception e) {
            System.out.println("Caught Exception: " + e.getMessage());
        } catch (Error e) {
            System.out.println("Caught Error: " + e.getMessage());
        }
        System.out.println("End of program.");
    }
}
Q327 hard code output
What does this code print?
java
class Base {
    public Base() {
        System.out.print("Base ctor. ");
        display();
    }
    public void display() {
        System.out.println("Base display.");
    }
}
class Derived extends Base {
    private int x = 10;
    public Derived() {
        System.out.print("Derived ctor. ");
    }
    @Override
    public void display() {
        System.out.println("Derived display: x=" + x);
    }
}
public class Test {
    public static void main(String[] args) {
        new Derived();
    }
}
Q328 hard
Regarding `null` elements in a `HashSet`, which statement is true?
Q329 hard code output
What is the final value of `counter` printed?
java
import java.util.concurrent.Semaphore;

public class SemaphoreTest {
    private final Semaphore semaphore = new Semaphore(2); // 2 permits
    private int counter = 0;

    public void performTask() {
        try {
            semaphore.acquire();
            Thread.sleep(50); // Simulate work
            synchronized (this) { // Ensure counter increment is atomic
                counter++;
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } finally {
            semaphore.release();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        SemaphoreTest test = new SemaphoreTest();
        for (int i = 0; i < 5; i++) {
            new Thread(test::performTask).start();
        }
        Thread.sleep(1000); // Allow all threads to complete
        System.out.println("Final counter: " + test.counter);
    }
}
Q330 easy code error
What kind of error will occur when compiling this Java code?
java
public class Main {
    public static void main(String[] args) {
        int num = 7;
        if (num == 7)); {
            System.out.println("Seven");
        }
    }
}
Q331 medium code error
What compile-time error will occur in this Java code snippet?
java
public class LoopError {
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            int i = 10;
            System.out.println(i);
        }
    }
}
Q332 easy code output
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> words = new ArrayList<>();
        Iterator<String> it = words.iterator();
        if (!it.hasNext()) {
            System.out.println("List is empty");
        } else {
            System.out.println(it.next());
        }
    }
}
Q333 medium
Can an `ArrayList` store `null` elements?
Q334 easy
What is the underlying data structure used by `java.util.LinkedList`?
Q335 easy code output
What will be the content of the file 'output.txt' after this code executes?
java
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterTest {
    public static void main(String[] args) {
        char[] data = {'J', 'a', 'v', 'a', ' ', 'I', 'O'};
        try (FileWriter writer = new FileWriter("output.txt")) {
            writer.write(data, 0, 4); // Write 'Java'
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Q336 hard code error
Does this Java code compile? If not, what is the compile-time error?
java
import java.io.IOException;
class MyClass {
    public MyClass() {
        this(true);
    }
    public MyClass(boolean flag) throws IOException {
        if (flag) {
            throw new IOException("Failed to initialize");
        }
    }
}
public class Main {
    public static void main(String[] args) {
        // MyClass obj = new MyClass();
    }
}
Q337 hard code error
What is the output of this code?
java
import java.util.LinkedList;
import java.util.Iterator;
import java.util.Arrays;

public class MyClass {
    public static void main(String[] args) {
        LinkedList<Integer> numbers = new LinkedList<>(Arrays.asList(1, 2, 3, 4, 5));
        Iterator<Integer> iterator = numbers.iterator();
        while (iterator.hasNext()) {
            Integer num = iterator.next();
            if (num % 2 == 0) {
                numbers.remove(num); // Modifying list directly, not via iterator
            }
        }
        System.out.println(numbers);
    }
}
Q338 easy code output
What is the output of the following Java program?
java
public class Main {
    public static void main(String[] args) {
        boolean isActive = true;
        System.out.println(isActive);
    }
}
Q339 medium
To significantly improve the performance of writing many small pieces of character data using `FileWriter`, what other Java I/O class should it typically be wrapped with?
Q340 hard
In a Java 8+ interface, what is the nature of `static` methods with respect to abstraction and inheritance?
← Prev 1516171819 Next → Page 17 of 200 · 3994 questions