NEW
Font size
WorksheetsPython Programming II CSE A
Total questions: 20
Worksheet time: 7mins
What will be the output of the following code? x = 10 if x > 5: x += 2 else: x -= 2 print(x)
10
12
8
7
Which of the following is correct about Python functions?
A function is defined using the def keyword
A function can return multiple values
A function can be passed as an argument to another function
All of the above
What will be the output of the following code? s = 'Hello' s = s[::-1] print(s)
olleH
Hello
oHell
lleHo
Which of the following operations can be used to add an element to a list?
append()
extend()
insert()
All of the above
What will be the result of the following code? l = [1, 2, 3, 4] l[1:3] = [7, 8] print(l)
[1, 7, 8, 4]
[1, 2, 3, 4]
[1, 7, 4]
Error
What will be the output of the following code? my_set = {1, 2, 3, 4} my_set.add(2) print(my_set)
{1, 2, 3, 4}
{1, 2, 3}
{1, 2, 3, 4, 2}
Error
Which method is used to remove a key from a dictionary and return its value?
del
remove()
pop()
discard()
What will be the result of the following code? t = (1, 2, 3) t[0] = 4 print(t)
(4, 2, 3)
(1, 2, 3)
Error
None
What is the output of the following code? x = [1, 2, 3] y = x y.append(4) print(x)
[1, 2, 3]
[1, 2, 3, 4]
[1, 2]
[4, 2, 3]
Which of the following is true about Python sets?
They are unordered
They are immutable
They allow duplicate elements
They are not iterable
What is the output of the following code? x = ['ab', 'cd'] for i in x: x.append(i.upper()) print(x)
Infinite loop
['ab', 'cd', 'AB', 'CD']
Error
None
Which of the following is not a valid dictionary method?
keys()
values()
append()
get()
What will be the output of the following code? x = 'python' x[0] = 'j' print(x)
jython
python
Error
None
What is the output of the following code? def myfunc(x): return [i ** 2 for i in x] print(myfunc([1, 2, 3]))
[1, 4, 9]
[1, 2, 3]
[1, 8, 27]
Error
What will be the result of the following code? my_dict = {1: 'a', 2: 'b', 3: 'c'} print(list(my_dict.keys()))
[a, b, c]
[1, 2, 3]
[(1, a), (2, b), (3, c)]
Error
Which of the following statements is true about tuples?
They are mutable
They can contain elements of different types
They allow duplicate elements
Both B and C
What will be the output of the following code? l = [1, 2, 3, 4] print(l[::2])
[1, 2, 3, 4]
[1, 3]
[2, 4]
[1, 4]
Which of the following can be used to iterate over a dictionary's keys and values at the same time?
items()
keys()
values()
None of the above
What will be the output of the following code? def f(x=[]): x.append(1) print(x) f() f()
[1] [1]
[1] [1, 1]
[1] Error
None
What will be the output of the following code? my_tuple = (1, 2) print(my_tuple * 2)
(1, 2)
(1, 2, 1, 2)
Error
None
