iGET

Java Programming - MCQ Practice Questions

Java interviews and campus tests lean heavily on object oriented behaviour rather than API recall. This set covers inheritance and polymorphism, interfaces and abstract classes, the collections framework, exception handling, multithreading, string handling, and JVM basics such as garbage collection. Output based questions are included because they expose the gaps that theory questions hide.

951 questions | 100% Free

Q.21Medium

Which statement correctly defines a generic class with multiple type parameters and bounds?

Q.22Medium

Consider: public static <T> T findMax(T a, T b) { return a; }. What is the issue with calling findMax(5, 3.5)?

Q.23Medium

What does List<?> represent in Java generics?

Q.24Medium

Which of the following will NOT compile?

Q.25Medium

Consider a generic interface: interface Comparable<T> { int compareTo(T obj); }. How should a class implement this for String comparison?

Q.26Medium

What happens when you use raw type List instead of List<String>?

Q.27Medium

What is the output?

List<String> strings = new ArrayList<String>();
List list = strings;
list.add(123);
String str = strings.get(0);
System.out.println(str);

Q.28Medium

How would you define a generic method that works with a pair of objects and returns the first one?

Q.29Medium

Consider the following code snippet:
public <T> T processData(List<? extends T> list) { return list.get(0); }

What is the relationship between the wildcard and type parameter T?

Q.30Medium

Which of the following will cause a compile-time error?

Q.31Medium

What does the following generic method signature indicate?
public static <T extends Comparable<T>> T findMax(T[] array)

Q.32Medium

Given: Map<String, ? extends List<?>> map = new HashMap<>();
Which operation is valid?

Q.33Medium

What is the output of this code?
List<String> strings = new ArrayList<>();
List raw = strings;
raw.add(123);
String str = strings.get(0);

Q.34Medium

Consider:
public class Pair<T, U> { }
Which declaration is invalid?

Q.35Medium

What is the correct way to declare a generic method that accepts a list and returns the maximum element?

Q.36Medium

Given the declaration: List<List<String>> listOfLists;
Which assignment is valid?

Q.37Medium

What happens when you try to cast a generic object?
List<String> strings = (List<String>) getSomeList();

Q.38Medium

In the context of generics, what does 'invariance' mean?

Q.39Medium

What does the following generic declaration mean?
public <T extends Number & Comparable<T>> T findMax(T a, T b)

Q.40Medium

Consider the code:
java
List<Number> list = new ArrayList<Integer>();

Will this compile?