C Programming

Arrays & Strings — Practice MCQs for CCAT

20 Questions Section B: Programming C Programming

Practice 20 Arrays & Strings multiple-choice questions designed for CDAC CCAT exam preparation. Click "Show Answer" to reveal the correct option with detailed explanation.

Q1.
What is an array in C?
ACollection of different data types
BCollection of same data types
CDynamic memory
DPointer only
Show Answer & Explanation

Correct Answer: B — 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?
ARandomly
BNon-contiguously
CContiguously
DIn stack only
Show Answer & Explanation

Correct Answer: C — 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]);
A0
B3
CGarbage
DCompilation error
Show Answer & Explanation

Correct Answer: A — 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\t
C\0
D\r
Show Answer & Explanation

Correct Answer: C — \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
B5
C6
DCompilation error
Show Answer & Explanation

Correct Answer: B — 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
B6
C4
DDepends on compiler
Show Answer & Explanation

Correct Answer: B — 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?
Astrcpy()
Bstrcmp()
Cstrcat()
Dstrlen()
Show Answer & Explanation

Correct Answer: A — 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]);`
A5
B0
CGarbage value
DCompilation error
Show Answer & Explanation

Correct Answer: C — 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?
ACompares two strings
BCopies a string
CConcatenates two strings
DFinds string length
Show Answer & Explanation

Correct Answer: C — 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]);`
AW
BWorld
CAddress
DCompilation error
Show Answer & Explanation

Correct Answer: A — W

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

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

Correct Answer: C — 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
CBy passing the array name
DArrays cannot be passed
Show Answer & Explanation

Correct Answer: C — 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
B0
CCompilation error
DRuntime error
Show Answer & Explanation

Correct Answer: B — 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
B20
C60
D15
Show Answer & Explanation

Correct Answer: C — 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<string.h>
D<ctype.h>
Show Answer & Explanation

Correct Answer: C — <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]);`
A0 0
B0 garbage
Cgarbage garbage
DCompilation error
Show Answer & Explanation

Correct Answer: A — 0 0

When an array is initialized with {0}, all elements are set to 0. So both arr[0] and arr[4] are 0.