☕ Java MCQ Questions – Page 168

Questions 3341–3360 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3341 hard code error
Identify the compile-time error in this inheritance scenario.
java
class SuperClass {
    SuperClass(String name) {
        System.out.println("SuperClass: " + name);
    }
}

class SubClass extends SuperClass {
    SubClass() {
        // Implicit call to super() here
    }
}
Q3342 medium
How do you correctly start a new thread of execution using a `Runnable` object named `myRunnable`?
Q3343 easy
To determine if a `File` object represents a directory rather than a regular file, which method should be used?
Q3344 medium
To define a custom unchecked exception in Java, which class should it typically extend?
Q3345 hard
Which of the following operations is `TreeMap` *not* inherently optimized for compared to `HashMap` or `LinkedHashMap`?
Q3346 medium code output
What does this Java code print to the console?
java
import java.util.Arrays;
import java.util.List;

public class StreamFilterLambda {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        numbers.stream()
               .filter(n -> n % 2 == 0)
               .forEach(System.out::print);
    }
}
Q3347 easy
Is it possible for a single `try` block to have multiple `catch` blocks associated with it?
Q3348 medium code error
What is the error in the following Java code? (Assume classes are in separate files in their respective packages)
java
// File: com/package1/DataHolder.java
package com.package1;

public class DataHolder {
    int value = 10; // Default (package-private) access
}

// File: com/package2/MainApp.java
package com.package2;

import com.package1.DataHolder;

public class MainApp {
    public static void main(String[] args) {
        DataHolder holder = new DataHolder();
        System.out.println(holder.value);
    }
}
Q3349 easy code error
What compile-time error will occur when compiling this Java code?
java
public class MyClass {
    public static void main(String[] args) {
        int day = 1;
        switch (day) {
            case 1:
                System.out.println("Monday");
                break;
            case 2:
                System.out.println("Tuesday");
                break;
            case 1:
                System.out.println("Another Monday");
                break;
            default:
                System.out.println("Other day");
        }
    }
}
Q3350 easy code output
What is the output of this code?
java
import java.util.function.Consumer;

public class LambdaTest {
    public static void main(String[] args) {
        Consumer<String> printer = s -> System.out.println(s.toUpperCase());
        printer.accept("java");
    }
}
Q3351 medium
What is the primary purpose of an instance initializer block (`{}`) in a Java class?
Q3352 medium
Which Java String method is specifically designed to replace all occurrences of a substring that matches a given regular expression?
Q3353 medium
What is the behavior of the `continue` statement when executed within a `for` loop in Java?
Q3354 medium code output
What is the output of the following code snippet?
java
public class StringBuilderTest {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("madam");
        sb.reverse();
        System.out.println(sb);
    }
}
Q3355 medium
When does a `HashMap` typically perform a 'rehashing' operation?
Q3356 hard code error
Which compile-time error will this Java program produce?
java
public class CharNegativeTest {
    public static void main(String[] args) {
        char unicodeChar = -1; // char type is unsigned and cannot hold negative values
        System.out.println(unicodeChar);
    }
}
Q3357 hard
Given the following Java class: java class Calculator { void calculate(int a, double b) { System.out.println("int, double"); } void calculate(double a, int b) { System.out.println("double, int"); } } What is the result of compiling and running the following code? `new Calculator().calculate(10, 20);`
Q3358 easy
What does 'SAM' stand for in the context of functional interfaces?
Q3359 easy code output
What is the output of this code?
java
public class Main {
    public static void main(String[] args) {
        int[][] values = {{1, 2, 3}, {4, 5, 6}};
        int count = 0;
        for (int i = 0; i < values.length; i++) {
            if (values[i][0] % 2 == 0) {
                count++;
            }
        }
        System.out.println(count);
    }
}
Q3360 medium code error
Identify the compilation error in the provided Java code, which attempts to modify an object's state incorrectly within an 'immutable' context.
java
class MyImmutableContainer {
    private final StringBuilder content;
    public MyImmutableContainer(StringBuilder content) {
        this.content = content;
    }
    public void modifyContent(String newText) {
        this.content = new StringBuilder(newText); // Attempt to reassign final field
    }
}
← Prev 166167168169170 Next → Page 168 of 200 · 3994 questions