Programming concepts (3)
Resources |
Revision Questions |
Computer Science
Login to see all questions
Click on a question to view the answer
1.
Question 2
Write a program to calculate the sum of all the numbers from 1 to 100 using a for loop. However, you should only add the numbers that are divisible by both 2 and 3.
Answer:
The program should use a for loop to iterate through numbers from 1 to 100. Inside the loop, it should check if the current number is divisible by both 2 and 3 (i.e., divisible by 6). If it is, the number should be added to a running total.
Here's a possible pseudocode representation:
- Initialize a variable 'total' to 0.
- For each number 'i' from 1 to 100:
- Display the 'total'.
The expected output is 420. This is because the numbers divisible by 6 between 1 and 100 are 6, 12, 18, ..., 96. This is an arithmetic series with a common difference of 6. The number of terms is 16 (96/6 = 16). The sum of an arithmetic series is (n/2)(first term + last term) = (16/2)(6 + 96) = 8 * 102 = 816. There was an error in the previous version. The correct sum is 816.
2.
Consider the following Python code:
x = 10 # Global variable
def my_function():
x = 5 # Local variable
print("Inside function: x =", x)
my_function()
print("Outside function: x =", x)
What output will this code produce? Explain why.
The output of this code will be:
Inside function: x = 5
Outside function: x = 10
Explanation:
The variable x
is declared globally. Inside the my_function
function, a new variable also named x
is declared. This is a local variable. When print("Inside function: x =", x)
is executed, it refers to the local variable x
within the function, which has the value 5. However, the global variable x
retains its original value of 10. The global variable is not modified by the local variable with the same name. Therefore, when print("Outside function: x =", x)
is executed, it refers to the global variable x
, which still has the value 10.
3.
Consider the following pseudocode:
Procedure CalculateArea
Input radius
Area = 3.14 * radius * radius
Display Area
End Procedure
Explain what this procedure does and how it would be used in a larger program. What are the advantages of using a procedure in this case?
Explanation: The pseudocode defines a procedure called "CalculateArea". This procedure takes a single input parameter, 'radius', which represents the radius of a circle. Inside the procedure, the area of the circle is calculated using the formula 3.14 * radius * radius. Finally, the calculated area is displayed to the user.
Use in a larger program: This procedure could be used in a program designed to calculate the area of multiple circles. For example, a program that manages a collection of circular shapes could call this procedure for each circle to determine its area. The procedure would be called with the radius of each circle as input.
Advantages of using a procedure:
- Reusability: The procedure can be called multiple times with different radii without rewriting the area calculation code.
- Modularity: It breaks down the larger program into smaller, more manageable parts, making the code easier to understand and maintain.
- Readability: Using a named procedure (like 'CalculateArea') makes the code more readable and self-documenting.