GATE CS 2024 Set 2 | Question 33

Programming & Data Structures › Arrays & Strings · MSQ · 1 mark · Answer: A, B, D

  1. Question 33Programming & Data Structuresmsq1 mark
    int fX(char *a){ char *b=a; while(*b) b++; return b-a; } Which statements is/are TRUE?
    • A.
      fX("abcd") will always return a value
      ✓
    • B.
      With char c[]="abcd" in main, fX(c) always returns a value
      ✓
    • C.
      The function will not compile
    • D.
      With char *c="abcd" in main, fX(c) always returns a value
      ✓

    Answer: A, B, D

    Solution

    Answer: (A), (B) and (D) fX computes the string length by advancing to the terminating '\0'. It works for any valid, null-terminated string: a string literal argument (A), a char array (B), and a char pointer to a literal (D) all give length 4. The code compiles fine, so (C) is false.

More GATE PYQ on Arrays & Strings

All GATE CS Programming & Data Structures PYQ →

Attempt GATE CS 2024 Set 2 in real GATE exam mode

Full paper with a timer, the GATE interface and a topic-wise analysis — free.

Create free account →