NEW
Font size
WorksheetsCC103 - Part 3
Total questions: 40
Worksheet time: 20mins
What does the following code do? import tkinter as tk root = tk.Tk() label = tk.Label(root, text="Hello, World!") label.pack() root.mainloop()
Displays a label with text inside a window
Opens a text file
Creates a button
Closes a window
What is the purpose of mainloop() in Tkinter? root.mainloop()
Exits the application
Creates a widget
Starts the GUI event loop
Destroys the root window
What does the pack() method do? button.pack()
Destroys a widget
Adds a widget to the grid
Arranges a widget in the window
Changes a widget’s text
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()
Closes the system
Creates an exit dialog
Closes the window
Minimizes the window
Which geometry manager allows specifying row and column?
pack()
grid()
place()
align()
Which of the following correctly adds an entry widget? entry = tk.Entry(root) entry.pack()
Adds a dropdown
Adds a label
Adds a text box for input
Adds a button
How can you get the content of an Entry widget? entry.get()
entry.value()
entry.text()
entry.content()
entry.get()
What will this code do? label = tk.Label(root, text="Click Me") label.config(fg="blue") label.pack()
Display a red label
Make text bold
Change text color to blue
Set font size
What happens if you call pack() and grid() on the same widget?
Works fine
Raises a warning
Raises a TclError
Automatically chooses one
What does this snippet do? tk.Label(root, text="Name").grid(row=0, column=0) tk.Entry(root).grid(row=0, column=1)
Adds a dropdown
Adds a label only
Adds a label and input field side by side
Adds a label below input field
How do you set the window title in Tkinter? root.title("My App")
root.name("My App")
root.label("My App")
root.set_title("My App")
root.title("My App")
Which widget allows multiline text input?
Entry
Label
Text
StringVar
How to change the background color of a window? root.configure(bg="lightblue")
root.color("lightblue")
root.setBackground("lightblue")
root.background = "lightblue"
root.configure(bg="lightblue")
What does command=my_function mean in a button?
Runs the function immediately
Attaches the function to button click
Creates a new window
Changes button color
What is the default layout manager in Tkinter?
grid
pack
place
flex
Which method can position a widget at an absolute location?
grid()
pack()
place()
align()
How to bind a keypress to an event handler? root.bind("", on_enter)
root.set("", on_enter)
root.event("", on_enter)
root.bind("", on_enter)
root.attach("", on_enter)
How do you insert text into a Text widget? text.insert("1.0", "Hello!")
text.add("Hello!")
text.put("1.0", "Hello!")
text.insert("1.0", "Hello!")
text.set("Hello!")
What does this code create? from tkinter import messagebox messagebox.showinfo("Info", "Operation Complete")
A label
A warning box
A confirmation box
An informational pop-up
Which widget is used to group related widgets?
Label
Frame
Entry
Listbox
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()))
1
2
3
0
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")
Overwritten with "Python"
Contains only "Python"
Contains "Hello\nWorld\nPython"
Error
What does the with statement ensure in file handling?
File is opened in append mode
File is deleted after use
Automatic closing of file
File is locked
What happens if you read a file that does not exist? with open("nonexistent.txt", "r") as f: print(f.read())
Returns empty string
Creates the file
Raises FileNotFoundError
Raises IOError
Which mode opens a file for both reading and writing?
"w"
"r"
"a"
"r+"
What is the output? with open("file.txt", "w+") as f: f.write("12345") f.seek(0) print(f.read(2))
12345
12
345
1
What is the effect of seek(0)?
Closes the file
Resets file pointer to beginning
Moves to end of file
Deletes file contents
What does this code do? with open("file.txt", "w") as f: pass
Deletes the file
Creates an empty file
Appends text
Raises an error
Which method checks if file is closed?
isclosed()
closed()
f.closed
f.isclosed()
What will this print? with open("file.txt", "w") as f: f.write("test") print(f.closed)
False
True
Error
None
What does "a" mode do?
Replaces content
Adds to existing content
Only reads file
Deletes file
Which method reads all lines into a list?
readline()
readlines()
read()
readall()
Output? with open("test.txt", "w") as f: f.writelines(["A\n", "B\n"])
Writes string "writelines"
Writes "A" and "B" on separate lines
Error
Writes only A
What does this code do? f = open("log.txt", "x")
Appends if file exists
Overwrites file
Creates file, error if exists
Reads file
What will f.tell() return? with open("a.txt", "w") as f: f.write("abc") print(f.tell())
0
1
3
Error
Which function writes a string to file?
writefile()
writelines()
write()
put()
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)
Correct
Error
Needs readline()
Needs readlines()
What does f.flush() do?
Closes file
Writes buffer to disk
Reopens file
Deletes content
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()
"abc"
"a"
"b"
Error
What's the result? with open("data.txt", "w") as f: f.write("line1\nline2") with open("data.txt") as f: print(f.readline())
"line1"
"line1\n"
"line2"
Error
