Data Structure MCQ Questions
Array and Array Operations
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
View Answer
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);
View Answer
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);
View Answer
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;
View Answer
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
View Answer
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
View Answer
7. When does the ArrayIndexOutOfBoundsException occur?
a) Compile-time
b) Run-time
c) Not an error
d) Not an exception at all
View Answer
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
View Answer
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
View Answer
Answer: d
Explanation: Arrays stores elements of same data type and present in
continuous memory locations.
10. What are the disadvantages of arrays?
a) Data structure like queue or stack cannot be implemented
b) There are chances of wastage of memory space if elements inserted
in an array are lesser than the allocated size
c) Index value of an array can be negative
d) Elements are sequentially accessed
View Answer
Answer: b
Explanation: Arrays are of fixed size. If we insert elements less than
the allocated size, unoccupied positions can’t be used again. Wastage
will occur in memory.
11. Assuming int is of 4bytes, what is the size of int arr[15];?
a) 15
b) 19
c) 11
d) 60
View Answer
Answer: d
Explanation: Since there are 15 int elements and each int is of 4bytes,
we get 15*4 = 60bytes.
12. In general, the index of the first element in an array is
__________
a) 0
b) -1
c) 2
d) 1
View Answer
Answer: a
Explanation: In general, Array Indexing starts from 0. Thus, the index
of the first element in an array is 0.
13. Elements in an array are accessed _____________
a) randomly
b) sequentially
c) exponentially
d) logarithmically
View Answer
Answer: a
Explanation: Elements in an array are accessed randomly. In Linked
lists, elements are accessed sequentially.
1. Process of inserting an element in stack is called ____________
a) Create
b) Push
c) Evaluation
d) Pop
View Answer
Answer: b
Explanation: Push operation allows users to insert elements in stack.
If stack is filled completely and trying to perform push operation
stack – overflow can happen.
2. Process of removing an element from stack is called __________
a) Create
b) Push
c) Evaluation
d) Pop
View Answer
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.
3. 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
View Answer
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.
4. 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
View Answer
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.
5. 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
View Answer
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).
6. 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
View Answer
Answer: d
Explanation: Data transfer between the two asynchronous process
uses the queue data structure for synchronisation. The rest are all
stack applications.
7. 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
View Answer
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 3.
8. 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
View Answer
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.
9. What is the value of the postfix expression 6 3 2 4 + – *:
a) 1
b) 40
c) 74
d) -18
View Answer
Answer: d
Explanation: Postfix Expression is (6+(3-(2*4))) which results -18 as
output.
10. Here is an infix expression: 4 + 3*(6*3-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
View Answer
Answer: d
Explanation: When we perform the conversion from infix to postfix
expression +, *, (, * symbols are placed inside the stack. A maximum
of 4 symbols are identified during the entire conversion.
1. How many stacks are required for applying evaluation of infix
expression algorithm?
a) one
b) two
c) three
d) four
View Answer
Answer: b
Expression: Two stacks are required for evaluation of infix expression
– one for operands and one for operators.
2. How many passes does the evaluation of infix expression algorithm
makes through the input?
a) One
b) Two
c) Three
d) Four
View Answer
Answer: a
Explanation: Evaluation of infix expression algorithm is linear and
makes only one pass through the input.
3. Identify the infix expression from the list of options given below.
a) a/b+(c-d)
b) abc*+d+ab+cd+*ce-f-
c) ab-c-
d) +ab
View Answer
Answer: a
Explanation: a/b+(c-d) is an infix expression since the operators are
placed in between the operands.
4. Which of the following statement is incorrect with respect to
evaluation of infix expression algorithm?
a) Operand is pushed on to the stack
b) If the precedence of operator is higher, pop two operands and
evaluate
c) If the precedence of operator is lower, pop two operands and
evaluate
d) The result is pushed on to the operand stack
View Answer
Answer: b
Explanation: If the precedence of the operator is higher than the stack
operator, then it is pushed on to the stack operator.
5. Evaluate the following statement using infix evaluation algorithm
and choose the correct answer. 1+2*3-2
a) 3
b) 6
c) 5
d) 4
View Answer
Answer: c
Explanation: According to precedence of operators, * is evaluated
first. + and – have equal priorities. Hence, 1+6-2= 5.
6. Evaluation of infix expression is done based on precedence of
operators.
a) True
b) False
View Answer
Answer: a
Explanation: During evaluation of infix expression, the operators with
higher precedence are evaluated first, followed by operators with
lower precedence.
7. Of the following choices, which operator has the lowest precedence?
a) ^
b) +
c) /
d) #
View Answer
Answer: d
Explanation: The operator with the lowest precedence is #, preceded by +, / and then ^.
8. The system throws an error if parentheses are encountered in an infix expression
evaluation algorithm.
a) True
b) False
View Answer
Answer: b
Explanation: The algorithm holds good for infix expression with parentheses. The system
does not throw error.
9. Evaluate the following and choose the correct answer.
a/b+c*d where a=4, b=2, c=2, d=1.
a) 1
b) 4
c) 5
d) 2
View Answer
Answer: b
Explanation: * and / have higher priority. Hence, they are evaluated first. Then, + is
evaluated. Hence, 2+2=4.
10. Evaluate the following statement using infix evaluation algorithm and choose the correct
answer. 4*2+3-5/5
a) 10
b) 11
c) 16
d) 12
View Answer
Answer: a
Explanation: 4*2 and 5/5 are evaluated first and then, 8+3-1 is evaluated and the result is
obtained as 10.
11. Using the evaluation of infix expression, evaluate a^b+c and choose the correct answer.
(a=2, b=2, c=2)
a) 12
b) 8
c) 10
d) 6
View Answer
Answer: d
Explanation: ^ has the highest precedence. Hence, 2^2 is evaluated and then 4+2 gives 6.
12. Evaluate the following infix expression using algorithm and choose the correct answer.
a+b*c-d/e^f where a=1, b=2, c=3, d=4, e=2, f=2.
a) 6
b) 8
c) 9
d) 7
View Answer
Answer: a
Explanation: ^ has the highest order of precedence. Hence, 2^2 is evaluated first, and then,
2*3 and 4/4 are evaluated. Therefore, 1+6-1=6.
13. From the given expression tree, identify the infix expression, evaluate it and choose the
correct result.
a) 5
b) 10
c) 12
d) 16
View Answer
Answer: c
Explanation: From the given expression tree, the result of the infix expression is evaluated to
be 12.