☕ Java MCQ Questions – Page 183

Questions 3641–3660 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3641 medium
What is the effect of declaring a class as `final` in Java?
Q3642 hard code error
What error will this Java code produce when compiled and run?
java
import java.util.ArrayList;

public class ArrayCovariance {
    public static void main(String[] args) {
        Object[] objects = new String[5];
        objects[0] = "Hello";
        // Attempt to store an Integer in an array originally typed as String[]
        objects[1] = new Integer(123);
        System.out.println(objects[0]);
    }
}
Q3643 easy code output
What is the output of the following Java code?
java
public class MyClass {
    public static void main(String[] args) {
        String fruit = "Apple";
        switch (fruit) {
            case "Banana": System.out.print("Yellow"); break;
            case "Apple": System.out.print("Red"); break;
            case "Orange": System.out.print("Orange"); break;
            default: System.out.print("Unknown color");
        }
    }
}
Q3644 hard code error
What is the result of executing this Java code snippet?
java
public class MultiArrayCasting {
    public static void main(String[] args) {
        String[][] stringMatrix = {{"A", "B"}, {"C", "D"}};
        Object[][] objMatrix = stringMatrix; 

        Integer[][] intMatrix = (Integer[][]) objMatrix; 
        System.out.println(intMatrix[0][0]);
    }
}
Q3645 hard code error
What error will be thrown when `main` attempts to execute the `synchronized` block?
java
public class SyncOnNull {
    public static void main(String[] args) {
        Object lockObject = null;
        synchronized (lockObject) {
            System.out.println("This line will not be reached.");
        }
    }
}
Q3646 easy
Which annotation is commonly used in Java to indicate that a method is intended to override a method in a superclass or implement an interface method?
Q3647 medium code error
What is the compile-time error in the following Java code?
java
class MyClass {
    int value;
    public MyClass() {
        System.out.println("Default constructor");
        this(0); // This should be the first statement
    }
    public MyClass(int value) {
        this.value = value;
        System.out.println("Parameterized constructor: " + value);
    }
}

public class ConstructorError2 {
    public static void main(String[] args) {
        new MyClass();
    }
}
Q3648 medium code error
What error does this Java code produce when executed?
java
public class Main {
    public static void main(String[] args) {
        String fruit = "apple";
        System.out.println(fruit.charAt(-1));
    }
}
Q3649 easy
What is an abstract method in Java?
Q3650 hard code output
What is the output of this code?
java
public class LabeledBreakWhile {
    public static void main(String[] args) {
        int i = 0;
        int j = 0;
        String result = "";
        outerLoop: while (i < 3) {
            result += "O" + i;
            j = 0;
            while (j < 3) {
                result += "I" + j;
                if (i == 1 && j == 1) {
                    break outerLoop;
                }
                j++;
            }
            i++;
        }
        System.out.println(result);
    }
}
Q3651 easy code error
What is the compilation error in the provided Java code?
java
@FunctionalInterface
interface RunnableTask {
    void run();
}

public class Main {
    public static void main(String[] args) {
        int counter = 0;
        RunnableTask task = () -> {
            counter++;
            System.out.println("Counter: " + counter);
        };
        task.run();
    }
}
Q3652 hard code error
Examine the following Java code. What will be the outcome when executed?
java
import java.io.*;

public class FileWriterError3 {
    public static void main(String[] args) {
        File file = new File("closed_test.txt");
        try {
            FileWriter fw = new FileWriter(file);
            fw.write("First line.");
            fw.close(); // Stream is closed
            fw.write("Second line?"); // Attempt to write to a closed stream
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        } finally {
            file.delete(); // Clean up
        }
    }
}
Q3653 easy
Which method checks if a string has a length of 0?
Q3654 easy
What is a primary benefit of using polymorphism in Java?
Q3655 medium code output
What does this Java code print?
java
class MyThread extends Thread {
    public void run() {
        System.out.print("Thread running: ");
        System.out.println(getName());
    }
}
public class Main {
    public static void main(String[] args) throws InterruptedException {
        MyThread t = new MyThread();
        t.setName("MyWorker");
        t.start();
        t.join();
        System.out.print("Main done.");
    }
}
Q3656 easy code output
What is the output of this Java code snippet?
java
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        TreeMap<String, String> treeMap = new TreeMap<>();
        treeMap.put("key1", "value1");
        treeMap.put("key2", null);
        System.out.println(treeMap.get("key2"));
    }
}
Q3657 medium code output
What is printed to the console when this code executes, assuming 'nonexistent_dir/file.txt' is an invalid path that cannot be created on the system?
java
import java.io.FileWriter;
import java.io.IOException;

public class InvalidPathTest {
    public static void main(String[] args) {
        try (FileWriter writer = new FileWriter("nonexistent_dir/file.txt")) {
            writer.write("This should not be written.");
        } catch (IOException e) {
            System.out.println("Caught an IOException: " + e.getMessage());
        }
    }
}
Q3658 medium code error
What error will this Java code produce?
java
public class ArrayError {
    public static void main(String[] args) {
        int[2][] matrix; 
        matrix = new int[2][3];
        System.out.println(matrix[0][0]);
    }
}
Q3659 hard
If a method declares a local variable with the same name as an instance variable of the class, how can the instance variable be accessed from within that method?
Q3660 medium
In a method designed to validate user input, if the input is found to be invalid, how should a custom `InvalidInputException` (a checked exception) typically be used to signal this error?
← Prev 181182183184185 Next → Page 183 of 200 · 3994 questions