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

 

 Help authour, Buy PDF Ebook   >>>Click Here<<<

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

Click on any option to know the CORRECT ANSWERS

Question 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] != '
D
none of the mentioned
History Questions answers

Question 1 Explanation: 
The line "s[len] != '\0'" should be inserted to complete the above code.

Question 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
English literature Questions answers

Question 2 Explanation: 
The program prints the length of the string "lengthofstring", which is 14.

Question 3
What is the time complexity of the above code used to find the length of the string?
A
O(1)
B
O(n)
C
O(n2)
D
O(logn)
Current affairs Questions answers

Question 3 Explanation: 
The time complexity of the code used to find the length of the string is O(n).

Question 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
Geography Questions answers

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

Question 5
Which of the following can be the base case for the recursive implementation used to find the length of a string?
A
if(string[len] == 1) return 1
B
if(string[len+1] == 1) return 1
C
if(string[len] == '
D
if(string[len] == '\0') return 1
Microbiology Questions answers

Question 5 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.

There are 5 questions to complete.