Week-01 Assignment (1) PDF, NPTEL, Data Structures and Algorithms
Document Details
Indian Institute of Technology, Kharagpur
NPTEL
Tags
Summary
This NPTEL assignment covers data structures and algorithms using Java. It includes multiple-choice questions (MCQs) on topics such as generic programming, type erasure, and wildcards.
Full Transcript
NPTEL Online Certification Courses Indian Institute of Technology Kharagpur DATA STRUCTURES AND ALGORITHMS USING JAVA Assignment 1 TYPE OF QUESTION: MCQ Number of questions: 10...
NPTEL Online Certification Courses Indian Institute of Technology Kharagpur DATA STRUCTURES AND ALGORITHMS USING JAVA Assignment 1 TYPE OF QUESTION: MCQ Number of questions: 10 Total marks: 10× 1 = 10 ______________________________________________________________________________ QUESTION 1: What is “generic programming” in Java? a. A programming paradigm that focuses on reusability and type safety. b. A technique to optimize code execution in Java programs. c. A feature to dynamically modify variable types at runtime. d. A method to generate random data in Java programs. Correct Answer: a 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. public class Test { public static < T > void printArray( T[] input ) { for(T t : input) { System.out.printf("%s ", t); } } public static void main(String args[]) { Integer[] A ={ 5, 4, 3, 2, 1} ; printArray(A); } } Which of the following is true? a. The code suffers from syntax errors. b. Program will give a compile-time error. c. It will produce output: 5 4 3 2 1 d. Program will yield a run-time error. 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? public class DemoClass { static void DemoMethod(int...v) { for (int i: v) System.out.print(i + " "); } public static void main(String args[]) { DemoMethod(9, 5, 4); DemoMethod(7); } } 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. methodUBA(? extends A) { … } ___________________________________________________________________________ QUESTION 7: Can primitive types be used as type arguments in Java generics? a. Yes, any primitive type can be used. b. No, only reference types are allowed as type arguments. c. Yes, but they need to be wrapped in their corresponding wrapper classes. d. No, primitive types are not compatible with 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: You should use generics because: 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 { private T data; public MyGenericClass(T data) { this.data = data; } public double square() { return data.doubleValue() * data.doubleValue(); } } MyGenericClass integerObj = new MyGenericClass(5); MyGenericClass stringObj = new MyGenericClass("Hello"); double result1 = integerObj.square(); double result2 = stringObj.square(); Which of the following statements about the code snippet is correct? a. The code will compile and execute without any error. b. code will compile but throw a runtime exception. c. The code will not compile due to a type mismatch error d. The code will not compile due to a missing implementation error. 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. In the code, MyGenericClass integerObj = new MyGenericClass(5); creates an instance of MyGenericClass with the type argument as Integer. Since Integer is a subclass of Number, this line of code will compile without any error. However, MyGenericClass stringObj = new MyGenericClass("Hello"); creates an instance of MyGenericClass with the type argument as String. Since String is not a subclass of Number, this line of code will cause a compilation error. The compiler will detect a type mismatch error and prevent the code from compiling. NPTEL Online Certification Courses Indian Institute of Technology Kharagpur Therefore, option c) is the correct answer as the code will not compile due to a type mismatch error when creating MyGenericClass stringObj. ************END************