May 2025 Flashcards
(128 cards)
Is public ListInteger> getList() a valid override of public List? extends Number> getList()?
Yes —
List<Integer> matches List<? extends Number>because Integer extends Number.
Is public ListObject> getList() a valid override of public List? extends Number> =-?9 |
No — Object is not a subtype of Number, so
List<Object>does not match
List<? extends Number>.
Can public ArrayListDouble> getList() override public List? extends Number> getList()?
Yes — Double extends Number, and ArrayList<Double> is a subtype of List<? extends Number>.</Double>
Is public ListObject> getList() a valid override of public List? super Integer> getList()?
Yes — because List<object> is a valid instantiation of List<? super Integer>.</object>
Can public ListNumber> getList() override public List? super Integer> getList()?
Yes — Number is a supertype of Integer, so List<Number> matches List<? super Integer>.</Number>
Is public ListDouble> getList() a valid override of public List? super Integer> getList()?
No — Double is not a supertype of Integer; it’s a sibling in the type hierarchy, so List<Double> does not match List<? super Integer>.</Double>
Can a char be assigned a byte or short without a cast?
No. Even though byte (8-bit) and short (16-bit signed) are smaller than char (16-bit unsigned), a cast is required because Java doesn’t allow implicit conversion from signed to unsigned types.
Can a char be assigned an int value directly without a cast?
No. int (32-bit) is wider than char (16-bit), so an explicit cast is required to assign an int to a char.
Can a char be assigned a character literal like ‘A’ or a numeric constant within the char range (e.g. 65)?
Yes. If the value is a compile-time constant within the range 0 to 65535, it can be assigned to a char without a cast.
Which of the following lambda expressions correctly implements Runnable?
A) () -> System.out.println("Running") B) () -> "Done"
A: A. Runnable has a void run() method, so the lambda must not return a value. B returns “Done” and is invalid.
Which functional interface is most appropriate for a method like int operate(int a, int b)?
A) BiFunctionInteger, Integer, Integer>
B) BinaryOperatorInteger>
A: B. While both work, BinaryOperator<T> is more specific for operations with two same-type inputs and one same-type output.</T>
Can the following interface be used as a functional interface with a lambda?
interface MyFunc {
default void help() {}
int compute(int x);
}
A: Yes. It has only one abstract method (compute), so it’s a valid functional interface despite having a default method.
What is the diamond problem in the context of Java inheritance?
It occurs when a class inherits from two classes that both inherit from a common superclass, potentially leading to ambiguity in method resolution. Java avoids this by not allowing multiple inheritance of classes.
How does Java prevent the diamond problem with classes?
Java does not allow a class to inherit from more than one class. It only allows multiple inheritance through interfaces, which do not carry implementation by default.
Can the diamond problem occur with interfaces in Java 8+?
Yes, if two interfaces provide the same default method, the implementing class must override it to resolve ambiguity.
What is the difference between multiple inheritance of type and multiple implementation inheritance in Java?
Multiple inheritance of type means a class can implement multiple interfaces (method signatures only). Multiple implementation inheritance (inheriting code from multiple classes) is not allowed in Java.
What type of function does Stream.flatMap() require as an argument?
A function that maps each element to a Stream, i.e., Function<? super T, ? extends Stream<? extends R».
What is the main purpose of the flatMap() method in Java Streams?
To apply a one-to-many transformation and flatten the resulting nested streams into a single stream.
What happens if you pass an undefined variable like bs to flatMap()?
It causes a compilation error, because flatMap() expects a valid mapping function.
What does the mark(int readAheadLimit) method do in BufferedReader, and what are its constraints?
The mark(int readAheadLimit) method records the current position in the stream. A subsequent call to reset() will attempt to reposition the stream to this point.
* readAheadLimit defines how many characters can be read before the mark becomes invalid.
* If readAheadLimit < 0, it throws IllegalArgumentException.
* A large limit may cause the buffer to grow.
What is variable shadowing in Java?
Variable shadowing occurs when a local variable or parameter has the same name as an instance variable, making the instance variable inaccessible without using this.
How do you access an instance variable that’s being shadowed by a parameter in a method or constructor?
Use this.variableName to refer explicitly to the instance variable.
Can a Java record extend another class? Why or why not?
No, a Java record cannot extend another class because it implicitly extends java.lang.Record and Java does not support multiple class inheritance.
Can a Java record implement interfaces?
Yes, a record can implement interfaces.