Data Structure Questions and Answers – Length of a String using Recursion

This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “Length of a String using Recursion”.

1. Consider the following iterative implementation to find the length of the string:

#include<stdio.h>
int get_len(char *s)
{
      int len = 0;
      while(________)
        len++;
      return len;
}
int main()
{
      char *s = "harsh";
      int len = get_len(s);
      printf("%d",len);
      return 0;
}

Which of the following lines should be inserted to complete the above code?
a) s[len-1] != 0
b) s[len+1] != 0
c) s[len] != ‘\0’
d) s[len] == ‘\0’
View Answer

Answer: c
Explanation: The line “s[len] != ‘\0′” should be inserted to complete the above code.
advertisement
advertisement

2. What is the output of the following code?

#include<stdio.h>
int get_len(char *s)
{
      int len = 0;
      while(s[len] != '\0')
        len++;
      return len;
}
int main()
{
      char *s = "lengthofstring";
      int len = get_len(s);
      printf("%d",len);
      return 0;
}

a) 14
b) 0
c) Compile time error
d) Runtime error
View Answer

Answer: a
Explanation: The program prints the length of the string “lengthofstring”, which is 14.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

3. What is the time complexity of the following code used to find the length of the string?

advertisement
#include<stdio.h>
int get_len(char *s)
{
      int len = 0;
      while(s[len] != '\0')
        len++;
      return len;
}
int main()
{
      char *s = "lengthofstring";
      int len = get_len(s);
      printf("%d",len);
      return 0;
}

a) O(1)
b) O(n)
c) O(n2)
d) O(logn)
View Answer

Answer: b
Explanation: The time complexity of the code used to find the length of the string is O(n).
advertisement

4. What is the output of the following code?

#include<stdio.h>
int get_len(char *s)
{
      int len = 0;
      while(s[len] != '\0')
        len++;
      return len;
}
int main()
{
      char *s = "";
      int len = get_len(s);
      printf("%d",len);
      return 0;
}

a) 0
b) 1
c) Runtime error
d) Garbage value
View Answer

Answer: a
Explanation: The program prints the length of an empty string, which is 0.

5. Which of the following can be the base case for the recursive implementation used to find the length of a string?

#include<stdio.h>
int get_len(char *s)
{
      int len = 0;
      while(s[len] != '\0')
        len++;
      return len;
}
int main()
{
      char *s = "";
      int len = get_len(s);
      printf("%d",len);
      return 0;
}

a) if(string[len] == 1) return 1
b) if(string[len+1] == 1) return 1
c) if(string[len] == ‘\0’) return 0
d) if(string[len] == ‘\0’) return 1
View Answer

Answer: c
Explanation: “if(string[len] == ‘\0’) return 0” can be used as base case in the recursive implementation used to find the length of the string.

6. Consider the following recursive implementation used to find the length of a string:

#include<stdio.h>
int recursive_get_len(char *s, int len)
{
      if(s[len] == 0)
        return 0;
      return ________;
}
int main()
{
      char *s = "abcdef";
      int len = recursive_get_len(s,0);
      printf("%d",len);
      return 0;
}

Which of the following lines should be inserted to complete the above code?
a) 1
b) len
c) recursive_get_len(s, len+1)
d) 1 + recursive_get_len(s, len+1)
View Answer

Answer: d
Explanation: The line “1 + recursive_get_len(s, len+1)” should be inserted to complete the code.

7. What is the output of the following code?

#include<stdio.h>
int recursive_get_len(char *s, int len)
{
      if(s[len] == 0)
        return 0;
      return 1 + recursive_get_len(s, len+1);
}
int main()
{
      char *s = "abcdef";
      int len = recursive_get_len(s,0);
      printf("%d",len);
      return 0;
}

a) 5
b) 6
c) 7
d) 8
View Answer

Answer: b
Explanation: The above code prints the length of the string “abcdef”, which is 6.

8. What is the time complexity of the following recursive implementation used to find the length of the string?

#include<stdio.h>
int recursive_get_len(char *s, int len)
{
      if(s[len] == 0)
        return 0;
      return 1 + recursive_get_len(s, len+1);
}
int main()
{
      char *s = "abcdef";
      int len = recursive_get_len(s,0);
      printf("%d",len);
      return 0;
}

a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
View Answer

Answer: b
Explanation: The time complexity of the above recursive implementation used to find the length of the string is O(n).

9. How many times is the function recursive_get_len() called when the following code is executed?

#include<stdio.h>
int recursive_get_len(char *s, int len)
{
      if(s[len] == 0)
        return 0;
      return 1 + recursive_get_len(s, len+1);
}
int main()
{
      char *s = "adghjkl";
      int len = recursive_get_len(s,0);
      printf("%d",len);
      return 0;
}

a) 6
b) 7
c) 8
d) 9
View Answer

Answer: c
Explanation: The function recursive_get_len() is called 8 times when the above code is executed.

10. What is the output of the following code?

#include<stdio.h>
int recursive_get_len(char *s, int len)
{
      if(s[len] == 0)
        return 0;
      return 1 + recursive_get_len(s, len+1);
}
int main()
{
      char *s = "123-1-2-3";
      int len = recursive_get_len(s,0);
      printf("%d",len);
      return 0;
}

a) 3
b) 6
c) 9
d) 10
View Answer

Answer: c
Explanation: The above program prints the length of the string “123-1-2-3”, which is 9.

Sanfoundry Global Education & Learning Series – Data Structures & Algorithms.

To practice all areas of Data Structures & Algorithms, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.