☕ Java MCQ Questions – Page 110

Questions 2181–2200 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q2181 easy code error
What is the problem with this Java code during compilation?
java
class Animal {
    private String species = "Mammal";
}

class Dog extends Animal {
    public void printSpecies() {
        System.out.println("Species: " + species);
    }
}

public class Zoo {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.printSpecies();
    }
}
Q2182 medium
When an unlabeled `continue` statement is executed within a `for` loop, what is the next step in the loop's execution sequence?
Q2183 medium
What is "type erasure" in the context of Java Generics?
Q2184 medium
Which statement correctly describes the primary difference between `getAbsolutePath()` and `getCanonicalPath()` methods of the `File` class?
Q2185 hard code output
What is the output of this Java code snippet?
java
import java.io.File;
import java.io.IOException;

public class EmptyPathComparison {
    public static void main(String[] args) throws IOException {
        File dotFile = new File(".");
        File emptyPathFile = new File("");

        System.out.println("dotFile exists: " + dotFile.exists());
        System.out.println("emptyPathFile exists: " + emptyPathFile.exists());
        System.out.println("dotFile isDirectory: " + dotFile.isDirectory());
        System.out.println("emptyPathFile isDirectory: " + emptyPathFile.isDirectory());
        System.out.println("Canonical paths equal: " + dotFile.getCanonicalPath().equals(emptyPathFile.getCanonicalPath()));
    }
}
Q2186 medium
How do interfaces contribute to polymorphism in Java?
Q2187 easy code output
What does this Java code print?
java
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        TreeMap<Integer, String> treeMap = new TreeMap<>();
        treeMap.put(5, "Apple");
        treeMap.put(2, "Banana");
        treeMap.put(8, "Cherry");
        for (Integer key : treeMap.keySet()) {
            System.out.print(key + " ");
        }
    }
}
Q2188 easy
If you need to build a string by appending many small pieces in a single-threaded environment, which class would generally offer better performance than `StringBuffer`?
Q2189 hard code output
What is the output of this code?
java
public class StringFormatTest {
    public static void main(String[] args) {
        String message = String.format("Values: %2$d, %1$s.", "Hello", 123);
        System.out.println(message);
    }
}
Q2190 hard
Consider a custom class `MyKey` used as a `HashMap` key. If `MyKey` overrides `equals()` but *not* `hashCode()`, what is the primary consequence when attempting to retrieve an entry?
Q2191 easy code output
What is the output of this Java code?
java
import java.util.LinkedList;

public class MyClass {
    public static void main(String[] args) {
        LinkedList<String> colors = new LinkedList<>();
        colors.add("Red");
        colors.add("Green");
        colors.add("Blue");
        colors.removeFirst();
        colors.removeLast();
        System.out.println(colors);
    }
}
Q2192 easy code output
What is printed to the console when this code is executed?
java
import java.io.*;

class NonSerializableClass { 
    private String message;
    public NonSerializableClass(String message) {
        this.message = message;
    }
    public String getMessage() {
        return message;
    }
}

public class Main {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        NonSerializableClass obj = new NonSerializableClass("Hello");
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
             ObjectOutputStream oos = new ObjectOutputStream(bos)) {
            oos.writeObject(obj);
        } catch (IOException e) {
            System.out.println(e.getClass().getName());
        }
    }
}
Q2193 easy code error
What is the compilation error in the `Main` class?
java
class Calculator {
    int add(int a, int b) {
        return a + b;
    }
}

public class Main {
    public static void main(String[] args) {
        int result = Calculator.add(5, 3);
        System.out.println(result);
    }
}
Q2194 easy code output
What is the output of this code?
java
public class Test {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40};
        System.out.println(numbers[2]);
    }
}
Q2195 easy code output
What does this code print?
java
class MyCustomRuntimeException extends RuntimeException {
    public MyCustomRuntimeException(String message) {
        super(message);
    }
}

public class Main {
    public static void main(String[] args) {
        // No try-catch block here
        throw new MyCustomRuntimeException("This is an unchecked error!");
    }
}
Q2196 medium code error
What is the compilation error in the provided Java code?
java
interface Logger {
    default void log(String message) {
        System.out.println("Default Log: " + message);
    }
}

public class ConsoleLogger implements Logger {
    protected void log(String message) { // Changing visibility
        System.out.println("Console Log: " + message);
    }
}
Q2197 easy code error
What is the error in this code?
java
import java.util.Comparator;
import java.util.TreeMap;

public class Test {
    public static void main(String[] args) {
        Comparator<String> customComparator = (s1, s2) -> s1.compareTo(s2);
        TreeMap<String, String> map = new TreeMap<>(customComparator);
        map.put("key1", "value1");
        map.put(null, "value2"); // Inserting a null key
        System.out.println(map.size());
    }
}
Q2198 easy code output
What does this Java code print?
java
class Book {
    String title;
    Book(String t) {
        title = t;
    }
    void displayTitle() {
        System.out.println("Title: " + title);
    }
}
public class Main {
    public static void main(String[] args) {
        Book myBook = new Book("Java Basics");
        myBook.displayTitle();
    }
}
Q2199 easy
Which of the following is generally true about the memory footprint of `LinkedList` compared to `ArrayList` for the same number of elements?
Q2200 medium code error
What error will occur when compiling this Java code?
java
public class ConditionCheck {
    public static void main(String[] args) {
        int flag = 1;
        if (flag) {
            System.out.println("Flag is true");
        } else {
            System.out.println("Flag is false");
        }
    }
}
← Prev 108109110111112 Next → Page 110 of 200 · 3994 questions