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.

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.

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?