What will be the output of the following code?
char str[] = "Code";
printf("%c", str[2]);
Answer: C
String "Code" has indices: C(0), o(1), d(2), e(3). str[2] refers to 'd'.
Q.2Medium
What is the difference between char str[] = "Test" and char *str = "Test"?
Answer: B
char[] creates a modifiable array with its own storage. char* points to read-only string literal in memory.
Q.3Medium
Which function is used to reverse a string in C library?
Answer: C
There is no standard library function for string reversal. strrev() is non-standard (available in some compilers). Manual reversal or custom functions are needed.
Q.4Medium
What is the output of the following code?
int arr[] = {10, 20, 30};
printf("%d", sizeof(arr)/sizeof(arr[0]));
Answer: C
sizeof(arr) gives total bytes of array. sizeof(arr[0]) gives bytes of one element. Division gives number of elements: 3.
Q.5Medium
What will be printed by: printf("%s", "Hello\0World");?
Answer: B
\0 is the null terminator which marks the end of a string. %s stops printing at the first null character.
Advertisement
Q.6Medium
Consider: int *ptr = arr; where arr is an array. What does ptr[2] represent?
Answer: B
ptr[2] is equivalent to *(ptr+2), which points to the third element (index 2) of the array.
Q.7Medium
What will the following code output?
char str[20];
strcpy(str, "Competitive");
strcat(str, " Exam");
printf("%s", str);
Answer: C
strcpy copies "Competitive" to str. strcat appends " Exam" to it, resulting in "Competitive Exam".
Q.8Medium
What is the time complexity of searching for an element in an unsorted array of size n?
Answer: C
Linear search on unsorted array requires checking each element in worst case, giving O(n) time complexity.
Q.9Medium
What will be the output of this code?
int arr[5];
for(int i=0; i<5; i++) arr[i] = i*i;
printf("%d %d %d", arr[1], arr[2], arr[4]);
Which of the following correctly declares and initializes a 2D array?
Answer: C
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.
Q.11Medium
What is the relationship between array name and pointer in C?
Answer: A
Array names decay to pointers to the first element in expressions, but they are not truly pointer variables. The array name cannot be reassigned.
Q.12Medium
If str = "Hello", what is the value of str[5]?
Answer: A
"Hello" has 5 characters, so valid indices are 0-4. Index 5 contains the null terminator '\0' that marks the end of the string.
Q.13Medium
In a 2D array int matrix[4][5], which statement about memory layout is true?
Answer: A
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.
Q.14Medium
What is the output of sizeof(arr)/sizeof(arr[0]) when arr is declared as int arr[10]?
Answer: A
This expression calculates array length. sizeof(arr) gives total bytes (40 on 32-bit system), sizeof(arr[0]) gives bytes per element (4), so 440=10.
Q.15Medium
Which string function modifies the original string in-place?
Answer: D
Both strtok() (modifies by replacing delimiters with '\0') and strcpy() (copies and overwrites destination) modify strings in-place.
Q.16Medium
What happens when you try to modify a string literal in C?
Answer: C
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.
Q.17Medium
What is the time complexity of inserting an element at the beginning of an unsorted array?
Answer: B
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.
Q.18Medium
Which function is used to find the first occurrence of a character in a string?
Answer: B
strchr() finds the first occurrence of a character. strrchr() finds the last occurrence. strstr() finds substrings.
Q.19Medium
What will be the output?
char arr[] = "ABC";
printf("%d", sizeof(arr));
Answer: B
sizeof includes the null terminator. "ABC" has 4 bytes (A, B, C, \0).
Q.20Medium
Consider the code: int *p; int arr[5] = {10, 20, 30, 40, 50}; p = arr; What is *(p+2)?
Answer: B
p points to arr[0]. p+2 points to arr[2], which contains 30.