Home Subjects C Programming Arrays & Strings

C Programming
Arrays & Strings

C language from basics to advanced placement prep

30 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 21–30 of 30
Topics in C Programming
What does strlen() function return for the string "C2025"?
A 5
B 6
C 4
D Undefined behavior
Correct Answer:  A. 5
EXPLANATION

strlen() counts characters before the null terminator. "C2025" has 5 characters, so strlen returns 5. The null terminator is not counted.

Test
What is the time complexity of accessing an element at index 5 in an array?
A O(1) - Constant time
B O(n) - Linear time
C O(log n) - Logarithmic time
D O(n²) - Quadratic time
Correct Answer:  A. O(1) - Constant time
EXPLANATION

Array access by index is O(1) because arrays use contiguous memory allocation, allowing direct access via base address + offset calculation.

Test
How much memory is allocated for char name[50] declaration?
A 50 bytes
B 49 bytes
C 51 bytes
D Depends on compiler
Correct Answer:  A. 50 bytes
EXPLANATION

char name[50] allocates exactly 50 bytes. The null terminator '\0' must fit within this 50-byte allocation.

Test
In C, a string is internally represented as:
A An array of characters terminated by '\0'
B An array of integers with length prefix
C A special data type with built-in length
D A pointer to the first character with separate length variable
Correct Answer:  A. An array of characters terminated by '\0'
EXPLANATION

In C, strings are null-terminated character arrays. The null character '\0' marks the end of the string.

Test
If an integer array arr[10] is declared globally in C, what will be the initial values of its elements?
A All elements initialized to 0
B All elements contain garbage values
C All elements initialized to 1
D Elements are uninitialized until explicitly set
Correct Answer:  A. All elements initialized to 0
EXPLANATION

Global arrays in C are automatically initialized to 0 by default. Only local arrays contain garbage values.

Test
Consider a 2D array declared as: int matrix[3][4]. How many elements does it have?
A 3
B 4
C 7
D 12
Correct Answer:  D. 12
EXPLANATION

A 2D array with dimensions 3x4 has 3*4 = 12 total elements.

Test
What does the following code do? strcpy(dest, src);
A Compares two strings
B Copies string src to dest
C Concatenates two strings
D Finds length of string
Correct Answer:  B. Copies string src to dest
EXPLANATION

strcpy() copies the contents of the source string to the destination string.

Test
Which of the following is NOT a valid string initialization?
A char str[] = "Program";
B char str[10] = {'P','r','o','g'};
C char str[10]; str = "Program";
D char *str = "Program";
Correct Answer:  C. char str[10]; str = "Program";
EXPLANATION

Array names are constant pointers and cannot be reassigned. Direct assignment to char array after declaration is invalid.

Test
What will be the output of strlen("Hello") in C?
A 4
B 5
C 6
D Error
Correct Answer:  B. 5
EXPLANATION

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

Test
What is the correct way to declare a 1D array of 10 integers in C?
A int arr[10];
B int arr(10);
C int arr{10};
D int [10]arr;
Correct Answer:  A. int arr[10];
EXPLANATION

In C, arrays are declared with the syntax: datatype arrayname[size]. Option A follows the correct syntax.

Test
IGET
IGET AI
Online · Exam prep assistant
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips