☕ Java MCQ Questions – Page 140

Questions 2781–2800 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2781 hard code output
What is the output of this code?
java
abstract class Shape {
    public final abstract double getArea();
}

class Circle extends Shape {
    private double radius;
    public Circle(double radius) { this.radius = radius; }
    @Override
    public double getArea() { return Math.PI * radius * radius; }
}

public class ErrorTest {
    public static void main(String[] args) {
        // Code would be here if no error
    }
}
Q2782 medium
Which statement best describes the primary use case of the enhanced for-loop (for-each loop) when iterating over a Java array?
Q2783 hard
Consider the following code snippet: `new FileWriter("nonExistentFile.txt", true);`. If 'nonExistentFile.txt' does not exist prior to this call, what is the behavior upon successful instantiation of the FileWriter?
Q2784 easy code error
What is the output or error of the following Java code?
java
public class ExceptionTest {
    public static void main(String[] args) {
        try {
            throw new NullPointerException();
        } catch (Exception e) {
            System.out.println("Caught Exception");
        } catch (Throwable t) {
            System.out.println("Caught Throwable");
        }
    }
}
Q2785 medium code output
What is the output of this code?
java
public class ThreadStateDemo1 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> System.out.println("Thread running."));
        System.out.println("1: " + t.getState());
        t.start();
        System.out.println("2: " + t.getState());
        Thread.sleep(100); // Give thread time to finish
        System.out.println("3: " + t.getState());
    }
}
Q2786 medium code output
What is the output of this code?
java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");
        Iterator<String> it = fruits.iterator();
        StringBuilder sb = new StringBuilder();
        while (it.hasNext()) {
            sb.append(it.next()).append(" ");
        }
        System.out.println(sb.toString().trim());
    }
}
Q2787 easy code output
What does this Java code print to the console?
java
public class TypeCast {
    public static void main(String[] args) {
        float value = 123.45f;
        long l = (long) value;
        System.out.println(l);
    }
}
Q2788 medium
What is the primary purpose of using the `@Override` annotation in Java when a method is intended to override a superclass method?
Q2789 medium code error
What error occurs when running this Java code?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Programming");
        sb.replace(12, 15, "Code");
        System.out.println(sb);
    }
}
Q2790 easy code output
What is the output of this code?
java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<Double> grades = new ArrayList<>();
        grades.add(90.5);
        grades.add(85.0);
        Iterator<Double> it = grades.iterator();
        it.next(); 
        it.remove(); 
        try {
            it.remove(); 
        } catch (IllegalStateException e) {
            System.out.println("Caught: " + e.getClass().getSimpleName());
        }
    }
}
Q2791 medium
What type of conversion occurs automatically when assigning a `byte` variable to an `int` variable in Java?
Q2792 hard code error
What is the compilation error in the following Java code snippet?
java
interface MyRunnable {
    void run();
}

class BasePrinter {
    public void printBase() { System.out.println("Base Print"); }
}

class DerivedPrinter extends BasePrinter {
    public void printDerived() {
        MyRunnable r = () -> {
            super.printBase(); // Attempting to use 'super'
        };
        r.run();
    }
}

public class Main {
    public static void main(String[] args) {
        new DerivedPrinter().printDerived();
    }
}
Q2793 easy code output
What does this code print?
java
public class Main {
    public static void main(String[] args) {
        int[][] data = {{1, 2}, {3, 4}};
        data[0][0] = 10;
        System.out.println(data[0][0] + data[0][1]);
    }
}
Q2794 medium code error
What error will occur when compiling and running this Java code?
java
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class HashSetError3 {
    public static void main(String[] args) {
        Set<String> fruits = new HashSet<>();
        fruits.add("Apple");
        fruits.add("Banana");

        Set<String> unmodifiableFruits = Collections.unmodifiableSet(fruits);
        unmodifiableFruits.add("Cherry"); // ERROR line
        System.out.println(unmodifiableFruits);
    }
}
Q2795 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) {
        try (FileWriter writer = new FileWriter("output.txt")) {
            writer.write("Hello");
            writer.write(" World");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Q2796 easy code error
What is the compilation error in the following Java code snippet?
java
public class Question2 {
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        names.add("Alice");
        System.out.println(names);
    }
}
Q2797 medium code error
What error will occur when running the following Java code?
java
public class InvalidPriority {
    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            System.out.println("Thread running");
        });
        t.setPriority(11); // Priority must be between Thread.MIN_PRIORITY (1) and Thread.MAX_PRIORITY (10)
        t.start();
    }
}
Q2798 medium code output
What does this code print?
java
class Printer {
    void print(int i) {
        System.out.println("Printing int: " + i);
    }
    void print(double d) {
        System.out.println("Printing double: " + d);
    }
    void print(String s) {
        System.out.println("Printing String: " + s);
    }
}

public class Main {
    public static void main(String[] args) {
        Printer p = new Printer();
        p.print(10);
        p.print("Hello");
        p.print(5.5f); // float will be promoted to double
    }
}
Q2799 medium
When an object is deserialized using Java's standard serialization mechanism, which constructor (if any) is invoked?
Q2800 hard code output
What is the output of this code?
java
public class ArrayShallowCopy {
    public static void main(String[] args) {
        StringBuilder[] sba1 = {new StringBuilder("A"), new StringBuilder("B")};
        StringBuilder[] sba2 = sba1.clone();
        sba2[0].append("C");
        sba2[1] = new StringBuilder("D");
        System.out.println(sba1[0] + " " + sba1[1]);
    }
}
← Prev 138139140141142 Next → Page 140 of 200 · 3994 questions