CPSC 203: Introduction to computers  

James Tam

Final exam review: Computer programming

Question 1: Tracing a program

What is the output of the program show below with the inputs:

(a) 0

(b) 20000

(c) 20001

(d) 40000

(e) 100000

def start (income):

     taxCredit = 0
     taxRate = 0
     taxPaid = 0
     afterTaxIncome = 0
     if (income >= 0) and (income <= 20000):

         taxRate = 0.1
         taxCredit = 1000

     if (income > 20000) and (income <= 50000):

         taxRate = 0.2

     if (income > 50000):

         taxRate = 0.3

  taxPaid = income * taxRate;
  afterTaxIncome = income - taxPaid
  print income, taxPaid, afterTaxIncome + taxCredit

[Solution for Question 1]

 

Question 2: Tracing a program

What is the output of the following program:

def fun1 (num1, num2):

     num1 = num1 * 10

     num2 = num2 * 20

     print num1, num2

def start ():

     num1 = 1

     num2 = 2

     print num1, num2

    fun1 (num1, num2)

    print num1, num2

[Solution for Question 2]

 

Question 3: Modifying a program

For the program shown below you are to write a function 'swap' so that the contents of variable 'num1' and 'num2' are swapped.

def start ():

     num1 = 11

     num2 = 17

     print num1, num2

     num1, num2 = swap (num1, num2)

     print num1, num2

[Solution for Question 3]

 

Multiple choice questions

 
  1. Which of the following is a correct way of defining a program according to the required format for JPython programs?
   
  1. def fun
          write "fun stuff"
   
  1. def fun ():
          write "fun stuff"
   
  1. def fun ():
   
  1. All of the above approaches are correct.
   
  1. None of the above approaches are correct.
 
  1. What is the output of the following program?
    def start ():
     ch = 'X'
     print 'ch', ch
   
  1. ch
  2. X
  3. ch X
  4. X ch
  5. None of the above
 
  1. How may times will the loop shown below run?
    for i in range (0, 10, 1):
   
  1. 1
  2. 9
  3. 10
  4. 11
  5. The loop never runs

[Multiple choice key]