arrow
arrow
arrow
Given the following C code snippet: Which of the following statements about the output of the program is correct?
Question



Given the following C code snippet:

Which of the following statements about the output of the program is correct?

A.

Length of str1 is 12, Length of str2 is 1, Length of str3 is 1

B.

Length of str1 is 12, Length of str2 is 1, Length of str3 is 0

C.

Length of str1 is 11, Length of str2 is 0, Length of str3 is 0

D.

Length of str1 is 13, Length of str2 is 1, Length of str3 is 0

E.

Length of str1 is 11, Length of str2 is 1, Length of str3 is 0

Correct option is B


1. str1: "Hello, IBPS!"
· The strlen(str1) function calculates the length of the string excluding the null terminator.
· Therefore, strlen("Hello, IBPS!") returns 12.
2. str2: " "
· The strlen(str2) function calculates the length of the string including the single space character.
· Therefore, strlen(" ") returns 1.
3. str3: ""
· The custom_strlen(str3) function calculates the length of the string using recursion.
· Since the string is empty, the base case *str == '\0' is true on the first call.
· Therefore, custom_strlen("") returns 0.
Important Key Points:
1. String Length Calculation: The strlen function calculates the length of a string by counting the number of characters before the null terminator ('\0').
2. Recursion in custom_strlen: The custom_strlen function demonstrates the use of recursion to calculate the length of a string.
3. Null Terminator: The null terminator ('\0') marks the end of a string in C and is not included in the length returned by strlen.
Knowledge Booster:
1. Recursive Functions: Understanding how recursion works is crucial for implementing functions that solve problems by breaking them down into smaller sub-problems.
2. String Handling in C: Mastering string functions and memory management in C is essential for efficient and error-free programming.
3. Edge Cases in String Handling: Recognizing edge cases, such as empty strings, helps in writing robust code that can handle various input scenarios.

test-prime-package

Access ‘IBPS SO IT Officer’ Mock Tests with

  • 60000+ Mocks and Previous Year Papers
  • Unlimited Re-Attempts
  • Personalised Report Card
  • 500% Refund on Final Selection
  • Largest Community
students-icon
163k+ students have already unlocked exclusive benefits with Test Prime!

Free Tests

Free
Must Attempt

NICL AO Prelims Full Mock Test -01

languageIcon English
  • pdpQsnIcon100 Questions
  • pdpsheetsIcon100 Marks
  • timerIcon60 Mins
languageIcon English
Free
Must Attempt

NICL AO Prelims Full Mock Test -02

languageIcon English
  • pdpQsnIcon100 Questions
  • pdpsheetsIcon100 Marks
  • timerIcon60 Mins
languageIcon English
Free
Must Attempt

NICL AO Prelims Reasoning Section Test -1

languageIcon English
  • pdpQsnIcon35 Questions
  • pdpsheetsIcon35 Marks
  • timerIcon20 Mins
languageIcon English