GATE CS 2024 Set 2 — Question 33
Go beyond PYQs with Success TrackerAI-powered personalised practice and doubt support. Unlimited practice on eligible plans; AI usage limits apply.Programming & Data Structures → C Programming → Arrays & Strings in C
Last updated
Question
int fX(char *a){
char *b = a;
while(*b)
b++;
return b - a;}
Correct answer
(A) The function call fX("abcd") will always return a value; (B) Assuming a character array c is declared as char c[] = "abcd" in main(), the function call fX(c) will always return a value; (D) Assuming a character pointer c is declared as char c = "abcd" in main(), the function call fX(c) will always return a value
Solution
strlen). It operates as follows:while loop until it encounters the null terminator (\0).- Option (A):
"abcd"is a string literal. In C, string literals are stored in a read-only data segment and are automatically null-terminated. Passing this to is valid, and the function will return 4. Thus, it will always return a value. - Option (B):
char c[] = "abcd"declares a local character array and initializes it with the characters 'a', 'b', 'c', 'd', and '\0'. This is a valid null-terminated string. Passing it to will return 4. Thus, it will always return a value. - Option (C): The code is syntactically correct C. It uses valid pointer arithmetic and standard control flow. It will compile without errors.
- Option (D):
char *c = "abcd"makes point to the first character of the null-terminated string literal"abcd". Passing this pointer to will return 4. Thus, it will always return a value.
Continue learning with Success Tracker
A step still unclear? Work through it with support
Use Success Tracker to ask about the reasoning, then try another GATE CS question to check your understanding.
- PYQs with solutions
- Attempt available previous-year questions, then compare your reasoning with the worked solution. Coverage varies by stream.
- Practice that adapts
- Choose a topic, work on weaker areas and bookmark questions to revisit. Your attempts feed your progress tracking.
- AI doubt support
- Ask follow-up questions about a step or concept while practising, instead of stopping at the final answer.
Unlimited practice is available on eligible plans. Free practice and AI usage have limits; check the current plan allowances before choosing.
This page stays readable without an account. AI responses can be wrong; check them against the solution and source material.