LC101 Class 1.10                                                          Name: ____________________

Review Questions (Ch. 10-11)

1. What is the printout of the following code?

  1. universe_a = [1, 2, 3, 4, 5]  
  2. universe_b = universe_a  
  3.   
  4. def parallel_universe(b):  
  5.     b[2] = 42  
  6.   
  7. parallel_universe(universe_a)  
  8. print(universe_b[2])  
  9. print(universe_a[2]) 

2. Identify the compiler error. What might the user have intended to print?

  1. def failing_to_scramble(ingredients):  
  2.     for x in range(len(ingredients)):  
  3.         temp = x  
  4.         ingredients[x] = ingredients[x+1]  
  5.         ingredients[x+1] = temp  
  6.   
  7. ingredients = ('e''g''g''s')  
  8. print(failing_to_scramble(ingredients))  

3. What is the printout of the following function?

  1. def shuffle_n_cheat(deck):  
  2.     new_deck = []  
  3.     for card in deck:  
  4.         new_deck.append(card)  
  5.     new_deck[len(deck) - 1] = new_deck[0]  
  6.     return new_deck  
  7. deck = ["A♠", "4♥", "8♣", "J♦"]  
  8. print(shuffle_n_cheat(deck)) 

4. What is the printout of the following code?

  1. def control(the_matrix):  
  2.     for i in range(len(the_matrix)):  
  3.         for j in range(len(the_matrix[i])):  
  4.             if i == j:  
  5.                 the_matrix[i][j] = 0  
  6.                   
  7. the_matrix = [[1,0,0],  
  8.               [0,1,0],  
  9.               [0,0,1]]  
  10. control(the_matrix)  
  11. print(the_matrix)