Arrays & Strings Question Bank for C-CAT
Topic-wise Arrays & Strings MCQs for CDAC C-CAT preparation with answers and explanations.
Show Answer & Explanation
Correct Answer: D - Collection of same data types
An array stores multiple elements of the same data type in contiguous memory locations.
Show Answer & Explanation
Correct Answer: A - Contiguously
Array elements occupy consecutive memory locations, which allows index-based access.
What will be the output?
int a[5] = {1,2,3};
printf("%d", a[3]);Show Answer & Explanation
Correct Answer: B - 0
Uninitialized elements in a partially initialized array are set to 0.
Show Answer & Explanation
Correct Answer: B - 0
In C, array indexing starts from 0.
Show Answer & Explanation
Correct Answer: B - \0
Strings in C are null-terminated, meaning they end with the null character \0.
What will be the output?
int arr[3] = {10, 20, 30};
printf("%d", *(arr + 1));Show Answer & Explanation
Correct Answer: B - 20
*(arr + 1) is equivalent to arr[1], which is 20. Adding 1 to the array pointer moves to the next element.
Show Answer & Explanation
Correct Answer: B - int arr[3][4];
A 2D array is declared as data_type name[rows][columns]. So int arr[3][4] creates 3 rows and 4 columns.
Show Answer & Explanation
Correct Answer: D - 5
strlen() returns the length of the string excluding the null terminator. "Hello" has 5 characters.
What is the output?
char str[] = "Hello";
printf("%lu", sizeof(str));Show Answer & Explanation
Correct Answer: C - 6
sizeof() includes the null terminator, so "Hello" occupies 6 bytes (5 characters + 1 null character).
Show Answer & Explanation
Correct Answer: D - strcpy()
strcpy(dest, src) copies the source string to the destination string.
What will be the output?
int arr[] = {1, 2, 3, 4, 5};
printf("%d", arr[5]);Show Answer & Explanation
Correct Answer: A - Garbage value
arr[5] is out of bounds (valid indices are 0-4). Accessing it results in undefined behavior, typically a garbage value.
Show Answer & Explanation
Correct Answer: A - Concatenates two strings
strcat(dest, src) appends the source string at the end of the destination string.
What is the output?
char *str = "World";
printf("%c", str[0]);Show Answer & Explanation
Correct Answer: B - W
str[0] accesses the first character of the string, which is "W".
Show Answer & Explanation
Correct Answer: D - Negative value
strcmp() returns negative if first string is less than second. "abc" < "abd" because 'c' < 'd'.
Show Answer & Explanation
Correct Answer: D - By passing the array name
When you pass the array name, you pass the base address (pointer to first element). Arrays decay to pointers when passed to functions.
What is the output?
char str1[10] = "Hello";
char str2[10] = "Hello";
printf("%d", str1 == str2);Show Answer & Explanation
Correct Answer: D - 0
str1 and str2 are different arrays at different memory locations. Comparing them compares addresses, not content, so result is 0 (false).
Show Answer & Explanation
Correct Answer: B - 60
Total elements = 5 × 4 × 3 = 60. A 3D array with dimensions [5][4][3] can store 60 integers.
What is the output?
char str[] = "Hello\0World";
printf("%s", str);Show Answer & Explanation
Correct Answer: A - Hello
printf with %s stops printing at the first null character (\0). So only "Hello" is printed.
Show Answer & Explanation
Correct Answer: D - <string.h>
String manipulation functions are declared in <string.h> header file.
What will be the output?
int arr[5] = {0};
printf("%d %d", arr[0], arr[4]);Show Answer & Explanation
Correct Answer: D - 0 0
When an array is initialized with {0}, all elements are set to 0. So both arr[0] and arr[4] are 0.
What is the output of the following code?
int arr[] = {10, 20, 30, 40, 50};
printf("%d", *(arr + 3));Show Answer & Explanation
Correct Answer: A - 40
*(arr + 3) accesses the element at index 3, which is 40. Pointer arithmetic adds 3 * sizeof(int) bytes to the base address.
What will be the output?
char str[] = "Hello";
printf("%lu", sizeof(str));Show Answer & Explanation
Correct Answer: C - 6
sizeof(str) returns 6 because the string "Hello" has 5 characters plus the null terminator '\0', totaling 6 bytes.
What does this code print?
int a[5] = {1, 2, 3};
printf("%d %d", a[3], a[4]);Show Answer & Explanation
Correct Answer: C - 0 0
When an array is partially initialized, the remaining elements are automatically initialized to 0. So a[3] and a[4] are both 0.
What is the output?
char s1[] = "abc";
char s2[] = "abc";
printf("%d", s1 == s2);Show Answer & Explanation
Correct Answer: C - 0
s1 and s2 are two different arrays stored at different memory locations. Comparing them with == compares their addresses, not their contents, so the result is 0.
What will strlen() return for the following?
char str[] = "Hello\0World";
printf("%lu", strlen(str));Show Answer & Explanation
Correct Answer: B - 5
strlen() counts characters until it encounters the first null terminator. It stops at '\0' after "Hello", returning 5.
What is the output?
int arr[3][2] = {{1,2},{3,4},{5,6}};
printf("%d", arr[1][1]);Show Answer & Explanation
Correct Answer: A - 4
arr[1][1] accesses row index 1, column index 1. Row 1 is {3,4}, so the element at column 1 is 4.
Show Answer & Explanation
Correct Answer: B - strcat()
strcat() is the standard library function declared in <string.h> used to concatenate (append) one string to the end of another.
What is the output?
char str[10] = "CCAT";
strcat(str, "2026");
printf("%s", str);Show Answer & Explanation
Correct Answer: C - CCAT2026
strcat() appends "2026" to the end of "CCAT", resulting in "CCAT2026". The buffer str[10] is large enough to hold the result.
What does this code produce?
int arr[] = {5, 10, 15, 20};
int *p = arr;
p++;
printf("%d", *p);Show Answer & Explanation
Correct Answer: A - 10
p initially points to arr[0]. After p++, it points to arr[1] which is 10. Incrementing a pointer moves it by sizeof(int) bytes.
What will be the output?
char s[] = "CDAC";
printf("%c", s[4]);Show Answer & Explanation
Correct Answer: B - Null character
The string "CDAC" has indices 0-3 for characters C, D, A, C. Index 4 contains the null terminator '\0'.
Show Answer & Explanation
Correct Answer: C - Negative value
strcmp() compares strings lexicographically. Since 'a' (97) comes before 'b' (98) in ASCII, "apple" < "banana", so it returns a negative value.
What is the output?
int arr[2][3] = {1,2,3,4,5,6};
printf("%d", *(*(arr+1)+2));Show Answer & Explanation
Correct Answer: D - 6
*(arr+1) gives the pointer to the second row {4,5,6}. *(*(arr+1)+2) accesses the element at index 2 of the second row, which is 6.
What does this code print?
char *p = "Exam";
printf("%c", *(p+2));Show Answer & Explanation
Correct Answer: C - a
p points to 'E' at index 0. *(p+2) accesses index 2, which is 'a' in the string "Exam".
Show Answer & Explanation
Correct Answer: D - char s[12] = "Programming";
"Programming" has 11 characters plus a null terminator, requiring at least 12 bytes. char s[12] is the minimum valid size. Single quotes in option D are invalid for strings.
What is the output?
int a[] = {1, 2, 3, 4, 5};
printf("%d", 2[a]);Show Answer & Explanation
Correct Answer: B - 3
In C, a[i] is equivalent to *(a+i), which is the same as *(i+a) or i[a]. So 2[a] is the same as a[2], which is 3.
What will be the output?
char str1[20] = "Hello";
char str2[20] = "World";
strcpy(str1, str2);
printf("%s", str1);Show Answer & Explanation
Correct Answer: D - World
strcpy() copies the content of str2 into str1, replacing the original content. After the copy, str1 contains "World".
Show Answer & Explanation
Correct Answer: B - 80
A 2D array int arr[5][4] has 5 × 4 = 20 elements. Each int is 4 bytes, so total size is 20 × 4 = 80 bytes.
What does this code output?
char s[] = "abcdef";
printf("%c", s[sizeof(s)-2]);Show Answer & Explanation
Correct Answer: C - f
sizeof(s) is 7 (6 chars + null). s[7-2] = s[5] = 'f'. The last character before the null terminator.
What is the output?
int arr[] = {10, 20, 30, 40};
int *p = arr + 2;
printf("%d", p[-1]);Show Answer & Explanation
Correct Answer: B - 20
p points to arr[2] (value 30). p[-1] is equivalent to *(p-1), which accesses arr[1], giving 20.
What happens when you try to modify a string literal?
char *str = "Hello";
str[0] = 'M';Show Answer & Explanation
Correct Answer: A - Undefined behavior
String literals are stored in read-only memory. Attempting to modify them through a pointer leads to undefined behavior, which often causes a segmentation fault.
Show Answer & Explanation
Correct Answer: D - Copies src and pads remaining with null characters
When the source string is shorter than n, strncpy() copies all characters from src and pads the rest of the n characters with null ('\0') characters.
What is the output?
int a[3] = {5, 10, 15};
int *p = a;
printf("%d", *(p++) + 1);Show Answer & Explanation
Correct Answer: C - 6
Post-increment p++ returns the current value of p before incrementing. So *(p++) dereferences the original p pointing to a[0]=5, then adds 1, giving 6.
Show Answer & Explanation
Correct Answer: A - void func(int arr[][3])
When passing a 2D array to a function, the first dimension can be omitted but all subsequent dimensions must be specified. void func(int arr[][3]) is correct.
What is the output?
char str[] = "CCAT Exam";
char *p = strchr(str, 'E');
printf("%s", p);Show Answer & Explanation
Correct Answer: A - Exam
strchr() returns a pointer to the first occurrence of 'E' in the string. Printing from that pointer gives "Exam".
What does this code print?
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d", n);Show Answer & Explanation
Correct Answer: C - 5
sizeof(arr) gives total bytes (20 on 4-byte int), sizeof(arr[0]) gives bytes per element (4). Dividing gives the number of elements: 20/4 = 5.
What is the output?
char a[] = "xyz";
char b[] = "xyz";
printf("%d", strcmp(a, b));Show Answer & Explanation
Correct Answer: A - 0
strcmp() returns 0 when both strings are identical. Since a and b both contain "xyz", the comparison returns 0.
What will the following code print?
int arr[4] = {0};
for(int i=0; i<4; i++)
arr[i] = i * i;
printf("%d", arr[3]);Show Answer & Explanation
Correct Answer: C - 9
The loop sets arr[i] = i*i. For i=3, arr[3] = 3*3 = 9.
What is the output?
char str[] = "Hello World";
char *token = strtok(str, " ");
token = strtok(NULL, " ");
printf("%s", token);Show Answer & Explanation
Correct Answer: A - World
The first strtok() call returns "Hello". The second call with NULL continues tokenizing the same string, returning "World".
What is the output?
int arr[] = {10, 20, 30};
printf("%ld", (long)(&arr[2] - &arr[0]));Show Answer & Explanation
Correct Answer: C - 2
Pointer subtraction gives the number of elements between two pointers, not the byte difference. &arr[2] - &arr[0] = 2 elements.
What does the following code output?
char s[20];
sprintf(s, "%d+%d=%d", 3, 4, 7);
printf("%s", s);Show Answer & Explanation
Correct Answer: A - 3+4=7
sprintf() works like printf() but writes the formatted output to a string buffer instead of stdout. It formats the integers into the string "3+4=7".