NEW
Font size
WorksheetssADAHBN j12q3b4e1hjbasDI!J@#!(Python))
Total questions: 30
Worksheet time: 15mins
What is the output of ('a', 'b', 'c') + ('d',)?
('a', 'b', 'c', 'd')
('a', 'b', 'c', 'd',)
('abcd')
Error
Which of these will create an empty set?
{}
set()
[]
()
What file mode opens a file for writing and creates it if it doesn't exist?
"r"
"a"
"w"
"x"
Which dictionary method returns None if a key doesn't exist instead of raising an error?
dict[key]
dict.get(key)
dict.find(key)
dict.search(key)
What does the finally block do in exception handling?
Runs only if there's an exception
Runs only if there's no exception
Runs regardless of whether there's an exception
Prevents exceptions from occurring
Which of these is NOT a valid set operation?
Union (|)
Intersection (&)
Concatenation (+)
Difference (-)
What does .rstrip() do when reading a file?
Removes whitespace from the right side including newlines
Removes whitespace from the left side
Removes all whitespace
Reads the file in reverse
What is the output of len({1, 2, 2, 3, 3, 3})?
6
3
1
Error
Which exception is raised when dividing by zero?
ValueError
ZeroDivisionError
MathError
DivideError
How do you access the value for key "age" in dictionary person?
person[age]
person("age")
person["age"]
person.get["age"]
What does the file mode "a" do?
Opens for reading
Opens for writing (overwrites)
Opens for appending
Opens for both reading and writing
Which of these tuple methods counts occurrences of an element?
.count()
.find()
.occurrences()
.total()
What will set1 ^ set2 return?
Union
Intersection
Difference
Symmetric difference
Which statement correctly opens a file for reading and automatically closes it?
file = open("data.txt")
with open("data.txt") as file:
open("data.txt").read()
Both b and c
What is the main advantage of tuples over lists?
They can be modified
They are mutable
They are more memory efficient
They support more methods
Which of these will cause a KeyError?
Yes
No
Only if using .get()
Never
What does file.readlines() return?
A string of the entire file
A list of strings (lines)
A single line
A dictionary of lines
Which data structure guarantees unique elements?
List
Tuple
Set
Dictionary
What exception is raised when trying to convert "abc" to an integer?
TypeError
ValueError
ConversionError
IntError
How do you iterate through both keys and values in a dictionary?
for key in dict:
for value in dict.values():
for key, value in dict.items():
Both a and c
What is the purpose of the continue statement in file processing?
To stop reading the file
To skip to the next line
To close the file
To write to the file
Which of these is immutable?
List
Set
Dictionary
Tuple
What does "Hello" in {"Hello", "World"} return?
True
False
1
"Hello"
How do you handle multiple exceptions in one except block?
except ValueError, TypeError:
except [ValueError, TypeError]:
except (ValueError, TypeError):
except ValueError and TypeError:
What is the output of ("apple", "banana")[1]?
"apple"
"banana"
1
Error
Which method removes and returns an item from a dictionary?
.remove()
.pop()
.delete()
.extract()
What does open("file.txt", "r") return if the file doesn't exist?
Creates the file
Returns an empty file object
Raises FileNotFoundError
Returns None
Which of these operations removes common elements from the first set?
Union
Intersection
Difference
Symmetric difference
What is the correct way to add a new key-value pair to a dictionary?
dict.add("key", "value")
dict["key"] = "value"
dict.insert("key", "value")
dict.append("key", "value")
Which block executes if no exception occurs in a try-except structure?
except
else
finally
then
