☕ Java MCQ Questions – Page 25

Questions 481–500 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q481 hard code output
What does this code print?
java
import java.io.*;

public class Test {
    public static void main(String[] args) throws IOException {
        String data = "Line1";
        BufferedReader br = new BufferedReader(new StringReader(data));
        br.mark(10);
        br.read(); // L
        br.read(); // i
        br.skip(2);
        br.reset();
        System.out.print(br.readLine());
        br.close();
    }
}
Q482 easy
What does the `=` operator do when used with variables in Java?
Q483 hard
Consider the following Java code snippet: `double d1 = 0.1; double d2 = 0.2; double d3 = 0.3; System.out.println(d1 + d2 == d3);` What will be the output and why?
Q484 easy
What defines an immutable object in Java?
Q485 easy
Is `FileReader` inherently buffered for efficient reading of large files?
Q486 easy
What state is a thread in after its `run()` method has completed its execution?
Q487 easy code error
What is the result of running this Java code?
java
public class StringError {
    public static void main(String[] args) {
        String text = "coding";
        System.out.println(text.substring(0, 7));
    }
}
Q488 medium code output
What is the output of the following Java program?
java
public class WideningAutoboxing {
    void operate(long l) {
        System.out.println("Long: " + l);
    }
    void operate(Integer i) {
        System.out.println("Integer: " + i);
    }
    public static void main(String[] args) {
        WideningAutoboxing wab = new WideningAutoboxing();
        wab.operate(100);
    }
}
Q489 hard
In the context of immutability, why might using the Builder pattern be particularly advantageous for constructing complex immutable objects?
Q490 medium code error
Which exception will be thrown when executing this Java code?
java
public class StringError3 {
    public static void main(String[] args) {
        String numStr = "123a";
        int num = Integer.parseInt(numStr);
        System.out.println(num);
    }
}
Q491 easy
Which method returns the number of characters in a String object?
Q492 medium
Given an array `int[] data = {10, 20, 30};`, what would be the result of attempting to access `data[3]`?
Q493 hard code error
What is the most likely error when executing this Java code snippet?
java
import java.io.BufferedWriter;
import java.io.StringWriter;
import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        StringWriter sw = new StringWriter();
        try (BufferedWriter bw = new BufferedWriter(sw)) {
            String nullString = null;
            bw.append(nullString);
            bw.flush();
        } catch (IOException e) {
            System.err.println("IO Error: " + e.getMessage());
        } catch (NullPointerException e) {
            System.err.println("Runtime Error: " + e.getClass().getName());
        }
        System.out.println("Content: " + sw.toString());
    }
}
Q494 hard
Given the following Java class: java class Overload { void perform(int value) { System.out.println("Fixed-arity"); } void perform(int... values) { System.out.println("Varargs"); } } What will be printed when the following code is executed? `new Overload().perform(100);`
Q495 easy
If the condition in a simple `if` statement (without an `else`) evaluates to `false`, what happens?
Q496 medium
What is the primary advantage of using a `HashSet` over an `ArrayList` when you frequently need to check for the existence of an element?
Q497 easy code error
What kind of error will occur when compiling this Java code?
java
class Calculator {
    public static void add(int a, int b) {
        System.out.println(a + b);
    }
}

class AdvancedCalculator extends Calculator {
    public void add(int a, int b) {
        System.out.println("Advanced Sum: " + (a + b));
    }
}

public class Main {
    public static void main(String[] args) {
        AdvancedCalculator ac = new AdvancedCalculator();
        ac.add(5, 3);
    }
}
Q498 hard
Consider the nested `if` structure: `if (x > 0) { if (x > 5) { /* logic A */ } else { /* logic B */ } } else { /* logic C */ }`. If `logic A` is reached, what can be definitively said about `x`'s value?
Q499 hard code error
What is the compilation error in the following Java code?
java
public class ReportGenerator {
    private class ReportData {
        String content = "Secret Report";
    }

    public void generate() {
        ReportData data = new ReportData(); // This is allowed
        System.out.println(data.content);
    }
}

public class TestReport {
    public static void main(String[] args) {
        ReportGenerator generator = new ReportGenerator();
        // Attempt to instantiate a private inner class from outside
        ReportGenerator.ReportData invalidData = generator.new ReportData();
    }
}
Q500 medium code output
What does this code print?
java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class UnmodifiableListTest {
    public static void main(String[] args) {
        List<String> mutableList = new ArrayList<>();
        mutableList.add("Apple");
        List<String> unmodList = Collections.unmodifiableList(mutableList);
        
        mutableList.add("Banana"); 
        
        System.out.println(unmodList.size());
    }
}
← Prev 2324252627 Next → Page 25 of 200 · 3994 questions