Home Subjects C Programming Arrays & Strings

C Programming
Arrays & Strings

C language from basics to advanced placement prep

49 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 31–40 of 49
Topics in C Programming
Q.31 Medium Arrays & Strings
What will be the output?
char arr[] = "ABC";
printf("%d", sizeof(arr));
A 3
B 4
C Depends on compiler
D Runtime error
Correct Answer:  B. 4
EXPLANATION

sizeof includes the null terminator. "ABC" has 4 bytes (A, B, C, \0).

Test
Q.32 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.

Test
Q.33 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.

Test
Q.34 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.

Test
Q.35 Medium Arrays & Strings
Which string function modifies the original string in-place?
A strtok()
B strlen()
C strcpy()
D Both A and C
Correct Answer:  D. Both A and C
EXPLANATION

Both strtok() (modifies by replacing delimiters with '\0') and strcpy() (copies and overwrites destination) modify strings in-place.

Test
Q.36 Medium Arrays & Strings
What is the output of sizeof(arr)/sizeof(arr[0]) when arr is declared as int arr[10]?
A 10
B 40
C 4
D Depends on system architecture
Correct Answer:  A. 10
EXPLANATION

This expression calculates array length. sizeof(arr) gives total bytes (40 on 32-bit system), sizeof(arr[0]) gives bytes per element (4), so 40/4=10.

Test
Q.37 Medium Arrays & Strings
In a 2D array int matrix[4][5], which statement about memory layout is true?
A Elements are stored in row-major order (row by row)
B Elements are stored in column-major order (column by column)
C Elements are randomly scattered in memory
D Memory layout depends on the compiler version
Correct Answer:  A. Elements are stored in row-major order (row by row)
EXPLANATION

C uses row-major order for 2D arrays. matrix[0][0], matrix[0][1]...matrix[0][4], matrix[1][0]... are stored sequentially in memory.

Test
Q.38 Medium Arrays & Strings
If str = "Hello", what is the value of str[5]?
A '\0' (null character)
B 'o'
C Garbage value
D Error - index out of bounds
Correct Answer:  A. '\0' (null character)
EXPLANATION

"Hello" has 5 characters, so valid indices are 0-4. Index 5 contains the null terminator '\0' that marks the end of the string.

Test
Q.39 Medium Arrays & Strings
What is the relationship between array name and pointer in C?
A Array name decays to a pointer to its first element in most contexts
B Array name is exactly equivalent to a pointer variable
C Array names and pointers have no relationship
D Array name is a constant pointer that can be modified
Correct Answer:  A. Array name decays to a pointer to its first element in most contexts
EXPLANATION

Array names decay to pointers to the first element in expressions, but they are not truly pointer variables. The array name cannot be reassigned.

Test
Q.40 Medium Arrays & Strings
Which of the following correctly declares and initializes a 2D array?
A int arr[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
B int arr[3][3] = {1,2,3,4,5,6,7,8,9};
C Both A and B are correct
D Neither A nor B is valid
Correct Answer:  C. Both A and B are correct
EXPLANATION

Both syntax forms are valid in C. Elements can be provided in nested braces or as a linear list, and they fill row by row.

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