NPTEL Data Structure And Algorithms Using Java Week 3 Assignment Answers 2024
1. Which of the following statements about arrays in Java is true?
a. Arrays in Java are dynamic and can change their size after creation.
b. You can store different types of elements (e.g., integers and strings) in the same array in Java.
c. The default value of elements in an array of integers in Java is null.
d. The length of an array in Java can be determined using the length property.
Answer :- For Answers Click Here
2. Which of the following statements is true about the order of elements in the matrix array in Java?
a. Java stores 2D arrays in column-major order, so elements are stored column by column.
b. Java stores 2D arrays in row-major order, so elements are stored row by row.
c. Java allows you to specify whether a 2D array should be stored in row-major or column-major order.
d. Java does not support 2D arrays directly; it only supports arrays of arrays.
Answer :- For Answers Click Here
3. Consider the following program.
public class Main {
public static void main(String args[]) {
String[] names = new String[2];
names[0] = "Alice";
names[1] = "Bob";
names[2] = "Charlie";
System.out.println(names[2]);
Which of the following is an output of the above program?
a. Charlie
b. Bob
c. Alice
d. Compilation Error
Answer :-
4. Which of the following statements correctly declares an array in Java?
a. int[] numbers = new int();
b. int numbers[] = new int[10];
c. int numbers[10] = new int();
d. int numbers = new int[10];
Answer :-
5. Which of the following statements is true regarding ArrayList in Java?
a. ArrayList is a synchronized data structure, making it thread-safe.
b. ArrayList allows duplicate elements and maintains insertion order.
c. ArrayList has a fixed size that must be specified at creation and cannot be changed.
d. ArrayList can only store primitive data types like int, double, and char.
Answer :- For Answers Click Here
6. Consider a Program:
public class Test {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i <= numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
Which of the following is an output of the above program ?
a. It will print numbers from 1 to 5
b. It will print numbers from 1 to 5 and then throw an exception
c. It will compile but not run
d. It will not compile
Answer :-
7. What would be the result of the following code?
import java.util.ArrayList;
public class ArrayListQuestion {
public static void main(String[] args) {
ArrayList<String> colors1 = new ArrayList<>();
colors1.add("red");
colors1.add("green");
colors1.add("blue");
colors1.add("yellow");
ArrayList<String> colors2 = new ArrayList<>(colors1);
colors2.add("orange");
colors2.add("purple");
colors2.add("pink");
System.out.println(colors2.get(5));
}
}
a. It will print “purple”
b. It will print “pink”
c. It will print “green”
d. It will throw an exception
Answer :- For Answers Click Here
8. What will be the state of the ArrayList after executing the following code?
import java.util.ArrayList;>
public class ArrayListModification {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
fruits.add("apple");
fruits.add("banana");
fruits.add("cherry");
fruits.add("date");
fruits.add(2, "elderberry");
fruits.remove(4);
System.out.println(fruits);
}
}
a. [apple, banana, elderberry, cherry]
b. [apple, banana, elderberry, cherry, date]
c. [apple, elderberry, banana, cherry, date]
d. [apple, banana, cherry, elderberry]
Answer :-
9. In Java, what is the average time complexity of adding an element to the end of an ArrayList and what is the worst-case time complexity of adding an element to the end of an ArrayList when it requires resizing??
a. Average: O(1), Worst-case: O(n)
b. Average: O(n), Worst-case: O(n log n)
c. Average: O(log n), Worst-case: O(n)
d. Average: O(1), Worst-case: O(1)
Answer :-
10. In Java, how can you search for the index of an element in an ArrayList named items and what is the time complexity of this operation?
a. Using items.get(element) with time complexity O(1)
b. Using items.indexOf(element) with time complexity O(n)
c. Using items.binarySearch(element) with time complexity O(log n)
d. Using items.find(element) with time complexity O(n log n)
Answer :- For Answers Click Here
NPTEL Data Structure And Algorithms Using Java Assignment Solutions
NPTEL Data Structure And Algorithms Using Java Week 3 Assignment Answers 2023
1. Which data structure is the collection of finite, ordered, and homogeneous data elements?
a. Array
b. Graph
c. Мар
d. Trees
Answer :- For Answers Click Here
2. Consider the following piece of code.
public class Main {
public static void main(String args[]) {
char[] a = {'a', 'b', 'c', 'd', 'e', 'f'};
for (int i = 0; i < a.length; i = i + 2)
System.out.print(" " + a[i]);
}
}
Which of the following is an output of the above program?
a. abodef
b. ace
c. bdf
d. abdf
Answer :- For Answers Click Here
3. Consider the following program.
public class Main {
public static void main(String args[]) {
int[] a = new int[0];
a[0] = 12;
System.out.println(a);
}
}
Which of the following is an output of the above program?
a. 12
b. Exception
c. O
d. Null
Answer :-
5. Which of the following statements is true regarding ArrayList in Java?
a. ArrayList is a synchronized data structure, making it thread-safe.
b. ArrayList allows duplicate elements and maintains insertion order.
c. ArrayList is a resizable array that automatically increases its size when elements are added.
d. ArrayList can only store primitive data types like int, double, and char.
Answer :- For Answers Click Here
6. Which of the following classes implement a dynamic array?
a. ArrayList
b. LinkedList
c. DynamicList
d. AbstractList
Answer :-
7. Which method is used to remove all elements from an ArrayList?
a. removeAllO
b. clear
c. deleteAllO
d. eraseA110
Answer :-
8. What will be the output of the following Java code?
![NPTEL Data Structure And Algorithms Using Java Week 3 Assignment Answers 2023 2 image 15](https://gecmunger.in/wp-content/uploads/2023/08/image-15.png)
Which of the following is an output of the above program?
а. “Apple”
b. “Banana”
c. “Orange”
d. IndexOutOfBoundsException will be thrown.
Answer :-
9. What is the time complexity of adding an element at the end of an ArrayList?
a. O(1)
b. O(log n)
c. O(n)
d. O(n log n)
Answer :-
10. Which of the following methods is used to find the index of the first occurrence of a specific element in an ArravList?
a. findIndex
b. indexOf0
c. search
d. findFirstO
Answer :- For Answers Click Here
NPTEL Data Structure And Algorithms Using Java Week 2 Assignment Answers 2023
1. Which of the following is/are FALSE in the case of Data Structure in Java?
a. Array is an indexed linear data structure
b. linked list is a sequential non- linear data structure
C. stack and queue are Linear data structure
d. set, tree, table, and graph are Non-linear data structure
Answer :- b. linked list is a sequential non- linear data structure
2. Which of the following is True in the case of Java Collection Framework(JCF)?
a. JCF provides mid-performance software encoding
b. Extending and or adapting a collection is easy and not flexible with JCF
c. The JCF is consist of : Collections and Facilities
d. The JCF doesn’t allow different types of collections to work with a high degree of interoperability.
Answer :- c. The JCF is consist of : Collections and Facilities
3. Which among the following is a Java Collection Framework(JCF) Class?
a. Set
b. List
c. Queue
d. Vector
Answer :- d. Vector
4. Choose the right option to complete the following sentence in the case of EnumSet EnumSet extends AbstractSet and implements
a. Enum type
b. Enum
c. AbstractSet
d. Set
Answer :- d. Set
5. Which of the following statement is incorrect with respect to ‘Map’ in Java?
a. A Map is an obiect that maps keys to values
b. Map is a collection of key-value pairs.
c. Map is a generic interface that has the declaration: interface Map <I>
d. SortedMap interface doesn’t extend Map.
Answer :- d. SortedMap interface doesn't extend Map.
6. Which of the following statements is/are incorrect for Java Collection Framework (JCF)?
a. Vector is similar to. ArrayList, which represents a dynamic array
b. Stack is a subclass of Vector that implements a standard last-in. first-out stack.
c. Unlike HashMap. Hashtable doesn’t store Key/value pairs.
d. Class Properties are used to maintain a list of values in which both key and value are String.
Answer :- c. Unlike HashMap. Hashtable doesn't store Key/value pairs.
7. In Java Collection Framework(JCF) Dictionary is ___________?
a. A concrete implementation
b. An interface
c. An abstract class
d. A data structure for storing only integers
Answer :-c. An abstract class
8. Which of the following interface(s) is (are) NOT descendant(s) of java.util.Collection?
a. java.util. SortedMap
b. java.util.Set
c. java.util. SortedSet
d. java.util. Queue
Answer :- a. java.util. SortedMap
9. Which of the following declaration(s) is(are) not correct to instantiate a collection object?
a. List <data-type list1 = new ArrayList():
b. List <data-type list2 = new LinkedList();
c. List <data-type list3 = new Vector():
d. List <data-type list4 = new PriorityQueue() :
Answer :- d. List <data-type list4 = new PriorityQueue :
10. Which of these interface handle sequences?
a. Set
b. Array
c. List
d. Collection
Answer :- c. List
NPTEL Data Structure And Algorithms Using Java Week 1 Assignment Answers 2023
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.
Answer :- a. A programming paradigm that focuses on reusability and type safety. "Generic programming" in Java refers to the use of generics, a programming feature introduced in Java 5 (J2SE 5.0). Generics allow classes, interfaces, and methods to be parameterized with types. By using generics, you can create classes and methods that can work with different types while ensuring type safety. This helps to improve code reusability and maintainability by writing more generic and flexible components.
2. Which symbol is used to denote a generic type in Java?
a. *
b. &
c. #
d. <>
Answer :- d. <> The symbol used to denote a generic type in Java is <>. It is referred to as the diamond operator or angle brackets. Generics in Java allow you to create classes, interfaces, and methods that can work with different types while providing type safety. The diamond operator is used when defining a generic class or when instantiating an object of a generic class to specify the type parameter.
3. Consider the following piece of code.
![NPTEL Data Structure And Algorithms Using Java Week 3 Assignment Answers 2023 3 image 8](https://gecmunger.in/wp-content/uploads/2023/07/image-8.png)
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.
Answer :- c. It will produce output: 5 4 3 2 1 The code is correct syntactically and will execute without any compilation errors or runtime errors. It starts the loop with i initialized to 5, and then decrements i in each iteration until it becomes 0 (excluding 0). Therefore, it will print the numbers 5, 4, 3, 2, and 1, each on a separate line.
4. What will be the output of the following code snippet?
![NPTEL Data Structure And Algorithms Using Java Week 3 Assignment Answers 2023 4 image 9](https://gecmunger.in/wp-content/uploads/2023/07/image-9.png)
a. Compile-time eifor
b. 9 5 47
c. Run-time error
d. 9 5 4 7
Answer :- d. 9 5 4 7
5.Which of these is wildcard symbol?
a. ?
b. !
C. &
d. %
Answer :- a. Compile-time eifor The option "Compile-time eifor" suggests that there is a typo in the code, and there may be a misspelling of the for keyword. If the code snippet contains "eifor" instead of "for," it will result in a compilation error.
6. Which of the following keywords is used to declare an upper bounded wildcard?
a. bound
b. extends
c. implement
d. super
Answer :- b. extends In Java, the keyword "extends" is used to declare an upper bounded wildcard when working with generics. An upper bounded wildcard restricts the type that can be used as a parameter to a specific class or its subclasses.
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.
Answer :- c. Yes, but they need to be wrapped in their corresponding wrapper classes.
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.
Answer :- a. The process of automatically converting generic types to their raw types during compilation. Type erasure in Java generics is the process by which the compiler removes or "erases" the type parameters used in generic code during compilation. This means that the generic type information is not retained at runtime. Instead, the generic types are replaced with their raw types (non-generic types), which are used for compatibility with pre-generic Java code.
9. Which of the following statements) 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.
Answer :- 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 the case of a generic method, the same code can work for different types of data.
10.
![NPTEL Data Structure And Algorithms Using Java Week 3 Assignment Answers 2023 5 image 10](https://gecmunger.in/wp-content/uploads/2023/07/image-10.png)
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.
Answer :- c. The code will not compile due to a type mismatch error