C++ Programming MCQ – Arrays

This section on C++ interview questions and answers focuses on “Arrays”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ interview questions on “Arrays” along with answers, explanations and/or solutions:

1. Which of the following correctly declares an array?
a) int array[10];
b) int array;
c) array{10};
d) array array[10];
View Answer

Answer: a
Explanation: Because array variable and values need to be declared after the datatype only.

2. What is the index number of the last element of an array with 9 elements?
a) 9
b) 8
c) 0
d) Programmer-defined
View Answer

Answer: b
Explanation: Because the first element always starts at 0. So it is on 8 position.

3. What is the correct definition of an array?
a) An array is a series of elements of the same type in contiguous memory locations
b) An array is a series of element
c) An array is a series of elements of the same type placed in non-contiguous memory locations
d) An array is an element of the different type
View Answer

Answer: a
Explanation: Correct definition of an array is An array is a series of elements of the same type in contiguous memory locations.
advertisement
advertisement

4. Which of the following accesses the seventh element stored in array?
a) array[6];
b) array[7];
c) array(7);
d) array;
View Answer

Answer: a
Explanation: The array location starts from zero, So it can accessed by array[6].

5. Which of the following gives the memory address of the first element in array?
a) array[0];
b) array[1];
c) array(2);
d) array;
View Answer

Answer: d
Explanation: To get the address of ith index of an array, we use following syntax (arr + i). So as we need address of first index we will use (arr + 0) equivalent to arr.

6. What will be the output of the following C++ code?

  1.     #include <stdio.h>
  2.     #include<iostream>
  3.     using namespace std;
  4.     int array1[] = {1200, 200, 2300, 1230, 1543};
  5.     int array2[] = {12, 14, 16, 18, 20};
  6.     int temp, result = 0;
  7.     int main()
  8.     {
  9.         for (temp = 0; temp < 5; temp++) 
  10.         {
  11.             result += array1[temp];
  12.         }
  13.         for (temp = 0; temp < 4; temp++)
  14.         {
  15.             result += array2[temp];
  16.         }
  17.         cout << result;
  18.         return 0;
  19.     }

a) 6553
b) 6533
c) 6522
d) 12200
View Answer

Answer: b
Explanation: In this program we are adding the every element of two arrays. Finally we got output as 6533.
Output:

advertisement
$ g++ array.cpp
$ a.out
6533

7. What will be the output of the following C++ code?

advertisement
  1.     #include <stdio.h>
  2.     #include<iostream>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         int array[] = {0, 2, 4, 6, 7, 5, 3};
  7.         int n, result = 0;
  8.         for (n = 0; n < 8; n++) 
  9.         {
  10.             result += array[n];
  11.         }
  12.         cout << result;
  13.         return 0;
  14.     }

a) 25
b) 26
c) 27
d) 21
View Answer

Answer: c
Explanation: We are adding all the elements in the array and printing it. Total elements in the array is 7, but our for loop will go beyond 7 and add a garbage value.

8. What will be the output of the following C++ code?

  1.     #include <stdio.h>
  2.     #include<iostream>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         int a = 5, b = 10, c = 15;
  7.         int arr[3] = {&a, &b, &c};
  8.         cout << *arr[*arr[1] - 8];
  9.         return 0;
  10.     }

a) 15
b) 18
c) garbage value
d) compile time error
View Answer

Answer: d
Explanation: The conversion is invalid in this array. So it will arise error. The following compilation error will be raised:
cannot convert from ‘int *’ to ‘int’
This is because &a, &b and &c represent int* whereas the array defined is of int type.

9. What will be the output of the following C++ code?

  1.     #include <stdio.h>
  2.     #include <iostream>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         char str[5] = "ABC";
  7.         cout << str[3];
  8.         cout << str;
  9.         return 0;
  10.     }

a) ABC
b) ABCD
c) AB
d) AC
View Answer

Answer: a
Explanation: We are just printing the values of first 3 values.

$ g++ array.cpp
$ a.out
ABC

10. What will be the output of the following C++ code?

  1.     #include <stdio.h>
  2.     #include <iostream>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         int array[] = {10, 20, 30};
  7.         cout << -2[array];
  8.         return 0;
  9.     }

a) -15
b) -30
c) compile time error
d) garbage value
View Answer

Answer: b
Explanation: It’s just printing the negative value of the concern element.

$ g++ array.cpp
$ a.out
-30

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, 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.