CS 110: Introduction to Computing Fall 2012 - Section 2 Exam 1 Review Problems # === 1) GRAPHICS ==================== # For all code snippets, assume the Processing module has been loaded and a window created. # What happens when the following code snippets are executed? # --- (1.1) -------------------- ellipseMode(CENTER) ellipse(0, 0, 100, 100) # --- (1.2) -------------------- rectMode(CORNER) rect(0, 0, 100, 100) # --- (1.3) -------------------- w, h = width(), height() line( 0, 0, w, h) # --- (1.4) -------------------- w, h = width(), height() line( w, 0, 0, h) # --- (1.5) -------------------- w, h = width(), height() triangle( 0, 0, w, h, w, 0) # --- (1.6) -------------------- # How would the appearance of the sketch generated by these two function calls differ? w, h = width(), height() rect(0, 0, w, h) w, h = width(), height() quad(0, 0, 0, h, w, h, w, 0) # --- (1.7) -------------------- # How would the appearance of the sketch generated by these two function calls differ? w, h = width(), height() rect(0.5*w, 0, 0.5*w, h) w, h = width(), height() quad(0, 0, 0, h, 0.5*w, h, 0.5*w, 0) # - -- (1.8) -------------------- # What would be the background color after these functions are invoked. background( 255, 0, 0 ) # --- (1.9) -------------------- background( 0, 255,0 ) # --- (1.10) -------------------- background( 0, 0, 255 ) # --- (1.11) -------------------- # What would be drawn as a result of this command? ellipse(10, 10, 50, 40) # a) An ellipse that is wider that it is tall # b) An ellipse that is taller than it is wide # c) A perfect circle with diameter 10 # d) A perfect circle with radius 10 # === 2) OPERATORS AND EXPRESSIONS ==================== # What would happen after executing the following code snippets? # --- (2.1) -------------------- a = 20 b = 10 c = a > b print( c ) # --- (2.2) -------------------- x = [2, 4, 6, 8] print( x ) # --- (2.3) -------------------- i = 1 i += 2 print( i ) # --- (2.4) -------------------- j = 9 print( j % 9 ) print( j % 8 ) print( j % 7 ) print( j % 6 ) print( j % 5 ) print( j % 4 ) print( j % 3 ) print( j % 2 ) print( j % 1 ) # --- (2.5) -------------------- j = 9 print( j // 9 ) print( j // 8 ) print( j // 7 ) print( j // 6 ) print( j // 5 ) print( j // 4 ) print( j // 3 ) print( j // 2 ) print( j // 1 ) # --- (2.6) -------------------- y = 200 print( y > 100 ) # --- (2.7) -------------------- x = 100 y = 200 print( y > 100 and x > 100) # --- (2.8) -------------------- print(-10 > -5) # --- (2.9) -------------------- print(10 >= 10) # --- (2.10) -------------------- print( not True ) print( not not True ) # --- (2.11) -------------------- a = True b = False print( not (a and b) == (not a) or (not b) ) print( not (a or b) == (not a) and (not b) ) # === 3) CONDITIONALS ==================== # What gets printed when each code snippet is executed? # --- (3.1) -------------------- T, F = True, False if T: print("Bazinga!") # --- (3.2) -------------------- T, F = True, False if not F: print("Bazinga!") # --- (3.3) -------------------- T, F = True, False if F or T: print("Bazinga!") # --- (3.4) -------------------- T, F = True, False if F and T: print("Bazinga!") # --- (3.5) -------------------- x = 10 y = 20 if y < 15 and x > 15: print("Bazinga!") # --- (3.6) -------------------- x = 10 y = 20 if y < 15 or x > 15: print("Bazinga!") # --- (3.7) -------------------- x = 10 y = 20 if y < 15 and not x > 15: print("Bazinga!") # --- (3.8) -------------------- x = 10 y = 20 if x < 15: if y > 15: print("Bazinga!") else: print("Shazam!") # --- (3.9) -------------------- x = 10 y = 20 if y > 15: if x > 15: print("Bazinga!") elif y < 15: print("Hey-dee-ho, neighbor!") # --- (3.10) -------------------- # Write a conditional expression (if-statement) that sets fill color to # red when the mouse x-position is greater than 200 and sets to fill to blue otherwise # --- (3.11) -------------------- Write a conditional expression (if-statement) that sets stroke color to yellow when the mouse y-position is in the lower half of the sketch window, and purple in the upper half of the sketch window, regarless of sketch window size y = mouseY() h = height() if y > 0.5*h: stroke(255, 255, 0) else: stroke(255, 0, 255) # --- (3.12) -------------------- # Write a conditional expression that would print to the screen "weekday" # whenever the integer variable, day, is not equal to 0 and not equal to 6 # === 4) LOOPS ==================== # --- (4.1) -------------------- i = 0 while i < 10: print(i) i = i + 1 # --- (4.2) -------------------- i = 10 while i > 0: print(i) i = i - 1 # --- (4.3) -------------------- i = 0 while i > 0: print(i) i = i + 1 # --- (4.4) -------------------- for i in range(10): print(i) # --- (4.5) -------------------- for i in range(10): print("Hello World") # --- (4.6) -------------------- for i in range(10): print(i) i = i + 1 # --- (4.7) -------------------- for i in range(10): i = i + 1 print(i) # --- (4.8) -------------------- for i in range(20): if i % 2 == 0: print(i, " is even") # === (5) FUNCTIONS ==================== # --- (5.1) -------------------- # What is printed when this code fragment executes? def doThis(x): print(x) doMore(x) def doMore(x): print("more: " + str(x)) def main(): a = 'cowbell' b = 'marimba' doThis(a) doMore(b) main() # --- (5.2) -------------------- Write a function named IsValid() that takes one argument named V. The IsValid() function should return True if V is greater than 0 and less than or equal to 255, and False otherwise. # --- (5.3) -------------------- # Write a function named IsOdd() that takes one argument named N. # The function should return True of N is odd, and False if N is even. # --- (5.4) -------------------- # Rewrite the following code using a while-loop. The results of the two code fragments should be identical. num = 65 for i in range(4): print(num) num = num // 2 print(num) # --- (5.5) -------------------- # What is printed by the previous code fragment? # === (6) VARIABLE VISIBILITY ==================== # --- (6.1) -------------------- # What does this code snippet print? x = 10 def start(): test() print(x) def test(): global x x = 20 print(x) start() # --- (6.2) -------------------- # What does this code snippet print? s = 'B' def first(): second() print(s) third() def second(): global s s = 'M' def third(): global s s = 'C' print(s) first() print(s) # --- (6.3) -------------------- # What does this code snippet print? c = 10 def process(): global c c += 1 i = 0 while i<10: process() print(c) i = i + 1 # === (7) LISTS ==================== # --- (7.1) -------------------- # Write a function that prints all the values of the following list, one per line. ['a', 'b', 'c'] # --- (7.2) -------------------- # Write a function that prints all the values of all nested lists, one per line. [['a', 'b', 'c'], ['e', 'f', 'g']] # --- (7.3) -------------------- # Write a function that prints every item on a list with even index ['a', 'b', 'c', 'd', 'e', 'f', 'g'] # 8) DEBUGGING ==================== # --- (8.1) -------------------- Consider a function which toggles the value of a global boolean variable name lightOn that tracks whether a light is on or off. Two people named Left and Right are claiming to be expert light switchers. Both have written a function to toggle the value of lightOn. Both implementations are below. Left's implementation is on the left, and Right's is on the right. # Left's implementation # Right's implementation lightOn = False lightOn = False def toggleLight(): def toggleLight(): global lightOn global lightOn if lightOn: if lightOn: lightOn = False lightOn = False else: if not lightOn: lightOn = True lightOn = True Are these two implementations equivalent? Are both expert light switchers or are one or both of them frauds? Why? Explain your answer. # --- (8.2) -------------------- The following patriotic program is supposed to draw a red rectangle when the mouse is pressed in the left third of the sketch, a white rectangle when pressed in the center third, and a blue circle in the right third. It is printing the red and white rectangle correctly, but not the blue. Why? from Processing import * window(600, 400) def drawRect(o, e): x, y = mouseX(), mouseY() if x < 200: # Red panel fill(255, 0, 0) elif x >= 200: # White panel fill(255) elif x >= 400: # Blue panel fill(0, 0, 255) rect(x, y, 40, 40) onMousePressed += drawRect # --- (8.3) -------------------- Fix the bug in the previous problem. Write your new solution below. # --- (8.4) -------------------- ''' The following program is supposed to draw a red ball at a random location such that the entire ball appears on the sketch. Instead, sometimes it draws the ball so that part of it is off the sketch. Show how you can fix this program. Explain your answer. ''' x = random(width()) y = random(height()) diam = 10 def draw(o, e): fill(255, 0, 0) ellipseMode(CENTER) ellipse(x, y, diam, diam) # 9) CLASSES ==================== # --- (9.1) -------------------- ''' Write a class called Box that has four instance variables named x, y, w, and h which represent the Box's location (x, y), its width (w) and its height (h). The Box constructor should initialize these fields using four arguments passed into it. The Box class has one method called display() that draws the Box instance at its location. A start of the class has already been defined for you, along with a program to use the Box class. Complete the Box class definition ''' from Processing import * window(500, 500) class Box: # Your implementation goes here myBox = Box( random(500), random(500), 40, 30 ) myBox.display() # --- (9.2) -------------------- ''' Define a class called MoodRing that has one instance variables named scale. The scale variable can be set to any value from 0 to 255. A lower value represents a 'blue' mood, and a higher value represents a 'white' mood. The MoodRing class has one method named draw() that draws the ring as an ellipse with the appropriate color, indicated by the value of scale. In other words, a scale value of 0 should draw a blue ring and 255 should draw a white ring. Value in between should draw shades of blue. Following is a start to the class and a test program. ''' from Processing import * window(500, 500) class MoodRing: # Your implementation here m1 = MoodRing(0) m1.draw() # Draws blue ring # --- (9.3) -------------------- ''' Define a class called Organism that has three instance variables named kingdom, order and species. The constructor should take all three values as arguments and initialize instance variables accordingly. The class should have one method called whatAreYou() that prints out a string identifying the organism's classification. ''' class Organism: # Your implementation here human = Organism("Animalia", "Primate", "Homo sapiens") dog = Organism("Animalia", "Carnivora", "Canis lupus") mapleTree = Organism("Plantae", "Sapindales", "Acer Douglasa") human.whatAreYou() # Prints ... # Kingdom: Animalia # Order: Primate # Species: Homo sapiens