Given the following function definition int mystery1(int x, int y) { if(x<=y)…

2026

Given the following function definition

int mystery1(int x, int y) {
if(x<=y) return x;
else
return mystery1(x-y, y);
}

What would be the return value of this function call mystery1(15,5)?

  1. A.

    5

  2. B.

    10

  3. C.

    0

  4. D.

    15

Attempted by 314 students.

Show answer & explanation

Correct answer: A

To evaluate mystery1(15,5), we trace the recursive calls step by step:

1. mystery1(15,5): Since 15 > 5, the condition x <= y is false. The function calls mystery1(15-5, 5) = mystery1(10,5).

2. mystery1(10,5): Since 10 > 5, the condition x <= y is false. The function calls mystery1(10-5, 5) = mystery1(5,5).

3. mystery1(5,5): Since 5 <= 5, the condition x <= y is true. The function returns 5.

Thus, the final return value is 5.

हिन्दी उत्तर:

mystery1(15,5) का मान ज्ञात करने के लिए, हम रिकर्शन कॉल का चरण-दर-चरण ट्रेस करते हैं:

1. mystery1(15,5): चूँकि 15 > 5, शर्त x <= y गलत है। फ़ंक्शन mystery1(15-5, 5) = mystery1(10,5) को कॉल करता है।

2. mystery1(10,5): चूँकि 10 > 5, शर्त x <= y गलत है। फ़ंक्शन mystery1(10-5, 5) = mystery1(5,5) को कॉल करता है।

3. mystery1(5,5): चूँकि 5 <= 5, शर्त x <= y सही है। फ़ंक्शन 5 लौटाता है।

इसलिए, अंतिम लौटाए गए मान 5 है।

Explore the full course: Coding For Placement