This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “Variable Length Array”.
1. Which of the following is an advantage of using variable-length arrays?
a) Deciding the length of an array at the time of execution
b) Memory allocation at compile time
c) Initializing array at compile time
d) Faster execution of code
View Answer
Explanation: The advantage of using a variable-length array is that you can fix the size of an array at the time of execution on a parameter value or a variable. There is no need to specify the length of the array at the time of compilation.
2. Where does the GNU C compiler allocates memory for variable-length arrays?
a) Tree
b) Stack
c) Linked List
d) Queue
View Answer
Explanation: For variable length arrays, memory is allocated on stack along with local variables by GNU C Compiler. However, it can also be stored in heap, but since stack is faster and straightforward, it is used by most of the compilers.
3. Consider the following piece of code in C++. What does the following code implement?
#include <iostream> using namespace std; int main() { int *arr_vla; int size; cout<<"Enter the size of variable length array: "; cin>>size; arr_vla = new int [size]; for (int i = 0; i < size; i++) { cout<<"Enter the integers to be inserted in the variable length array: "; cin>>arr_vla[i]; } for(int i = 0; i < size; i++) { cout<<arr_vla[i]<<" "; } cout<<endl; return 0; }
a) Variable-length array
b) Fixed-size array
c) Dynamic memory allocation
d) Error
View Answer
Explanation: The above program implements a variable-length array in C++. Here, first the array is initialized and then the array size is taken as an input from the user, the elements to be inserted in the array are also taken from the user.
4. Arbitrary expressions can be used while declaring variable-length arrays.
a) True
b) False
View Answer
Explanation: Arbitrary expressions can be used while declaring variable-length arrays and the values to the variables in the expression will be provided at run time. For example: int a[6+7*2], int a[b/6]. Variable-length arrays can also be used as an argument to functions.
5. Consider the following piece of code in C++, how many elements will be stored in the array ‘arr’ if the user enters the values of a, b, c and d as 10, 20, 30, and 40 respectively?
#include<iostream> using namespace std; int main() { int a, b, c, d; cout<<”Enter the value of a, b, c, d: “; cin>>a>>b>>c>>d; int arr[a - b/c + d]; }
a) 30
b) 40
c) 50
d) 60
View Answer
Explanation: We can declare a variable-length array in C++ using arbitrary expressions and the values of the variables in the expression will be computed at the run time. Since, division has higher priority than addition and subtraction, it will be performed first. The number of elements that can be stored in the array is 50.
6. Which among the following is the worst-case time complexity for appending an element in a variable-length array?
a) O(n)
b) O(1)
c) O(n2)
d) O(log n)
View Answer
Explanation: When we want to insert an element into an array that is full, we need to traverse the whole array in order to append the element, which takes O(n) time. Hence, in the worst-case it takes O(n) time. Here, n is the number of elements present in an array. While it takes only O(1) time to append in a fixed-length array.
Sanfoundry Global Education & Learning Series – Data Structure.
To practice all areas of Data Structure, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice Programming MCQs
- Check Data Structure Books
- Practice Design & Analysis of Algorithms MCQ
- Check Computer Science Books
- Check Programming Books