iGET

Java Programming - MCQ Practice Questions

Practice free Java Programming multiple-choice questions with detailed answers and explanations. Perfect for competitive exam preparation.

951 questions | 100% Free

Q.701Medium

Consider the code: List<? extends Number> list = new ArrayList<>(); list.add(5); // Will this compile?

Q.702Medium

Which of the following represents a bounded type parameter?

Q.703Medium

What is the difference between List<?> and List<Object>?

Q.704Medium

Consider this generic interface: interface Comparable<T> { int compareTo(T o); } Which class definition correctly implements this?

Q.705Medium

What will be the output? List<Integer> list = new ArrayList<>(); list.add(10); Object obj = list.get(0); System.out.println(obj instanceof Integer);

Q.706Hard

Which of these correctly demonstrates the Producer Extends Consumer Super (PECS) principle?

Q.707Hard

What will happen with this code? List list = new ArrayList<String>(); list.add(123); // Adding Integer String s = (String) list.get(0);

Q.708Hard

Consider this code: public <T extends Comparable<T>> T getMax(T a, T b) { return a.compareTo(b) > 0 ? a : b; } What is the benefit of this recursive bound <T extends Comparable<T>>?

Q.709Easy

What is the output of this generic code? class Pair<K, V> { public void display(K key, V value) { System.out.println(key + ": " + value); } } Pair<String, Integer> p = new Pair<>(); p.display("Age", 25);

Q.710Hard

Which statement is true about generic inheritance?

Q.711Hard

What will be the result of this code? List<String> list = new ArrayList<>(); list.add("Hello"); List raw = list; // Unchecked assignment raw.add(123); // Adding Integer to raw type String s = list.get(1);

Q.712Medium

Which of these declarations would NOT cause a compilation warning or error?

Q.713Easy

In Java generics, what does the wildcard '?' represent?

Q.714Easy

What is the primary advantage of using generics in Java?

Q.715Medium

In the declaration 'List<? extends Number> list', what types can be added to this list at runtime?

Q.716Medium

Which statement correctly uses the lower-bounded wildcard in generics?

Q.717Medium

Which generic declaration would allow storing both String and Integer in the same collection?

Q.718Medium

What does the PECS principle stand for in generics context?

Q.719Easy

Which of the following will compile successfully?

Q.720Medium

In a generic class 'class Box<T extends Comparable<T>>', what constraint is placed on T?