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 21–30 of 49
Topics in C Programming
Q.21 Medium Arrays & Strings
What is the output of the following code?
char str[] = "GATE";
printf("%d", sizeof(str));
A 4
B 5
C 8
D Compilation error
Correct Answer:  B. 5
EXPLANATION

sizeof(str) returns 5 because the array includes 4 characters plus 1 null terminator.

Test
Q.22 Medium Arrays & Strings
Which of the following correctly reverses a string in-place?
A strrev(str);
B reverse(str);
C str_reverse(str);
D reversestr(str);
Correct Answer:  A. strrev(str);
EXPLANATION

strrev() is a non-standard but commonly used function (available in string.h in some compilers) that reverses a string in-place.

Test
Q.23 Medium Arrays & Strings
What happens when you try to modify a string literal in C?
char *str = "Hello";
str[0] = 'J';
A String is successfully modified to 'Jello'
B Undefined behavior or runtime error
C Compilation error
D String remains 'Hello'
Correct Answer:  B. Undefined behavior or runtime error
EXPLANATION

String literals are stored in read-only memory. Attempting to modify them causes undefined behavior or segmentation fault.

Test
Q.24 Medium Arrays & Strings
What is the correct way to declare a pointer to a character array?
A char *ptr[];
B char (*ptr)[];
C *char ptr[];
D char **ptr;
Correct Answer:  B. char (*ptr)[];
EXPLANATION

char (*ptr)[] declares ptr as a pointer to a character array. The parentheses are crucial for correct precedence.

Test
Q.25 Medium Arrays & Strings
What does strtok(str, delim) return on each call until the string ends?
A The entire remaining string
B A pointer to the next token
C The delimiter character
D The length of the token
Correct Answer:  B. A pointer to the next token
EXPLANATION

strtok() returns a pointer to the next token separated by the delimiter. Returns NULL when no more tokens exist.

Test
Q.26 Medium Arrays & Strings
Consider: char str[10]; strcpy(str, "Hello"); What is the state of memory after this?
A str[5] contains '\0', str[6] is uninitialized
B All elements from str[0] to str[9] contain characters
C str[0] to str[4] have 'H','e','l','l','o', str[5] has '\0'
D Runtime error occurs
Correct Answer:  C. str[0] to str[4] have 'H','e','l','l','o', str[5] has '\0'
EXPLANATION

strcpy copies "Hello" and adds null terminator at position 5. Positions 6-9 remain uninitialized.

Test
Q.27 Medium Arrays & Strings
What does the following code do?
char str[] = "HELLO";
for(int i = 0; str[i]; i++) str[i] = str[i] + 32;
A Converts uppercase to lowercase
B Converts lowercase to uppercase
C Shifts ASCII values by 32
D Causes compilation error
Correct Answer:  A. Converts uppercase to lowercase
EXPLANATION

Adding 32 to uppercase ASCII values converts them to lowercase (A=65, a=97, difference=32).

Test
Q.28 Medium Arrays & Strings
What is the correct syntax to concatenate two strings safely in modern C?
A strcat(dest, src)
B strncat(dest, src, n)
C sprintf(dest, "%s%s", dest, src)
D Both B and C are safer options
Correct Answer:  D. Both B and C are safer options
EXPLANATION

strcat() is unsafe due to buffer overflow risk. strncat() and sprintf() provide bounds checking.

Test
Q.29 Medium Arrays & Strings
Which of the following will correctly compare two strings?
A if (str1 == str2)
B if (strcmp(str1, str2) == 0)
C Both A and B
D Neither A nor B
Correct Answer:  B. if (strcmp(str1, str2) == 0)
EXPLANATION

A compares pointers, not string content. strcmp() compares actual string values character by character.

Test
Q.30 Medium Arrays & Strings
Consider the code: int *p; int arr[5] = {10, 20, 30, 40, 50}; p = arr; What is *(p+2)?
A 20
B 30
C 40
D 50
Correct Answer:  B. 30
EXPLANATION

p points to arr[0]. p+2 points to arr[2], which contains 30.

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