Study the following pseudocode which involves variable scope:
\( \text{SET globalVar TO 10} \)
\( \text{FUNCTION calculate(n)} \)
\( \text{SET localVar TO n * 2} \)
\( \text{SET globalVar TO globalVar + 5} \)
\( \text{RETURN localVar} \)
\( \text{ENDFUNCTION} \)
If the main program calls \( \text{calculate(4)} \) once, what will be the final value of globalVar and the returned result?
Pearson Edexcel IGCSE · Computer Science
Constructs:练习题
2 道选择题即时批改,另有 4 道文字题附完整解题步骤,全部围绕「Constructs」。
Consider the following pseudocode fragment:
\( \text{SET count TO 0} \)
\( \text{FOR r FROM 0 TO 2} \)
\( \text{FOR c FROM 0 TO 1} \)
\( \text{IF vals[r][c] > 10 THEN} \)
\( \text{SET count TO count + 1} \)
\( \text{END IF} \)
\( \text{END FOR} \)
\( \text{END FOR} \)
The 2D array \( \text{vals} \) contains the following data:
Row 0: 12, 5
Row 1: 8, 15
Row 2: 20, 7
What will be the final value of the variable \( \text{count} \) after execution?
Explain the primary difference between a count-controlled iteration and a condition-controlled iteration.
先自己写一遍答案,再对照解题步骤。
An algorithm uses a nested loop to calculate the sum of values in a 2D array. If the inner loop is defined as \( \text{FOR j FROM 0 TO 5} \) but the array rows only contain 4 elements (indices 0 to 3), identify the type of error that occurs and explain its impact on the program execution.
先自己写一遍答案,再对照解题步骤。
Selection and iteration are fundamental programming constructs.
(a) Describe the difference between a count-controlled loop (FOR loop) and a condition-controlled loop (WHILE loop).
(b) Provide one specific programming scenario where a WHILE loop would be a more appropriate choice than a FOR loop.
先自己写一遍答案,再对照解题步骤。
A programmer is writing a subprogram to calculate the discount on a customer order.
(a) Explain the difference between passing by value and passing by reference when providing arguments to a subprogram.
(b) Discuss the benefits of using local variables within this subprogram instead of using global variables to track calculation steps, referring to data security and memory management.
先自己写一遍答案,再对照解题步骤。
* thinka提供的内容由AI生成,可能并非总是准确或最新。请将其用作辅助资源,并与官方材料进行核实。
想多做几道同类题目?立即开始练习这个课题,边做边批改。
立即练习