Q3 – Please decide the result of the following script.
import turtle
S1 = turtle.Turtle()
for i in range(20):
S1.forward(i * 10)
S1.right(144)
turtle.done()
A. Many stars.
B. A spiraling star
C. Many circles.
D. Five ovals.
Q4. Please complete the following script so it will create a button on the canvas.
from tkinter import *
tk = Tk()
btn = Button(tk, text="click me")
A. btn.pack()
B. tk.pack()
C. Button.fresh()
D. btn.pack(“update”)
Q5. How to create a line from (0,0) to (500,500) using tkinter? Given the following script for setting the environment.
from tkinter import *
tk = Tk()
canvas = Canvas(tk, width=500, height=500)
canvas.pack()
A. canvas.drawLine(0,0,500,500)
B. canvas.create_line(0, 0, 500, 500)
C. goto(500,500)
D. pen.goto(500,500)
Q6. How would you create a triangle with tkinter? (Given similar setting in question 5).
A. canvas.create_triangle(10, 10, 100, 10, 100, 110, fill="",outline="black")
B. canvas.create_line(10, 10, 100, 10, 100, 110,)
C. canvas.polygon(10, 10, 100, 10, 100, 110, outline="black")
D. canvas.create_polygon(10, 10, 100, 10, 100, 110, fill="",outline="black")
Q7. How to change an object to a different color? Please choose the right code.
>>> from tkinter import *
>>> tk = Tk()
>>> canvas = Canvas(tk, width=400, height=400)
>>> canvas.pack()
>>> myObject = canvas.create_polygon(10, 10, 10, 60, 50, 35, fill='red')
A. canvas.itemconfig(myObject, fill='blue')
B. myObject.fill(“blue”)
C. canvas.myObject(fill=”blue”)
D. canvas.pack(fill=”blue”)
Question 8-9 – Given the definition of the ball class, please answer the following 2 questions (assuming the canvas object has been created already):
class Ball:
def __init__(self, canvas, color):
self.canvas = canvas
self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
self.canvas.move(self.id, 245, 100)
def draw(self):
self.canvas.move(self.id, 0, -1)
Q8 – Please pick the correct way to create a blue ball object.
A. blueBall = Ball(canvas, “blue”)
B. blueBall = draw(Ball, “blue”)
C. blueBall = Ball(self, canvas, “blue”)
D. blueBall = Ball(canvas, “blue”)
Q9 – Please add a new attribute “size” to the Ball class definition. Choose the right way below:
A. def __init__(self, canvas, color, size):
self.size = size
B. def __init__(self, canvas, color, size):
ball.size = size
C. def __init__(self, canvas, color):
self.size = size
D. def __init__(canvas, color, size):
self.size = size