




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A practice exam for an introductory course on interactive programming in python. It includes multiple-choice questions covering topics such as simplegui handlers, collision detection, data types, lambda expressions, object-oriented design, and game loop mechanics. Each question is accompanied by a detailed explanation of the correct answer, making it a valuable resource for students preparing for certification or seeking to reinforce their understanding of python programming concepts in a game development context. The exam focuses on practical application and problem-solving skills relevant to interactive programming.
Typology: Exams
1 / 102
This page cannot be seen from the preview
Don't miss anything!





























































































Question 1. In the RiceRocks project, which variable typically stores the number of lives the player has left? A) score B) lives C) time D) started Answer: B Explanation: The lives variable tracks how many chances the player has before the game ends. Question 2. Which SimpleGUI handler is responsible for drawing all visual elements on the canvas each frame? A) keydown handler B) mouseclick handler C) draw handler D) timer handler Answer: C Explanation: The draw handler is called repeatedly to render the background, ship, rocks, missiles, and UI text. Question 3. What is the purpose of the group_collide function in the game? A) To update positions of all rocks B) To check collisions between a single object and a group of objects C) To spawn new missiles D) To reset the score
Answer: B Explanation: group_collide iterates through a set of sprites (e.g., rocks) and tests each for collision with a single sprite (e.g., the ship). Question 4. Which Python data type is ideal for fast membership testing when checking if a missile already exists in a collection? A) list B) tuple C) set D) dictionary Answer: C Explanation: Sets provide O(1) average‑case membership tests, making them efficient for checking existence. Question 5. In the context of the game, what does the dist helper function compute? A) The angle between two vectors B) The Euclidean distance between two points C) The dot product of two velocities D) The magnitude of a sprite’s velocity Answer: B Explanation: dist(p, q) returns the straight‑line distance between positions p and q, used for collision detection.
Answer: A Explanation: lambda x: x * 2 returns twice the input and works with map to apply it to every list element. Question 9. In an object‑oriented design, which method is automatically called when a new instance of a class is created? A) __str__ B) __repr__ C) __init__ D) __del__ Answer: C Explanation: __init__ is the constructor that initializes instance attributes. Question 10. When a missile collides with a rock, which of the following actions should usually occur? A) Increase the ship’s lives B) Decrease the score C) Remove both missile and rock from their groups D) Freeze the game timer Answer: C Explanation: Collision handling removes the missile and the rock, optionally spawning smaller rocks and awarding points. Question 11. Which SimpleGUI function creates a timer that calls a handler every 1000 milliseconds?
A) simplegui.create_draw_handler B) simplegui.create_timer(1000, handler) C) simplegui.create_keydown_handler D) simplegui.create_mousehandler Answer: B Explanation: simplegui.create_timer(interval, handler) sets up a periodic callback. Question 12. What is the effect of calling sound.play() inside the ship’s thrust keydown handler? A) It pauses the background music. B) It starts the thrust sound effect while the key is held. C) It reduces the ship’s velocity. D) It resets the game score to zero. Answer: B Explanation: Playing the thrust sound gives auditory feedback for the thrust action. Question 13. Which collection type is most appropriate for storing all active missile objects when order does not matter? A) list B) tuple C) set D) dict Answer: C
A) All elements whose first coordinate is positive are kept. B) All elements are doubled. C) All elements are removed. D) The list is sorted in ascending order. Answer: A Explanation: filter retains items for which the lambda returns True; here, it keeps points with a positive x‑value. Question 17. Which attribute of a sprite typically determines its collision radius? A) image_center B) radius C) angle_vel D) velocity Answer: B Explanation: The radius attribute defines the size of the circular collision boundary. Question 18. In the game loop, why is it important to call group.update() before drawing the group? A) To change the background color. B) To ensure positions are current for the visual frame. C) To pause the timer. D) To reset the score. Answer: B
Explanation: Updating moves each sprite according to physics; drawing afterwards shows the new positions. Question 19. Which of the following statements about generators is TRUE? A) They store all generated values in memory at once. B) They can only be iterated once. C) They require the return keyword to produce values. D) They cannot be used with the for statement. Answer: B Explanation: Generators yield values lazily and become exhausted after a single complete iteration. Question 20. What does the map function return in Python 3? A) A list of transformed items. B) A dictionary of transformed items. C) A map object (iterator). D) Nothing; it modifies the original iterable in place. Answer: C Explanation: In Python 3, map returns a lazy iterator; converting it to a list materializes the results. Question 21. Which of these is a correct way to define a class Rock that inherits from a generic Sprite class? A) class Rock(Sprite):
Question 24. When a ship is thrusting, which physical quantity is typically increased? A) Angular velocity B) Linear velocity (acceleration) C) Collision radius D) Score Answer: B Explanation: Thrust adds acceleration in the direction the ship is facing, changing its velocity. Question 25. Which of the following is NOT a valid set operation in Python? A) set1.union(set2) B) set1.intersection(set2) C) set1.difference(set2) D) set1.append(set2) Answer: D Explanation: Sets have no append method; they use add for single elements. Question 26. In the draw handler, why is it necessary to call canvas.draw_text after drawing the background image? A) To ensure the text appears on top of the background. B) To reset the canvas coordinates. C) To change the background image. D) To stop the timer.
Answer: A Explanation: Drawing order matters; later draws overlay earlier ones, so text must be drawn after the background. Question 27. Which of the following best describes the role of the __repr__ method? A) Provides an official string representation useful for debugging. B) Returns the length of the object. C) Executes when the object is deleted. D) Controls how the object is compared with others. Answer: A Explanation: __repr__ should return a string that, if possible, can recreate the object and is helpful for developers. Question 28. What is the primary advantage of using a set to store rocks instead of a list? A) Sets maintain insertion order. B) Sets allow duplicate rocks. C) Membership tests and removal are faster (average O(1)). D) Sets can be indexed by position. Answer: C Explanation: Sets provide constant‑time membership checking and removal, which is useful for collision handling.
Answer: A Explanation: Index 0 of the tuple corresponds to the x‑coordinate. Question 32. When using filter to keep only rocks with a radius greater than 15, which call is appropriate? A) filter(lambda r: r.radius > 15, rock_set) B) filter(lambda r: r.radius < 15, rock_set) C) filter(lambda r: r.radius == 15, rock_set) D) filter(lambda r: r.radius != 15, rock_set) Answer: A Explanation: The lambda returns True for rocks whose radius exceeds 15, and filter retains those. Question 33. What does the update method of a sprite typically return in the RiceRocks project? A) The new score value. B) A Boolean indicating whether the sprite should be removed (e.g., missile expired). C) The current frame number. D) Nothing; it only modifies internal state. Answer: B Explanation: Many implementations have update return True when the sprite’s lifetime is over, signaling removal.
Question 34. Which of the following is a correct way to iterate over all missiles in a set called missiles and call their draw method? A) for m in missiles: m.draw(canvas) B) while missiles: missiles.pop().draw(canvas) C) missiles.map(lambda m: m.draw(canvas)) D) draw(missiles) Answer: A Explanation: Standard for loop over a set accesses each missile object. Question 35. In Python, which of the following is hashable? A) List [1,2,3] B) Dictionary {'a':1} C) Tuple (1,2,3) D) Set {1,2,3} Answer: C Explanation: Tuples are immutable and thus hashable; lists, dicts, and sets are mutable and not hashable. Question 36. What is the effect of calling canvas.draw_image(image, center, size, pos, size) with center equal to the image’s center? A) The image is drawn rotated by 90 degrees. B) The image is drawn at position pos with its original dimensions. C) The image is drawn upside down. D) The image is not drawn at all.
Question 39. Which of the following statements about the map function combined with list is correct? A) list(map(func, iterable)) creates a new list with the transformed elements. B) list(map(func, iterable)) modifies the original iterable in place. C) list(map(func, iterable)) returns a set. D) list(map(func, iterable)) cannot be used with lambda functions. Answer: A Explanation: Converting the map object to a list materializes the results. Question 40. In the context of the game’s timer, what does a timer interval of 1/60 seconds achieve? A) Updates the game 60 times per second, providing smooth animation. B) Pauses the game for one minute. C) Draws the background only once. D) Increases the ship’s speed by 60 units. Answer: A Explanation: 1/60 s ≈ 16.67 ms, which yields 60 frames per second, a common game refresh rate. Question 41. Which Python construct can be used to create an infinite sequence of numbers without storing them all in memory? A) List comprehension B) Generator expression C] Tuple literal
D) Dictionary comprehension Answer: B Explanation: Generator expressions produce values lazily, allowing infinite sequences. Question 42. What is the purpose of the set.add(element) method in the game’s missile management? A) To remove a missile from the set. B) To insert a new missile into the active missile collection. C) To change the missile’s image. D) To reset the missile’s lifetime. Answer: B Explanation: add inserts the element into the set if it is not already present. Question 43. Which attribute of the ship determines whether the thrust sound should be looping? A) image B) thrust (Boolean) C) angle D) score Answer: B Explanation: The thrust flag indicates the ship is accelerating, triggering the looping thrust sound.
Answer: A Explanation: The lambda returns True for ships whose alive attribute is truthy. Question 47. Which of the following best describes a “torus” (toroidal) geometry used in the game world? A) Objects bounce off the edges. B) Objects disappear when they reach the edge. C) Objects that leave one side re‑enter from the opposite side. D) Objects are confined to a circular arena. Answer: C Explanation: Toroidal space wraps coordinates around both axes, creating a continuous playing field. Question 48. In the ship’s draw method, why might you choose a different image frame when self.thrust is True? A) To display a different background. B) To show engine flames indicating thrust. C) To change the ship’s size. D) To pause the game. Answer: B Explanation: Switching to a “thrust” frame visually communicates that the ship’s engines are firing.
Question 49. Which Python built‑in function can be used to convert a set of numbers into a sorted list? A) sorted(set_of_numbers) B) list(set_of_numbers).sort() C) set(set_of_numbers).order() D) order(set_of_numbers) Answer: A Explanation: sorted returns a new list containing the elements in ascending order. Question 50. What does the __del__ method do in a Python class? A) Initializes the object. B) Provides a string representation. C) Is called when the object is about to be destroyed. D) Returns the length of the object. Answer: C Explanation: __del__ is the destructor, invoked when reference count drops to zero. Question 51. Which of the following is a correct way to create a dictionary that maps rock IDs to their sprite objects? A) rock_dict = {} then rock_dict[rock_id] = rock_sprite B) rock_dict = [] then rock_dict.append(rock_id, rock_sprite) C) rock_dict = set() then rock_dict.add((rock_id, rock_sprite)) D) rock_dict = (rock_id: rock_sprite)