☕ Java MCQ Questions – Page 160

Questions 3181–3200 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3181 easy code output
What is the output of this code?
java
class Calculator {
    public void add(int a, int b) {
        System.out.print(a + b);
    }

    public void add(int a, int b, int c) {
        System.out.print(a + b + c);
    }
}

public class Main {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        calc.add(5, 10);
    }
}
Q3182 easy code error
What kind of error will occur when compiling this Java code?
java
public class Main {
    public static void main(String[] args) {
        int a = 10;
        if (a > 5 && "hello") {
            System.out.println("Condition met");
        }
    }
}
Q3183 easy code error
What is the expected error when compiling and running the following Java code?
java
import java.util.LinkedList;

public class MyClass {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        String item = list.get(-1); // Accessing negative index
    }
}
Q3184 easy
What is the primary purpose of a `while` loop in Java?
Q3185 easy
Which of these conversions is an example of an implicit (widening) type cast?
Q3186 medium code error
What happens when you run this Java code?
java
public class LoopTest {
    public static void main(String[] args) {
        String text = null;
        do {
            System.out.println("Attempting to access text...");
        } while (text.equals("hello"));
    }
}
Q3187 medium code error
What kind of error will occur when running this Java code?
java
public class ArrayError {
    public static void main(String[] args) {
        String[] names = new String[3];
        System.out.println(names[0].length());
    }
}
Q3188 hard
Which statement about the relationship between `this()` and `super()` calls in a constructor is correct?
Q3189 medium
In which of the following contexts is it illegal to use either the `break` or `continue` statement?
Q3190 hard code error
Which compile-time error will this Java code produce?
java
public class TypeTest {
    public static void main(String[] args) {
        long bigNumber = 2147483648; // Integer literal is 'int' by default
        System.out.println(bigNumber);
    }
}
Q3191 hard code error
What is the compile-time error in this Java code snippet involving a lambda expression?
java
import java.io.IOException;
@FunctionalInterface
interface MyFunction {
    void execute();
}
public class LambdaProblem {
    public static void main(String[] args) {
        MyFunction func = () -> {
            System.out.println("Executing...");
            throw new IOException("Lambda error");
        };
    }
}
Q3192 medium code output
What does this Java program print?
java
class RethrowExample {
    static void riskyOperation() throws Exception {
        try {
            Integer.parseInt("abc"); // Throws NumberFormatException
        } catch (NumberFormatException e) {
            System.out.println("NFE caught, rethrowing as generic exception.");
            throw new Exception("Failed to parse", e);
        }
    }

    public static void main(String[] args) {
        try {
            riskyOperation();
        } catch (Exception e) {
            System.out.println("Caught in main: " + e.getMessage());
        }
    }
}
Q3193 easy
When adding elements to a `HashSet`, how does it determine if an element is unique?
Q3194 easy code error
Which error will occur when running this Java code?
java
import java.io.FileReader;
import java.io.IOException;

public class FileReaderIssue {
    public static void main(String[] args) {
        FileReader reader = null;
        try {
            reader = new FileReader("nonexistent.txt");
            int data = reader.read();
        } catch (IOException e) {
            System.out.println("Caught an IO Exception.");
        } finally {
            // Missing null check for reader.close()
            // reader.close(); // Problematic line if reader is null
            try { if (reader != null) reader.close(); } catch (IOException e) { /* handle */ } 
        }
    }
}
Q3195 easy code output
Consider the following Java code. What is its output?
java
public class Main {
    public static void main(String[] args) {
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 3; j++) {
                if (j == 1) {
                    continue;
                }
                System.out.print("" + i + j);
            }
        }
    }
}
Q3196 medium code error
Which compile-time error will occur when the `changeValue` method attempts to modify `VALUE`?
java
class MyConstant {
    final int VALUE;

    public MyConstant(int val) {
        this.VALUE = val;
    }

    public void changeValue(int newVal) {
        this.VALUE = newVal; 
    }
}

public class Main {
    public static void main(String[] args) {
        MyConstant mc = new MyConstant(5);
        mc.changeValue(10);
    }
}
Q3197 medium code output
What is the output of the following Java program?
java
import java.util.stream.Stream;
import java.util.stream.Collectors;

public class StreamTest {
    public static void main(String[] args) {
        String result = Stream.iterate(1, n -> n + 2)
                              .limit(3)
                              .map(String::valueOf)
                              .collect(Collectors.joining(","));
        System.out.println(result);
    }
}
Q3198 hard code error
What is the error encountered when running this Java code?
java
import java.util.Arrays;

public class CopyRangeError {
    public static void main(String[] args) {
        int[] original = {10, 20, 30, 40, 50};
        int[] copy = Arrays.copyOfRange(original, 3, 2);
        System.out.println(Arrays.toString(copy));
    }
}
Q3199 hard
In Java, what occurs when a `catch` block attempts to catch a checked exception type that is guaranteed to be unreachable due to a preceding more general `catch` block or the explicit throwing of a narrower exception type?
Q3200 medium
What is the immediate effect of executing an unlabeled `continue` statement inside a `while` loop?
← Prev 158159160161162 Next → Page 160 of 200 · 3994 questions