☕ Java MCQ Questions – Page 158

Questions 3141–3160 of 3994 total — Java interview practice

▶ Practice All Java Questions
Q3141 hard
How do bounded wildcards (`? extends T` and `? super T`) specifically facilitate or restrict polymorphism when working with Java Generics?
Q3142 easy code output
What is the output of this code?
java
class Item {
    static void getType() {
        System.out.println("Generic Item");
    }
}

class Book extends Item {
    static void getType() {
        System.out.println("Book Item");
    }
}

public class Main {
    public static void main(String[] args) {
        Item myItem = new Book();
        myItem.getType();
    }
}
Q3143 medium
How must a `final` instance variable of a class be initialized in Java?
Q3144 easy code error
What exception will be thrown when executing the following Java code?
java
import java.util.HashSet;
import java.util.Set;

public class MyClass {
    public static void main(String[] args) {
        Set<String> words = new HashSet<>();
        words.add("Hello");
        words.add(null);
        words.add("World");
        
        for (String word : words) {
            if (word != null && word.equals("Hello")) {
                // Do nothing
            } else if (word != null && word.equals("World")) {
                // Do nothing
            } else {
                System.out.println(word.toUpperCase()); // This line causes an error when word is null
            }
        }
    }
}
Q3145 medium
Why does Java not support multiple inheritance of implementation for classes?
Q3146 medium code error
What is the outcome when this Java code is executed?
java
import java.util.LinkedList;

public class Test {
    public static void main(String[] args) {
        LinkedList<Character> chars = new LinkedList<>();
        chars.add('A');
        chars.add(0, 'B');
        chars.add(3, 'C');
        System.out.println(chars);
    }
}
Q3147 medium
A class `Configuration` has a field `String settings;` (without any explicit access modifier). This field is accessible by:
Q3148 hard code error
What compile-time error will this Java code produce?
java
public class OperatorError9 {
    public static void main(String[] args) {
        double d = 10.5;
        double result = ~d; // Error line
        System.out.println(result);
    }
}
Q3149 easy
Can an abstract class implement an interface in Java without implementing all of its methods?
Q3150 medium code error
What will be printed to the console when the following Java code is executed?
java
import java.io.BufferedReader;
import java.io.Reader;
import java.io.IOException;

public class TestClass {
    public static void main(String[] args) {
        Reader r = null;
        try (BufferedReader br = new BufferedReader(r)) {
            String line = br.readLine();
            System.out.println(line);
        } catch (IOException e) {
            System.err.println("IO Error: " + e.getMessage());
        } catch (NullPointerException e) {
            System.err.println("NPE Caught!");
        }
    }
}
Q3151 easy code output
What does this code print?
java
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        TreeSet<Integer> ts = new TreeSet<>();
        ts.add(100);
        ts.add(50);
        ts.add(200);
        System.out.println(ts.first() + " " + ts.last());
    }
}
Q3152 easy
Which interface in the Java Collections Framework represents a Queue?
Q3153 easy code error
What is the error in the following Java code?
java
import java.util.TreeSet;

class MyObject {
    int id;
    public MyObject(int id) { this.id = id; }
}

public class Main {
    public static void main(String[] args) {
        TreeSet<MyObject> mySet = new TreeSet<>();
        mySet.add(new MyObject(1));
        mySet.add(new MyObject(2));
    }
}
Q3154 medium code output
What is the output of this code?
java
import java.util.LinkedList;
import java.util.Queue;
import java.util.NoSuchElementException;

public class QueueErrorHandling {
    public static void main(String[] args) {
        Queue<Integer> queue = new LinkedList<>();
        queue.offer(10);
        queue.poll();
        try {
            System.out.println(queue.remove());
        } catch (NoSuchElementException e) {
            System.out.println("Queue Empty");
        }
    }
}
Q3155 easy code error
What is the error in this Java code?
java
import java.util.HashMap;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        Map<String, Integer> myMap = new HashMap<>();
        myMap.put(101, 200);
        System.out.println(myMap.get(101));
    }
}
Q3156 hard code output
What does this program print?
java
interface MyInterface {
    default void greet() { System.out.println("Hello from MyInterface"); }
}
class ParentClass {
    public void greet() { System.out.println("Hello from ParentClass"); }
}
class MyClass extends ParentClass implements MyInterface {
    // No explicit override
}
public class Main {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        obj.greet();
    }
}
Q3157 medium
What happens if a thread calls `object.wait()` without owning the monitor (lock) of `object`?
Q3158 easy
If a non-static method `myMethod()` in an object `obj` is declared `synchronized`, which lock is acquired when a thread calls `obj.myMethod()`?
Q3159 hard code error
What compilation error will occur when compiling this Java code?
java
public class ParallelLabelBreak {
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            if (i == 1) {
                break myLabel;
            }
            System.out.println("Inner loop i: " + i);
        }

        myLabel: { 
            System.out.println("This is myLabel block");
        }
    }
}
Q3160 hard code output
What is the output of this code?
java
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;

public class Test {
    public static void main(String[] args) throws IOException {
        StringWriter sw = new StringWriter();
        BufferedWriter bw = new BufferedWriter(sw); // Default buffer size is 8192 chars
        String longString = "A".repeat(8191);
        bw.write(longString);
        bw.flush();
        bw.write('B');
        bw.close();
        System.out.println(sw.toString().length());
    }
}
← Prev 156157158159160 Next → Page 158 of 200 · 3994 questions