☕ Java MCQ Questions – Page 174

Questions 3461–3480 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3461 hard code output
What is the output of this code?
java
interface MyInterfaceA { default void foo() { System.out.println("MyInterfaceA::foo"); } }
interface MyInterfaceB { default void foo() { System.out.println("MyInterfaceB::foo"); } }
class Parent { public void foo() { System.out.println("Parent::foo"); } }

class MyClass extends Parent implements MyInterfaceA, MyInterfaceB {
}

public class Test {
    public static void main(String[] args) {
        MyInterfaceA objA = new MyClass();
        objA.foo();
    }
}
Q3462 medium code output
What is the output of this code?
java
String s1 = "Java";
String s2 = s1.concat(" Developer");
s1 = s1.concat(" Program");
System.out.println(s1 + " " + s2);
Q3463 medium
What is a potential drawback of extensively using immutable objects in scenarios where an object's state needs to be updated frequently?
Q3464 medium
Which of the following statements about `PriorityQueue` in Java is true?
Q3465 easy
In Java, which of the following is an example of an immutable class?
Q3466 easy code error
What is the error in the following Java code? java interface MyPrintable { void print(); } class MyData { int value = 10; }
java
public class Main {
    public static void main(String[] args) {
        MyData data = new MyData();
        MyPrintable printable = (MyPrintable) data;
        System.out.println(printable);
    }
}
Q3467 easy
What is the default initial capacity of a Java HashMap when no capacity is specified?
Q3468 medium
Consider a method that throws a `RuntimeException`. Which statement about declaring `throws RuntimeException` in the method signature is true?
Q3469 hard
Which statement accurately describes a key characteristic of a loop invariant specific to a `do-while` loop, especially contrasting it with a standard `while` loop?
Q3470 easy code output
What is the output of this Java code snippet?
java
public class Main {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            if (i == 3) {
                break;
            }
            System.out.print(i + " ");
        }
    }
}
Q3471 medium code output
What is the result of executing this code?
java
public class StringTest {
    public static void main(String[] args) {
        String message = "Java is awesome";
        System.out.println(message.indexOf("is") + message.indexOf('a', 2));
    }
}
Q3472 medium code error
Which exception will be thrown when executing this Java code?
java
public class StringError5 {
    public static void main(String[] args) {
        String data = "java"; // Length is 4
        String sub = data.substring(2, 5);
        System.out.println(sub);
    }
}
Q3473 hard code output
What is the output of this Java code?
java
import java.util.Comparator;
import java.util.TreeMap;

class TrickyComparator implements Comparator<String> {
    @Override
    public int compare(String s1, String s2) {
        // Compare by length first, then alphabetically
        int lenCompare = Integer.compare(s1.length(), s2.length());
        if (lenCompare != 0) {
            return lenCompare;
        }
        return s1.compareTo(s2); // Secondary sort
    }
}

public class Test {
    public static void main(String[] args) {
        TreeMap<String, Integer> map = new TreeMap<>(new TrickyComparator());
        map.put("abc", 1);
        map.put("a", 2);
        map.put("abcd", 3);
        map.put("ab", 4);
        map.put("aa", 5); // Length 2, comes before "ab" alphabetically

        System.out.println(map.keySet());
    }
}
Q3474 medium code error
What will be the compilation error in this code?
java
class Animal {
    private void eat() {
        System.out.println("Animal eats.");
    }
}

class Dog extends Animal {
    @Override
    public void eat() {
        System.out.println("Dog eats bones.");
    }
}
Q3475 easy code error
What is the error in the following Java code snippet?
java
public class ArrayError {
    public static void main(String[] args) {
        int[] values = new int[-5];
        System.out.println(values.length);
    }
}
Q3476 easy
Which of the following is a valid Java variable name?
Q3477 medium
What is the primary purpose of using the `java.util.Optional` class in Java?
Q3478 easy
What is the default initial capacity of a `StringBuffer` when it is created using the no-argument constructor (e.g., `new StringBuffer()`)?
Q3479 medium code output
What is the content of 'flushed.txt' after this code executes?
java
import java.io.FileWriter;
import java.io.IOException;

public class FlushTest {
    public static void main(String[] args) {
        FileWriter writer = null;
        try {
            writer = new FileWriter("flushed.txt");
            writer.write("Data to be flushed.");
            writer.flush(); // Explicitly flush
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        } finally {
            // writer.close(); // Intentionally omitted for this question
        }
    }
}
Q3480 hard
When designing a Java application to process a log file continuously as new lines are appended, which approach concerning `FileReader` is generally considered problematic for efficiency and resource management?
← Prev 172173174175176 Next → Page 174 of 200 · 3994 questions