☕ Java MCQ Questions – Page 92

Questions 1821–1840 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1821 hard
Consider the expression: `(true ? 10 : 20.0)`. What is the type of the result?
Q1822 easy code error
What is the result of running this Java code?
java
public class StringError {
    public static void main(String[] args) {
        String s1 = null;
        String s2 = "test";
        System.out.println(s1.equals(s2));
    }
}
Q1823 easy code error
What is the error in this code?
java
// import java.util.TreeMap; // This line is missing
import java.util.HashMap;

public class Test {
    public static void main(String[] args) {
        TreeMap<String, String> map = new TreeMap<>();
        map.put("key", "value");
        System.out.println(map.size());
    }
}
Q1824 easy code error
What kind of error will occur when compiling or running the following Java code snippet?
java
import java.io.File;

public class FileError2 {
    public static void main(String[] args) {
        File file = new File("existingFile.txt");
        try {
            file.createNewFile(); // Ensure file exists for the next step
            File[] files = file.listFiles();
            System.out.println(files[0].getName());
        } catch (IOException e) {
            System.out.println("Error creating file: " + e.getMessage());
        } catch (NullPointerException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}
Q1825 hard code output
What is the output of this code?
java
public class Test {
    public static void main(String[] args) {
        try {
            System.out.print("Try ");
            System.exit(0);
        } catch (Exception e) {
            System.out.print("Catch ");
        } finally {
            System.out.print("Finally ");
        }
        System.out.print("End");
    }
}
Q1826 medium code error
What error will this Java code produce?
java
public class ArrayError {
    public static void main(String[] args) {
        int[][] matrix = new int[2][2];
        matrix[0] = 10; 
        System.out.println(matrix[0][0]);
    }
}
Q1827 hard code output
What is the output of this Java code?
java
public class InterruptStatusAndSleep {
    public static void main(String[] args) {
        Thread.currentThread().interrupt();
        System.out.println("Main thread interrupted status (before sleep): " + Thread.currentThread().isInterrupted());
        try {
            Thread.sleep(100);
            System.out.println("Main thread: Sleep completed.");
        } catch (InterruptedException e) {
            System.out.println("Main thread: Caught InterruptedException.");
            System.out.println("Main thread interrupted status (after catch): " + Thread.currentThread().isInterrupted());
        }
        System.out.println("Main thread: Exiting.");
    }
}
Q1828 medium
If a thread calls `object.wait()` within a synchronized block without a timeout, what state does the thread enter, assuming no `notify()` call occurs immediately?
Q1829 easy code output
What does this code print?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Programming");
        sb.insert(4, "-");
        System.out.print(sb);
    }
}
Q1830 hard code output
What is the output of this code?
java
import java.util.TreeSet;
import java.util.Comparator;

public class App {
    static class Person implements Comparable<Person> {
        String name; int age;
        public Person(String name, int age) { this.name = name; this.age = age; }
        @Override public int compareTo(Person other) { return this.name.compareTo(other.name); }
        @Override public String toString() { return name + ":" + age; }
    }
    public static void main(String[] args) {
        TreeSet<Person> people = new TreeSet<>(new Comparator<Person>() {
            @Override public int compare(Person p1, Person p2) { return Integer.compare(p1.age, p2.age); }
        });
        people.add(new Person("Alice", 30));
        people.add(new Person("Bob", 25));
        people.add(new Person("Charlie", 30)); // Same age as Alice
        System.out.println(people.size());
    }
}
Q1831 hard
When `BufferedReader.skip(long n)` is called, how does it typically interact with the internal buffer of the `BufferedReader`?
Q1832 easy code error
What error will occur when this code is executed?
java
public class Main {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer(-5);
        sb.append("Data");
        System.out.println(sb);
    }
}
Q1833 hard
Which of the following types is NOT permitted as the selector expression for a Java `switch` *statement* (prior to Java 12 `switch` expressions)?
Q1834 medium code output
What is the output of this code?
java
String s1 = new String("test");
String s2 = "test";
String s3 = s1.intern();
System.out.println(s1 == s2);
System.out.println(s2 == s3);
System.out.println(s1.equals(s2));
System.out.println(s2.equals(s3));
Q1835 medium code error
Which error will be encountered during the compilation of this Java code snippet?
java
public class MyWorker {
    public void performWork() {
        System.out.println("Worker performing work.");
    }
}

public class Main {
    public static void main(String[] args) {
        MyWorker worker = new MyWorker();
        Thread thread = new Thread(worker);
        thread.start();
    }
}
Q1836 easy code error
Examine the following abstract class. What happens when you try to compile it?
java
abstract class Shape {
    abstract void draw();

    void draw() {
        System.out.println("Drawing a generic shape.");
    }
}
Q1837 hard code output
What is the output of this code?
java
public class Test {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("initial");
        sb.ensureCapacity(20);
        sb.append(" more text");
        sb.trimToSize();
        System.out.println(sb.capacity());
    }
}
Q1838 easy code error
What will be the output or error when running this code?
java
class Printer {
    void printMessage() {
        System.out.println("Hello from Printer!");
    }
}

public class Main {
    public static void main(String[] args) {
        Printer p = null;
        p.printMessage();
    }
}
Q1839 hard
Which of the following statements correctly describes a characteristic of primitive type casting in Java?
Q1840 medium code output
What does this Java code print?
java
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        ArrayList<String> items = new ArrayList<>();
        items.add("Pen");
        items.add("Book");
        System.out.println(items.contains("Book") + " " + items.contains("Pencil"));
    }
}
← Prev 9091929394 Next → Page 92 of 200 · 3994 questions