wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

CC103 - Part 3

Total questions: 40

Worksheet time: 20mins

Name
Class
Date
1.

What does the following code do? import tkinter as tk root = tk.Tk() label = tk.Label(root, text="Hello, World!") label.pack() root.mainloop()

a)

Displays a label with text inside a window

b)

Opens a text file

c)

Creates a button

d)

Closes a window

2.

What is the purpose of mainloop() in Tkinter? root.mainloop()

a)

Exits the application

b)

Creates a widget

c)

Starts the GUI event loop

d)

Destroys the root window

3.

What does the pack() method do? button.pack()

a)

Destroys a widget

b)

Adds a widget to the grid

c)

Arranges a widget in the window

d)

Changes a widget’s text

4.

How do you create a button that closes the window when clicked? import tkinter as tk root = tk.Tk() button = tk.Button(root, text="Exit", command=root.destroy) button.pack() root.mainloop()

a)

Closes the system

b)

Creates an exit dialog

c)

Closes the window

d)

Minimizes the window

5.

Which geometry manager allows specifying row and column?

a)

pack()

b)

grid()

c)

place()

d)

align()

6.

Which of the following correctly adds an entry widget? entry = tk.Entry(root) entry.pack()

a)

Adds a dropdown

b)

Adds a label

c)

Adds a text box for input

d)

Adds a button

7.

How can you get the content of an Entry widget? entry.get()

a)

entry.value()

b)

entry.text()

c)

entry.content()

d)

entry.get()

8.

What will this code do? label = tk.Label(root, text="Click Me") label.config(fg="blue") label.pack()

a)

Display a red label

b)

Make text bold

c)

Change text color to blue

d)

Set font size

9.

What happens if you call pack() and grid() on the same widget?

a)

Works fine

b)

Raises a warning

c)

Raises a TclError

d)

Automatically chooses one

10.

What does this snippet do? tk.Label(root, text="Name").grid(row=0, column=0) tk.Entry(root).grid(row=0, column=1)

a)

Adds a dropdown

b)

Adds a label only

c)

Adds a label and input field side by side

d)

Adds a label below input field

11.

How do you set the window title in Tkinter? root.title("My App")

a)

root.name("My App")

b)

root.label("My App")

c)

root.set_title("My App")

d)

root.title("My App")

12.

Which widget allows multiline text input?

a)

Entry

b)

Label

c)

Text

d)

StringVar

13.

How to change the background color of a window? root.configure(bg="lightblue")

a)

root.color("lightblue")

b)

root.setBackground("lightblue")

c)

root.background = "lightblue"

d)

root.configure(bg="lightblue")

14.

What does command=my_function mean in a button?

a)

Runs the function immediately

b)

Attaches the function to button click

c)

Creates a new window

d)

Changes button color

15.

What is the default layout manager in Tkinter?

a)

grid

b)

pack

c)

place

d)

flex

16.

Which method can position a widget at an absolute location?

a)

grid()

b)

pack()

c)

place()

d)

align()

17.

How to bind a keypress to an event handler? root.bind("", on_enter)

a)

root.set("", on_enter)

b)

root.event("", on_enter)

c)

root.bind("", on_enter)

d)

root.attach("", on_enter)

18.

How do you insert text into a Text widget? text.insert("1.0", "Hello!")

a)

text.add("Hello!")

b)

text.put("1.0", "Hello!")

c)

text.insert("1.0", "Hello!")

d)

text.set("Hello!")

19.

What does this code create? from tkinter import messagebox messagebox.showinfo("Info", "Operation Complete")

a)

A label

b)

A warning box

c)

A confirmation box

d)

An informational pop-up

20.

Which widget is used to group related widgets?

a)

Label

b)

Frame

c)

Entry

d)

Listbox

21.

What does the following code print? with open("sample.txt", "w") as f: f.write("Line 1\nLine 2\nLine 3") with open("sample.txt", "r") as f: print(len(f.readlines()))

a)

1

b)

2

c)

3

d)

0

22.

What will be the file content after executing the code? with open("file.txt", "w") as f: f.write("Hello\nWorld") with open("file.txt", "a") as f: f.write("\nPython")

a)

Overwritten with "Python"

b)

Contains only "Python"

c)

Contains "Hello\nWorld\nPython"

d)

Error

23.

What does the with statement ensure in file handling?

a)

File is opened in append mode

b)

File is deleted after use

c)

Automatic closing of file

d)

File is locked

24.

What happens if you read a file that does not exist? with open("nonexistent.txt", "r") as f: print(f.read())

a)

Returns empty string

b)

Creates the file

c)

Raises FileNotFoundError

d)

Raises IOError

25.

Which mode opens a file for both reading and writing?

a)

"w"

b)

"r"

c)

"a"

d)

"r+"

26.

What is the output? with open("file.txt", "w+") as f: f.write("12345") f.seek(0) print(f.read(2))

a)

12345

b)

12

c)

345

d)

1

27.

What is the effect of seek(0)?

a)

Closes the file

b)

Resets file pointer to beginning

c)

Moves to end of file

d)

Deletes file contents

28.

What does this code do? with open("file.txt", "w") as f: pass

a)

Deletes the file

b)

Creates an empty file

c)

Appends text

d)

Raises an error

29.

Which method checks if file is closed?

a)

isclosed()

b)

closed()

c)

f.closed

d)

f.isclosed()

30.

What will this print? with open("file.txt", "w") as f: f.write("test") print(f.closed)

a)

False

b)

True

c)

Error

d)

None

31.

What does "a" mode do?

a)

Replaces content

b)

Adds to existing content

c)

Only reads file

d)

Deletes file

32.

Which method reads all lines into a list?

a)

readline()

b)

readlines()

c)

read()

d)

readall()

33.

Output? with open("test.txt", "w") as f: f.writelines(["A\n", "B\n"])

a)

Writes string "writelines"

b)

Writes "A" and "B" on separate lines

c)

Error

d)

Writes only A

34.

What does this code do? f = open("log.txt", "x")

a)

Appends if file exists

b)

Overwrites file

c)

Creates file, error if exists

d)

Reads file

35.

What will f.tell() return? with open("a.txt", "w") as f: f.write("abc") print(f.tell())

a)

0

b)

1

c)

3

d)

Error

36.

Which function writes a string to file?

a)

writefile()

b)

writelines()

c)

write()

d)

put()

37.

What is the correct way to read a file line by line? with open("data.txt", "r") as f: for line in f: print(line)

a)

Correct

b)

Error

c)

Needs readline()

d)

Needs readlines()

38.

What does f.flush() do?

a)

Closes file

b)

Writes buffer to disk

c)

Reopens file

d)

Deletes content

39.

What does this code output? with open("test.txt", "w") as f: f.write("abc") f = open("test.txt", "r") print(f.read(1)) f.close()

a)

"abc"

b)

"a"

c)

"b"

d)

Error

40.

What's the result? with open("data.txt", "w") as f: f.write("line1\nline2") with open("data.txt") as f: print(f.readline())

a)

"line1"

b)

"line1\n"

c)

"line2"

d)

Error