Back to Practice C Programming

Arrays & Strings - Practice MCQs for CCAT

50 Questions Section B: Programming C Programming

Arrays & Strings Question Bank for C-CAT

Topic-wise Arrays & Strings MCQs for CDAC C-CAT preparation with answers and explanations.

Q1.
What is an array in C?
ACollection of different data types
BPointer only
CDynamic memory
DCollection of same data types
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.

Q2.
How are array elements stored in memory?
AContiguously
BNon-contiguously
CRandomly
DIn stack only
Show Answer & Explanation

Correct Answer: A - Contiguously

Array elements occupy consecutive memory locations, which allows index-based access.

Q3.

What will be the output?

int a[5] = {1,2,3};
printf("%d", a[3]);
A3
B0
CGarbage
DCompilation error
Show Answer & Explanation

Correct Answer: B - 0

Uninitialized elements in a partially initialized array are set to 0.

Q4.
What is the index of the first element in a C array?
A1
B0
C-1
DDepends on array
Show Answer & Explanation

Correct Answer: B - 0

In C, array indexing starts from 0.

Q5.
Which character marks the end of a string in C?
A\n
B\0
C\t
D\r
Show Answer & Explanation

Correct Answer: B - \0

Strings in C are null-terminated, meaning they end with the null character \0.

Q6.

What will be the output?

int arr[3] = {10, 20, 30};
printf("%d", *(arr + 1));
A10
B20
C30
DAddress of 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.

Q7.
What is the correct way to declare a 2D array with 3 rows and 4 columns?
Aint arr[4][3];
Bint arr[3][4];
Cint arr[3,4];
Dint arr{3}{4};
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.

Q8.
What will strlen("Hello") return?
A4
BCompilation error
C6
D5
Show Answer & Explanation

Correct Answer: D - 5

strlen() returns the length of the string excluding the null terminator. "Hello" has 5 characters.

Q9.

What is the output?

char str[] = "Hello";
printf("%lu", sizeof(str));
A5
B4
C6
DDepends on compiler
Show Answer & Explanation

Correct Answer: C - 6

sizeof() includes the null terminator, so "Hello" occupies 6 bytes (5 characters + 1 null character).

Q10.
Which function is used to copy one string to another?
Astrlen()
Bstrcmp()
Cstrcat()
Dstrcpy()
Show Answer & Explanation

Correct Answer: D - strcpy()

strcpy(dest, src) copies the source string to the destination string.

Q11.

What will be the output?

int arr[] = {1, 2, 3, 4, 5};
printf("%d", arr[5]);
AGarbage value
B0
C5
DCompilation error
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.

Q12.
What does strcat() function do?
AConcatenates two strings
BCopies a string
CCompares two strings
DFinds string length
Show Answer & Explanation

Correct Answer: A - Concatenates two strings

strcat(dest, src) appends the source string at the end of the destination string.

Q13.

What is the output?

char *str = "World";
printf("%c", str[0]);
AWorld
BW
CAddress
DCompilation error
Show Answer & Explanation

Correct Answer: B - W

str[0] accesses the first character of the string, which is "W".

Q14.
What will strcmp("abc", "abd") return?
A0
BPositive value
CCompilation error
DNegative value
Show Answer & Explanation

Correct Answer: D - Negative value

strcmp() returns negative if first string is less than second. "abc" < "abd" because 'c' < 'd'.

Q15.
How to pass an array to a function in C?
ABy value only
BBy reference only
CArrays cannot be passed
DBy passing the array name
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.

Q16.

What is the output?

char str1[10] = "Hello";
char str2[10] = "Hello";
printf("%d", str1 == str2);
A1
BRuntime error
CCompilation error
D0
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).

Q17.
What is the maximum number of elements that can be stored in an array int arr[5][4][3]?
A12
B60
C20
D15
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.

Q18.

What is the output?

char str[] = "Hello\0World";
printf("%s", str);
AHello
BHelloWorld
CHello\0World
DCompilation error
Show Answer & Explanation

Correct Answer: A - Hello

printf with %s stops printing at the first null character (\0). So only "Hello" is printed.

Q19.
Which header file is required for string functions like strlen(), strcpy()?
A<stdlib.h>
B<stdio.h>
C<ctype.h>
D<string.h>
Show Answer & Explanation

Correct Answer: D - <string.h>

String manipulation functions are declared in <string.h> header file.

Q20.

What will be the output?

int arr[5] = {0};
printf("%d %d", arr[0], arr[4]);
ACompilation error
B0 garbage
Cgarbage garbage
D0 0
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.

Q21.

What is the output of the following code?

int arr[] = {10, 20, 30, 40, 50};
printf("%d", *(arr + 3));
A40
B30
C20
D50
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.

Q22.

What will be the output?

char str[] = "Hello";
printf("%lu", sizeof(str));
A5
B4
C6
DDepends on compiler
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.

Q23.

What does this code print?

int a[5] = {1, 2, 3};
printf("%d %d", a[3], a[4]);
AGarbage values
B3 3
C0 0
DCompilation error
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.

Q24.

What is the output?

char s1[] = "abc";
char s2[] = "abc";
printf("%d", s1 == s2);
A1
BCompilation error
C0
DUndefined behavior
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.

Q25.

What will strlen() return for the following?

char str[] = "Hello\0World";
printf("%lu", strlen(str));
A11
B5
C10
D12
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.

Q26.

What is the output?

int arr[3][2] = {{1,2},{3,4},{5,6}};
printf("%d", arr[1][1]);
A4
B2
C3
D5
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.

Q27.
Which function is used to concatenate two strings in C?
Astrjoin()
Bstrcat()
Cstradd()
Dstrmerge()
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.

Q28.

What is the output?

char str[10] = "CCAT";
strcat(str, "2026");
printf("%s", str);
ACCAT
B2026
CCCAT2026
DCompilation error
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.

Q29.

What does this code produce?

int arr[] = {5, 10, 15, 20};
int *p = arr;
p++;
printf("%d", *p);
A10
B5
C15
D6
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.

Q30.

What will be the output?

char s[] = "CDAC";
printf("%c", s[4]);
AC
BNull character
CGarbage value
DCompilation error
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'.

Q31.
What is the result of strcmp("apple", "banana")?
APositive value
B0
CNegative value
DUndefined
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.

Q32.

What is the output?

int arr[2][3] = {1,2,3,4,5,6};
printf("%d", *(*(arr+1)+2));
A3
B5
C4
D6
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.

Q33.

What does this code print?

char *p = "Exam";
printf("%c", *(p+2));
AE
Bx
Ca
Dm
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".

Q34.
Which of the following correctly declares a character array to hold the string "Programming"?
Achar s[10] = "Programming";
Bchar s[11] = "Programming";
Cchar s[] = 'Programming';
Dchar s[12] = "Programming";
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.

Q35.

What is the output?

int a[] = {1, 2, 3, 4, 5};
printf("%d", 2[a]);
A2
B3
CCompilation error
DUndefined behavior
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.

Q36.

What will be the output?

char str1[20] = "Hello";
char str2[20] = "World";
strcpy(str1, str2);
printf("%s", str1);
AHello
BCompilation error
CHelloWorld
DWorld
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".

Q37.
What is the size of int arr[5][4] in bytes on a system where int is 4 bytes?
A20
B80
C40
D36
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.

Q38.

What does this code output?

char s[] = "abcdef";
printf("%c", s[sizeof(s)-2]);
Ae
Bd
Cf
DNull character
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.

Q39.

What is the output?

int arr[] = {10, 20, 30, 40};
int *p = arr + 2;
printf("%d", p[-1]);
A10
B20
C30
DCompilation error
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.

Q40.

What happens when you try to modify a string literal?

char *str = "Hello";
str[0] = 'M';
AUndefined behavior
BCompilation error
COutput: Mello
DRuntime warning
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.

Q41.
What does strncpy(dest, src, n) do when strlen(src) < n?
ACopies src and fills remaining with garbage
BReturns an error
CCopies only strlen(src) characters
DCopies src and pads remaining with null characters
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.

Q42.

What is the output?

int a[3] = {5, 10, 15};
int *p = a;
printf("%d", *(p++) + 1);
A5
B11
C6
D16
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.

Q43.
Which statement correctly passes a 2D array to a function?
Avoid func(int arr[][3])
Bvoid func(int arr[][])
Cvoid func(int arr[2])
Dvoid func(int *arr[])
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.

Q44.

What is the output?

char str[] = "CCAT Exam";
char *p = strchr(str, 'E');
printf("%s", p);
AExam
BCCAT Exam
CE
DCCAT
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".

Q45.

What does this code print?

int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d", n);
A4
B20
C5
D1
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.

Q46.

What is the output?

char a[] = "xyz";
char b[] = "xyz";
printf("%d", strcmp(a, b));
A0
B-1
C1
DUndefined
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.

Q47.

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]);
A3
B6
C9
D0
Show Answer & Explanation

Correct Answer: C - 9

The loop sets arr[i] = i*i. For i=3, arr[3] = 3*3 = 9.

Q48.

What is the output?

char str[] = "Hello World";
char *token = strtok(str, " ");
token = strtok(NULL, " ");
printf("%s", token);
AWorld
BHello
CHello World
DNULL
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".

Q49.

What is the output?

int arr[] = {10, 20, 30};
printf("%ld", (long)(&arr[2] - &arr[0]));
A4
B8
C2
D6
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.

Q50.

What does the following code output?

char s[20];
sprintf(s, "%d+%d=%d", 3, 4, 7);
printf("%s", s);
A3+4=7
B%d+%d=%d
C3 4 7
DCompilation error
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".

Showing 1-10 of 50 questions