‹ Back to the paper
The following function think( ) is part of some class. Assume is the lower bound and is the upper…
The following function think( ) is part of some class. Assume $f$ is the lower bound and $l$ is the upper bound of $arr[\ ]$.
Answer the questions given below along with the dry run / working.
int think(int arr[ ], int f, int l)
{
if (f > l)
return 0;
else
{
int current = (arr[f] % 2 == 0) ? arr[f] : 0;
return current + think (arr, f + 1, l);
}
}(a)[2.0]
What will the function think( ) return, if $arr[\ ] = \{9, 7, 12, 16, 19, 25\}$, $f = 0$ and $l = 5$?
(b)[1.0]
What is the function think( ) performing apart from recursion?
Answer
Answer (a)
AIDry run: think(arr,0,5) for arr[] = {9,7,12,16,19,25}
think(0): arr[0]=9 (odd) → current=0 → 0 + think(1)
think(1): arr[1]=7 (odd) → current=0 → 0 + think(2)
think(2): arr[2]=12 (even) → current=12 → 12 + think(3)
think(3): arr[3]=16 (even) → current=16 → 16 + think(4)
think(4): arr[4]=19 (odd) → current=0 → 0 + think(5)
think(5): arr[5]=25 (odd) → current=0 → 0 + think(6)
think(6): f=6 > l=5 → returns 0
Unwinding: think(5)=0, think(4)=0, think(3)=16+0=16, think(2)=12+16=28, think(1)=0+28=28, think(0)=0+28=28
think() returns 28
Answer (b)
AIApart from recursion, the function computes and returns the sum of all the even elements present in the array arr[] from index f to index l.
From ISC 2025 Improvement Computer Science Paper 1, question 2(iii).