You are on page 1of 6

NPTEL Online Certification Courses

Indian Institute of Technology Kharagpur

DATA STRUCTURES AND ALGORITHMS USING JAVA


Assignment 3
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10× 1 = 10
______________________________________________________________________________

QUESTION 1:
Which data structure is the collection of finite, ordered, and homogeneous data elements?
a. Array
b. Graph
c. Map
d. Trees

Correct Answer: a

Detailed Solution:
An array is a finite, ordered and collection of homogeneous data elements. An array is finite
because it contains only a limited number of elements, and ordered, as all the elements are stored
one by one in contiguous locations of the computer memory in a linear ordered fashion. All the
elements of an array are of the same data type (say, integer) only and hence it is termed a collection
of homogeneous elements.
____________________________________________________________________________

QUESTION 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. a b c d e f
b. a c e
c. b d f
d. a b d f
Correct Answer: b
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

Detailed Solution:
The counter increments by 2 in every iteration. Therefore, starting from 0th index, values on 0th,
2nd, and 4th indices are printed.
___________________________________________________________________________

QUESTION 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. 0
d. Null

Correct Answer: b
Detailed Solution:

_________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 4:
Consider the program given below.

public class Main {


public static void main(String args[]) {
int [] a = new int[20];
System.out.println(a[10]);
}
}

Which of the following is an output of the above program?


a. 0
b. Compile time error
c. Runtime error
d. Garbage vale

Correct Answer: a

Detailed Solution:
Array are objects and by default object elements are initialized with 0 or null.
_________________________________________________________

QUESTION 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.
Correct Answer: b

Detailed Solution:
Option A is incorrect: ArrayList is not synchronized by default, which means it is not thread-safe.
To achieve thread safety, you can use the Collections.synchronizedList() method to create a
synchronized version of the ArrayList.
Option B is correct: ArrayList allows duplicate elements, and it maintains the order of elements as
they are inserted.
Option C is incorrect: ArrayList does automatically increase its size as elements are added, but it
does so by creating a new array and copying the elements from the old array to the new one. It
does not automatically resize its original array.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

Option D is incorrect: ArrayList can store both primitive data types and objects. However, when
storing primitives, they are automatically boxed into their corresponding wrapper classes (e.g., int
to Integer).
________________________________________________________________________

QUESTION 6:
Which of the following classes implement a dynamic array?
a. ArrayList
b. LinkedList
c. DynamicList
d. AbstractList

Correct Answer: a

Detailed Solution:
ArrayList supports dynamic arrays that can grow as needed. ArrayList class implements the
AbstractList class. LinkedList folles sequential collection different from an array which is an
indexed collection. There is no class like DynamicList.
___________________________________________________________________________

QUESTION 7:
Which method is used to remove all elements from an ArrayList?

a. removeAll()
b. clear()
c. deleteAll()
d. eraseAll()

Correct Answer: b

Detailed Solution:
The correct method to remove all elements from an ArrayList is the clear() method. It removes
all the elements from the list, leaving it empty

__________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

sQUESTION 8:
What will be the output of the following Java code?
import java.util.ArrayList;

public class ArrayListTest {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");

fruits.remove(1);
System.out.println(fruits.get(1));
}
}

Which of the following is an output of the above program?


a. "Apple"
b. "Banana"
c. "Orange"
d. IndexOutOfBoundsException will be thrown.

Correct Answer: c

Detailed Solution:
The code creates an ArrayList named fruits and adds three elements: "Apple", "Banana", and
"Orange".
fruits.remove(1); removes the element at index 1, which is "Banana".
After removal, the ArrayList contains only two elements: "Apple" at index 0 and "Orange" at index
1.
fruits.get(1) returns the element at index 1, which is "Orange".
Therefore, the output will be "Orange"
___________________________________________________________________________

QUESTION 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)

Correct Answer: a
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

Detailed Solution:
Adding an element at the end of an ArrayList takes constant time complexity (O(1)) on average.
When the ArrayList's internal array needs to be resized, a new array is created, and the elements
are copied over, which takes linear time (O(n)). However, this resizing operation happens
infrequently and does not occur for every element insertion. Thus, the average time complexity
remains O(1) for adding elements to the end.
___________________________________________________________________________

QUESTION 10:
Which of the following methods is used to find the index of the first occurrence of a specific
element in an ArrayList?
a. findIndex()
b. indexOf()
c. search()
d. findFirst()

Correct Answer: b

Detailed Solution:
The correct method to find the index of the first occurrence of a specific element in an ArrayList
is the indexOf() method.
The indexOf() method takes an object as an argument and returns the index of the first occurrence
of that object in the ArrayList. If the element is not found in the list, it returns -1.

___________________________________________________________________________

************END************

You might also like