Arrays and Strings # MCQs Practice set

Q.1 Which of the following is a correct way to declare an array of 10 integers in C?

int arr[10];
int arr();
array int arr[10];
int arr{};
Explanation - In C, arrays are declared by specifying the type and size in square brackets. 'int arr[10];' declares an integer array of size 10.
Correct answer is: int arr[10];

Q.2 What is the index of the first element in a C array?

0
1
-1
Depends on compiler
Explanation - In C, arrays are zero-indexed, so the first element has index 0.
Correct answer is: 0

Q.3 Which function is used to find the length of a string in C?

strlen()
strlength()
size()
length()
Explanation - 'strlen()' from <string.h> returns the number of characters in a string excluding the null terminator.
Correct answer is: strlen()

Q.4 What is the output of the following code? char str[] = "HELLO"; printf("%c", str[1]);

H
E
L
O
Explanation - Arrays are zero-indexed. str[1] accesses the second character 'E'.
Correct answer is: E

Q.5 Which of the following correctly initializes a string in C?

char str[] = 'HELLO';
char str[] = "HELLO";
char str = "HELLO";
string str = "HELLO";
Explanation - Strings in C are arrays of characters and must be enclosed in double quotes.
Correct answer is: char str[] = "HELLO";

Q.6 What is the result of the expression: sizeof(int arr[10])?

Size of int
10
10 * sizeof(int)
Depends on array contents
Explanation - 'sizeof(arr)' gives the total memory occupied by the array, which is number of elements multiplied by the size of each element.
Correct answer is: 10 * sizeof(int)

Q.7 Which of the following is true about C strings?

They are null-terminated
They are always fixed length
They cannot contain spaces
They are immutable
Explanation - C strings are arrays of characters terminated by a null character '\0'.
Correct answer is: They are null-terminated

Q.8 How can you copy one string to another in C?

strcopy(dest, src);
strcpy(dest, src);
copy(dest, src);
strcpy(src, dest);
Explanation - 'strcpy()' copies the content of src string into dest. The order of parameters is dest first, then src.
Correct answer is: strcpy(dest, src);

Q.9 Which of the following statements about arrays in C is correct?

Array size can be changed at runtime
Array elements must all be of the same type
Array can store elements of different types
Array index can be negative
Explanation - All elements of a C array must be of the same data type, and size is fixed at compile time.
Correct answer is: Array elements must all be of the same type

Q.10 What is the index of the last element of an array of size n?

n
n-1
0
1
Explanation - Since arrays are zero-indexed, the last element is at index n-1.
Correct answer is: n-1

Q.11 Which of the following functions can concatenate two strings in C?

strappend()
strcat()
append()
concat()
Explanation - 'strcat(dest, src)' appends src at the end of dest string.
Correct answer is: strcat()

Q.12 What happens if you access an array index out of bounds in C?

Compile-time error
Runtime error
Undefined behavior
It wraps around automatically
Explanation - Accessing an array out of bounds in C leads to undefined behavior; it may crash or return garbage value.
Correct answer is: Undefined behavior

Q.13 How is a two-dimensional array of size 3x3 declared in C?

int arr[3,3];
int arr[3][3];
int arr(3)(3);
array arr[3][3];
Explanation - Two-dimensional arrays in C are declared using separate square brackets for each dimension.
Correct answer is: int arr[3][3];

Q.14 Which of the following will correctly compare two strings str1 and str2 in C?

str1 == str2
compare(str1, str2)
strcmp(str1, str2) == 0
strcompare(str1, str2)
Explanation - 'strcmp()' returns 0 if both strings are equal. Using '==' compares memory addresses, not contents.
Correct answer is: strcmp(str1, str2) == 0

Q.15 Which of the following is true for arrays in C++ compared to C?

Arrays in C++ are dynamic by default
Arrays in C++ can use vector class for dynamic size
C++ does not support arrays
C++ arrays can store multiple types
Explanation - C++ introduces STL vector, which allows dynamic resizing, unlike fixed-size arrays.
Correct answer is: Arrays in C++ can use vector class for dynamic size

Q.16 What is the output of the following code? char str[] = "ABC"; str[0] = 'X'; printf("%s", str);

ABC
XBC
XBX
Error
Explanation - Strings in arrays are mutable in C; assigning 'X' to str[0] changes the first character.
Correct answer is: XBC

Q.17 Which of the following is NOT a valid way to initialize an array in C?

int arr[3] = {1,2,3};
int arr[] = {1,2,3};
int arr[3] = {1,2};
int arr[3] = (1,2,3);
Explanation - Arrays cannot be initialized with parentheses. Curly braces are required.
Correct answer is: int arr[3] = (1,2,3);

Q.18 Which of the following statements about multidimensional arrays is true?

All rows must have the same number of columns
Rows can have variable lengths
Columns can have variable lengths
Multidimensional arrays are not supported in C
Explanation - In C, multidimensional arrays must have consistent column sizes for all rows.
Correct answer is: All rows must have the same number of columns

Q.19 What does the following code print? char s[] = "Hello"; printf("%c", *(s+2));

H
e
l
o
Explanation - Pointer arithmetic *(s+2) accesses the third character in the string 'Hello', which is 'l'.
Correct answer is: l

Q.20 Which of the following is true about string literals in C?

They are mutable
They are stored in read-only memory
They can be assigned to char arrays using =
They have no null terminator
Explanation - String literals are typically stored in read-only memory; modifying them leads to undefined behavior.
Correct answer is: They are stored in read-only memory

Q.21 What is the output of the following code? int arr[5] = {1,2,3,4,5}; printf("%d", arr[2]+arr[4]);

8
5
9
Error
Explanation - arr[2] is 3 and arr[4] is 5; their sum is 8.
Correct answer is: 8

Q.22 Which of the following is used to find the last character of a string in C?

strlast()
strlen(str)-1
strend()
length(str)-1
Explanation - The last character of a string is at index strlen(str)-1 since string indexing starts from 0.
Correct answer is: strlen(str)-1

Q.23 Which of the following is true about character arrays in C?

They can only hold one character
They are mutable unlike string literals
They cannot be passed to functions
They automatically resize
Explanation - Character arrays can be modified, unlike string literals stored in read-only memory.
Correct answer is: They are mutable unlike string literals

Q.24 Which of the following is true about the array name in C?

It is a pointer to the first element
It stores the address of the last element
It can be incremented like a normal pointer
It can be assigned to another array
Explanation - In most contexts, the array name acts as a pointer to the first element, but it is not a modifiable lvalue.
Correct answer is: It is a pointer to the first element

Q.25 Which of the following is true about passing arrays to functions in C?

The entire array is copied
A pointer to the first element is passed
Only the first element is passed
Arrays cannot be passed to functions
Explanation - In C, passing an array to a function passes a pointer to its first element, not the whole array.
Correct answer is: A pointer to the first element is passed