Home Subjects C Programming

C Programming

C language from basics to advanced placement prep

499 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 271–280 of 499
Topics in C Programming
Q.271 Medium Arrays & Strings
Consider: int arr[10]; int *ptr = arr; What does ptr[5] represent?
A The 5th element of array arr
B Address of the 5th element
C Value at address arr + 5*sizeof(int)
D Both A and C
Correct Answer:  D. Both A and C
EXPLANATION

ptr[5] is equivalent to *(ptr+5), which accesses the 5th element. Pointer arithmetic adjusts by the size of int.

Take Test
Q.272 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.

Take Test
Q.273 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.

Take Test
Q.274 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.

Take Test
Q.275 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.

Take Test
Q.276 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.

Take Test
Q.277 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.

Take Test
Q.278 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).

Take Test
Q.279 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.

Take Test
Q.280 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.

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