Font size
WorksheetsSet A – MCQ
Total questions: 71
Worksheet time: 38mins
What is a set in Python?
A mutable collection with duplicate items
An ordered collection of items
An unordered, mutable collection with no duplicates
A dictionary with only keys
Which data type is not allowed as a set element?
Integer
List
Tuple
String
Why can't a set contain duplicate elements in Python?
Because Python converts all duplicates to strings
Because sets are stored in ascending order
Because sets are based on hash tables which only allow unique keys
Because Python sorts sets internally
Which of these operations returns the symmetric difference of two sets?
set1 | set2
set1 & set2
set1 - set2
set1set2
Suppose s1 = {1, 2, 4, 3} and s2 = {1, 5, 4, 13}, what is s1 - s2?
{2, 3, 5, 13}
{1, 2, 4, 3, 5, 13}
{2, 3}
{1, 5, 4, 13}
What does the | (pipe) operator do when used between two sets in Python?
Returns the difference of the two sets
Returns the union of the two sets
Returns the intersection of the two sets
Returns the symmetric difference of the two sets
Suppose s1 = {1, 2, 4, 3} and s2 = {1, 5, 4, 13}, s1 + s2 is ____________?
{1, 2, 4, 3, 1, 5, 4, 13}
{1, 2, 4, 3, 5, 13}
{1, 2, 4, 3}
illegal
Suppose s = {1, 2}, 2 * s is ___________.
{2, 4}
{1, 2, 2, 4}
{1, 2, 4}
{1, 2, 3, 4}
Suppose s1 = {10, 20, 30} and s2 = {20, 40, 50}, s1 & s2 is ____________?
{10, 20, 30}
{20}
{40, 50}
{}
a = {1, 2, 3} print(a.pop()) print(a) What can you say about the output?
Always same order
First element always removed
Unpredictable order
Last element removed
What will happen if you call remove(x) on a set and x does not exist in the set?
Removes nothing
Returns None
What is the key difference between remove() and discard() when the element is not found?
remove() deletes silently; discard() raises an error
Both raise an error
remove() raises an error; discard() does nothing
Both do nothing
The __________ method removes all items from a set.
remove_all
del()
clear()
pop_all()
Set elements must be __________, i.e., hashable types.
Mutable
Iterable
Immutable
List
a = {1, 2, 3}
{1, 2, 3}
[1, 2, 3]
(1, 2, 3)
<1, 2, 3>
a.add((4, 5)) print(a) The output will be ________________?
{1, 2, 3, (4, 5)}
{1, 2, 3, 4, 5}
Error
[(1, 2, 3), (4, 5)]
Suppose s = {1, 2, 4, 3}, what happens when invoking s.add(4)?
There is no add method for a set object.
This method is executed fine and 4 is added to the set.
Since 4 is already in the set, Python raises a KeyError exception.
This method is executed fine and 4 is not added to the set since 4 is already in the set.
Suppose s = {1, 2, 4, 3}, what happens when invoking s.remove(12)?
There is no remove method for a set object.
This method is executed fine and no exception is raised.
Since 12 is not in the set, Python raises a KeyError exception.
You cannot remove an element from a set.
A = {100, 200} B = {100, 300} print(B - A) What is the output?
{100}
{200}
{300}
{100, 200, 300}
Fill in the blank: The output will be: __________
{200}
{100}
{300}
{}
def g(y): print(x) print(x+1) x = 5 g(x) print(x)
5 5 6
5 6 5
6 5 6
Error
print(x) x = 5 f(x) print(x) What is the output?
A. 5 2
B. 5 Error
C. 2 5
D. 2 Error
def h(y): x+=1 x = 5 h(x) print(x) What is the output?
KeyError: local variable 'x' referenced before assignment
UnboundLocalError: local variable 'x' referenced before assignment
SyntaxError: local variable 'x' referenced before assignment
Not all above
Given the following code: return a+b return x val = f()(3,4) print(val) What is the output?
A. 5
B. 7
C. 6
D. 8
Given the following code: def f(): def g(): print("inside function g") g() print("inside function f") g() What is the output?
TypeError
UnboundLocalError
SyntaxError
Not all above
Consider the following code: x = 'abc' x = x+1 print("g(x): x =",x) h() return x x = 3 z = g(x) What is the output?
g(x): x = 5
g(x): x = 4 + 'abc'
Error, due to addition of str and int
g(x): x = 4
What is the value returned by Import math math.fact(6)?
720
6
[1, 2, 3, 6]
error
What is the value returned by math.floor(3.4)?
3
4
4.0
3.0
What does math.sqrt(X, Y) do?
calculate the Xth root of Y
calculate the Yth root of X
error
return a tuple with the square root of X and Y
What is returned by math.isfinite(float('inf'))?
True
False
None
error
What is the output of print(math.trunc('3.1'))?
3
3.0
error
none of the mentioned
What are the outcomes of the functions shown below?
Error, 6
12, Error
12, 6
Error, Error
The ________ function returns its argument with a modified shape, whereas the ________ method modifies the array itself.
reshape, resize
resize, reshape
reshape2, resize
all of the mentioned
To create sequences of numbers, NumPy provides a function __________ analogous to range that returns arrays instead of lists.
arange
aspace
aline
all of the mentioned
The primary purpose of NumPy in Python is __________?
To do numerical calculations
To do scientific computing
What does datetime.strptime("2024-05-01", "%Y-%m-%d") do?
Converts date to string
Parses string to datetime
Formats current time
Returns today’s date
Which method returns today’s date in the datetime module?
date.now()
datetime.today()
datetime.date()
datetime.now().date()
Which class represents duration in the datetime module?
Duration
Timeduration
timedelta
time
What does random.randint(1, 10) return?
What does random.randint(1, 10) return?
Float between 1 and 10
Integer from 1 to 9
Integer from 1 to 10 (inclusive)
Any number
What will random.choice([1, 2, 3]) return?
Always 1
Always 3
Any element from the list
A shuffled list
How can you shuffle a list in place?
random.shuffle(list)
shuffle(list)
randomize(list)
list.shuffle()
What will be the result of file.read(0) if the file is not empty?
Full content of file
First character
Empty string
Error
Which mode opens a file for both reading and writing without truncating it?
w+
r+
a+
rb
Which of the following file modes allows both reading and writing to a file?
r+
w
a
w+
What happens when write() is used in 'r' mode?
Writes to file
Appends to file
Raises io.UnsupportedOperation
Overwrites file
Which of the following automatically closes the file after usage?
close()
end()
with open(...) as f:
file.done()
What is the default file access mode in open()?
"r"
"w"
"a"
"r+"
Which method reads only one line from the file?
readline()
read()
readlines()
write()
Which method is used to read a single line from a file?
file.readline()
file.read()
file.nextline()
file.readone()
What is the result of reading from a file opened in 'a' mode?
Reads from beginning
Cannot read, raises error
Reads from end
Reads whole content
What happens if close() is called multiple times on the same file object?
Error
File gets corrupted
Ignored after first close
Deletes the file
How can you ensure a file is always closed, even during an exception?
Use try-except
Use finally block
Use with statement
Call close() in except block
Which of the following modes will create the file if it doesn’t exist?
A) "r"
B) "w"
C) "r+"
D) "t"
What happens when you use "w" mode on an existing file?
Appends data
Reads data
Deletes existing content and writes new
Raises an error
In which mode does the file pointer stay at the end of the file and only appends data?
"r"
"w"
"x"
"a"
What is the behavior of the "x" mode in Python?
Opens file for reading
Appends data to an existing file
Which mode should be used if you want to prevent overwriting a file accidentally?
"w"
"x"
"a"
"r+"
In which file mode will the cursor always point at the beginning when opened?
a
r
x
Both B and C
What is the default delimiter in a CSV file read by csv.reader()?
Semicolon (;)
Tab (\t)
Pipe (|)
Comma (,)
Which of the following is used to write a single row into a CSV file?
writer.write()
writer.writeline()
writer.writerow()
writer.writerows()
What does writer.writerows() accept as an argument?
A single string
A list of lists
A dictionary
An integer
Which of the following best describes writer.writerow(['John', '25']) in CSV?
Writes a line as a string
Writes a row of values separated by tabs
Writes a row with values separated by commas
Writes a header row
How can you specify a custom delimiter in the writer?
csv.writer(file, del=",")
csv.writer(file, delimiter=';')
csv.writer(file).delimiter(';')
csv.writer(file, sep=';')
What does the newline parameter do in open(filename, 'w', newline='') when writing CSV files?
Adds an empty line between rows
Prevents extra blank lines in output
Removes all newline characters
Throws an error
What type of object must be passed to csv.writer()?
List
File object
String
Dictionary
How would you write multiple rows to a CSV file using csv module?
Use writer.write_rows()
Use writer.writerows(list_of_lists)
Use file.write() in loop
Use writer.writerlist()
What does the csv.writer() function return?
A file object
A list of rows
A writer object to write to CSV files
In which block is cleanup code written that must be executed no matter what?
A) try
B) except
C) else
D) finally
What is the purpose of the else clause in a try-except block?
Execute if exception occurs
Execute if no exception occurs
Always execute
Execute only if finally runs
What happens if try block has no exception and finally block has an exception?
Finally is skipped
Program crashes
Program executes normally
Only try executes
