Java Programming
Java OOP, collections, multithreading
476 Questions 10 Topics Take Test
Advertisement
Showing 81–90 of 476 questions
Q.81 Medium Lambda Expressions
What is the type of the following lambda expression: x -> x.length()?
A Function
B Consumer
C Supplier
D Predicate
Correct Answer:  A. Function
EXPLANATION

Function<T,R> takes one argument of type T and returns R. Here String -> Integer (length), so Function<String, Integer>.

Take Test
Q.82 Medium Lambda Expressions
Which of the following lambda expressions is invalid?
A () -> System.out.println("Hello")
B x -> x * 2
C (int x, int y) -> { return x + y; }
D -> x + y
Correct Answer:  D. -> x + y
EXPLANATION

Lambda must have parameter list (even if empty with ()). Missing parameter list '() -> x + y' is invalid syntax.

Take Test
Q.83 Medium Generics
In a microservices architecture, you're designing a Response wrapper class that should work with any data type, including primitives when boxed. Which implementation is correct?
A public class Response { private T data; private int statusCode; }
B public class Response { private Object data; private int statusCode; }
C public class Response { private T data; private int statusCode; }
D public class Response { private T data; private S statusCode; }
Correct Answer:  A. public class Response { private T data; private int statusCode; }
EXPLANATION

Option A provides proper generic type safety without unnecessary constraints. While Option C adds a Serializable bound (sometimes desirable), Option A is the most flexible and commonly used pattern for generic response wrappers.

Take Test
Q.84 Medium Generics
A utility class needs a method that accepts a List containing elements that are instances of a specific class or its subclasses. Which wildcard notation should be used?
A public static void process(List
B public static void process(List list)
C public static void process(List
D public static void process(List list)
Correct Answer:  A. public static void process(List
EXPLANATION

'? extends TargetClass' creates a covariant wildcard allowing lists of TargetClass or any subclass. This is ideal for reading operations when you need polymorphic list handling.

Take Test
Q.85 Medium Generics
A developer creates a generic method that accepts a collection of numbers and calculates their sum. Which type parameter declaration would be most appropriate for this scenario?
A public static double calculateSum(List numbers)
B public static double calculateSum(List numbers)
C public static double calculateSum(List numbers)
D public static double calculateSum(List numbers)
Correct Answer:  A. public static double calculateSum(List numbers)
EXPLANATION

Using 'T extends Number' establishes an upper bound, allowing the method to work with any Number subclass (Integer, Double, Float, etc.) while providing type safety and access to Number methods.

Take Test
Q.86 Medium Generics
What is the runtime class object for this generic variable?
java
List list = new ArrayList();
Class c = list.getClass();
A java.util.List
B java.util.ArrayList
C java.util.ArrayList
D java.lang.Object
Correct Answer:  C. java.util.ArrayList
EXPLANATION

Due to type erasure, getClass() returns ArrayList without generic parameters. Generic information exists only at compile time.

Take Test
Q.87 Medium Generics
Which of these is a valid generic interface implementation?
A class MyClass implements Comparable
B class MyClass implements Comparable
C class MyClass extends Comparable
D class MyClass implements Comparable
Correct Answer:  A. class MyClass implements Comparable
EXPLANATION

Implementing generic interfaces requires specifying the type parameter. 'implements Comparable<MyClass>' is the proper way to implement.

Take Test
Q.88 Medium Generics
What is the PECS principle in Java Generics?
A Public Extends Collections Structure
B Producer Extends, Consumer Super
C Parameter Extends Comparable Supports
D Polymorphic Extends Class Super
Correct Answer:  B. Producer Extends, Consumer Super
EXPLANATION

PECS (Producer Extends, Consumer Super) is a guideline: use 'extends' for producers (reading) and 'super' for consumers (writing).

Take Test
Q.89 Medium Generics
What does 'covariance' mean in the context of Java Generics?
A Types vary together in the same direction
B Types vary in opposite directions
C Types do not vary at all
D Types are interchangeable
Correct Answer:  A. Types vary together in the same direction
EXPLANATION

Covariance means if Dog is a subtype of Animal, then Producer<Dog> is a subtype of Producer<Animal>. Achieved using upper-bounded wildcards.

Take Test
Q.90 Medium Generics
Which wildcard usage follows the Producer pattern?
A List
B List
C List
D List
Correct Answer:  B. List
EXPLANATION

Producer (Get-Only pattern) uses upper-bounded wildcard (? extends T). It's used when you want to read/produce elements of specific types.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips