slug
type
status
category
summary
date
tags
password
icon
Question 1:
Solution to Question 1:
Let's analyze the
mystery
method step by step for the input 1234
.Method Breakdown:
Execution Steps for
mystery(1234)
:- First Call:
mystery(1234)
- Print:
1234 % 10
→4
- Check:
1234 / 10
→123
(not zero) - Recursive Call:
mystery(123)
- Second Call:
mystery(123)
- Print:
123 % 10
→3
- Check:
123 / 10
→12
(not zero) - Recursive Call:
mystery(12)
- Third Call:
mystery(12)
- Print:
12 % 10
→2
- Check:
12 / 10
→1
(not zero) - Recursive Call:
mystery(1)
- Fourth Call:
mystery(1)
- Print:
1 % 10
→1
- Check:
1 / 10
→0
(termination condition) - Print Again:
1 % 10
→1
- Return to Third Call
- Returning to Third Call:
mystery(12)
- Print Again:
12 % 10
→2
- Return to Second Call
- Returning to Second Call:
mystery(123)
- Print Again:
123 % 10
→3
- Return to First Call
- Returning to First Call:
mystery(1234)
- Print Again:
1234 % 10
→4
Sequence of Prints:
- From First Call:
4
- From Second Call:
3
- From Third Call:
2
- From Fourth Call:
1
and1
- Returning to Third Call:
2
- Returning to Second Call:
3
- Returning to First Call:
4
Final Output:
43211234
Answer:
(D) 43211234
Question 1:
Solutions to Question 2:
First, let's trace the execution step-by-step. The given method is:
We are interested in
recur(27)
.- Call
recur(27)
: - Since 27 > 10, it doesn't return immediately. Instead, it will return
recur(recur(27 / 3))
. - Compute 27/3 = 9, so this becomes
recur(recur(9))
.
- Call
recur(9)
: - Since 9 ≤ 10, it returns 9 * 2 = 18.
- So
recur(9) = 18
. recur(27) = recur(18)
.
Substituting back into the original call:
- Call
recur(18)
: - 18 > 10, so it returns
recur(recur(18 / 3))
. - Compute 18/3 = 6, so this becomes
recur(recur(6))
.
- Call
recur(6)
: - 6 ≤ 10, so return 6 * 2 = 12.
- Thus
recur(6) = 12
. recur(18) = recur(12)
.
Substitute this back:
- Call
recur(12)
: - 12 > 10, so it returns
recur(recur(12 / 3))
. - Compute 12/3 = 4, so this becomes
recur(recur(4))
.
- Call
recur(4)
: - 4 ≤ 10, so return 4 * 2 = 8.
- Thus
recur(4) = 8
. recur(12) = recur(8)
.
Substitute this back:
- Call
recur(8)
: - 8 ≤ 10, so return 8 * 2 = 16.
- Thus
recur(8) = 16
.
Substituting all the way back up:
recur(12) = 16
recur(18) = recur(12) = 16
recur(27) = recur(18) = 16
Final Answer: 16
This corresponds to choice (D).
- 作者:现代数学启蒙
- 链接:https://www.math1234567.com/csa001questions
- 声明:本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。
相关文章