ProgrammingModerateTCS NQT · Wipro

What will be the output of the following pseudocode? (All variables are integers, so / is integer division)

What will be the output of the following pseudocode? (All variables are integers, so / is integer division)
Function count7(Integer n)
    if (n == 0)
        return 0
    end if
    Integer here = 0
    if (n mod 10 == 7) here = 1
    return here + count7(n / 10)
End Function
Print count7(771707), count7(70)
  1. A. 3 0
  2. B. 4 2
  3. C. 5 1
  4. D. 4 1

Correct answer: D. 4 1

The function checks the last digit and recurses on the rest. 771707 has four 7's; 70 has one (its last digit 0 does not count, but the tens digit does). Output 4 1.

More Pseudocode and Code Tracing questions

Exams that ask this topic