Chapter 5: MCQs on Arrays and Strings in C++ | C++ Programming

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

  1. An array is a collection of:
    • A) Variables of different data types
    • B) Functions
    • C) Variables of the same data type
    • D) Constants
  2. What is the starting index of an array in C++?
    • A) -1
    • B) 0
    • C) 1
    • D) Depends on the compiler
  3. 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
  4. 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;
  5. How do you access the third element of an array named myArray?
    • A) myArray(2);
    • B) myArray[3];
    • C) myArray[2];
    • D) myArray{3};
  6. 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

  1. 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
  2. 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};
  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
  4. 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
  5. If an array has 7 elements, what is the index of the last element?
    • A) 6
    • B) 7
    • C) 5
    • D) Cannot be determined
  6. 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

  1. 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
  2. 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];
  3. 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];
  4. 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
  5. If a 2D array arr has dimensions [3][3], how many elements can it hold?
    • A) 3
    • B) 6
    • C) 9
    • D) 12
  6. 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++

  1. 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
  2. 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
  3. What is the default value of an uninitialized string in C++?
    • A) 0
    • B) null
    • C) Empty string
    • D) Undefined
  4. 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";
  5. Which function can be used to concatenate two C++ strings?
    • A) strcat
    • B) strjoin
    • C) concat
    • D) join
  6. In C++, a string is terminated by:
    • A) Null character '\0'
    • B) Space character ' '
    • C) Newline '\n'
    • D) None of the above

25-30: String Manipulation Functions (strlen, strcpy, strcat)

  1. The function strlen in C++ is used to:
    • A) Concatenate strings
    • B) Calculate the length of a string
    • C) Copy one string to another
    • D) Compare two strings
  2. What does strcpy do in C++?
    • A) Copies one string to another
    • B) Finds the length of a string
    • C) Concatenates two strings
    • D) None of the above
  3. Which header file is required to use strlen, strcpy, and strcat?
    • A) <cmath>
    • B) <iostream>
    • C) <cstring>
    • D) <cstdlib>
  4. What does strcat do?
    • A) Copies one string to another
    • B) Appends one string to another
    • C) Finds the length of a string
    • D) Splits a string
  5. If char str1[20] = "Hello"; and char str2[20] = "World";, what does strcat(str1, str2); do?
    • A) Hello
    • B) WorldHello
    • C) HelloWorld
    • D) Hello World
  6. What will strlen("Hello World"); return?
    • A) 11
    • B) 10
    • C) 12
    • D) 9

Answer Key

QnoAnswer
1C) Variables of the same data type
2B) 0
3A) A data structure that can store elements sequentially
4A) int arr[10];
5C) myArray[2];
6D) Undefined behavior
7B) They store elements in a sequential manner
8B) int arr[] = {1, 2, 3};
9C) First two elements are 1 and 2, rest are 0
10D) Deleting an element
11A) 6
12D) Both A and C
13B) An array of arrays
14C) int arr[3][4];
15A) matrix[1][2];
16B) They are arrays within arrays
17C) 9
18B) Undefined behavior
19B) Arrays of characters
20A) str[] is mutable; *str is a pointer to a constant
21D) Undefined
22D) string str = “Hello”;
23A) strcat
24A) Null character ‘\0’
25B) Calculate the length of a string
26A) Copies one string to another
27C) <cstring>
28B) Appends one string to another
29C) HelloWorld
30A) 11

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top