English

The following function workOut() is a part of some class. Assume 'n' is a positive integer. String workOut (int n) { if (n == 0) - Computer Science (Theory)

Advertisements
Advertisements

Question

The following function workOut() is a part of some class. Assume 'n' is a positive integer.

String workOut (int n)

{ if (n == 0)

       return"";

    int rem = n% 16;

    char cr = (rem < 10)? (char) (rem + '0'): (char) (rem - 10+'A');

    return workOut (n/16) + cr;

}

Answer the questions given below with the dry run / working.

  1. What will the function workOut(220) return?   [2]
  2. What is the function workOut() performing apart from recursion?   [1]
Short Answer
Advertisements

Solution

(a) Dry run:

220 ÷ 16 → Quotient = 13, Remainder = 12 → C

13 ÷ 16 → Quotient = 0, Remainder = 13 → D

Function calls:

workOut(220) = workOut(13) + 'C'

workOut(13) = workOut(0) + 'D'

workOut(0) = ""

workOut(13) = "D"

workOut(220) = "D" + "C"

workOut(220) = "DC"

(b) The function converts a decimal number into its hexadecimal equivalent using recursion.

shaalaa.com
  Is there an error in this question or solution?
2025-2026 (March) Official Board Paper
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×