Variable Length Array Multiple Choice Questions and Answers (MCQs)

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

Answer: a
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

Answer: b
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?

advertisement
advertisement
#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

Answer: a
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.
Note: Join free Sanfoundry classes at Telegram or Youtube

4. Arbitrary expressions can be used while declaring variable-length arrays.
a) True
b) False
View Answer

Answer: a
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?

advertisement
#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

Answer: c
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.
advertisement

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

Answer: a
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.