Home Subjects C Programming Arrays & Strings

C Programming
Arrays & Strings

C language from basics to advanced placement prep

100 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 61–70 of 100
Topics in C Programming
Q.61 Medium Arrays & Strings
Which function is used to find the first occurrence of a character in a string?
A strstr()
B strchr()
C strtok()
D strrchr()
Correct Answer:  B. strchr()
EXPLANATION

strchr() finds the first occurrence of a character. strrchr() finds the last occurrence. strstr() finds substrings.

Take Test
How many bytes are allocated by: int arr[10][20];?
A 200 bytes
B 400 bytes
C 800 bytes
D 2000 bytes
Correct Answer:  C. 800 bytes
EXPLANATION

10 rows × 20 columns × 4 bytes per int = 800 bytes (assuming 4-byte integer).

Take Test
What is the output of strlen("Hello") in C?
A 5
B 6
C 4
D Runtime error
Correct Answer:  A. 5
EXPLANATION

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

Take Test
Which of the following correctly declares a 2D array of strings?
A char str[5][20];
B char *str[5];
C Both A and B are valid
D char str[5, 20];
Correct Answer:  C. Both A and B are valid
EXPLANATION

Both are valid ways to declare arrays of strings. Option A creates a 2D array, Option B creates an array of pointers to strings.

Take Test
What will be the output of the following code?
int arr[] = {1, 2, 3, 4, 5};
printf("%d", *(arr + 3));
A 4
B 3
C 5
D Compilation error
Correct Answer:  A. 4
EXPLANATION

arr + 3 points to the 4th element (index 3) which is 4. Dereferencing gives 4.

Take Test
Q.66 Medium Arrays & Strings
What is the time complexity of inserting an element at the beginning of an unsorted array?
A O(1) - Constant time
B O(n) - Linear time (need to shift all elements)
C O(log n) - Logarithmic time
D O(n²) - Quadratic time
Correct Answer:  B. O(n) - Linear time (need to shift all elements)
EXPLANATION

Inserting at the beginning requires shifting all n elements one position right, making it O(n). Insertion at the end is O(1) if space exists.

Take Test
In the declaration int *arr[10], what is arr?
A Array of 10 pointers to int
B Pointer to array of 10 integers
C Array of 10 integers
D Pointer to 10 pointers
Correct Answer:  A. Array of 10 pointers to int
EXPLANATION

int *arr[10] declares an array of 10 pointers to int (subscript binds tighter than dereference). Use int (*arr)[10] for pointer to array.

Take Test
What is the correct way to safely copy strings in modern C?
A strcpy(dest, src)
B strncpy(dest, src, sizeof(dest)-1); dest[sizeof(dest)-1]='\0';
C sprintf(dest, "%s", src)
D memcpy(dest, src, strlen(src))
Correct Answer:  B. strncpy(dest, src, sizeof(dest)-1); dest[sizeof(dest)-1]='\0';
EXPLANATION

strncpy with buffer size check and explicit null termination is the standard safe approach. Option A lacks bounds checking; C and D have issues with null termination.

Take Test
Q.69 Medium Arrays & Strings
What happens when you try to modify a string literal in C?
A The modification succeeds and permanent change occurs
B Compile-time error is generated
C Runtime error (segmentation fault or undefined behavior)
D The string is automatically copied before modification
Correct Answer:  C. Runtime error (segmentation fault or undefined behavior)
EXPLANATION

String literals are stored in read-only memory. Attempting modification causes undefined behavior (often segmentation fault). This is why char *str = "text"; str[0]='T'; is dangerous.

Take Test
If ptr is a pointer to int array and ptr = arr, what does ptr[3] represent?
A The 4th element of the array (same as arr[3])
B The address 3 positions ahead in memory
C 12 bytes ahead of array start (assuming 4-byte int)
D Both A and C are true
Correct Answer:  D. Both A and C are true
EXPLANATION

ptr[3] is equivalent to arr[3] and accesses the element at address *(ptr+3). For int*, ptr+3 moves 12 bytes (3×4 bytes) in memory.

Take Test
IGET
iget AI
Online · Ask anything about exams
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