Generic Methods And Generic Classes Java Mcq

This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Generics”. What is the output of this program? Import java.util.; public class genericstack. Overloaded Methods b) Generic methods c) Class methods d) Overriding methods View Answer. Java Generics Multiple Type Parameters - Learn Java Generics in simple and easy steps starting from basic to advanced concepts with examples including Overview, Environment Setup, Generic Classes, Type Parameter Naming Conventions, Type inference, Multiple Type Parameters, Parameterized Types, Raw Types, Generic Methods, Bounded Type Parameters, Multiple Bounds, Inbuilt Classes, Generic. “Java Generics are a language feature that allows for definition and use of generic types and methods.” Generic types are instantiated to form parameterized types by providing actual type arguments that replace the formal type parameters. A class like LinkedList is a generic type, that has a type parameter E.

  1. Generic Methods And Generic Classes Java Mcquade

Generics in Java is similar to. The idea is to allow type (Integer, String, etc and user defined types) to be a parameter to methods, classes and interfaces. For example, classes like HashSet, ArrayList, HashMap, etc use generics very well.

We can use them for any type.Generic ClassLike C, we use to specify parameter types in generic class creation. To create objects of generic class, we use following syntax. // To create an instance of generic classBaseType obj = new BaseType Note: In Parameter type we can not use primitives like'int','char' or 'double'. FilternoneOutput: java.lang.Integer = 11java.lang.String = GeeksForGeeksjava.lang.Double = 1.0Advantages of Generics:Programs that uses Generics has got many benefits over non-generic code. Code Reuse: We can write a method/class/interface once and use for any type we want. Type Safety: Generics make errors to appear compile time than at run time (It’s always better to know problems in your code at compile time rather than making your code fail at run time).

Suppose you want to create an ArrayList that store name of students and if by mistake programmer adds an integer object instead of string, compiler allows it. But, when we retrieve this data from ArrayList, it causes problems at runtime.

Filternone. Implementing generic algorithms: By using generics, we can implement algorithms that work on different types of objects and at the same they are type safe too.References:This article is contributed by Dharmesh Singh.

If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Generic Methods And Generic Classes Java Mcquade

Collection Java - MCQ set 1 (25 questions, 55 marks)Q1 - Q5, 5 EASY level difficulty questions 1 mark each. 1. 5 = 5 marks. Which of these is not a interface in the Collections Framework?a.

List. Which interface restricts duplicate elements?a. (All of these). Which of these collection class has the ability to grow dynamically?a. (None of these). The accuracy and efficiency of a HashMap can be guaranteed with:a. Override equals methodb.

Java method generic type

Override hashCode methodc. (None of these)d. (All of these).

Also be respectful and maintain reddiquette with regards to voting. For example no memes or reaction.gifs. No selling of game copy's or anything money related. Warhammer 40k soulstorm mods. Only link your own content if you're a participating member of the community.

A HashMap allows the existence of:a. Null valuesb.

One null keyc. (None of these)d. (All of these)Q6 - Q15, 10 MEDIUM level difficulty questions 2 mark each. 2.

10 = 20 marks. What implementation of Iterator can traverse a collection in both directions?a. MapIterator. The Comparable interface contains which called?a.

CompareWith. Which is faster and uses less memory?a. ListIterator. What Iterator can throw a ConcurrentModificationException?a. Fail-fast Iteratorsb. Fail-safe Iteratorsc.

(All of these)d. (None of these). What is the default number of Partitions/segments in Concurrent Hash Map?a. 16. Which is best suited to a multi-threaded environment?a. ConcurrentHashMap.

The default capacity of a Vector is:a. 16. Which does NOT implement the Collection interface?a. (None of these). The default capacity of a ArrayList is:a.

10. Which provides better performance for the insertion and removal from the middle of the list?a. (All of these)Q16 - Q25, 10 HARD level difficulty questions 3 mark each. 3. 10 = 30 marks. What will be output of given code -a.

Compile time exceptionb. 2 3 1. What will be output of given code -a. 1 followed by exceptionc. Compile time exceptiond. Runtime Exception - ClassCastException. What will be output of given code -a.

Afollowed by ConcurrentModificationExceptionb. A c. An unordered array has a search time complexity of:a. O(1). The add and remove methods of TreeSet have a time complexity of:a.

O(log n). After resizing, size of ArrayList is increased by:a. (None of these). After resizing, size of Vector is increased by:a. (None of these). What will be output of given code -a.

Will execute without any exceptionb. (None of these)c. Compilation errord. Throw ConcurrentModificationException.

Deque and Queue are derived from:a. List. What will be output of given code -a. name=ankit, name=bradb. name=brad, name=ankitc.

Runtime Exceptiond. Compile time exceptionQuiz 1 - Correct answersEASY1) b2) a3) c4) d5) dMEDIUM6) b7) c8) c9) a10) d11) d12) a13) b14) d15) cHARD16) b17) d18) a19) b20) d21) b22) b23) d24) b25) a.

Posted on