wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Set A – MCQ

Total questions: 71

Worksheet time: 38mins

Name
Class
Date
1.

What is a set in Python?

a)

A mutable collection with duplicate items

b)

An ordered collection of items

c)

An unordered, mutable collection with no duplicates

d)

A dictionary with only keys

2.

Which data type is not allowed as a set element?

a)

Integer

b)

List

c)

Tuple

d)

String

3.

Why can't a set contain duplicate elements in Python?

a)

Because Python converts all duplicates to strings

b)

Because sets are stored in ascending order

c)

Because sets are based on hash tables which only allow unique keys

d)

Because Python sorts sets internally

4.

Which of these operations returns the symmetric difference of two sets?

a)

set1 | set2

b)

set1 & set2

c)

set1 - set2

d)

set1set2set1 ^ set2

5.

Suppose s1 = {1, 2, 4, 3} and s2 = {1, 5, 4, 13}, what is s1 - s2?

a)

{2, 3, 5, 13}

b)

{1, 2, 4, 3, 5, 13}

c)

{2, 3}

d)

{1, 5, 4, 13}

6.

What does the | (pipe) operator do when used between two sets in Python?

a)

Returns the difference of the two sets

b)

Returns the union of the two sets

c)

Returns the intersection of the two sets

d)

Returns the symmetric difference of the two sets

7.

Suppose s1 = {1, 2, 4, 3} and s2 = {1, 5, 4, 13}, s1 + s2 is ____________?

a)

{1, 2, 4, 3, 1, 5, 4, 13}

b)

{1, 2, 4, 3, 5, 13}

c)

{1, 2, 4, 3}

d)

illegal

8.

Suppose s = {1, 2}, 2 * s is ___________.

a)

{2, 4}

b)

{1, 2, 2, 4}

c)

{1, 2, 4}

d)

{1, 2, 3, 4}

9.

Suppose s1 = {10, 20, 30} and s2 = {20, 40, 50}, s1 & s2 is ____________?

a)

{10, 20, 30}

b)

{20}

c)

{40, 50}

d)

{}

10.

a = {1, 2, 3} print(a.pop()) print(a) What can you say about the output?

a)

Always same order

b)

First element always removed

c)

Unpredictable order

d)

Last element removed

11.

What will happen if you call remove(x) on a set and x does not exist in the set?

a)

Removes nothing

b)

Returns None

12.

What is the key difference between remove() and discard() when the element is not found?

a)

remove() deletes silently; discard() raises an error

b)

Both raise an error

c)

remove() raises an error; discard() does nothing

d)

Both do nothing

13.

The __________ method removes all items from a set.

a)

remove_all

b)

del()

c)

clear()

d)

pop_all()

14.

Set elements must be __________, i.e., hashable types.

a)

Mutable

b)

Iterable

c)

Immutable

d)

List

15.

a = {1, 2, 3}

a)

{1, 2, 3}

b)

[1, 2, 3]

c)

(1, 2, 3)

d)

<1, 2, 3>

16.

a.add((4, 5)) print(a) The output will be ________________?

a)

{1, 2, 3, (4, 5)}

b)

{1, 2, 3, 4, 5}

c)

Error

d)

[(1, 2, 3), (4, 5)]

17.

Suppose s = {1, 2, 4, 3}, what happens when invoking s.add(4)?

a)

There is no add method for a set object.

b)

This method is executed fine and 4 is added to the set.

c)

Since 4 is already in the set, Python raises a KeyError exception.

d)

This method is executed fine and 4 is not added to the set since 4 is already in the set.

18.

Suppose s = {1, 2, 4, 3}, what happens when invoking s.remove(12)?

a)

There is no remove method for a set object.

b)

This method is executed fine and no exception is raised.

c)

Since 12 is not in the set, Python raises a KeyError exception.

d)

You cannot remove an element from a set.

19.

A = {100, 200} B = {100, 300} print(B - A) What is the output?

a)

{100}

b)

{200}

c)

{300}

d)

{100, 200, 300}

20.

Fill in the blank: The output will be: __________

a)

{200}

b)

{100}

c)

{300}

d)

{}

21.

def g(y): print(x) print(x+1) x = 5 g(x) print(x)

a)

5 5 6

b)

5 6 5

c)

6 5 6

d)

Error

22.

print(x) x = 5 f(x) print(x) What is the output?

a)

A. 5 2

b)

B. 5 Error

c)

C. 2 5

d)

D. 2 Error

23.

def h(y): x+=1 x = 5 h(x) print(x) What is the output?

a)

KeyError: local variable 'x' referenced before assignment

b)

UnboundLocalError: local variable 'x' referenced before assignment

c)

SyntaxError: local variable 'x' referenced before assignment

d)

Not all above

24.

Given the following code: return a+b return x val = f()(3,4) print(val) What is the output?

a)

A. 5

b)

B. 7

c)

C. 6

d)

D. 8

25.

Given the following code: def f(): def g(): print("inside function g") g() print("inside function f") g() What is the output?

a)

TypeError

b)

UnboundLocalError

c)

SyntaxError

d)

Not all above

26.

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?

a)

g(x): x = 5

b)

g(x): x = 4 + 'abc'

c)

Error, due to addition of str and int

d)

g(x): x = 4

27.

What is the value returned by Import math math.fact(6)?

a)

720

b)

6

c)

[1, 2, 3, 6]

d)

error

28.

What is the value returned by math.floor(3.4)?

a)

3

b)

4

c)

4.0

d)

3.0

29.

What does math.sqrt(X, Y) do?

a)

calculate the Xth root of Y

b)

calculate the Yth root of X

c)

error

d)

return a tuple with the square root of X and Y

30.

What is returned by math.isfinite(float('inf'))?

a)

True

b)

False

c)

None

d)

error

31.

What is the output of print(math.trunc('3.1'))?

a)

3

b)

3.0

c)

error

d)

none of the mentioned

32.

What are the outcomes of the functions shown below?

a)

Error, 6

b)

12, Error

c)

12, 6

d)

Error, Error

33.

The ________ function returns its argument with a modified shape, whereas the ________ method modifies the array itself.

a)

reshape, resize

b)

resize, reshape

c)

reshape2, resize

d)

all of the mentioned

34.

To create sequences of numbers, NumPy provides a function __________ analogous to range that returns arrays instead of lists.

a)

arange

b)

aspace

c)

aline

d)

all of the mentioned

35.

The primary purpose of NumPy in Python is __________?

a)

To do numerical calculations

b)

To do scientific computing

36.

What does datetime.strptime("2024-05-01", "%Y-%m-%d") do?

a)

Converts date to string

b)

Parses string to datetime

c)

Formats current time

d)

Returns today’s date

37.

Which method returns today’s date in the datetime module?

a)

date.now()

b)

datetime.today()

c)

datetime.date()

d)

datetime.now().date()

38.

Which class represents duration in the datetime module?

a)

Duration

b)

Timeduration

c)

timedelta

d)

time

39.

What does random.randint(1, 10) return?

4 lines
40.

What does random.randint(1, 10) return?

a)

Float between 1 and 10

b)

Integer from 1 to 9

c)

Integer from 1 to 10 (inclusive)

d)

Any number

41.

What will random.choice([1, 2, 3]) return?

a)

Always 1

b)

Always 3

c)

Any element from the list

d)

A shuffled list

42.

How can you shuffle a list in place?

a)

random.shuffle(list)

b)

shuffle(list)

c)

randomize(list)

d)

list.shuffle()

43.

What will be the result of file.read(0) if the file is not empty?

a)

Full content of file

b)

First character

c)

Empty string

d)

Error

44.

Which mode opens a file for both reading and writing without truncating it?

a)

w+

b)

r+

c)

a+

d)

rb

45.

Which of the following file modes allows both reading and writing to a file?

a)

r+

b)

w

c)

a

d)

w+

46.

What happens when write() is used in 'r' mode?

a)

Writes to file

b)

Appends to file

c)

Raises io.UnsupportedOperation

d)

Overwrites file

47.

Which of the following automatically closes the file after usage?

a)

close()

b)

end()

c)

with open(...) as f:

d)

file.done()

48.

What is the default file access mode in open()?

a)

"r"

b)

"w"

c)

"a"

d)

"r+"

49.

Which method reads only one line from the file?

a)

readline()

b)

read()

c)

readlines()

d)

write()

50.

Which method is used to read a single line from a file?

a)

file.readline()

b)

file.read()

c)

file.nextline()

d)

file.readone()

51.

What is the result of reading from a file opened in 'a' mode?

a)

Reads from beginning

b)

Cannot read, raises error

c)

Reads from end

d)

Reads whole content

52.

What happens if close() is called multiple times on the same file object?

a)

Error

b)

File gets corrupted

c)

Ignored after first close

d)

Deletes the file

53.

How can you ensure a file is always closed, even during an exception?

a)

Use try-except

b)

Use finally block

c)

Use with statement

d)

Call close() in except block

54.

Which of the following modes will create the file if it doesn’t exist?

a)

A) "r"

b)

B) "w"

c)

C) "r+"

d)

D) "t"

55.

What happens when you use "w" mode on an existing file?

a)

Appends data

b)

Reads data

c)

Deletes existing content and writes new

d)

Raises an error

56.

In which mode does the file pointer stay at the end of the file and only appends data?

a)

"r"

b)

"w"

c)

"x"

d)

"a"

57.

What is the behavior of the "x" mode in Python?

a)

Opens file for reading

b)

Appends data to an existing file

58.

Which mode should be used if you want to prevent overwriting a file accidentally?

a)

"w"

b)

"x"

c)

"a"

d)

"r+"

59.

In which file mode will the cursor always point at the beginning when opened?

a)

a

b)

r

c)

x

d)

Both B and C

60.

What is the default delimiter in a CSV file read by csv.reader()?

a)

Semicolon (;)

b)

Tab (\t)

c)

Pipe (|)

d)

Comma (,)

61.

Which of the following is used to write a single row into a CSV file?

a)

writer.write()

b)

writer.writeline()

c)

writer.writerow()

d)

writer.writerows()

62.

What does writer.writerows() accept as an argument?

a)

A single string

b)

A list of lists

c)

A dictionary

d)

An integer

63.

Which of the following best describes writer.writerow(['John', '25']) in CSV?

a)

Writes a line as a string

b)

Writes a row of values separated by tabs

c)

Writes a row with values separated by commas

d)

Writes a header row

64.

How can you specify a custom delimiter in the writer?

a)

csv.writer(file, del=",")

b)

csv.writer(file, delimiter=';')

c)

csv.writer(file).delimiter(';')

d)

csv.writer(file, sep=';')

65.

What does the newline parameter do in open(filename, 'w', newline='') when writing CSV files?

a)

Adds an empty line between rows

b)

Prevents extra blank lines in output

c)

Removes all newline characters

d)

Throws an error

66.

What type of object must be passed to csv.writer()?

a)

List

b)

File object

c)

String

d)

Dictionary

67.

How would you write multiple rows to a CSV file using csv module?

a)

Use writer.write_rows()

b)

Use writer.writerows(list_of_lists)

c)

Use file.write() in loop

d)

Use writer.writerlist()

68.

What does the csv.writer() function return?

a)

A file object

b)

A list of rows

c)

A writer object to write to CSV files

69.

In which block is cleanup code written that must be executed no matter what?

a)

A) try

b)

B) except

c)

C) else

d)

D) finally

70.

What is the purpose of the else clause in a try-except block?

a)

Execute if exception occurs

b)

Execute if no exception occurs

c)

Always execute

d)

Execute only if finally runs

71.

What happens if try block has no exception and finally block has an exception?

a)

Finally is skipped

b)

Program crashes

c)

Program executes normally

d)

Only try executes