Master Arrays and Strings in C++ | 30 Key MCQs for Comprehensive Learning
Understanding arrays and strings is crucial for any C++ programmer. These 30 MCQs cover the fundamentals of single and multi-dimensional arrays, strings, and essential string manipulation functions like strlen, strcpy, and strcat.
MCQs
1-6: Introduction to Arrays
An array is a collection of:
A) Variables of different data types
B) Functions
C) Variables of the same data type
D) Constants
What is the starting index of an array in C++?
A) -1
B) 0
C) 1
D) Depends on the compiler
Which of the following best describes an array?
A) A data structure that can store elements sequentially
B) A list of unrelated variables
C) A file storage system
D) A single variable with a fixed value
The syntax for declaring an array of 10 integers in C++ is:
A) int arr[10];
B) int arr;
C) int arr[10][10];
D) int[10] arr;
How do you access the third element of an array named myArray?
A) myArray(2);
B) myArray[3];
C) myArray[2];
D) myArray{3};
What happens if you try to access an array index that is out of bounds in C++?
A) The program crashes
B) Random data is accessed
C) Compile-time error
D) Undefined behavior
7-12: Single-Dimensional Arrays
Which of the following statements about single-dimensional arrays is correct?
A) They can store elements of different data types
B) They store elements in a sequential manner
C) They require the class keyword
D) They are dynamic by default
How can you initialize an array in C++ with specific values?
A) int arr = {1, 2, 3};
B) int arr[] = {1, 2, 3};
C) arr[3] = int {1, 2, 3};
D) array int arr = {1, 2, 3};
What will be the output if the following array is initialized as int arr[5] = {1, 2};?
A) Only 1 and 2 are stored; the rest are undefined
B) All elements are zero
C) First two elements are 1 and 2, rest are 0
D) Error due to incomplete initialization
Which of the following operations cannot be performed directly on a single-dimensional array?
A) Inserting an element
B) Modifying an element
C) Accessing an element
D) Deleting an element
If an array has 7 elements, what is the index of the last element?
A) 6
B) 7
C) 5
D) Cannot be determined
Which function can you use to find the size of an array in C++?
A) sizeof
B) strlen
C) sizeof() / sizeof(array[0])
D) Both A and C
13-18: Multi-Dimensional Arrays
What is a multi-dimensional array?
A) An array containing elements of different data types
B) An array of arrays
C) An array with a single row
D) A dynamic array
How do you declare a 2D array with 3 rows and 4 columns in C++?
A) int arr[4][3];
B) int arr[3,4];
C) int arr[3][4];
D) int arr[3x4];
How do you access the element in the second row and third column of a 2D array named matrix?
A) matrix[1][2];
B) matrix[2][3];
C) matrix[3][2];
D) matrix[2][1];
Which of the following statements is true about multi-dimensional arrays?
A) They only allow integer values
B) They are arrays within arrays
C) They always require a pointer
D) They are dynamic in nature
If a 2D array arr has dimensions [3][3], how many elements can it hold?
A) 3
B) 6
C) 9
D) 12
What happens if you try to access an element outside the range of a 2D array?
A) Syntax error
B) Undefined behavior
C) Compiler warning
D) Default value returned
19-24: Introduction to Strings in C++
In C++, strings are:
A) A data type for numeric operations
B) Arrays of characters
C) Always defined using char
D) None of the above
What is the difference between char str[] = "Hello"; and char *str = "Hello";?
A) str[] is mutable; *str is a pointer to a constant
B) str[] is immutable; *str is mutable
C) str[] requires a pointer
D) Both are identical
What is the default value of an uninitialized string in C++?
A) 0
B) null
C) Empty string
D) Undefined
Which of the following is NOT a way to initialize a C++ string?
A) std::string str("Hello");
B) std::string str = "Hello";
C) std::string str;
D) string str = "Hello";
Which function can be used to concatenate two C++ strings?