☕ Java MCQ Questions – Page 23

Questions 441–460 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q441 medium code output
What is the output of this Java code?
java
public class StringBuilderTest {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Programming");
        sb.delete(3, 7);
        System.out.println(sb);
    }
}
Q442 medium
What does the `ready()` method of `BufferedReader` indicate?
Q443 hard code error
What happens when the following Java code is executed?
java
public class NullAutoboxingCheck {
    public static void main(String[] args) {
        Integer data = null;
        if (data == 5) { // Autounboxing `null` to primitive `int` for comparison.
            System.out.println("Data is 5.");
        } else {
            System.out.println("Data is not 5.");
        }
    }
}
Q444 easy code output
What does this Java code print?
java
import java.io.File;

public class FileTest {
    public static void main(String[] args) {
        File file = new File("nonExistentFileToDelete.txt");
        boolean deleted = file.delete();
        System.out.println(deleted + " " + file.exists());
    }
}
Q445 medium code output
What is the output of the following Java program?
java
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        boolean removed = fruits.remove("Apple");
        System.out.println(removed + " " + fruits.size());
    }
}
Q446 medium
What is the generally accepted naming convention for custom exception classes in Java?
Q447 easy code error
What compile-time error will this Java code produce concerning constructors?
java
class Book {
    String title;

    Book(String title) {
        this.title = title;
    }

    Book(String title) {
        this.title = title + " (Default Author)";
    }
}
Q448 hard
In a multi-package application, how does Java's default (package-private) access modifier primarily contribute to encapsulation?
Q449 easy code output
What is the output of this code?
java
class StaticBlockCounter {
    private static int count = 0;
    private static final Object lock = new Object();
    public void increment() {
        synchronized(lock) {
            count++;
            System.out.print(count);
        }
    }
}

public class MyProgram {
    public static void main(String[] args) throws InterruptedException {
        StaticBlockCounter sbc1 = new StaticBlockCounter();
        StaticBlockCounter sbc2 = new StaticBlockCounter();
        Thread t1 = new Thread(() -> sbc1.increment());
        Thread t2 = new Thread(() -> sbc2.increment());
        t1.start();
        t2.start();
        t1.join();
        t2.join();
    }
}
Q450 easy code output
What does this Java code print?
java
class Vehicle {
    Vehicle() {
        System.out.println("No-arg Vehicle");
    }
    Vehicle(int wheels) {
        System.out.println("Vehicle with " + wheels + " wheels");
    }
}
public class Main {
    public static void main(String[] args) {
        Vehicle v1 = new Vehicle();
        Vehicle v2 = new Vehicle(4);
    }
}
Q451 medium code output
What is the output of this code?
java
import java.util.LinkedList;
import java.util.Queue;

public class QueuePeekPoll {
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<>();
        queue.add("First");
        queue.add("Second");
        System.out.println(queue.peek());
        System.out.println(queue.poll());
        System.out.println(queue.peek());
    }
}
Q452 medium code error
What kind of error will occur when executing this Java code?
java
import java.util.TreeSet;

class CustomComparable implements Comparable<CustomComparable> {
    int value;
    public CustomComparable(int value) { this.value = value; }
    @Override
    public int compareTo(CustomComparable o) { return Integer.compare(this.value, o.value); }
}

public class Main {
    public static void main(String[] args) {
        TreeSet set = new TreeSet(); // Raw type
        set.add(new CustomComparable(10));
        set.add(5); // Adding an Integer, which is not CustomComparable
        System.out.println(set.size());
    }
}
Q453 easy code error
What compile-time error will occur in the `Child` class?
java
import java.io.IOException;

class Parent {
    public void processData() {
        System.out.println("Processing data");
    }
}

class Child extends Parent {
    @Override
    public void processData() throws IOException {
        System.out.println("Child processing data");
    }
}
Q454 hard
If a `try-finally` block is entirely contained within the `do` block of a `do-while` loop, and a `break` statement is executed within the `try` block, what is the precise execution sequence?
Q455 easy
Which abstract class does `FileReader` directly extend in Java's character stream hierarchy?
Q456 easy code error
What is the compile-time error in this Java code snippet?
java
public class ArrayTest {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};
        int arraySize = numbers.length();
        System.out.println(arraySize);
    }
}
Q457 easy code error
What error will occur when compiling the following Java code?
java
import java.util.HashSet;

public class MyClass {
    public static void main(String[] args) {
        HashSet<Integer> nums = new HashSet<>(10.5f); // This line will cause an error
        nums.add(5);
        System.out.println(nums.contains(5));
    }
}
Q458 easy
What is the correct general syntax for a do-while loop in Java?
Q459 easy code error
What is the expected error when executing this Java code?
java
import java.util.Arrays;
import java.util.List;
import java.util.Iterator;

public class IteratorError {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("X", "Y", "Z"); // Returns a fixed-size list
        Iterator<String> it = list.iterator();
        it.next();
        it.remove(); // Attempting to modify a fixed-size list
    }
}
Q460 medium
What is the scope of a member (variable or method) declared with no explicit access modifier (default/package-private) in Java?
← Prev 2122232425 Next → Page 23 of 200 · 3994 questions