☕ Java MCQ Questions – Page 64

Questions 1261–1280 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q1261 hard code output
What does this code print?
java
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

class MyNumber {
    private int value;
    public MyNumber(int value) { this.value = value; }
    public int getValue() { return value; }
    public static MyNumber create(int v) { return new MyNumber(v); }
    public void printValue() { System.out.print(value + " "); }
    public String toString() { return String.valueOf(value); }
}

public class MethodReferencesTypes {
    public static void main(String[] args) {
        Function<Integer, MyNumber> constructorRef1 = MyNumber::create;
        MyNumber n1 = constructorRef1.apply(10);
        System.out.println("Static method ref result: " + n1.getValue());

        Function<Integer, MyNumber> constructorRef2 = MyNumber::new;
        MyNumber n2 = constructorRef2.apply(20);
        System.out.println("Constructor ref result: " + n2.getValue());

        List<MyNumber> numbers = Arrays.asList(new MyNumber(1), new MyNumber(2), new MyNumber(3));
        numbers.forEach(MyNumber::printValue);
        System.out.println();

        MyNumber instance = new MyNumber(100);
        Supplier<String> supplier = instance::toString;
        System.out.println("Specific instance method ref: " + supplier.get());
    }
}
Q1262 easy
Can elements be removed from the underlying collection using an `Iterator`?
Q1263 hard code error
What does this code print?
java
import java.io.*;

public class BufferNullInit {
    public static void main(String[] args) {
        BufferedReader br = null;
        try {
            br = new BufferedReader(null); // Invalid argument: null Reader
            System.out.println(br.readLine());
        } catch (IOException e) {
            System.out.println("IOException: " + e.getMessage());
        } catch (NullPointerException e) {
            System.out.println("NullPointerException: " + e.getMessage());
        } finally {
            try {
                if (br != null) br.close();
            } catch (IOException e) {
                System.out.println("Error closing: " + e.getMessage());
            }
        }
    }
}
Q1264 medium code error
What is the output of this Java code?
java
public class ExceptionFlow5 {
    public static void main(String[] args) {
        System.out.println(testMethod());
    }

    public static String testMethod() {
        try {
            System.out.println("Try block");
            return "From try";
        } catch (Exception e) {
            System.out.println("Catch block");
            return "From catch";
        } finally {
            System.out.println("Finally block");
            return "From finally";
        }
    }
}
Q1265 easy code error
What error occurs when `join()` is called on a `Thread` object that has not yet been started?
java
class MyThread extends Thread {
    public void run() {
        System.out.println("Child thread running.");
    }
}

public class Main {
    public static void main(String[] args) {
        MyThread t = new MyThread();
        t.join(); // Calling join() on an unstarted thread
    }
}
Q1266 hard code error
What kind of error will occur when compiling or running the following Java code?
java
public class SBError {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("HelloWorld");
        sb.setLength(-1); // Attempt to set negative length
        System.out.println(sb.length());
    }
}
Q1267 hard
What is the outcome when a subclass declares a `static` method with the exact same signature as a `static` method in its superclass?
Q1268 easy
Which keyword is used to declare an abstract class in Java?
Q1269 hard code output
What does this code print?
java
import java.util.TreeSet;
import java.util.SortedSet;

public class App {
    public static void main(String[] args) {
        TreeSet<Integer> numbers = new TreeSet<>();
        for (int i = 1; i <= 5; i++) { numbers.add(i); }

        SortedSet<Integer> subset = numbers.headSet(3, true); // Elements <= 3
        numbers.add(0); // Add to original set, falls within subset range
        subset.add(4); // Attempt to add element outside subset's boundary
        System.out.println(subset);
    }
}
Q1270 easy code output
What does this code print to the console?
java
public class MyClass {
    public static void main(String[] args) {
        int num = 5;
        String result;
        switch (num) {
            case 1: result = "Low"; break;
            case 2: result = "Medium"; break;
            case 3: result = "Medium"; break;
            default: result = "High";
        }
        System.out.print(result);
    }
}
Q1271 medium
What is the effect of the `break` statement when encountered inside a `while` loop?
Q1272 easy code output
What does this Java code print?
java
public class TypeCast {
    public static void main(String[] args) {
        Object obj = "Hello Java";
        String str = (String) obj;
        System.out.println(str);
    }
}
Q1273 easy code error
What is the error in the following Java code?
java
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        TreeSet<String> words = new TreeSet<>();
        words.put("Hello", "World");
    }
}
Q1274 easy code output
What is the output of the following Java code?
java
public class LoopTest {
    public static void main(String[] args) {
        boolean condition = true;
        int value = 0;
        do {
            System.out.print(value);
            value++;
            if (value > 0) {
                condition = false;
            }
        } while (condition);
    }
}
Q1275 hard code error
What is the compile-time error in this Java code?
java
abstract class MyBase {
    private abstract void process();
    public void execute() {
        // Calls to process() would be here
    }
}
Q1276 easy code error
What kind of error will occur when compiling this Java code?
java
public class Main {
    public static void main(String[] args) {
        if (true) {
            int data = 100;
        }
        System.out.println(data);
    }
}
Q1277 easy
For a `File` object representing a regular file, what information does the `length()` method provide?
Q1278 hard code error
What is the compile-time error in this Java code?
java
public class CallJoinOnRunnable {
    public static void main(String[] args) throws InterruptedException {
        Runnable work = () -> System.out.println("Working...");
        work.join(); // Line 4
    }
}
Q1279 medium
What is the most common checked exception that methods of `FileWriter` might throw during file operations?
Q1280 easy code error
What kind of error will occur when this Java code is executed?
java
public class Test {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("abc");
        sb.deleteCharAt(3);
        System.out.println(sb);
    }
}
← Prev 6263646566 Next → Page 64 of 200 · 3994 questions