PRASHNIKAप्रश्निका
Back to the paper

The following is a function of class Armstrong. This recursive function calculates and returns the…

Computer Science20253 marksFill in
The following is a function of class Armstrong. This recursive function calculates and returns the sum of the cubes of all the digits of num, where num is an integer data member of the class Armstrong. [A number is said to be Armstrong if the sum of the cubes of all its digits is equal to the original number]. There are some places in the code marked by ?1?, ?2?,?3? which may be replaced by a statement/expression so, that the function works properly.
public int sumOfPowers(int num)  
    { 
            if (num == 0)  
                   return ?1?; 
           int digit = ?2?; 
                  return (int) Math.pow(digit, 3) + ?3?; 
    } 
(a)[1.0]
What is the expression or statement at ?1?
(b)[1.0]
What is the expression or statement at ?2?
(c)[1.0]
What is the expression or statement at ?3?

Answer

Answer (a)

Official answer key
?1? = $0$ - when num becomes 0 there are no more digits left, so the base case of the recursion should return 0.

Answer (b)

Official answer key
?2? = num % 10 - this extracts the last (rightmost/units) digit of num.

Answer (c)

Official answer key
?3? = sumOfPowers(num / 10) - this recursively calls the function on num with its last digit removed, so the cubes of the remaining digits get added to the cube of the current digit.
Implementation of algorithms to solve problems

From ISC 2025 Specimen Computer Science Paper 1, question 2(iv).