C++ Programming MCQ – Arrays

Q 1. Which of the following correctly declares an array?
A. int array;
B. int array[10];
C. array{10};
D. array array[10];

Show Answer Answer:-B. int array[10];
Explanation Because array variable and values need to be declared after the datatype only.

Q2. What is the index number of the last element of an array with 9 elements?
A. 8
B. 9
C. 10
D. Programmer-defined

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

Q 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

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

Q 4. Which of the following accesses the seventh element stored in array?
A. array[6];
B. array[7];
C. array(7);
D. array;

Show Answer Answer:-A. array[6];
Explanation The array location starts from zero, So it can accessed by array[6].

Q 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;

Show Answer Answer:-D. array;
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.

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

#include <stdio.h>

#include<iostream>

using namespace std;

int array1[] = {1200, 200, 2300, 1230, 1543};

int array2[] = {12, 14, 16, 18, 20};

int temp, result = 0;

int main()

{

for (temp = 0; temp < 5; temp++)

{

result += array1[temp];

}

for (temp = 0; temp < 4; temp++)

{

result += array2[temp];

}

cout << result;

return 0;

}

A. 6533
B. 6553
C. 6522
D. 12200

Show Answer Answer:-A. 6533
Explanation In this program we are adding the every element of two arrays. Finally we got output as 6533. Output: $ g++ array.cpp $ a.out 6533

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

#include <stdio.h>

#include<iostream>

using namespace std;

int main ()

{

int array[] = {0, 2, 4, 6, 7, 5, 3};

int n, result = 0;

for (n = 0; n < 8; n++)

{

result += array[n];

}

cout << result;

return 0;

}

A. 25
B. 26
C. 27
D. 21

Show Answer Answer:-C. 27
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.

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

#include <stdio.h>

#include<iostream>

using namespace std;

int main()

{

int a = 5, b = 10, c = 15;

int arr[3] = {&a, &b, &c};

cout << *arr[*arr[1] – 8];

return 0;

}

A. 15
B. 18
C. garbage value
D. compile time error

Show Answer Answer:-D. compile time error
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.

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

#include <stdio.h>

#include <iostream>

using namespace std;

int main()

{

char str[5] = “ABC”;

cout << str[3];

cout << str;

return 0;

}

A. ABC
B. ABCD
C. AB
D. AC

Show Answer Answer:-A. ABC
Explanation We are just printing the values of first 3 values. $ g++ array.cpp $ a.out ABC

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

#include <stdio.h>

#include <iostream>

using namespace std;

int main()

{

int array[] = {10, 20, 30};

cout << -2[array];

return 0;

}

A. -15
B. -30
C. compile time error
D. garbage value

Show Answer Answer:-B. -30
Explanation It’s just printing the negative value of the concern element. $ g++ array.cpp $ a.out -30

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page