Quiz on C++ Arrays

Last Updated :
Discuss
Comments

Question 1

What is the size of the array char arr[] = "geeksforgeeks";?

  • 12

  • 13

  • 15

  • 14

Question 2

What is the index of the last element in an array int arr[20];?

  • 20

  • 19

  • 21

  • 18

Question 3

How do you pass an array to a function in C++?

  • By copying

  • By value

  • By pointer

  • By name

Question 4

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

C++
#include <iostream>
using namespace std;

int main() {
    int arr[3] = {1, 2};
    for(int i = 0; i < 3; i++)
        cout << arr[i] << " ";
    return 0;
}
  • 1 2 3

  • 1 2 1

  • 1 2 0

  • Compilation error

Question 5

What will be the output of the following code?

C++
#include <iostream>
using namespace std;

void modifyArray(int arr[], int size) {
    for(int i = 0; i < size; i++) {
        arr[i] += 5;
    }
}

int main() {
    int arr[3] = {1, 2, 3};
    modifyArray(arr, 3);
    for(int i = 0; i < 3; i++) {
        cout << arr[i] << " ";
    }
    return 0;
}
  • 1 2 3

  • 6 7 8

  • 1 7 3

  • Compilation error

Question 6

Which of the following are true about multidimensional arrays in C++?

  • They are arrays of arrays

  • The syntax to declare a 2D array is type arrayName[rows][columns];

  • They can have two or more dimensions

  • All of the above

Question 7

What will be the output of the following code?

C++
#include <iostream>
using namespace std;

int main() {
    int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
    cout << arr[1][1];
    return 0;
}
  • 1

  • 2

  • 5

  • 6

Question 8

Which of the following is a correct way to dynamically allocate a 1D array of 20 integers in C++?

  • int* arr = new int[20];

  • int* arr = new int();

  • int* arr = new int[20]();

  • int* arr = new int* [20];

Question 9

Which of the following statements about arrays is/are incorrect?

  • Arrays are always passed by value to functions

  • The name of the array is a pointer to its first element

  • Arrays can be initialized at the time of their declaration

  • None of the above

Question 10

What will be the output of the following code?

C++
#include <iostream>
using namespace std;

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int* p = arr;
    cout << *(p + 2);
    return 0;
}
  • 1

  • 3

  • 4

  • 2

There are 23 questions to complete.

Take a part in the ongoing discussion