Week-01 Assignment
Week-01 Assignment
Week-01 Assignment
QUESTION 1:
What is “generic programming” in Java?
Detailed Solution:
Java Generic Programming is a programming paradigm that enables the creation of reusable code
by allowing the definition of classes and methods that can work with different types while
maintaining type safety.
____________________________________________________________________________
QUESTION 2:
Which symbol is used to denote a generic type in Java?
a. *
b. &
c. #
d. <>
Correct Answer: d
Detailed Solution:
In Java, the diamond operator (<>) is used to denote a generic type. It is typically used when
declaring and instantiating generic classes or when invoking generic methods.
___________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
Consider the following piece of code.
Correct Answer: c
Detailed Solution:
printArray() is a generic function which an array of any data type as input and prints it.
___________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 4:
What will be the output of the following code snippet?
a. Compile-time error
b. 9 5 47
c. Run-time error
d. 9547
Correct Answer: d
Detailed Solution:
This is an example of varargs methods using Ellipsis. Therefore, the output will be 9 5 4 7 as the
method can have zero or more parameters.
_________________________________________________________
QUESTION 5:
Which of these is wildcard symbol?
a. ?
b. !
c. &
d. %
Correct Answer: a
Detailed Solution:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
The question mark symbol (?) is known as the wildcard in generic programming in Java
________________________________________________________________________
QUESTION 6:
Which of the following keywords is used to declare an upper bounded wildcard?
a. bound
b. extends
c. implement
d. super
Correct Answer: b
Detailed Solution:
To declare an upper-bounded wildcard, use the wildcard character ?, followed by the extends
keyword, followed by its upper bound class name.
___________________________________________________________________________
QUESTION 7:
Can primitive types be used as type arguments in Java generics?
Correct Answer: c
Detailed Solution:
Java generics only work with reference types, so if you want to use a primitive type as a type
argument, it needs to be wrapped in its corresponding wrapper class. For example, "Integer" for
int, "Double" for double, etc.
___________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 8:
What is type erasure in Java generics?
a. The process of automatically converting generic types to their raw types during
compilation.
b. The ability to perform runtime type checking of generic types.
c. The process of inferring type arguments based on method calls.
d. The feature that allows dynamic dispatch of the generic method.
Correct Answer: a
Detailed Solution:
Type erasure in Java generics refers to the process in which the compiler replaces the type
parameters and uses the raw types during compilation. This is done to ensure backward
compatibility with older versions of Java that did not support generics.
___________________________________________________________________________
QUESTION 9:
Which of the following statement(s) is(are) true with respect to the use of generics?
a. The Java compiler enforces tighter type checks on generic code at compile time.
b. Generics support programming types as parameters.
c. Generics enable you to implement generic algorithms.
d. In case of a generic method, the same code can work for different types of data.
Correct Answer: a, b, c, d
Detailed Solution:
● The Java compiler enforces tighter type checks on generic code at compile time.
● Generics support programming types as parameters.
● Generics enable you to implement generic algorithms.
● In case of generic method, same code can work for the different type of data.
__________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 10:
class MyGenericClass<T extends Number> {
private T data;
public MyGenericClass(T data) {
this.data = data;
}
public double square() {
return data.doubleValue() * data.doubleValue();
}
}
Correct Answer: c
Detailed Solution:
In the given code snippet, MyGenericClass is a generic class that has a type parameter T that
extends the Number class. It means that the type argument passed while creating an instance of
MyGenericClass must be a subclass of Number.
Therefore, option c) is the correct answer as the code will not compile due to a type mismatch error
when creating MyGenericClass<String> stringObj.
************END************