











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












1. Which of these best describes an array? a) A data structure that shows a hierarchical behaviour b) Container of objects of similar types c) Arrays are immutable once initialised d) Array is not a data structure Answer: b Explanation: Array contains elements only of the same type. 2. How do you initialize an array in C? a) int arr[3] = (1,2,3); b) int arr(3) = {1,2,3}; c) int arr[3] = {1,2,3}; d) int arr(3) = (1,2,3); Answer: c Explanation: This is the syntax to initialize an array in C. 3. How do you instantiate an array in Java? a) int arr[] = new int(3); b) int arr[]; c) int arr[] = new int[3]; d) int arr() = new int(3); Answer: c Explanation: Note that option b is declaration whereas option c is to instantiate an array. 4. Which of the following is a correct way to declare a multidimensional array in Java? a) int[] arr; b) int arr[[]]; c) int[][]arr; d) int[[]] arr; Answer: c Explanation: The syntax to declare multidimensional array in java is either int[][] arr; or int arr[][]; 5. What is the output of the following piece of code?
public class array { public static void main(String args[]) { int []arr = {1,2,3,4,5}; System.out.println(arr[2]); System.out.println(arr[4]); } }
a) 3 and 5 b) 5 and 3 c) 2 and 4 d) 4 and 2
Answer: a Explanation: Array indexing starts from 0.
6. What is the output of the following piece of code?
public class array { public static void main(String args[]) { int []arr = {1,2,3,4,5}; System.out.println(arr[5]); } }
a) 4 b) 5 c) ArrayIndexOutOfBoundsException d) InavlidInputException Answer: c Explanation: Trying to access an element beyond the limits of an array gives ArrayIndexOutOfBoundsException.
7. When does the ArrayIndexOutOfBoundsException occur? a) Compile-time b) Run-time c) Not an error d) Not an exception at all Answer: b Explanation: ArrayIndexOutOfBoundsException is a run-time exception and the compilation is error-free. 8. Which of the following concepts make extensive use of arrays? a) Binary trees b) Scheduling of processes c) Caching d) Spatial locality Answer: d Explanation: Whenever a particular memory location is referred, it is likely that the locations nearby are also referred, arrays are stored as contigous blocks in memory, so if you want to access array elements, spatial locality makes it to access quickly. 9. What are the advantages of arrays? a) Objects of mixed data types can be stored b) Elements in an array cannot be sorted c) Index of first element of an array is 1 d) Easier to store elements of same data type
15. Process of removing an element from stack is called __________ a) Create b) Push c) Evaluation d) Pop Answer: d Explanation: Elements in stack are removed using pop operation. Pop operation removes the top most element in the stack i.e. last entered element. 16. In a stack, if a user tries to remove an element from empty stack it is called _________ a) Underflow b) Empty collection c) Overflow d) Garbage Collection Answer: a Explanation: Underflow occurs when the user performs a pop operation on an empty stack. Overflow occurs when the stack is full and the user performs a push operation. Garbage Collection is used to recover the memory occupied by objects that are no longer used. 17. Pushing an element into stack already having five elements and stack size of 5, then stack becomes a) Overflow b) Crash c) Underflow d) User flow Answer: a Explanation: The stack is filled with 5 elements and pushing one more element causes a stack overflow. This results in overwriting memory, code and loss of unsaved work on the computer.
18. Entries in a stack are “ordered”. What is the meaning of this statement? a) A collection of stacks is sortable b) Stack entries may be compared with the ‘<‘ operation c) The entries are stored in a linked list d) There is a Sequential entry that is one by one Answer: d Explanation: In stack data structure, elements are added one by one using push operation. Stack follows LIFO Principle i.e. Last In First Out(LIFO).
19. Which of the following applications may use a stack? a) A parentheses balancing program b) Tracking of local variables at run time c) Compiler Syntax Analyzer d) Data Transfer between two asynchronous process Answer: d Explanation: Data transfer between the two asynchronous process uses the queue data structure for synchronisation. The rest are all stack applications.
20. Consider the usual algorithm for determining whether a sequence of parentheses is balanced. The maximum number of parentheses that appear on the stack AT ANY ONE TIME when the algorithm analyzes: (()(())(())) are: a) 1 b) 2 c) 3 d) 4 or more Answer: c Explanation: In the entire parenthesis balancing method when the incoming token is a left parenthesis it is pushed into stack. A right parenthesis makes pop operation to delete the elements in stack till we get left parenthesis as top most element. 3 elements are there in stack before right parentheses comes. Therefore, maximum number of elements in stack at run time is
21. Consider the usual algorithm for determining whether a sequence of parentheses is balanced. Suppose that you run the algorithm on a sequence that contains 2 left parentheses and 3 right parentheses (in some order). The maximum number of parentheses that appear on the stack AT ANY ONE TIME during the computation? a) 1 b) 2 c) 3 d) 4 or more Answer: b Explanation: In the entire parenthesis balancing method when the incoming token is a left parenthesis it is pushed into stack. A right parenthesis makes pop operation to delete the elements in stack till we get left parenthesis as top most element. 2 left parenthesis are pushed whereas one right parenthesis removes one of left parenthesis. 2 elements are there before right parenthesis which is the maximum number of elements in stack at run time. 22. What is the value of the postfix expression 6 3 2 4 + – : a) 1 b) 40 c) 74 d) - Answer: d Explanation: Postfix Expression is (6+(3-(24))) which results -18 as output. 23. Here is an infix expression: 4 + 3(63-12). Suppose that we are using the usual stack algorithm to convert the expression from infix to postfix notation. The maximum number of symbols that will appear on the stack AT ONE TIME during the conversion of this expression?** a) 1 b) 2 c) 3 d) 4
The purpose of stack is to reverse the order of the operators in the expression. It also serves as a storage structure, as no operator can be printed until both of its operands have appeared.
28. Which data structure is used for implementing recursion? a) Queue b) Stack c) Array d) List Answer: b Explanation: Stacks are used for the implementation of Recursion. 29. Which of the following statement(s) about stack data structure is/are NOT correct? a) Linked List are used for implementing Stacks b) Top of the Stack always contain the new node c) Stack is the FIFO data structure d) Null link is present in the last node at the bottom of the stack Answer: c Explanation: Stack follows LIFO. 30. Consider the following operation performed on a stack of size 5. Push(1); Pop(); Push(2); Push(3); Pop(); Push(4); Pop(); Pop(); Push(5); After the completion of all operation, the number of elements present in stack are a) 1 b) 2 c) 3 d) 4 Answer: a Explanation: Number of elements present in stack is equal to the difference between number of push operations and number of pop operations. Number of elements is 5-4=1. 31. Which of the following is not an inherent application of stack? a) Reversing a string b) Evaluation of postfix expression c) Implementation of recursion d) Job scheduling Answer: d Explanation: Job Scheduling is not performed using stacks.
32. The type of expression in which operator succeeds its operands is? a) Infix Expression b) Prefix Expression c) Postfix Expression d) Both Prefix and Postfix Expressions Answer: c Explanation: The expression in which operator succeeds its operands is called postfix expression. The expression in which operator precedes the operands is called prefix expression. If an operator is present between two operands, then it is called infix expressions.
33. If the elements “A”, “B”, “C” and “D” are placed in a stack and are deleted one at a time, what is the order of removal? a) ABCD b) DCBA c) DCAB d) ABDC Answer: b Explanation: Stack follows LIFO(Last In First Out). So the removal order of elements are DCBA.
34. A linear list of elements in which deletion can be done from one end (front) and insertion can take place only at the other end (rear) is known as a? a) Queue b) Stack c) Tree d) Linked list Answer: a Explanation: Linear list of elements in which deletion is done at front side and insertion at rear side is called Queue. In stack we will delete the last entered element first. 35. The data structure required for Breadth First Traversal on a graph is? a) Stack b) Array c) Queue d) Tree Answer: c Explanation: In Breadth First Search Traversal, BFS, starting vertex is first taken and adjacent vertices which are unvisited are also taken. Again, the first vertex which was added as an unvisited adjacent vertex list will be considered to add further unvisited vertices of the graph. To get first unvisited vertex we need to follows First In First Out principle. Queue uses FIFO principle. 36. A queue follows __________ a) FIFO (First In First Out) principle b) LIFO (Last In First Out) principle c) Ordered array d) Linear tree
Answer: c Explanation: Simulation of recursion uses stack data structure. Simulation of arbitrary linked lists uses linked lists. Simulation of resource allocation uses queue as first entered data needs to be given first priority during resource allocation. Simulation of heap sort uses heap data structure.
42. Which of the following is not the type of queue? a) Ordinary queue b) Single ended queue c) Circular queue d) Priority queue Answer: b Explanation: Queue always has two ends. So, single ended queue is not the type of queue. 43. A linear collection of data elements where the linear node is given by means of pointer is called? a) Linked list b) Node list c) Primitive list d) Unordered list Answer: a Explanation: In Linked list each node has its own data and the address of next node. These nodes are linked by using pointers. Node list is an object that consists of a list of all nodes in a document with in a particular selected set of nodes. 44. Consider an implementation of unsorted singly linked list. Suppose it has its representation with a head pointer only. Given the representation, which of the following operation can be implemented in O(1) time? i) Insertion at the front of the linked list ii) Insertion at the end of the linked list iii) Deletion of the front node of the linked list iv) Deletion of the last node of the linked list a) I and II b) I and III c) I, II and III d) I, II and IV Answer: b Explanation: We know the head node in the given linked list. Insertion and deletion of elements at the front of the linked list completes in O (1) time whereas for insertion and deletion at the last node requires to traverse through every node in the linked list. Suppose there are n elements in a linked list, we need to traverse through each node. Hence time complexity becomes O(n). 45. In linked list each node contain minimum of two fields. One field is data field to store the data second field is? a) Pointer to character b) Pointer to integer c) Pointer to node
d) Node
Answer: c Explanation: Each node in a linked list contains data and a pointer (reference) to the next node. Second field contains pointer to node.
46. What would be the asymptotic time complexity to add a node at the end of singly linked list, if the pointer is initially pointing to the head of the list? a) O(1) b) O(n) c) θ(n) d) θ(1) Answer: c Explanation: In case of a linked list having n elements, we need to travel through every node of the list to add the element at the end of the list. Thus asymptotic time complexity is θ(n). 47. The concatenation of two list can performed in O(1) time. Which of the following variation of linked list can be used? a) Singly linked list b) Doubly linked list c) Circular doubly linked list d) Array implementation of list 48. Consider the following definition in c programming language
struct node { int data; struct node * next; } typedef struct node NODE; NODE *ptr;
Which of the following c code is used to create new node? a) ptr = (NODE)malloc(sizeof(NODE)); b) ptr = (NODE)malloc(NODE); c) ptr = (NODE)malloc(sizeof(NODE)); d) ptr = (NODE)malloc(sizeof(NODE)); Answer: a Explanation: As it represents the right way to create a node.
49. What kind of linked list is best to answer question like “What is the item at position n?” a) Singly linked list b) Doubly linked list c) Circular linked list d) Array implementation of linked list
Answer: d Explanation: To access an element in a linked list, we need to traverse every element until we reach the desired element. This will take more time than arrays as arrays provide random access to its elements.
55. What does the following function do for a given Linked List with first node as head?
void fun1(struct node* head) { if(head == NULL) return; fun1(head->next); printf("%d ", head->data); }
a) Prints all nodes of linked lists b) Prints all nodes of linked list in reverse order c) Prints alternate nodes of Linked List d) Prints alternate nodes in reverse order Answer: b Explanation: fun1() prints the given Linked List in reverse manner. For Linked List 1->2->3->4->5, fun1() prints 5->4->3->2->1.
56. Which of the following sorting algorithms can be used to sort a random linked list with minimum time complexity? a) Insertion Sort b) Quick Sort c) Heap Sort d) Merge Sort Answer: d Explanation: Both Merge sort and Insertion sort can be used for linked lists. The slow random- access performance of a linked list makes other algorithms (such as quicksort) perform poorly, and others (such as heapsort) completely impossible. Since worst case time complexity of Merge Sort is O(nLogn) and Insertion sort is O(n^2 ), merge sort is preferred. 57. In the worst case, the number of comparisons needed to search a singly linked list of length n for a given element is a) log 2 n b) n⁄ 2 c) log 2 n – 1 d) n Answer: d Explanation: In the worst case, the element to be searched has to be compared with all elements of linked list. 58. Given pointer to a node X in a singly linked list. Only one pointer is given, pointer to head node is not given, can we delete the node X from given linked list? a) Possible if X is not last node b) Possible if size of linked list is even
c) Possible if size of linked list is odd d) Possible if X is not first node Answer: a Explanation: Following are simple steps.
struct node *temp = X->next; X->data = temp->data; X->next = temp->next; free(temp);
59. You are given pointers to first and last nodes of a singly linked list, which of the following operations are dependent on the length of the linked list? a) Delete the first element b) Insert a new element as a first element c) Delete the last element of the list d) Add a new element at the end of the list Answer: c Explanation: Deletion of the first element of the list is done in O (1) time by deleting memory and changing the first pointer. Insertion of an element as a first element can be done in O (1) time. We will create a node that holds data and points to the head of the given linked list. The head pointer was changed to a newly created node. Deletion of the last element requires a pointer to the previous node of last, which can only be obtained by traversing the list. This requires the length of the linked list. Adding a new element at the end of the list can be done in O (1) by changing the pointer of the last node to the newly created node and last is changed to a newly created node. 60. In the worst case, the number of comparisons needed to search a singly linked list of length n for a given element is a) log2 n b) n⁄ 2 c) log2 n – 1 d) n Answer: d Explanation: The worst-case happens if the required element is at last or the element is absent in the list. For this, we need to compare every element in the linked list. If n elements are there, n comparisons will happen in the worst case. 61. How many children does a binary tree have? a) 2 b) any number of children c) 0 or 1 or 2 d) 0 or 1 Answer: c Explanation: Can have atmost 2 nodes. 62. What is/are the disadvantages of implementing tree using normal arrays? a) difficulty in knowing children nodes of a node
67. Consider a situation of writing a binary tree into a file with memory storage efficiency in mind, is array representation of tree is good? a) yes because we are overcoming the need of pointers and so space efficiency b) yes because array values are indexable c) No it is not efficient in case of sparse trees and remaning cases it is fine d) No linked list representation of tree is only fine Answer: c Explanation: In case of sparse trees (where one node per level in worst cases), the array size (2h)- 1 where h is height but only h indexes will be filled and (2h)-1-h nodes will be left unused leading to space wastage. 68. Can a tree stored in an array using either one of inorder or post order or pre order traversals be again reformed? a) Yes just traverse through the array and form the tree b) No we need one more traversal to form a tree c) No in case of sparse trees d) Yes by using both inorder and array elements Answer: b Explanation: We need any two traversals for tree formation but if some additional stuff or techniques are used while storing a tree in an array then one traversal can facilitate like also storing null values of a node in array. 69. Advantages of linked list representation of binary trees over arrays? a) dynamic size b) ease of insertion/deletion c) ease in randomly accessing a node d) both dynamic size and ease in insertion/deletion Answer: d Explanation: It has both dynamic size and ease in insertion and deletion as advantages. 70. Disadvantages of linked list representation of binary trees over arrays? a) Randomly accessing is not possible b) Extra memory for a pointer is needed with every element in the list c) Difficulty in deletion d) Random access is not possible and extra memory with every element Answer: d Explanation: Random access is not possible with linked lists. 71. Which of the following traversing algorithm is not used to traverse in a tree? a) Post order b) Pre order c) Post order d) Randomized Answer: d Explanation: Generally, all nodes in a tree are visited by using preorder, inorder and postorder traversing algorithms.
72. Level order traversal of a tree is formed with the help of a) breadth first search b) depth first search c) dijkstra’s algorithm d) prims algorithm Answer: a Explanation: Level order is similar to bfs.
73. Identify the reason which doesn’t play a key role to use threaded binary trees? a) The storage required by stack and queue is more b) The pointers in most of nodes of a binary tree are NULL c) It is Difficult to find a successor node d) They occupy less size Answer: d Explanation: Threaded binary trees are introduced to make the Inorder traversal faster without using any stack or recursion. Stack and Queue require more space and pointers in the majority of binary trees are null and difficulties are raised while finding successor nodes. Size constraints are not taken on threaded binary trees, but they occupy less space than a stack/queue.
74. In a max-heap, element with the greatest key is always in the which node? a) Leaf node b) First node of left sub tree c) root node d) First node of right sub tree Answer: c Explanation: This is one of the property of max-heap that root node must have key greater than its children. 75. Heap exhibits the property of a binary tree? a) True b) False Answer: a Explanation: Yes, because the leaf nodes are present at height h or h-1, which is a property of complete binary tree. 76. What is the complexity of adding an element to the heap. a) O(log n) b) O(h) c) O(log n) & O(h) d) O(n) Answer: c Explanation: The total possible operation in re locating the new location to a new element will be equal to height of the heap. 77. The worst case complexity of deleting any arbitrary node value element from heap is __________ a) O(logn) b) O(n)