☕ Java MCQ Questions – Page 79

Questions 1561–1580 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1561 hard code error
Which compile-time error is present in the array initialization below?
java
public class ArrayInitTest {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30.0, 40}; // 30.0 is a double literal
        for (int num : numbers) {
            System.out.println(num);
        }
    }
}
Q1562 easy code error
What kind of error will occur when compiling this Java code?
java
import java.io.FileWriter;

public class Main {
    public static void main(String[] args) {
        // Missing import for BufferedWriter
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter("output.txt"));
            bw.write("Hello");
            bw.close();
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }
    }
}
Q1563 easy
Can a `java.util.LinkedList` store `null` elements?
Q1564 medium code error
What is the outcome when this Java code is executed?
java
import java.util.LinkedList;

public class Test {
    public static void main(String[] args) {
        LinkedList rawList = new LinkedList();
        rawList.add("String Data");
        rawList.add(123);
        String data = (String) rawList.get(1);
        System.out.println(data);
    }
}
Q1565 easy code error
Which error will be thrown when running the given Java code?
java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

public class IteratorError {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>(Arrays.asList("Alpha", "Beta"));
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            String s = it.next();
            if (s.equals("Alpha")) {
                list.add("Gamma"); // Modifying the list directly
            }
        }
    }
}
Q1566 easy
How do you convert a `StringBuilder` object back into a standard `String` object?
Q1567 easy
Does `FileWriter` primarily deal with writing characters or raw bytes to a file?
Q1568 medium
What is the effect of declaring a method as `final` on polymorphism?
Q1569 medium code error
What is the error in this Java code snippet?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Programming");
        sb.replace(12, 14, "Java");
        System.out.println(sb);
    }
}
Q1570 medium
Which statement is true regarding covariant return types in Java for method overriding?
Q1571 medium code error
What kind of error will this code throw at runtime?
java
public class LoopTest {
    public static void main(String[] args) {
        String[] data = {"10", "20", "invalid", "40"};
        int i = 0;
        while (i < data.length) {
            int num = Integer.parseInt(data[i]);
            System.out.println("Parsed: " + num);
            i++;
        }
    }
}
Q1572 easy code output
What is the output of this code?
java
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        ArrayList<Integer> scores = new ArrayList<>();
        scores.add(90);
        scores.add(85);
        scores.add(92);
        System.out.println(scores);
    }
}
Q1573 easy code output
What is the output of this code?
java
public class LambdaTest {
    public static void main(String[] args) {
        String prefix = "Value: ";
        Runnable r = () -> System.out.println(prefix + 10);
        r.run();
    }
}
Q1574 hard
How does the internal buffer of `BufferedReader` primarily enhance the efficiency of reading operations from an underlying `Reader`?
Q1575 easy
Can the `run()` method of the `Runnable` interface declare checked exceptions (e.g., `throws IOException`)?
Q1576 medium
What is the primary performance drawback of `StringBuffer` compared to `StringBuilder` in a single-threaded environment?
Q1577 medium code output
What is the output of this code?
java
String s1 = "Apple";
String s2 = "apple";
String s3 = "Banana";
System.out.println(s1.equals(s2));
System.out.println(s1.equalsIgnoreCase(s2));
System.out.println(s1.compareTo(s3) < 0);
System.out.println(s3.compareToIgnoreCase(s2) > 0);
Q1578 medium
How does `StringBuffer` ensure thread safety when multiple threads attempt to modify its contents concurrently?
Q1579 medium
Which statement accurately describes the behavior of `File.canWrite()` on a Unix-like system when a `File` object represents a file for which the user has read permissions but not write permissions?
Q1580 hard
Given the following Java classes: java class Base { public void test(Object obj) { System.out.println("Base Object"); } } class Derived extends Base { public void test(String str) { System.out.println("Derived String"); } } What will be printed when the following code is executed? `Derived d = new Derived(); d.test("hello");`
← Prev 7778798081 Next → Page 79 of 200 · 3994 questions