☕ Java MCQ Questions – Page 179

Questions 3561–3580 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3561 medium code error
Which exception will be thrown when executing this Java code?
java
public class StringError10 {
    public static void main(String[] args) {
        String input = "hello";
        String[] parts = input.split("x"); // results in {"hello"}
        System.out.println(parts[1]);
    }
}
Q3562 easy
Which method checks if a string begins with a specified prefix?
Q3563 medium code error
What is the compile-time error in the following Java code?
java
class ConfigManager {
    private String configName;

    private ConfigManager(String name) {
        this.configName = name;
        System.out.println("ConfigManager for: " + name);
    }
}

public class ConstructorError9 {
    public static void main(String[] args) {
        ConfigManager manager = new ConfigManager(); // Attempting to use default constructor
    }
}
Q3564 medium code error
What is the error in this Java code snippet?
java
public class MyClass {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello");
        sb.insert(6, " World");
        System.out.println(sb);
    }
}
Q3565 hard code output
What is the output of this code?
java
class Base {
    public final void print() {
        System.out.println("Base Print");
    }
}

class Derived extends Base {
    // Uncommenting the next lines would cause a compile-time error
    // @Override
    // public void print() {
    //     System.out.println("Derived Print");
    // }
    public void callPrint() {
        System.out.println("Derived calls Base's final method:");
        super.print();
    }
}

public class Main {
    public static void main(String[] args) {
        Derived d = new Derived();
        d.callPrint();
    }
}
Q3566 medium
What is the primary implication of Java's `String` class being immutable?
Q3567 hard code error
What runtime error will this Java code snippet throw?
java
public class Test {
    public static void main(String[] args) {
        int[][] matrix = new int[2][];
        matrix[0] = new int[3];
        matrix[1] = new int[1];
        System.out.println(matrix[0][3]);
    }
}
Q3568 medium code output
What is the output of this Java code snippet?
java
import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Set<String> words = new HashSet<>();
        words.add("apple");
        words.add(null);
        words.add("banana");
        words.add(null);
        System.out.println(words.size());
    }
}
Q3569 hard
What is the correct approach to handling a checked exception thrown within a static initializer block in Java?
Q3570 easy code output
What will be the content of the file 'output.txt' after this code executes?
java
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterTest {
    public static void main(String[] args) {
        char[] suffix = {'X', 'Y', 'Z'};
        try (FileWriter writer = new FileWriter("output.txt")) {
            writer.write("Prefix");
            writer.write(suffix);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Q3571 medium code output
What does this code print?
java
import java.util.HashMap;

public class Test {
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1, "One");
        map.put(null, "Zero");
        map.put(2, null);
        System.out.println(map.get(null));
    }
}
Q3572 hard
Consider a symbolic link `link.txt` pointing to `/absolute/path/to/target.txt`. If `new File("link.txt").getCanonicalPath()` is called from `/current/directory`, what will be the likely outcome compared to `getAbsolutePath()`?
Q3573 easy code error
What error will this code produce?
java
public class LoopError {
    public static void main(String[] args) {
        for (final int i = 0; i < 5; i++) {
            System.out.println(i);
        }
    }
}
Q3574 medium code error
What error will prevent the `Calculator` class from compiling?
java
class Calculator {
    int operand = 10;

    static void printOperand() {
        System.out.println(this.operand); 
    }
}

public class Main {
    public static void main(String[] args) {
        Calculator.printOperand();
    }
}
Q3575 hard code error
What is the compilation error, if any, for the provided Java code snippet?
java
import java.util.List;

class Overloader {
    public void add(List<String> list) {}
    public void add(List<Integer> list) {} // Line X

    public static void main(String[] args) {
        Overloader o = new Overloader();
    }
}
Q3576 easy
Which core Java interface does HashMap implement?
Q3577 medium code error
Consider the following Java code snippet. What will prevent it from compiling successfully?
java
public class ArrayMutationExample {
    public static void main(String[] args) {
        final int[] numbers = {1, 2, 3};
        numbers[0] = 10; // This is allowed as array contents are mutable
        numbers = new int[]{4, 5, 6}; // This line causes an error
    }
}
Q3578 easy code output
What is the output of this Java code snippet?
java
public class Main {
    public static void main(String[] args) {
        int num = 0;
        if (num > 0) {
            System.out.println("Positive");
        } else if (num < 0) {
            System.out.println("Negative");
        } else {
            System.out.println("Zero");
        }
    }
}
Q3579 hard code error
What is the compilation error in the `MyGenericException` class definition?
java
class MyGenericException<T> extends Exception { // This line causes the compilation error
    public MyGenericException(T data) {
        super(data.toString());
    }
}

public class Main {
    public static void main(String[] args) {
        // MyGenericException<String> e = new MyGenericException<>("Error");
    }
}
Q3580 medium code output
What will be the output when this Java program is run?
java
class Shape {
    void draw() {
        System.out.println("Drawing a generic shape");
    }
}

class Circle extends Shape {
    @Override
    void draw() {
        System.out.println("Drawing a Circle");
    }
}

class Square extends Shape {
    @Override
    void draw() {
        System.out.println("Drawing a Square");
    }
}

public class Main {
    public static void main(String[] args) {
        Shape s1 = new Circle();
        Shape s2 = new Square();
        s1.draw();
        s2.draw();
    }
}
← Prev 177178179180181 Next → Page 179 of 200 · 3994 questions