‹ Back to the paper
The following function is a part of some class: int jolly(int[ ] x, int n, int m) { if (n < 0)…
The following function is a part of some class:
int jolly(int[ ] x, int n, int m)
{
if (n < 0)
return m;
else if(n<x.length)
m = (x[n] > m)? x[n] : m;
return jolly(x, --n, m);
}(a)[2.0]
What will be the output of jolly( ) when the value of x[ ]={6,3,4,7,1} , n=4 and m=0?
(b)[1.0]
What function does jolly() perform, apart from recursion?
Answer
Answer (a)
AIThe output is 7.
Tracing jolly(x,4,0) with x={6,3,4,7,1}: m is updated at each call to the larger of the current x[n] and m, as n decreases from 4 to 0 (m becomes 1, then 7, and stays 7), then n becomes -1 and m=7 is returned.
Output: 7
Answer (b)
AIApart from recursion, jolly() finds and returns the maximum (largest) element present in the array x[ ].
From ISC 2024 Computer Science Paper 1, question 2(iii).