0% found this document useful (0 votes)
2K views106 pages

Python Programming (MCQS)

The document contains a multiple choice questions quiz about Python programming. It includes 20 questions about Python concepts like data types, operators, precedence, and conversions. The answers to the questions are provided at the end.

Uploaded by

Mohit Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2K views106 pages

Python Programming (MCQS)

The document contains a multiple choice questions quiz about Python programming. It includes 20 questions about Python concepts like data types, operators, precedence, and conversions. The answers to the questions are provided at the end.

Uploaded by

Mohit Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 106

www.aktutor.

in

Multiple Choice Questions(MCQ’s)

Subject: Python Programming

Subject Code: KNC402

1|P a g e
www.aktutor.in

PYTHON PROGRAMING (KNC – 402)

UNIT 1

 MULTIPLE CHOICE QUESTIONS ANSWERS

1. Python was developed by


A. Guido van Rossum
B. James Gosling
C. Dennis Ritchie
D. Bjarne Stroustrup

2. Python was developed in which year?


A. 1972
B. 1995
C. 1989
D. 1981

3. Python is written in which language?


A. C
B. C++
C. Java
D. None of the above

4. What is the extension of python file?


A. .p
B. .py
C. .python
D. None of the above

5. Python is Object Oriented Programming Language.


A. True
B. False
C. Neither true nor false
D. None of the above

6. Python 3.0 is released in which year?


A. 2000
B. 2008
C. 2011
D. 2016

7. Which of the following statements is true?


A. Python is a high level programming language.
B. Python is an interpreted language.
C. Python is an object-oriented language
D. All of the above

2|P a g e
www.aktutor.in

8. What is used to define a block of code in Python?


A. Parenthesis
B. Indentation
C. Curly braces
D. None of the above

9. By the use of which character, single line is made comment in Python?


A. *
B. @
C. #
D. !

10. What is a python file with .py extension called?


A. package
B. module
C. directory
D. None of the above

11. Which of the following statements are correct?


(i) Python is a high level programming language.
(ii) Python is an interpreted language.
(iii) Python is a compiled language.
(iv) Python program is compiled before it is interpreted.
A. i, ii
B. i, iv
C. ii, iii
D. ii, iv

12. Which of the following is incorrect variable name in Python?


A. variable_1
B. variable1
C. 1variable
D. _variable

13. Is Python case sensitive when dealing with identifiers?


a) yes
b) no
c) machine dependent
d) none of the mentioned

14. What is the maximum possible length of an identifier?


a) 31 characters
b) 63 characters
c) 79 characters
d) none of the mentioned

15. Which of the following is invalid?


a) _a = 1
b) __a = 1
c) __str__ = 1
d) none of the mentioned

3|P a g e
www.aktutor.in

16. Which of the following is an invalid variable?


a) my_string_1
b) 1st_string
c) foo
d) _

17. Why are local variable names beginning with an underscore discouraged?
a) they are used to indicate a private variables of a class
b) they confuse the interpreter
c) they are used to indicate global variables
d) they slow down execution

18. Which of the following is not a keyword?


a) eval
b) assert
c) nonlocal
d) pass

19. All keywords in Python are in _________


a) lower case
b) UPPER CASE
c) Capitalized
d) None of the mentioned

20. Which of the following is true for variable names in Python?


a) unlimited length
b) all private members must have leading and trailing underscores
c) underscore and ampersand are the only two special characters allowed
d) none of the mentioned

21. Which of the following is an invalid statement?


a) abc = 1,000,000
b) a b c = 1000 2000 3000
c) a,b,c = 1000, 2000, 3000
d) a_b_c = 1,000,000

22. Which of the following cannot be a variable?


a) __init__
b) in
c) it
d) on

23. Which of these in not a core data type?


a) Lists
b) Dictionary
c) Tuples
d) Class

24. Given a function that does not return any value, What value is thrown by default when
executed in shell.
a) int
b) bool
c) void

4|P a g e
www.aktutor.in

d) None

25. What will be the output of the following Python code?

>>>str="hello"
>>>str[:2]

a) he
b) lo
c) olleh
d) hello

26. Which of the following will run without errors?


a) round(45.8)
b) round(6352.898,2,5)
c) round()
d) round(7463.123,2,1)

27. What is the return type of function id?


a) int
b) float
c) bool
d) dict

28. In python we do not specify types, it is directly interpreted by the compiler, so consider the
following operation to be performed.

>>>x = 13 ? 2

objective is to make sure x has a integer value, select all that apply (python 3.xx)
a) x = 13 // 2
b) x = int(13 / 2)
c) x = 13 % 2
d) All of the mentioned

29. What error occurs when you execute the following Python code snippet?
apple = mango
a) SyntaxError
b) NameError
c) ValueError
d) TypeError

30. What will be the output of the following Python code snippet?

def example(a):
a = a + '2'
a = a*2
return a

>>>example("hello")

a) indentation Error

5|P a g e
www.aktutor.in

b) cannot perform mathematical operation on strings


c) hello2
d) hello2hello2

31. What data type is the object below?


L = [1, 23, 'hello', 1]
a) list
b) dictionary
c) array
d) tuple

32. In order to store values in terms of key and value we use what core data type.
a) list
b) tuple
c) class
d) dictionary

33. Which of the following results in a SyntaxError?


a) ‘”Once upon a time…”, she said.’
b) “He said, ‘Yes!'”
c) ‘3\’
d) ”’That’s okay”’

34. The following is displayed by a print function call. Select all of the function calls that result
in this output.
1. tom
2. dick
3. harry

a) print('''tom
\ndick
\nharry''')
b) print(”’tomdickharry”’)
c) print(‘tom\ndick\nharry’)
d) print('tom
dick
harry')

35. What is the average value of the following Python code snippet?

>>>grade1 = 80
>>>grade2 = 90
>>>average = (grade1 + grade2) / 2

a) 85.0
b) 85.1
c) 95.0
d) 95.1

36. Select all options that print.


hello-how-are-you

a) print(‘hello’, ‘how’, ‘are’, ‘you’)

6|P a g e
www.aktutor.in

b) print(‘hello’, ‘how’, ‘are’, ‘you’ + ‘-‘ * 4)


c) print(‘hello-‘ + ‘how-are-you’)
d) print(‘hello’ + ‘-‘ + ‘how’ + ‘-‘ + ‘are’ + ‘you’)

37. What is the return value of trunc()?


a) int
b) bool
c) float
d) None

38. How we can convert the given list into the set ?
a) list.set()
b) set.list()
c) set(list)
d) None of the above

39. If we change one data type to another, then it is called


a) Type conversion
b) Type casting
c) Both of the above
d) None of the above

40. What is list data type in Python ?


a) collection of integer number
b) collection of string
c) collection of same data type
d) collection of different data type

41. What is type casting in python ?


a) declaration of data type
b) destroy data type
c) Change data type property
d) None of the above

42. Which of the following can convert the string to float number ?
a) str(float,x)
b) float(str,int)
c) int(float(str))
d) float(str)

43. Which of the following function are used to convert the string into the list ?
a) map()
b) convertor()
c) split()
d) lambda

44. Which of the following is not the data type in python ?


a) List
b) Tuple
c) Set
d) Class

45. Which of the following is the data type possible in python?

7|P a g e
www.aktutor.in

a) int
b) list
c) dictionary
d) All of the above

46. Which of the following is the example of the type casting ?


a) int(2)
b) str(2)
c) str(list)
d) All of the above

ANSWERS
1.a 2.c 3.a 4.b 5.a 6.b 7.d 8.b 9.c 10.b 11.b 12.c 13.a 14.d 15.d
16.b 17.a (As Python has no concept of private variables, leading underscores are used to indicate
variables that must not be accessed from outside the class.) 18.a (eval can be used as a variable)
19.d 20.a 21b 22.b 23.d 24.d 25.a 26.a 27.a 28.d
29.b 30.a 31.a 32.d 33.c 34.c 35.a 36.c 37.a 38.c
39.c 40.d 41.c 42.d 43.c 44.d 45.d 46.d

MULTIPLE CHOICE QUESTIONS ANSWERS

1. Which is the correct operator for power(xy)?


a) X^y
b) X**y
c) X^^y
d) None of the mentioned

2. Which one of these is floor division?


a) /
b) //
c) %
d) None of the mentioned

3. What is the order of precedence in python?


i) Parentheses
ii) Exponential
iii) Multiplication
iv) Division
v) Addition
vi) Subtraction
a) i,ii,iii,iv,v,vi
b) ii,i,iii,iv,v,vi
c) ii,i,iv,iii,v,vi
d) i,ii,iii,iv,vi,v

4. What is the answer to this expression, 22 % 3 is?


a) 7
b) 1
c) 0
d) 5

8|P a g e
www.aktutor.in

5. Mathematical operations can be performed on a string.


a) True
b) False

6. Operators with the same precedence are evaluated in which manner?


a) Left to Right
b) Right to Left
c) Can’t say
d) None of the mentioned

7. What is the output of this expression, 3*1**3?


a) 27
b) 9
c) 3
d) 1

8. Which one of the following has the same precedence level?


a) Addition and Subtraction
b) Multiplication, Division and Addition
c) Multiplication, Division, Addition and Subtraction
d) Addition and Multiplication

9. The expression Int(x) implies that the variable x is converted to integer.


a) True
b) False

10. Which one of the following has the highest precedence in the expression?
a) Exponential
b) Addition
c) Multiplication
d) Parentheses

11. What is the output of print 0.1 + 0.2 == 0.3?


a) True
b) False
c) Machine dependent
d) Error

12. Which of the following is not a complex number?


a) k = 2 + 3j
b) k = complex(2, 3)
c) k = 2 + 3l
d) k = 2 + 3J

13. What is the type of inf?


a) Boolean
b) Integer
c) Float
d) Complex
14. What does ~4 evaluate to?
a) -5
b) -4
c) -3
d) +3

9|P a g e
www.aktutor.in

15. What does ~~~~~~5 evaluate to?


a) +5
b) -11
c) +11
d) -5

16. Which of the following is incorrect?


a) x = 0b101
b) x = 0x4f5
c) x = 19023
d) x = 03964

17. What is the result of cmp(3, 1)?


a) 1
b) 0
c) True
d) False

18. Which of the following is incorrect?


a) float(‘inf’)
b) float(‘nan’)
c) float(’56’+’78’)
d) float(’12+34′)

19. What is the result of round(0.5) – round(-0.5)?


a) 1.0
b) 2.0
c) 0.0
d) Value depends on Python version

20. What does 3 ^ 4 evaluate to?


a) 81
b) 12
c) 0.75
d) 7

21. What will be the output of the following Python code snippet if x=1?
x<<2
a) 8
b) 1
c) 2
d) 4

22. What will be the output of the following Python expression?


bin(29)
a) ‘0b10111’
b) ‘0b11101’
c) ‘0b11111’
d) ‘0b11011’

23. What will be the value of x in the following Python expression, if the result of

10 | P a g e
www.aktutor.in

that expression is 2?
x>>2
a) 8
b) 4
c) 2
d) 1

24. What will be the output of the following Python expression if x=15 and y=12?
x&y
a) b1101
b) 0b1101
c) 12
d) 1101

25. Which of the following represents the bitwise XOR operator?


a) &
b) ^
c) |
d) !

26. Bitwise _________ gives 1 if either of the bits is 1 and 0 when both of the bits are
1.
a) OR
b) AND
c) XOR
d) NOT

27. What will be the output of the following Python expression?


4^12
a) 2
b) 4
c) 8
d) 12

28. What will be the output of the following Python code if a=10 and b =20?
a=10
b=20
a=a^b
b=a^b
a=a^bprint(a,b)
a) 10 20
b) 10 10
c) 20 10
d) 20 20

29. What will be the output of the following Python expression?

11 | P a g e
www.aktutor.in

~100?
a) 101
b) -101
c) 100
d) -100

30. The value of the expressions 4/(3*(2-1)) and 4/3*(2-1) is the same.
a) True
b) False

31. What will be the value of the following Python expression?


4+3%5
a) 4
b) 7
c) 2
d) 0

32. Evaluate the expression given below if A = 16 and B = 15.


A % B // A
a) 0.0
b) 0
c) 1.0
d) 1

33. Which of the following operators has its associativity from right to left?
a) +
b) //
c) %
d) **

34. What will be the value of x in the following Python expression?


x = int(43.55+2/2)
a) 43
b) 44
c) 22
d) 23

35. What is the value of the following expression?


2+4.00, 2**4.0
a) (6.0, 16.0)
b) (6.00, 16.00)
c) (6, 16)
d) (6.00, 16.0)

36. Which of the following is the truncation division operator?


a) /

12 | P a g e
www.aktutor.in

b) %
c) //
d) |

37. What are the values of the following Python expressions?


2**(3**2)
(2**3)**2
2**3**2
a) 64, 512, 64
b) 64, 64, 64
c) 512, 512, 512
d) 512, 64, 512

38. What is the value of the following expression?


8/4/2, 8/(4/2)
a) (1.0, 4.0)
b) (1.0, 1.0)
c) (4.0. 1.0)
d) (4.0, 4.0)

39. What is the value of the following expression?


float(22//3+3/3)
a) 8
b) 8.0
c) 8.3
d) 8.33

40. What will be the output of the following Python expression?


print(4.00/(2.0+2.0))
a) Error
b) 1.0
c) 1.00
d) 1

41. What will be the value of X in the following Python expression?


X = 2+9*((3*12)-8)/10
a) 30.0
b) 30.8
c) 28.4
d) 27.2

42. Which of the following expressions involves coercion when evaluated in Python?
a) 4.7 – 1.5
b) 7.9 * 6.3
c) 1.7 % 2
d) 3.4 + 4.6

13 | P a g e
www.aktutor.in

43. What will be the output of the following Python expression?


24//6%3, 24//4//2
a) (1,3)
b) (0,3)
c) (1,0)
d) (3,1)

44. Which among the following list of operators has the highest precedence?
+, -, **, %, /, <<, >>, |
a) <<, >>
b) **
c) |
d) %

45. What will be the value of the following Python expression?


float(4+int(2.39)%2)
a) 5.0
b) 5
c) 4.0
d) 4

46. Which of the following expressions is an example of type conversion?


a) 4.0 + float(3)
b) 5.3 + 6.3
c) 5.0 + 3
d) 3 + 7

47. Which of the following expressions results in an error?


a) float(‘10’)
b) int(‘10’)
c) float(’10.8’)
d) int(’10.8’)

48. What will be the value of the following Python expression?


4+2**5//10
a) 3
b) 7
c) 77
d) 0

49. The expression 2**2**3 is evaluates as: (2**2)**3.


a) True
b) False

14 | P a g e
www.aktutor.in

ANSWERS:
1.b 2.b 3.a 4.b 5.b 6.a 7.c 8.a 9.a 10.d 11.b 12.c
13.c 14.a 15.a 16.d 17.a 18.d 19.d 20.d 21.d 22.b
23.a 24.c 25.b 26.c 27.c 28.c 29.b 30.a 31.b 32.b
33.d 34.b 35.a 36.c 37.d 38.a 39.b 40.b 41.d 42.c
43.a 44.b 45.c 46.a 47.d 48.b 49.b

15 | P a g e
www.aktutor.in

UNIT II

1. In Python, a decision can be made by using if else statement.


a. True b. False

2. Checking multiple conditions in Python requires elif statements.


a. True b. False

3. If the condition is evaluated to true, the statement(s) of if block will be


executed, otherwise the statement(s) in else block(if else is specified) will be
executed.
a. True b. False

4. What kind of statement is IF statement?


a. Concurrent b. Sequential
c. Assignment d. Selected assignment

5. Which one of the following is a valid Python if statement :

a. if (a => 22) b. if(a>=2)


c. if a>=2 : d. if a >= 22
6. What keyword would you use to add an alternative condition to an if
statement?
a. Else if b. Elseif
c. Elif d. None of the above

7. Can we write if/else into one line in python?

a. Yes
b. No
c. if/else not used in python
d. None of the above

8. In a Python program, a control structure:

a. Defines program-specific data structures


b. Directs the order of execution of the statements in the program
c. Dictates what happens before the program starts and after it terminates
d. None of the above
9. What will be output of this expression:
‘p’ + ‘q’ if ‘12’.isdigit() else ‘r’ + ‘s’

16 | P a g e
www.aktutor.in

a. pq
b. rs
c. pqrs
d. pq12

10. Which statement will check if a is equal to b?

a. if a = b:
b. if a == b:
c. if a === c:
d. if a == b
11. What will be the output of given Python code?
str1="hello"
c=0
for x in str1:
if(x!="l"):
c=c+1
else:
pass print(c)
a. 2
b. 0
c. 4
d. 3
12. What does the following code print to the console?
if 5 > 10:
print("fan")
elif 8 != 9:
print("glass")
else:
print("cream")
a. Cream
B. Glass
c. Fan
d. No output

13. What does the following code print to the console?


name = "maria"
if name == "melissa":
print("usa")
elif name == "mary":
print("ireland")
else:
print("colombia")

17 | P a g e
www.aktutor.in

a. usa b. ireland
c.colombia d. None of the above

14. What does the following code print to the console?


if "cat" == "dog":
print("prrrr")
else:
print("ruff")
a. ruff b. ruf
c. prrrr d. prr

15. What does the following code print to the console?


hair_color = "blue"
if 3 > 2:
if hair_color == "black":
print("You rock!")
else:
print("Boring")
a. blue b. black
c. You rock! d. Boring
16. What does the following code print to the console?
if True:
print(101)
else:
print(202)
a. true b. false
c. 101 d. 202

17. What does the following code print to the console?


if False:
print("Nissan")
elif True:
print("Ford")
elif True:
print("BMW")
else:

18 | P a g e
www.aktutor.in

print("Audi")
a. Nissan b. Ford
c. BMW d. Audi

18. What does the following code print to the console?


if 1:
print("1 is truthy!")
else:
print("???")
a. 1 is truthy! b. ???
c. Invalid d. None of the above

19. What does the following code print to the console?


if 0:
print("huh?")
else:
print("0 is falsy!")
a. o b. Huh
c. 0 is falsy! d. None of the above

20. What does the following code print to the console?


if 88 > 100:
print("cardio")
a. 88 b. 100
b. cardio d. Nothing is printed..

1.a 2.a 3.a 4.b 5.c 6.c 7.a (Yes, we can write if/else in one
line. For eg i = 5 if a > 7 else 0. So, option A is correct.) 8.b 9.a ( If condition
is true so pq will be the output. So, option A is correct.) 10.b 11.d 12.b
13.c 14.a 15.d 16.c 17.b (The first elif that is True will be
executed when multiple elif statements are True.) 18.a (1 does not equal True, but it's
considered True in a boolean context. Values that are considered True in a boolean
context are called "truthy" and values that are considered False in a boolean context are
called "falsy".) 19. c 20. D

19 | P a g e
www.aktutor.in

1.Which of the following is the loop in python ?

A. For
B. while
C. do while
D.1 and 2

2. for loop in python are work on


A. range
B. iteration
C. Both of the above
D. None of the above

3. For loop in python is


A. Entry control loop
B. Exit control loop
C. Simple loop
D. None of the above

4. for i in range(-3), how many times this loop will run ?


A. 0
B. 1
C. Infinite
D. Error

5. for i in [1,2,3]:, how many times a loop run ?


A. 0
B. 1
C. 2
D. 3
6. How many times it will print the statement ?, for i in range(100): print(i)
A. 101
B. 99
C. 100
D. 0

7. What is the final value of the i after this, for i in range(3): pass
A. 1
B. 2
C. 3
D. 0

8. What is the value of i after the for loop ?, for i in range(4):break


A. 1
B. 2
C. 3
D. 0

9. What is the output of the following?


x = 'abcd'
for i in x:
print(i.upper())

20 | P a g e
www.aktutor.in

A. a b c d
B. A B C D
C. a B C D
D. error

10. What is the output of the following?


x = 'abcd'
for i in range(len(x)):
x[i].upper()
print (x)
A. abcd
B. ABCD
C. error
D. none of the mentioned

11. What is the output of the following?


d = {0: 'a', 1: 'b', 2: 'c'}
for x in d.values():
print(x)
A. 0 1 2
B. a b c
C. 0 a 1 b 2 c
D. none of the mentioned

12. What will be the output of the following Python code?


x = ['ab', 'cd']for i in x: x.append(i.upper())print(x)
A. [‘AB’,’CD’]
B. [‘ab’,’cd’,’AB’,’CD’]
C. [‘ab’,’cd’]
D. None of the mentioned

13. What will be the output of the following Python code snippet?
x = 'abcd'
for i in range(len(x)):
x = 'a'
print(x)
A. a
B. abcd abcd abcd
C. a a a a
D. None of the mentioned

14.What will be the output of the following Python code snippet?


x = 'abcd'
for i in range(len(x)):
print(x)
x = 'a'
A. a
B. abcd abcd abcd abcd
C. a a a a
D. None of the mentioned

21 | P a g e
www.aktutor.in

15. What will be the output of the following Python code?


x = 123
for i in x:
print(i)
A. 1 2 3
B. 123
C. error
D. None of the mentioned

16. What will be the output of the following Python code?


for i in range(2.0):
print(i)
A. 0.0 1.0
B. 0 1
C. error
D. None of the mentioned

17. What will be the output of the following Python code snippet?
for i in [1, 2, 3, 4][::-1]:
print (i)

A. 1234
B. 4321
C. error
D. none of the mentioned

18. What will be the output of the following Python code?


for i in range(int(2.0)):
print(i)

A. 0.0 1.0
B. 0 1
C. error
D. none of the mentioned

19. What will be the output of the following Python code snippet?
x=2
for i in range(x):
x += 1
print (x)

A. 01234
B. 01
C. 3 4
D. 0 1 2 3

20. What will be the output of the following Python code snippet?
x=2
for i in range(x):
x -= 2

22 | P a g e
www.aktutor.in

print (x)

A. 01234
B. 0 -2
C. 30
D. Error

21. What will be the output of the following Python code?


for i in range(10):
if i == 5:
break
else:
print(i)
else:
print("Here")

A. 0 1 2 3 4 Here
B. 0 1 2 3 4 5 Here
C. 0 1 2 3 4
D. 0 1 2 3 4 5

ANSWERS :
1. D 2. C 3. A 4. A 5. D 6. C 7. B 8. D 9. B 10.
A 11. B 12.D ( The loop does not terminate as new elements are being added to the list in
each iteration.) 13.C ( range() is computed only at the time of entering the loop.) 14.D
15.C (Objects of type int are not iterable.) 16. C ( Object of type float cannot be
interpreted as an integer.) 17.B 18.B 19.C 20.B 21.C

 MULTIPLE CHOICE QUESTIONS ANSWERS

1. In which of the following loop in python, we can check the condition ?


A. for loop
B. while loop
C. do while loop
D. None of the above

2. It is possible to create a loop using goto statement in python ?


A. Yes
B. No
C. Sometimes
D. None of the above

3. To break the infinite loop , which keyword we use ?


A. continue
B. break
C. exit
D. None of the above

4. While(0), how many times a loop run ?


A. 0

23 | P a g e
www.aktutor.in

B. 1
C. 3
D. infinite

5. while(1==3):, how many times a loop run ?


A. 0
B. 1
C. 3
D. infinite

6. What is the output of the following?


i=2
while True:
if i%3 == 0:
break
print(i)
i += 2
A. 2 4 6 8 10 …
B. 2 4
C. 2 3
D. error

7. What is the output of the following?


x = "abcdef"
i = "i"
while i in x:
print(i, end=" ")

A. no output
B. i i i i i i …
C. a b c d e f
D. abcdef

8. What will be the output of the following Python code?


i=1
while True:
if i%0O7 == 0:
break
print(i)
i += 1

A. 123456
B. 1234567
C. error
D. none of the mentioned

9. What will be the output of the following Python code?


True = False
while True:
print(True)
break

A. True

24 | P a g e
www.aktutor.in

B. False
C. None
D. none of the mentioned

10. What will be the output of the following Python code?


i=0
while i < 5:
print(i)
i += 1
if i == 3:
Break
else:
print(0)

A. 0120
B. 012
C. None
D. none of the mentioned

11. What will be the output of the following Python code?


x = "abcdef"
while i in x:
print(i, end=" ")

A. a b c d e f
B. Abcdef
C. i i i i i i...
D. Error

12. What will be the output of the following Python code?


x = "abcdef" i = "a"
while i in x:
x = x[:-1]
print(i, end = " ")

A. a a a a a a
B. a a a a
C. i i i i i i
D. None of the mentioned

13. What will be the output of the following Python code?


i=1
while True: if i%3 == 0: break print(i) i+=1

A. 1 2
B. 1 2 3
C. Error
D. None of the mentioned

14. What will be the output of the following Python code?

25 | P a g e
www.aktutor.in

i=1
while True: if i%O.7 == 0: break print(i) i += 1

A. 1 2 3 4 5 6
B. 1 2 3 4 5 6 7
C. Error
D. None of the mentioned

15. What will be the output of the following Python code?


i = 5while True: if i%O.11 == 0: break print(i) i += 1

A. 5 6 7 8 9 10
B. 5 6 7 8
C. 5 6
D. Error

16. What will be the output of the following Python code?


i=5
while True: if i%O.9 == 0: break print(i) i += 1
A. 5 6 7 8
B. 5 6 7 8 9
C. 5 6 7 8 9 10 11 12 13 14 15....
D. Error

17. What will be the output of the following Python code?


i=1
while True: if i%2 == 0: break print(i) i += 2

A. 1
B. 1 2
C. 1 2 3 4 5 6 ....
D. 1 3 5 7 9 11 ....

18. What will be the output of the following Python code?


i=2
while True:
if i%3 == 0:
break
print(i)
i += 2

A. 2 4 6 8 10 ....
B. 2 4
C. 2 3
D. Error

19. What will be the output of the following Python code?


i = 1while False:
if i%2 == 0:
break
print(i)
i += 2

26 | P a g e
www.aktutor.in

A. 1
B. 1 3 5 7 ...
C. 1 2 3 4 ....
D. none of the mentioned

20. What will be the output of the following Python code?


True = False
while True:
print(True)
Break

A. true
B. false
C. error
D. none of the mentioned

21. What will be the output of the following Python code?


i=0
while i < 5:
print(i)
i += 1
if i == 3:
Break
else:
print(0)

A. 0 1 2 0
B. 0 1 2
C. error
D. none of the mentioned

22. What will be the output of the following Python code?


i=0
while i < 3:
print(i)
i += 1
else:
print(0)

A. 0 1 2 3 0
B. 0 1 2 0
C. 0 1 2
D. error

23. What will be the output of the following Python code?


x = "abcdef"
i = "a"
while i in x:
x = x[1:]
print(i, end = " ")

A. a a a a a a

27 | P a g e
www.aktutor.in

B. a
C. no output
D. error

ANSWERS :

1. B 2.B 3.B 4.A 5.A 6.B 7.A 8.A 9.D(SyntaxError, True


is a keyword and it’s value cannot be changed.) 10.B 11.D 12.A 13.C 14.A
15.B 16.D 17.D 18.B 19.D 20.D 21.B 22.B 23.B

 MULTIPLE CHOICE QUESTIONS ANSWERS

1. Which of the following functions is a built-in function in python?


A. seed()
B. sqrt()
C. factorial()
D. print()

2. What will be the output of the following Python expression?


round(4.576)

A. 4.5
B. 5
C. 4
D. 4.6

3. The function pow(x,y,z) is evaluated as:

A. (x**y)**z
B. (x**y)/z
C. (x**y)%z
D. (x**y)*z

4. What will be the output of the following Python function?


all([2,4,0,6])

A. Error
B. True
C. False
D. 0

5. What will be the output of the following Python function?


any([2>8, 4>2, 1>2])

A. Error

28 | P a g e
www.aktutor.in

B. True
C. False
D. 4>2

6. What will be the output of the following Python function?


import math
abs(math.sqrt(25))

A. Error
B. -5
C. 5
D. 5.0

7. What will be the output of the following Python function?


sum(2,4,6)sum([1,2,3])

A. Error,6
B. 12, Error
C. 12,6
D. Error, Error

8. What will be the output of the following Python function?


min(max(False,-3,-4), 2,7)

A. 2
B. False
C. -3
D. -4

9. What will be the output of the following Python functions?


chr(‘97’)chr(97)

A. a Error
B. ‘a’ Error
C. Error a
D. Error Error

10. What will be the output of the following Python function?


complex(1+2j)

A. Error
B. 1
C. 2j
D. 1+2j

11. The function divmod(a,b), where both ‘a’ and ‘b’ are integers is evaluated as:
A. (a%b,a//b)
B. (a//b,a%b)
C. (a//b,a*b)
D. (a/b,a%b)

12. What will be the output of the following Python function?


list(enumerate([2, 3]))

29 | P a g e
www.aktutor.in

A. Error
B. [(1,2),(2,3)]
C. [(0,2),(1,3)]
D. [(2,3)]

13. Which of the following functions does not necessarily accept only iterables as arguments?

A. enumerate()
B. all()
C. chr()
D. max()

14. Which of the following functions accepts only integers as arguments?

A. ord()
B. min()
C. chr()
D. any()

15. Which of the following functions will not result in an error when no arguments are passed
to it?

A. min()
B. divmod()
C. all()
D. float()

16. What will be the output of the following Python function?


hex(15)

A. f
B. OxF
C. OXf
D. Oxf

17. What will be the output of the following Python function?


len(["hello",2, 4, 6])

A. 4
B. 3
C. Error
D. 6

18. Which of the following is the use of function in python?

A. functions are reusable pieces of programs


B. functions don’t provide better modularity for your application
C. you can’t also create your own functions
D. all of the mentioned

19. Which keyword is used for function?


A. Fun

30 | P a g e
www.aktutor.in

B. Define
C. Def
D. Function

20. What will be the output of the following Python code?

def sayHello():
print('Hello World!')

sayHello()
sayHello()

A. Hello World! Hello World!


B. 'Hello World!' 'Hello World!'
C. Hello Hello
D. None of the mentioned

21. What will be the output of the following Python code?


def printMax(a, b):
if a > b:
print(a, 'is maximum')
elif a == b:
print(a, 'is equal to', b)
else:
print(b, 'is maximum')
printMax(3, 4)

A. 3
B. 4
C. 4 is maximum
D. None of the mentioned

22. What will be the output of the following Python code?


x = 50
def func(x):
print('x is', x)
x=2
print('Changed local x to', x)
func(x)
print('x is now', x)

A. x is now 50
B. x is now 2
C. x is now 100
D. None of the mentioned

23. What will be the output of the following Python code?

def say(message, times = 1):


print(message * times)

31 | P a g e
www.aktutor.in

say('Hello')
say('World', 5)

A. Hello WorldWorldWorldWorldWorld
B. Hello World 5
C. Hello World,World,World,World,World
D. Hello HelloHelloHelloHelloHello

24. What will be the output of the following Python code?

def func(a, b=5, c=10):


print('a is', a, 'and b is', b, 'and c is', c)
func(3, 7)
func(25, c = 24)
func(c = 50, a = 100)

A. a is 7 and b is 3 and c is 10 a is 25 and b is 5 and c is 24 a is 5 and b is 100 and c is 50


B. a is 3 and b is 7 and c is 10 a is 5 and b is 25 and c is 24 a is 50 and b is 100 and c is 5
C. a is 3 and b is 7 and c is 10 a is 25 and b is 5 and c is 24 a is 100 and b is 5 and c is 50
D. None of the mentioned

25. Which of the following is a feature of DocString?


A. Provide a convenient way of associating documentation with Python modules, functions, c
classes,and methods
B. All functions should have a docstring
C. Docstrings can be accessed by the __doc__ attribute on objects
D. All of the mentioned

26. Which are the advantages of functions in python?


A. Reducing duplication of code
B. Decomposing complex problems into simpler pieces
C. Improving clarity of the code
D. All of the mentioned

27. What are the two main types of functions?


A. Custom function
B. Built-in function & User defined function
C. User function
D. System function

28. Where is function defined?


A. Module
B. Class
C. Another function
D. All of the mentioned

29. What is called when a function is defined inside a class?


A. Module
B. Class
C. Another function
D. Method

30. Which of the following is the use of id() function in python?

32 | P a g e
www.aktutor.in

A. Id returns the identity of the object


B. Every object doesn’t have a unique id
C. All of the mentioned
D. None of the mentioned

31. Which of the following refers to mathematical function?


A. sqrt
B. rhombus
C. add
D. Rhombus

32. What will be the output of the following Python code?

def cube(x):

return x * x * x

x = cube(3)
print x

A. 9
B. 3
C. 27
D. 30

33. What will be the output of the following Python code?

def C2F(c):

return c * 9/5 + 32

print C2F(100)

print C2F(0)

A. 212
32
B. 314
24
C. 567
98
D. None of the mentioned

34. What will be the output of the following Python code?

def power(x, y=2):


r=1
for i in range(y):
r=r*x
return r
print power(3)
print power(3, 3)

33 | P a g e
www.aktutor.in

A. 212
32
B. 9
27
C. 567
98
D. None of the mentioned.

35. What will be the output of the following Python code?


def foo(k):
k[0] = 1
q = [0]
foo(q)print(q)
A. [0]
B. [1]
C. [1, 0]
D. [0, 1]

36. How are keyword arguments specified in the function heading?


A. one-star followed by a valid identifier
B. one underscore followed by a valid identifier
C. two stars followed by a valid identifier
D. two underscores followed by a valid identifier

37. How many keyword arguments can be passed to a function in a single function call?
A. zero
B. one
C. zero or more
D. one or more

38. What will be the output of the following Python code?


def foo(fname, val):
print(fname(val))
foo(max, [1, 2, 3])
foo(min, [1, 2, 3])
A. 3 1
B. 1 3
C. error
D. none of the mentioned

39. What will be the output of the following Python code?


def foo():
return total + 1
total = 0print(foo())
A. 0
B. 1
C. error
D. none of the mentioned

40. What will be the output of the following Python code?


def foo():
total += 1

34 | P a g e
www.aktutor.in

return total
total = 0print(foo())
A. 0
B. 1
C. error
D. none of the mentioned

41. What will be the output of the following Python code?


def foo(k):
k = [1]
q = [0]
foo(q)print(q)
A. [0]
B. [1]
C. [1, 0]
D. [0, 1]

42. What will be the output of the following Python code?


def f1():
x=15
print(x)
x=12
f1()
A. Error
B. 12
C. 15
D. 1512

43. What will be the output of the following Python code?


def f1():
x=100
print(x)
x=+1
f1()
A. Error
B. 100
C. 101
D. 99

44. What will be the output of the following Python code?


def san(x):
print(x+1)
x=-2
x=4
san(12)
A. 13
B. 10
C. 2
D. 5

45. What will be the output of the following Python code?


def f1():
global x

35 | P a g e
www.aktutor.in

x+=1
print(x)
x=12print("x")
A. Error
B. 13
C. 13
x
D. X

46. What will be the output of the following Python code?


def f1(x):
global x
x+=1
print(x)
f1(15)print("hello")
A. error
B. hello
C. 16
D. 16
hello

47. What will be the output of the following Python code?


x=12def f1(a,b=x):
print(a,b)
x=15
f1(4)
A. Error
B. 12 4
C. 4 12
D. 4 15

48. What will be the output of the following Python code?


def f1(a,b=[]):
b.append(a)
return bprint(f1(2,[3,4]))
A. [3,2,4]
B. [2,3,4]
C. Error
D. [3,4,2]

49. What will be the output of the following Python code?


def f(p, q, r):
global s
p = 10
q = 20
r = 30
s = 40
print(p,q,r,s)
p,q,r,s = 1,2,3,4
f(5,10,15)
A. 1 2 3 4
B. 5 10 15 4
C. 10 20 30 40

36 | P a g e
www.aktutor.in

D. 5 10 15 40

50. What will be the output of the following Python code?


def f(x):
print("outer")
def f1(a):
print("inner")
print(a,x)
f(3)
f1(1)
A. outer
error
B. inner
error
C. outer
inner
D. error

51. What will be the output of the following Python code?


x = 5 def f1():
global x
x = 4def f2(a,b):
global x
return a+b+x
f1()
total = f2(1,2)print(total)
A. Error
B. 7
C. 8
D. 15

52. What will be the output of the following Python code?


x=100def f1():
global x
x=90def f2():
global x
x=80print(x)
A. 100
B. 90
C. 80
D. Error

53. Read the following Python code carefully and point out the global variables?
y, z = 1, 2def f():
global x
x = y+z
A. x
B. y and z
C. x, y and z
D. Neither x, nor y, nor z

54. Which of the following data structures is returned by the functions globals() and locals()?

37 | P a g e
www.aktutor.in

A. list
B. set
C. dictionary
D. tuple

55. What will be the output of the following Python code?


x=1def cg():
global x
x=x+1
cg()
x
A. 2
B. 1
C. 0
D. Error

56. What happens if a local variable exists with the same name as the global variable you want to
access?
A. Error
B. The local variable is shadowed.
C. Undefined behavior.
D. The global variable is shadowed.

ANSWERS :

1.D 2.B 3.A 4.C 5.B 6.D 7.A 8.B 9.C


10.D 11.B 12.C 13.C 14.C 15.D 16.D 17.A
18.A 19.C 20.A 21.C 22.A 23.A 24.C 25.D
26.D 27.B 28.D 29.D 30.A 31.A 32.C 33.A 34.B
35.B 36.C 37.C 38.A 39.B 40.C 41.A 42.C 43.B 44.A
45.D 46.A 47.C 48.D 49.C 50.A 51.B 52.A 53.C 54.C
55.A 56.D

 MULTIPLE CHOICE QUESTIONS ANSWERS

1. What will be the output of the following Python statement?


"a"+"bc"
a)a
b)bc
c)bca
d) abc
Answer: d

2. What will be the output of the following Python statement?

38 | P a g e
www.aktutor.in

"abcd"[2:]
a)a
b)ab
c)cd
d) dc
Answer: c
3. The output of executing string.ascii_letters can also be achieved by:
a) string.ascii_lowercase_string.digits
b) string.ascii_lowercase+string.ascii_upercase
c) string.letters
d) string.lowercase_string.upercase
Answer: b

4. What will be the output of the following Python code?


>>> str1 = 'hello'
>>> str2 = ','
>>> str3 = 'world'
>>> str1[-1:]

a) olleh
b) hello
c) h
d) o
Answer: d
5. What arithmetic operators cannot be used with strings?
a)+
b)*
c) –
d) All of the mentioned
Answer: c
6. What will be the output of the following Python code?
>>>print (r"\nhello")
a) a new line and hello
b) \nhello
c) the letter r and then hello
d) error
Answer: b
7. What will be the output of the following Python statement?
>>>print('new' 'line')
a) Error
b) Output equivalent to print ‘new\nline’
c) newline
d) new line
Answer: c
8. What will be the output of the following Python code?
>>>str1="helloworld"
>>>str1[::-1]
a) dlrowolleh
b) hello
c) world
d) helloworld
Answer: a
9. print(0xA + 0xB + 0xC):

39 | P a g e
www.aktutor.in

a) 0xA0xB0xC
b) Error
c) 0x22
d) 33
Answer: d
10. What will be the output of the following Python code?
>>>example = "snow world"
>>>print("%s" % example[4:7])
a) wo
b) world
c) sn
d) rl
Answer: a
11. What will be the output of the following Python code?
>>>example = "snow world"
>>>example[3] = 's'
>>>print example
a) snow
b) snow world
c) Error
d) snos world
Answer: c

12. What will be the output of the following Python code?


>>>max("what are you")
a) error
b) u
c) t
d) y
Answer: d
13. Given a string example=”hello” what is the output of example.count(‘l’)?
a)2
b)1
c) None
d) 0
Answer: a
14. What will be the output of the following Python code?
>>>example = "helle"
>>>example.find("e")
a) Error
b) -1
c) 1
d) 0
Answer: c
15. What will be the output of the following Python code?
>>>example = "helle"
>>>example.rfind("e")
a) -1
b) 4
c) 3
d) 1
Answer: b
16. What will be the output of the following Python code?

40 | P a g e
www.aktutor.in

>>example="helloworld"
>>example[::-1].startswith("d")
a) dlrowolleh
b) True
c) -1
d) None
Answer: b
17. To concatenate two strings to a third what statements are applicable?
a)s3=s1s2
b)s3=s1.add(s2)
c)s3=s1.__add__(s2)
d) s3 = s1 * s2
Answer: c
Explanation: __add__ is another method that can be used for concatenation.
18. Which of the following statement prints hello\example\test.txt?
a)print(“hello\example\test.txt”)
b)print(“hello\\example\\test.txt”)
c)print(“hello\”example\”test.txt”)
d) print(“hello”\example”\test.txt”)
Answer: b
Explanation: \is used to indicate that the next \ is not an escape sequence.
19. Suppose s is “\t\tWorld\n”, what is s.strip()?
a)\t\tWorld\n
b)\t\tWorld\n
c)\t\tWORLD\n
d) World
Answer: d
20. The format function, when applied on a string returns ___________
a) Error
b) int
c) bool
d) str
Answer: d
21. What will be the output of the “hello” +1+2+3?
a) hello123
b) hello
c) Error
d) hello6
Answer: c
22. What will be the output of the following Python code?
>>>print("D", end = ' ')
>>>print("C", end = ' ')
>>>print("B", end = ' ')
>>>print("A", end = ' ')
a) DCBA
b) A,B,C,D
c) DCBA
d) D, C, B, A will be displayed on four lines
Answer: c
23. What will be displayed by print(ord(‘b’) – ord(‘a’))?
a)0
b)1
c)-1

41 | P a g e
www.aktutor.in

d) 2
Answer: b
Explanation: ASCII value of b is one more than a. Hence the output of this code is 98-97, which is
equal to 1.
24. Say s=”hello” what will be the return value of type(s)?
a) int
b) bool
c) str
d) String
Answer: c
25. What is “Hello”.replace(“l”, “e”)?
a) Heeeo
b) Heelo
c) Heleo
d) None
26. To retrieve the character at index 3 from string s=”Hello” what command do we execute
(multiple answers allowed)?
a) s[]
b) s.getitem(3)
c) s.__getitem__(3)
d) s.getItem(3)
27. To return the length of string s what command do we execute?
a) s.__len__()
b) len(s)
c) size(s)
d) s.size()
28. Suppose i is 5 and j is 4, i + j is same as ________
a) i.__add(j)
b) i.__add__(j)
c) i.__Add(j)
d) i.__ADD(j)

29. What function do you use to read a string?


a) input(“Enter a string”)
b) eval(input(“Enter a string”))
c) enter(“Enter a string”)
d) eval(enter(“Enter a string”))

30. What will be the output of the following Python code?


print("abc DEF".capitalize())
a) abc def
b) ABC DEF
c) Abc def
d) Abc Def

31. What will be the output of the following Python code?


print("xyyzxyzxzxyy".count('yy'))
a) 2
b) 0
c) error
d) none of the mentioned

42 | P a g e
www.aktutor.in

32. What will be the output of the following Python code?


print("xyyzxyzxzxyy".count('yy', 2))
a) 2
b) 0
c) 1
d) none of the mentioned
Explanation: Counts the number of times the substring ‘yy’ is present in the given string, starting from
position 2.
33. What will be the output of the following Python code?
print("xyyzxyzxzxyy".endswith("xyy"))
a) 1
b) True
c) 3
d) 2
34. What will be the output of the following Python code?
print("abcdef".find("cd"))
a) True
b) 2
c) 3
d) None of the mentioned

35. What will be the output of the following Python code?


print("Hello {0} and {1}".format('foo', 'bin'))
a) Hello foo and bin
b) Hello {0} and {1} foo bin
c) Error
d) Hello 0 and 1
36. What will be the output of the following Python code?
print("Hello {name1} and {name2}".format(name1='foo', name2='bin'))
a) Hello foo and bin
b) Hello {name1} and {name2}
c) Error
d) Hello and

37. What will be the output of the following Python code?


print('ab12'.isalnum())
a) True
b) False
c) None
d) Error

38. What will be the output of the following Python code?


print('a B'.isalpha())
a) True
b) False
c) None
d) Error
Explanation: Space is not a letter.
39. What will be the output of the following Python code snippet?
print('0xa'.isdigit())
a) True
b) False
c) None

43 | P a g e
www.aktutor.in

d) Error

40. What will be the output of the following Python code snippet?
print('for'.isidentifier())
a) True
b) False
c) None
d) Error

41. What will be the output of the following Python code snippet?
print('abc'.islower())
a) True
b) False
c) None
d) Error

42. What will be the output of the following Python code snippet?
print('11'.isnumeric())
a) True
b) False
c) None
d) Error

43. What will be the output of the following Python code snippet?
print('HelloWorld'.istitle())
a) True
b) False
c) None
d) Error

44. What will be the output of the following Python code snippet?
print('abcdef12'.replace('cd', '12'))
a) ab12ef12
b) abcdef12
c) ab12efcd
d) none of the mentioned

45. What will be the output of the following Python code snippet?
print('Ab!2'.swapcase())
a) AB!@
b) ab12
c) aB!2
d) aB1@

ANSWERS :

1.d 2.c 3.b 4.d 5.c 6.b 7.c 8.a 9.d 10.a 11.c 12.d 13.a
14.c 15.b 16.b 17.c 18.b 19.d 20.d 21.c 22.c 23.b
24.c 25.a 26.c 27.a 28.b 29.a 30.c 31.a 32.c 33.b
34.b 35.a 36.a 37.a 38.b 39.b 40.a 41.a 42.a 43.b
44.a 45.c

44 | P a g e
www.aktutor.in

 MULTIPLE CHOICE QUESTIONS ANSWERS

1. Which of the following is a Python tuple?


a) [1, 2, 3]
b) (1, 2, 3)
c) {1, 2, 3}
d) {}

2.Suppose t = (1, 2, 4, 3), which of the following is incorrect?


a) print(t[3])
b) t[3] = 45
c) print(max(t))
d) print(len(t))

3.What will be the output of the following Python code?


>>>t=(1,2,4,3)
>>>t[1:3]
a)(1,2)
b)(1,2,4)
c)(2,4)
d) (2, 4, 3)

4. What will be the output of the following Python code?


t=(1,2,4,3)
t[1:-1]
a)(1,2)
b)(1,2,4)
c)(2,4)
d) (2, 4, 3)

5. What will be the output of the following Python code?


t = (1, 2, 4, 3, 8, 9)
[t[i] for i in range(0, len(t), 2)]
a)[2,3,9]
b)[1,2,4,3,8,9]
c)[1,4,8]
d) (1, 4, 8)

6. What will be the output of the following Python code?


d = {"john":40, "peter":45}
d["john"]
a)40
b)45
c)“john”

45 | P a g e
www.aktutor.in

d) “peter”

7. What will be the output of the following Python code?


t = (1, 2)
2*t
a)(1,2,1,2)
b)[1,2,1,2]
c)(1,1,2,2)
d) [1, 1, 2, 2]

8. What will be the output of the following Python code?


t1 = (1, 2, 4, 3)
t2 = (1, 2, 3, 4)
t1 < t2
a)True
b)False
c)Error
d) None

9. What will be the output of the following Python code?


my_tuple = (1, 2, 3, 4)
my_tuple.append( (5, 6, 7) )
print len(my_tuple)
a)1
b)2
c)5
d) Error

10. What is the data type of (1)?a)Tuple


b)Integer
c)List
d) Both tuple and integer

11. If a=(1,2,3,4), a[1:-1] is _________


a)Error, tuple slicing doesn’t exist
b)[2,3]
c)(2,3,4)
d) (2,3)

12. What will be the output of the following Python code?


a=(1,2,(4,5))
b=(1,2,(3,4))
a<b
a) False
b) True
c) Error, < operator is not valid for tuples

46 | P a g e
www.aktutor.in

d) Error, < operator is valid for tuples but not if there are sub-tuples

13. What will be the output of the following Python code?


a=("Check")*3
a
a)(‘Check’,’Check’,’Check’)
b)* Operator not valid for tuples
c)(‘CheckCheckCheck’)
d) Syntax error

14. What will be the output of the following Python code?


a=(1,2,3,4)
del(a[2])
a)Now,a=(1,2,4)
b)Now,a=(1,3,4)
c)Now,a=(3,4)
d) Error as tuple is immutable

15. What will be the output of the following Python code?


a=(2,3,4)
sum(a,3)
a) Too many arguments for sum() method
b) The method sum() doesn’t exist for tuples
c)12
d) 9

16. Is the following Python code valid?


a=(1,2,3,4)
del a
a)No because tuple is immutable
b)Yes, first element in the tuple is deleted
c)Yes, the entire tuple is deleted
d) No, invalid syntax for del method

17. What type of data is: a=[(1,1),(2,4),(3,9)]?


a) Array of tuples
b) List of tuples
c) Tuples of lists
d) Invalid type

18. What will be the output of the following Python code?


a=(0,1,2,3,4)
b=slice(0,2)
a[b]
a)Invalid syntax for slicing
b)[0,2]

47 | P a g e
www.aktutor.in

c)(0,1)
d) (0,2)

19. Is the following Python code valid?


a=(1,2,3)
b=('A','B','C')
c=tuple(zip(a,b))
a)Yes, c will be ((1, ‘A’), (2, ‘B’), (3, ‘C’))
b)Yes, c will be ((1,2,3),(‘A’,’B’,’C’))
c)No because tuples are immutable
d) No because the syntax for zip function isn’t valid

20. Is the following Python code valid?


a,b,c=1,2,3
a,b,c
a)Yes,[1,2,3] is printed
b)No,invalid syntax
c)Yes,(1,2,3) is printed
d) 1 is printed

21. Is the following Python code valid?


a,b=1,2,3
a)Yes, this is an example of tuple unpacking. a=1 and b=2
b) Yes, this is an example of tuple unpacking. a=(1,2) and b=3
c) No, too many values to unpack
d) Yes, this is an example of tuple unpacking. a=1 and b=(2,3)

22. What will be the output of the following Python code?


a=(1,2)
b=(3,4)
c=a+b
c
a)(4,6)
b)(1,2,3,4)
c)Error as tuples are immutable
d) None

23. What will be the output of the following Python code?


a,b=6,7
a,b=b,a
a,b
a)(6,7)
b)Invalid syntax
c)(7,6)
d) Nothing is printed

48 | P a g e
www.aktutor.in

24. Is the following Python code valid?


a=2,3,4,5
a
a)Yes, 2 is printed
b)Yes, [2,3,4,5] is printed
c)No, too many values to unpack
d) Yes, (2,3,4,5) is printed

25. What will be the output of the following Python code?


a=(2,3,1,5)
a.sort()
a
a)(1,2,3,5)
b)(2,3,1,5)
c)None
d) Error, tuple has no attribute sort

26. Is the following Python code valid?


a=(1,2,3)
b=a.update(4,)
a)Yes, a=(1,2,3,4) and b=(1,2,3,4)
b)Yes, a=(1,2,3) and b=(1,2,3,4)
c)No because tuples are immutable
d) No because wrong syntax for update() method

27. What will be the output of the following Python code?


a=[(2,4),(1,2),(3,9)]
a.sort()
a
a)[(1, 2), (2, 4), (3, 9)]
b)[(2,4),(1,2),(3,9)]
c)Error because tuples are immutable
d) Error, tuple has no sort attribute

1.b 2.b 3.c 4.c 5.c 6.a 7.a 8.b 9.d(Tuples are immutable and
don’t have an append method. An exception is thrown in this case.) 10.b 11.d
12.a 13.c 14.d 15.c 16.c 17.b 18.c 19.a 20.c(A tuple
needn’t be enclosed in parenthesis.) 21.c (For unpacking to happen, the number of
values of the right hand side must be equal to the number of variables on the left hand
side.) 22.b 23.c 24.d 25.d (A tuple is immutable thus it doesn’t have
a sort attribute.) 26.c (Tuple doesn’t have any update() attribute because it is
immutable.) 27.a (A list of tuples is a list itself. Hence items of a list can be
sorted.)

49 | P a g e
www.aktutor.in

 MULTIPLE CHOICE QUESTIONS ANSWERS

1. Which of the following commands will create a list?


a)list1=list()
b)list1=[]
c)list1=list([1,2,3])
d) all of the mentioned

2. What is the output when we execute list(“hello”)?


a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
b) [‘hello’]
c) [‘llo’]
d) [‘olleh’]

3. Suppose listExample is [‘h’,’e’,’l’,’l’,’o’], what is len(listExample)?


a) 5
b) 4
c) None
d) Error

4. Suppose list1 is [2445,133,12454,123], what is max(list1)?


a) 2445
b) 133
c) 12454
d) 123

5. Suppose list1 is [3, 5, 25, 1, 3], what is min(list1)?


a) 3
b) 5
c) 25
d) 1

6. Suppose list1 is [1, 5, 9], what is sum(list1)?


a) 1
b) 9
c) 15
d) Error

7. To shuffle the list(say list1) what function do we use?


a) list1.shuffle()
b) shuffle(list1)
c) random.shuffle(list1)
d) random.shuffleList(list1)

8. Suppose list1 is [4, 2, 2, 4, 5, 2, 1, 0], Which of the following is correct syntax for slicing
operation?
a) print(list1[0])
b) print(list1[:2])
c) print(list1[:-2])
d) all of the mentioned

50 | P a g e
www.aktutor.in

9. Suppose list1 is [2, 33, 222, 14, 25], What is list1[-1]?


a) Error
b) None
c) 25
d) 2

10. Suppose list1 is [2, 33, 222, 14, 25], What is list1[:-1]?
a) [2, 33, 222, 14]
b) Error
c) 25
d) [25, 14, 222, 33, 2]

11. What will be the output of the following Python code?


>>>names = ['Amir', 'Bear', 'Charlton', 'Daman']
>>>print(names[-1][-1])
A
b) Daman
c) Error
d) n

12. Suppose list1 is [1, 3, 2], What is list1 * 2?


a) [2, 6, 4]
b) [1, 3, 2, 1, 3]
c) [1, 3, 2, 1, 3, 2]
d) [1, 3, 2, 3, 2, 1]

13. What will be the output of the following Python code?


>>>list1 = [11, 2, 23]
>>>list2 = [11, 2, 2]
>>>list1 < list2 is
True
b) False
c) Error
d) None

14. To add a new element to a list we use which command?


a) list1.add(5)
b) list1.append(5)
c) list1.addLast(5)
d) list1.addEnd(5)

15. To insert 5 to the third position in list1, we use which command?


a) list1.insert(3, 5)
b) list1.insert(2, 5)
c) list1.add(3, 5)
d) list1.append(3, 5)

16. To remove string “hello” from list1, we use which command?


a) list1.remove(“hello”)
b) list1.remove(hello)
c) list1.removeAll(“hello”)
d) list1.removeOne(“hello”)

51 | P a g e
www.aktutor.in

17. Suppose list1 is [3, 4, 5, 20, 5], what is list1.index(5)?


a) 0
b) 1
c) 4
d) 2

18. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1.count(5)?


a) 0
b) 4
c) 1
d) 2

19. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.reverse()?
a) [3, 4, 5, 20, 5, 25, 1, 3]
b) [1, 3, 3, 4, 5, 5, 20, 25]
c) [25, 20, 5, 5, 4, 3, 3, 1]
d) [3, 1, 25, 5, 20, 5, 4, 3]

20. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.extend([34,
5])?
a) [3, 4, 5, 20, 5, 25, 1, 3, 34, 5]
b) [1, 3, 3, 4, 5, 5, 20, 25, 34, 5]
c) [25, 20, 5, 5, 4, 3, 3, 1, 34, 5]
d) [1, 3, 4, 5, 20, 5, 25, 3, 34, 5]

21. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.pop(1)?
a) [3, 4, 5, 20, 5, 25, 1, 3]
b) [1, 3, 3, 4, 5, 5, 20, 25]
c) [3, 5, 20, 5, 25, 1, 3]
d) [1, 3, 4, 5, 20, 5, 25]
22. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.pop()?
a) [3, 4, 5, 20, 5, 25, 1]
b) [1, 3, 3, 4, 5, 5, 20, 25]
c) [3, 5, 20, 5, 25, 1, 3]
d) [1, 3, 4, 5, 20, 5, 25]

23. What will be the output of the following Python code?


>>>"Welcome to Python".split()
a) [“Welcome”, “to”, “Python”]
b) (“Welcome”, “to”, “Python”)
c) {“Welcome”, “to”, “Python”}
d) “Welcome”, “to”, “Python”

24. What will be the output of the following Python code?


>>>list("a#b#c#d".split('#'))
a) [‘a’, ‘b’, ‘c’, ‘d’]
b) [‘a b c d’]
c) [‘a#b#c#d’]
d) [‘abcd’]

25. What will be the output of the following Python code?


myList = [1, 5, 5, 5, 5, 1]
max = myList[0]

52 | P a g e
www.aktutor.in

indexOfMax = 0
for i in range(1, len(myList)):
if myList[i] > max:
max = myList[i]
indexOfMax = i
print(indexOfMax)
a) 1
b) 2
c) 3
d) 4

26. What will be the output of the following Python code?


>>>list1 = [1, 3]
>>>list2 = list1
>>>list1[0] = 4
>>>print(list2)
[1, 3]
b) [4, 3]
c) [1, 4]
d) [1, 3, 4]

27. What will be the output of the following Python code?


names1 = ['Amir', 'Bala', 'Chales']
if 'amir' in names1:
print(1)
else:
print(2)
a) None
b) 1
c) 2
d) Error

28. What will be the output of the following Python code?


numbers = [1, 2, 3, 4]
numbers.append([5,6,7,8])
print(len(numbers))
a) 4
b) 5
c) 8
d) 12

29. which of the following the “in” operator can be used to check if an item is in it?
a) Lists
b) Dictionary
c) Set
d) All of the mentioned

30. What will be the output of the following Python code?


list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
print(len(list1 + list2))
a) 2
b) 4

53 | P a g e
www.aktutor.in

c) 5
d) 8

31. What will be the output of the following Python code?


matrix = [[1, 2, 3, 4],
[4, 5, 6, 7],
[8, 9, 10, 11],
[12, 13, 14, 15]]
for i in range(0, 4):
print(matrix[i][1], end = " ")
a) 1 2 3 4
b) 4 5 6 7
c) 1 3 8 12
d) 2 5 9 13

32. What will be the output of the following Python code?


data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
print(data[1][0][0])
a) 1
b) 2
c) 4
d) 5

33. What will be the output of the following Python code?


points = [[1, 2], [3, 1.5], [0.5, 0.5]]
points.sort()
print(points)
[[1, 2], [3, 1.5], [0.5, 0.5]]
b) [[3, 1.5], [1, 2], [0.5, 0.5]]
c) [[0.5, 0.5], [1, 2], [3, 1.5]]
d) [[0.5, 0.5], [3, 1.5], [1, 2]]

34. What will be the output of the following Python code?


a=[[]]*3
a[1].append(7)
print(a)
a) Syntax error
b) [[7], [7], [7]]
c) [[7], [], []]
d) [[],7, [], []]

35. What will be the output of the following Python code?


b=[2,3,4,5]
a=list(filter(lambda x:x%2,b))
print(a)
a) [2,4]
b) [ ]
c) [3,5]
d) Invalid arguments for filter function

36. What will be the output of the following Python code?


lst=[3,4,6,1,2]
lst[1:2]=[7,8]

54 | P a g e
www.aktutor.in

print(lst)
a) [3, 7, 8, 6, 1, 2]
b) Syntax error
c) [3,[7,8],6,1,2]
d) [3,4,6,7,8]

37. What will be the output of the following Python code?


a=[1,2,3]
b=a.append(4)print(a)
print(b)
a)[1,2,3,4]
[1,2,3,4]
b)[1, 2, 3, 4]
None
c)Syntax error
d)[1,2,3]
[1,2,3,4]

38. What will be the output of the following Python code?


a=[13,56,17]
a.append([87])
a.extend([45,67])
print(a)
a) [13, 56, 17, [87], 45, 67]
b) [13, 56, 17, 87, 45, 67]
c) [13, 56, 17, 87,[ 45, 67]]
d) [13, 56, 17, [87], [45, 67]]

39. What will be the output of the following Python code?


lst=[[1,2],[3,4]]
print(sum(lst,[]))
a) [[3],[7]]
b) [1,2,3,4]
c) Error
d) [10]

ANSWERS :

1.d 2.a 3.a 4.c 5.d 6.c 7.c 8.d 9.c 10.a 11.d 12.c 13.b 14.b 15.b 16.a
17.d 18.d 19.d 20.a 21.c 22.a 23.a 24.a
25.a 26.b 27.c 28.b 29.d 30.d 31.d 32.d 33.c 34.b
35.c 36.a 37.b 38.a 39.b

 MULTIPLE CHOICE QUESTIONS ANSWERS

1. Suppose list1 = [0.5 * x for x in range(0, 4)], list1 is:


a) [0, 1, 2, 3]
b) [0, 1, 2, 3, 4]
c) [0.0, 0.5, 1.0, 1.5]

55 | P a g e
www.aktutor.in

d) [0.0, 0.5, 1.0, 1.5, 2.0]

2. What will be the output of the following Python code?


>>>m = [[x, x + 1, x + 2] for x in range(0, 3)]
a) [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
b) [[0, 1, 2], [1, 2, 3], [2, 3, 4]]
c) [1, 2, 3, 4, 5, 6, 7, 8, 9]
d) [0, 1, 2, 1, 2, 3, 2, 3, 4]

3. How many elements are in m?


m = [[x, y] for x in range(0, 4) for y in range(0, 4)]
a) 8
b) 12
c) 16
d) 32

4. What will be the output of the following Python code snippet?


k = [print(i) for i in my_string if i not in "aeiou"]
a) prints all the vowels in my_string
b) prints all the consonants in my_string
c) prints all characters of my_string that aren’t vowels
d) prints only on executing print(k)

5. What is the output of print(k) in the following Python code snippet?

k = [print(i) for i in my_string if i not in "aeiou"]


print(k)
a) all characters of my_string that aren’t vowels
b) a list of Nones
c) list of Trues
d) list of Falses

6. What will be the output of the following Python code snippet?

my_string = "hello world"


k = [(i.upper(), len(i)) for i in my_string]
print(k)
a) [(‘HELLO’, 5), (‘WORLD’, 5)]
b) [(‘H’, 1), (‘E’, 1), (‘L’, 1), (‘L’, 1), (‘O’, 1), (‘ ‘, 1), (‘W’, 1), (‘O’, 1), (‘R’, 1), (‘L’, 1),
(‘D’, 1)]
c) [(‘HELLO WORLD’, 11)]
d) none of the mentioned

56 | P a g e
www.aktutor.in

7. What will be the output of the following Python code snippet?

x = [i**+1 for i in range(3)]; print(x);


a) [0, 1, 2]
b) [1, 2, 5]
c) error, **+ is not a valid operator
d) error, ‘;’ is not allowed

8. What will be the output of the following Python code snippet?

print([i.lower() for i in "HELLO"])


a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
b) ‘hello’
c) [‘hello’]
d) hello

9. What will be the output of the following Python code snippet?

print([if i%2==0: i; else: i+1; for i in range(4)])


a) [0, 2, 2, 4]
b) [1, 1, 3, 3]
c) error
d) none of the mentioned

10. Which of the following is the same as list(map(lambda x: x**-1, [1, 2, 3]))?
a) [x**-1 for x in [(1, 2, 3)]]
b) [1/x for x in [(1, 2, 3)]]
c) [1/x for x in (1, 2, 3)]
d) error

11. What will be the output of the following Python code?

l1=[1,2,3]
l2=[4,5,6]
[x*y for x in l1 for y in l2]
a) [4, 8, 12, 5, 10, 15, 6, 12, 18]
b) [4, 10, 18]
c) [4, 5, 6, 8, 10, 12, 12, 15, 18]
d) [18, 12, 6, 15, 10, 5, 12, 8, 4]

12. Write the list comprehension to pick out only negative integers from a
given list ‘l’.

57 | P a g e
www.aktutor.in

a) [x<0 in l]
b) [x for x<0 in l]
c) [x in l for x<0]
d) [x for x in l if x<0]

13. Write a list comprehension for number and its cube for l=[1, 2, 3, 4, 5, 6, 7, 8, 9].
a) [x**3 for x in l]
b) [x^3 for x in l]
c) [x**3 in l]
d) [x^3 in l]

14. Read the information given below carefully and write a list comprehension such
that the output is: [‘e’, ‘o’]

w="hello"
v=('a', 'e', 'i', 'o', 'u')
a) [x for w in v if x in v]
b) [x for x in w if x in v]
c) [x for x in v if w in v]
d) [x for v in w for x in w]

15. What will be the output of the following Python code?

t=32.00
[round((x-32)*5/9) for x in t]
a) [0]
b) 0
c) [0.00]
d) Error

16. Write a list comprehension for producing a list of numbers between 1 and 1000
that are divisible by 3.
a) [x in range(1, 1000) if x%3==0]
b) [x for x in range(1000) if x%3==0]
c) [x%3 for x in range(1, 1000)]
d) [x%3=0 for x in range(1, 1000)]

17. Write a list comprehension to produce the list: [1, 2, 4, 8, 16……212].


a) [(2**x) for x in range(0, 13)]
b) [(x**2) for x in range(1, 13)]
c) [(2**x) for x in range(1, 13)]
d) [(x**2) for x in range(0, 13)]

58 | P a g e
www.aktutor.in

18. What will be the output of the following Python list comprehension?

[j for i in range(2,8) for j in range(i*2, 50, i)]


a) A list of prime numbers up to 50
b) A list of numbers divisible by 2, up to 50
c) A list of non prime numbers, up to 50
d) Error

19. What will be the output of the following Python code?

l=["good", "oh!", "excellent!", "#450"]


[n for n in l if n.isalpha() or n.isdigit()]
a) [‘good’, ‘oh’, ‘excellent’, ‘450’ ]
b) [‘good’]
c) [‘good’, ‘#450’]
d) [‘oh!’, ‘excellent!’, ‘#450’]

20. Write a list comprehension equivalent for the Python code shown below.

for i in range(1, 101):


if int(i*0.5)==i*0.5:
print(i)
a) [i for i in range(1, 100) if int(i*0.5)==(i*0.5)]
b) [i for i in range(1, 101) if int(i*0.5)==(i*0.5)]
c) [i for i in range(1, 101) if int(i*0.5)=(i*0.5)]
d) [i for i in range(1, 100) if int(i*0.5)=(i*0.5)]

1.c 2.b 3.c 4.c 5.b 6.b 7.a 8.a 9.c 10.c 11.c 12.d 13.a
14.b 15.d 16.b 17.a 18.c 19.b 20.b

 MULTIPLE CHOICE QUESTIONS ANSWERS

1. Which of these about a set is not true?


a) Mutable data type
b) Allows duplicate values
c) Data type with unordered values
d) Immutable data type

2. Which of the following is not the correct syntax for creating a set?
a) set([[1,2],[3,4]])
b) set([1,2,2,3,4])

59 | P a g e
www.aktutor.in

c) set((1,2,3,4))
d) {1,2,3,4}

3. What will be the output of the following Python code?


nums = set([1,1,2,3,3,3,4,4])
print(len(nums))
a) 7
b) Error, invalid syntax for formation of set
c) 4
d) 8

4. Which of the following statements is used to create an empty set?


a) {}
b) set()
c) []
d) ( )

5. What will be the output of the following Python code?


a={5,4}
b={1,2,4,5}
a<b
a) {1,2}
b) True
c) False
d) Invalid operation
a<b returns True if a is a proper subset of b.

6. If a={5,6,7,8}, which of the following statements is false?


a) print(len(a))
b) print(min(a))
c) a.remove(5)
d) a[2]=45
7. If a={5,6,7}, what happens when a.add(5) is executed?
a) a={5,5,6,7}
b) a={5,6,7}
c) Error as there is no add function for set data type
d) Error as 5 already exists in the set

8. What will be the output of the following Python code?


a={4,5,6}
b={2,8,6}
a+b
a) {4,5,6,2,8}
b) {4,5,6,2,8,6}
c) Error as unsupported operand type for sets
d) Error as the duplicate item 6 is present in both sets

9. What will be the output of the following Python code?


a={4,5,6}
b={2,8,6}
a-b
a) {4,5}
b) {6}

60 | P a g e
www.aktutor.in

c) Error as unsupported operand type for set data type


d) Error as the duplicate item 6 is present in both sets

10. What will be the output of the following Python code?


a={5,6,7,8}
b={7,8,10,11}
a^b
a) {5,6,7,8,10,11}
b) {7,8}
c) Error as unsupported operand type of set data type
d) {5,6,10,11}
^ operator returns a set of elements in set A or set B, but not in both (symmetric difference).

11. What will be the output of the following Python code?


s={5,6}
s*3
a) Error as unsupported operand type for set data type
b) {5,6,5,6,5,6}
c) {5,6}
d) Error as multiplication creates duplicate elements which isn’t allowed
The multiplication operator isn’t valid for the set data type.

12. What will be the output of the following Python code?


a={3,4,5}
b={5,6,7}
a|b
a) Invalidoperation
b) {3,4,5,6,7}
c) {5}
d) {3,4,6,7}

13. Is the following Python code valid?


a={3,4,{7,5}}
print(a[2][0])
a) Yes, 7 is printed
b) Error, elements of a set can’t be printed
c) Error, subsets aren’t allowed
d) Yes, {7,5} is printed

14. Which of these about a frozenset is not true?


a)Mutable data type
b)Allows duplicate values
c)Data type with unordered values
d) Immutable data type

15. What will be the output of the following Python code?


a={3,4,5}
a.update([1,2,3])
a
a) Error, no method called update for set data type
b) {1, 2, 3, 4, 5}
c) Error, list can’t be added to set

61 | P a g e
www.aktutor.in

d) Error, duplicate item present in list

16. What will be the output of the following Python code?


>>> a={1,2,3}
>>> a.intersection_update({2,3,4,5})
>>> a
a) {2,3}
b) Error, duplicate item present in list
c) Error, no method called intersection_update for set data type
d) {1,4,5}

17. What will be the output of the following Python code?


>>> a={1,2,3}
>>> b=a
>>> b.remove(3)
>>> a
a) {1,2,3}
b) Error, copying of sets isn’t allowed
c) {1,2}
d) Error, invalid syntax for remove

18. What will be the output of the following Python code?


>>> a={1,2,3}
>>> b=a.add(4)
>>> b
a) 0
b) {1,2,3,4}
c) {1,2,3}
d) Nothing is printed

19. What will be the output of the following Python code?


>>> a={5,6,7}
>>> sum(a,5)
a) 5
b) 23
c) 18
d) Invalid syntax for sum method, too many arguments

20. What will be the output of the following Python code?


>>> a={1,2,3}
>>> {x*2 for x in a|{4,5}}
a) {2,4,6}
b) Error, set comprehensions aren’t allowed
c) {8,2,10,4,6}
d) {8,10}
Set comprehensions are allowed

21. What will be the output of the following Python code?


>>> a={5,6,7,8}
>>> b={7,8,9,10}
>>> len(a+b)
a) 8
b) Error, unsupported operand ‘+’ for sets

62 | P a g e
www.aktutor.in

c) 6
d) Nothing is displayed

22. What will be the output of the following Python code?


a={1,2,3}
b={1,2,3}
c=a.issubset(b)
print(c)
a) True
b) Error, no method called issubset() exists
c) Syntax error for issubset() method
d) False

23. Is the following Python code valid?


a={1,2,3}
b={1,2,3,4}
c=a.issuperset(b)
print(c)
a) False
b) True
c) Syntax error for issuperset() method
d) Error, no method called issuperset()

24.Set makes use of __________


Dictionary makes use of ____________
a) keys, keys
b) key values, keys
c) keys, key values
d) key values, key values

25. What will be the output of the following Python code?


s={2, 5, 6, 6, 7}
s
a) {2,5,7}
b) {2,5,6,7}
c) {2,5,6,6,7}
d) Error

26. Which of the following functions cannot be used on heterogeneous sets?


a) pop
b) remove
c) update
d) sum

27. Which of the following functions will return the symmetric difference between two sets, x
and y?
a) x|y
b) x^y
c) x&y
d) x – y

28. What will be the output of the following Python code snippet?
z=set('abc$de')

63 | P a g e
www.aktutor.in

'a' in z
a) True
b) False
c) Nooutput
d) Error

29. What will be the output of the following Python code snippet?
s=set([1, 2, 3])
s.union([4, 5])
s|([4, 5])
a) {1, 2, 3, 4, 5}
{1, 2, 3, 4, 5}
b) Error
{1, 2, 3, 4, 5}
c) {1, 2, 3, 4, 5}
Error
d) Error
Error
30. What will be the output of the following Python code snippet?
for x in set('pqr'):
print(x*2)
a) pp
qq
rr
b) pqr
pqr
c) ppqqrr
d) pqrpqr

31. What will be the output of the following Python code snippet?
{a**2 for a in range(4)}
a) {1,4,9,16}
b) {0,1,4,9,16}
c) Error
d) {0, 1, 4, 9}

32. The ____________ function removes the first element of a set and the last element of a list.
a) remove
b) pop
c) discard
d) dispose

33. The difference between the functions discard and remove is that:
a) Discard removes the last element of the set whereas remove removes the first element of the set
b) Discard throws an error if the specified element is not present in the set whereas remove does
not throw an error in case of absence of the specified element
c) Remove removes the last element of the set whereas discard removes the first element of the
set
d) Remove throws an error if the specified element is not present in the set whereas discard does
not throw an error in case of absence of the specified element

34. If we have two sets, s1 and s2, and we want to check if all the elements of s1 are present in s2
or not, we can use the function:

64 | P a g e
www.aktutor.in

a) s2.issubset(s1)
b) s2.issuperset(s1)
c) s1.issuperset(s2)
d) s1.isset(s2)

35. What will be the output of the following Python code, if s1= {1, 2, 3}?
s1.issubset(s1)
a) True
b) Error
c) Nooutput
d) False

1.d 2.a 3.c 4.b 5.b 6.d 7.b 8.c 9.a 10.d 11.a 12.d 13.c
14.a 15.b 16.a 17.c 18.d 19.b 20.c 21.b 22.a 23.a 24.c
25.b 26.d 27.b 28.a 29.c 30.a 31.d 32.b 33.d
34.b 35.a

 MULTIPLE CHOICE QUESTIONS ANSWERS

1. Which of the following statements create a dictionary?


a) d={}
b) d={“john”:40,“peter”:45}
c) d={40:”john”,45:”peter”}
d) All of the mentioned

2. What will be the output of the following Python code snippet?


d = {"john":40, "peter":45}
a) “john”,40,45,and“peter”
b) “john”and“peter”
c) 40and45
d) d = (40:”john”, 45:”peter”)

3. What will be the output of the following Python code snippet?


d = {"john":40, "peter":45}
"john" in d
a) True
b) False
c) None
d) Error

4. What will be the output of the following Python code snippet?


d1 = {"john":40, "peter":45}
d2 = {"john":466, "peter":45}
d1 == d2
a) True
b) False
c) None
d) Error

65 | P a g e
www.aktutor.in

5. What will be the output of the following Python code snippet?


d1 = {"john":40, "peter":45}
d2 = {"john":466, "peter":45}
d1 > d2
a) True
b) False
c) Error
d) None

6. What will be the output of the following Python code snippet?


d = {"john":40, "peter":45}
d["john"]
a) 40
b) 45
c) “john”
d) “peter”

7. Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john” what command do we
use?
a) d.delete(“john”:40)
b) d.delete(“john”)
c) del d[“john”]
d) del d(“john”:40)

8. Suppose d = {“john”:40, “peter”:45}. To obtain the number of entries in dictionary which


command do we use?
a) d.size()
b) len(d)
c) size(d)
d) d.len()

9. What will be the output of the following Python code snippet?


d = {"john":40, "peter":45}
print(list(d.keys()))
a) [“john”,“peter”]
b) [“john”:40,“peter”:45]
c) (“john”,“peter”)
d) (“john”:40, “peter”:45)

10. Suppose d = {“john”:40, “peter”:45}, what happens when we try to retrieve a value using
the expression d[“susan”]?
a) Since “susan” is not a value in the set, Python raises a KeyError exception
b) It is executed fine and no exception is raised, and it returns None
c) Since “susan” is not a key in the set, Python raises a KeyError exception
d) Since “susan” is not a key in the set, Python raises a syntax error

11. Which of these about a dictionary is false?


a) The values of a dictionary can be accessed using keys
b) The keys of a dictionary can be accessed using values
c) Dictionaries aren’t ordered
d) Dictionaries are mutable

66 | P a g e
www.aktutor.in

12. Which of the following is not a declaration of the dictionary?


a) {1:‘A’,2:‘B’}
b) dict([[1,”A”],[2,”B”]])
c) {1,”A”,2”B”}
d) { }

13. What will be the output of the following Python code snippet?
a={1:"A",2:"B",3:"C"}
for i,j in a.items():
print(i,j,end=" ")
a) 1A2B3C
b) 123
c) ABC
d) 1:”A” 2:”B” 3:”C”

14. What will be the output of the following Python code snippet?
a={1:"A",2:"B",3:"C"}
print(a.get(1,4))
a) 1
b) A
c) 4
d) Invalid syntax for get method

15. What will be the output of the following Python code?


a={1:"A",2:"B",3:"C"}
b={4:"D",5:"E"}
a.update(b)
print(a)
a) {1:‘A’,2:‘B’,3:‘C’}
b) Method update() doesn’t exist for dictionaries
c) {1:‘A’,2:‘B’,3:‘C’,4:‘D’,5:‘E’}
d) {4: ‘D’, 5: ‘E’}

16. What will be the output of the following Python code?


a={1:"A",2:"B",3:"C"}
b=a.copy()
b[2]="D"
print(a)
a) Error, copy() method doesn’t exist for dictionaries
b) {1:‘A’,2:‘B’,3:‘C’}
c) {1:‘A’,2:‘D’,3:‘C’}
d) “None” is printed

17. What will be the output of the following Python code?


a={1:"A",2:"B",3:"C"}
a.clear()
print(a)
a) None
b) {None:None,None:None,None:None}
c) {1:None,2:None,3:None}
d) { }

67 | P a g e
www.aktutor.in

18. Which of the following isn’t true about dictionary keys?


a) More than one key isn’t allowed
b) Keys must be immutable
c) Keys must be integers
d) When duplicate keys encountered, the last assignment wins

19. What will be the output of the following Python code?


a={1:5,2:3,3:4}
a.pop(3)
print(a)
a) {1:5}
b) {1:5,2:3}
c) Error, syntax error for pop() method
d) {1: 5, 3: 4}

20. What will be the output of the following Python code?


a={1:5,2:3,3:4}
print(a.pop(4,9))
a) 9
b) 3
c) Too many arguments for pop() method
d) 4

21. What will be the output of the following Python code?


a={1:"A",2:"B",3:"C"}
for i in a:
print(i,end=" ")
a) 123
b) ‘A’‘B’‘C’
c) 1‘A’2‘B’3‘C’
d) Error, it should be: for i in a.items():

22. What will be the output of the following Python code?


>>> a={1:"A",2:"B",3:"C"}
>>> a.items()
a) Syntax error
b) dict_items([(‘A’),(‘B’),(‘C’)])
c) dict_items([(1,2,3)])
d) dict_items([(1, ‘A’), (2, ‘B’), (3, ‘C’)])

23. Which of the statements about dictionary values if false?


a) More than one key can have the same value
b) The values of the dictionary can be accessed as dict[key]
c) Values of a dictionary must be unique
d) Values of a dictionary can be a mixture of letters and numbers

24. What will be the output of the following Python code snippet?
>>> a={1:"A",2:"B",3:"C"}
>>> del a
a) method del doesn’t exist for the dictionary

68 | P a g e
www.aktutor.in

b) del deletes the values in the dictionary


c) del deletes the entire dictionary
d) del deletes the keys in the dictionary

25. If a is a dictionary with some key-value pairs, what does a.popitem() do?
a) Removes an arbitrary element
b) Removes all the key-value pairs
c) Removes the key-value pair for the key given as an argument
d) Invalid method for dictionary

26. What will be the output of the following Python code?


>>> a={'B':5,'A':9,'C':7}
>>> sorted(a)
a) [‘A’,’B’,’C’]
b) [‘B’,’C’,’A’]
c) [5,7,9]
d) [9,5,7]
27. What will be the output of the following Python code?
>>> a={i: i*i for i in range(6)}
>>> a
a) Dictionary comprehension doesn’t exist
b) {0:0,1:1,2:4,3:9,4:16,5:25,6:36}
c) {0:0,1:1,4:4,9:9,16:16,25:25}
d) {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

28. What will be the output of the following Python code?


>>> a={}
>>> a.fromkeys([1,2,3],"check")
a) Syntax error
b) {1:”check”,2:”check”,3:”check”}
c) “check”
d) {1:None,2:None,3:None}

29. What will be the output of the following Python code?


>>> b={}
>>> all(b)
a) {}
b) False
c) True
d) An exception is thrown

30. If b is a dictionary, what does any(b) do?


a) Returns True if any key of the dictionary is true
b) Returns False if dictionary is empty
c) Returns True if all keys of the dictionary are true
d) Method any() doesn’t exist for dictionary

31. What will be the output of the following Python code?


>>> a={"a":1,"b":2,"c":3}
>>> b=dict(zip(a.values(),a.keys()))
>>> b
a) {‘a’:1,‘b’:2,‘c’:3}

69 | P a g e
www.aktutor.in

b) An exception is thrown
c) {‘a’:‘b’:‘c’:}
d) {1: ‘a’, 2: ‘b’, 3: ‘c’}

32. What will be the output of the following Python code?


>>> a=dict()
>>> a[1]
a) An exception is thrown since the dictionary is empty
b) ‘‘
c) 1
d) 0

ANSWERS :

1.d 2.b 3.a 4.b 5.c 6.a 7.c 8.b 9.a 10.c 11.b 12.c 13.a 14.b 15.c 16.b
17.d 18.c 19.b 20.d 21.a 22.d 23.c 24.c 25.a 26.a 27.d 28.b 29.c
30.a 31.d 32.a

 MULTIPLE CHOICE QUESTIONS ANSWERS

1. Lambda is a function in python?


A. True
B. False
C. Lambda is a function in python but user can not use it.
D. None of the above

2. What is the output of the following program?


z = lambda x : x * x
print(z(6))
A. 6
B. 36
C. 0
D. Error

1.a2.b3.4.5.

 MULTIPLE CHOICE QUESTIONS ANSWERS

1. To open a file c:\scores.txt for reading, we use _____________


a) infile = open(“c:\scores.txt”, “r”)
b) infile = open(“c:\\scores.txt”, “r”)
c) infile = open(file = “c:\scores.txt”, “r”)
d) infile = open(file = “c:\\scores.txt”, “r”)

2. To open a file c:\scores.txt for writing, we use ____________


a) outfile = open(“c:\scores.txt”, “w”)

70 | P a g e
www.aktutor.in

b) outfile = open(“c:\\scores.txt”, “w”)


c) outfile = open(file = “c:\scores.txt”, “w”)
d) outfile = open(file = “c:\\scores.txt”, “w”)

3. To open a file c:\scores.txt for appending data, we use ____________


a) outfile = open(“c:\\scores.txt”, “a”)
b) outfile = open(“c:\\scores.txt”, “rw”)
c) outfile = open(file = “c:\scores.txt”, “w”)
d) outfile = open(file = “c:\\scores.txt”, “w”)

4. Which of the following statements are true?


a) When you open a file for reading, if the file does not exist, an error occurs
b) When you open a file for writing, if the file does not exist, a new file is created
c) When you open a file for writing, if the file exists, the existing file is overwritten with the new file
d) All of the mentioned

5. To read two characters from a file object infile, we use ____________


a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()

6. To read the entire remaining contents of the file as a string from a file object infile, we use
____________
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()

7. What will be the output of the following Python code?

f = None
for i in range (5):
with open("data.txt", "w") as f:
if i > 2:
break

print(f.closed)

a) True
b) False
c) None
d) Error

8. To read the next line of the file from a file object infile, we use ____________
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()

9. To read the remaining lines of the file from a file object infile, we use ____________
a) infile.read(2)

71 | P a g e
www.aktutor.in

b) infile.read()
c) infile.readline()
d) infile.readlines()

10. The readlines() method returns ____________


a) str
b) a list of lines
c) a list of single characters
d) a list of integers

11. Which are the two built-in functions to read a line of text from standard input, which by default
comes from the keyboard?
a) Raw_input & Input
b) Input & Scan
c) Scan & Scanner
d) Scanner

12. What will be the output of the following Python code?

str = raw_input("Enter your input: ");

print "Received input is : ", str

a) Enter your input: Hello Python


Received input is : Hello Python
b) Enter your input: Hello Python
Received input is : Hello
c) Enter your input: Hello Python
Received input is : Python
d) None of the mentioned

13. What will be the output of the following Python code?

str = input("Enter your input: ");

print "Received input is : ", str

a) Enter your input: [x*5 for x in range(2,10,2)]


Received input is : [x*5 for x in range(2,10,2)]
b) Enter your input: [x*5 for x in range(2,10,2)]
Received input is : [10, 30, 20, 40]
c) Enter your input: [x*5 for x in range(2,10,2)]
Received input is : [10, 10, 30, 40]
d) None of the mentioned

14. Which one of the following is not attributes of file?

a) closed
b) softspace
c) rename
d) mode

15. What is the use of tell() method in python?

72 | P a g e
www.aktutor.in

a) tells you the current position within the file


b) tells you the end position within the file
c) tells you the file is opened or not
d) none of the mentioned

16. What is the current syntax of rename() a file?

a) rename(current_file_name, new_file_name)
b) rename(new_file_name, current_file_name,)
c) rename(()(current_file_name, new_file_name))
d) none of the mentioned

17. What is the current syntax of remove() a file?


a) remove(file_name)
b) remove(new_file_name, current_file_name,)
c) remove(() , file_name))
d) none of the mentioned

18. What will be the output of the following Python code?

fo = open("foo.txt", "rw+")
print "Name of the file: ", fo.name

# Assuming file has following 5 lines

# This is 1st line

# This is 2nd line

# This is 3rd line

# This is 4th line

# This is 5th line

for index in range(5):


line = fo.next()
print "Line No %d - %s" % (index, line)

# Close opened file


fo.close()

a) Compilation Error
b) Syntax Error
c) Displays Output
d) None of the mentioned

19. What is the use of seek() method in files?


a) sets the file’s current position at the offset
b) sets the file’s previous position at the offset
c) sets the file’s current position within the file
d) none of the mentioned

20. What is the use of truncate() method in file?

73 | P a g e
www.aktutor.in

a) truncates the file size


b) deletes the content of the file
c) deletes the file size
d) none of the mentioned.

21. Which is/are the basic I/O connections in file?


a) Standard Input
b) Standard Output
c) Standard Errors
d) All of the mentioned

22. What will be the output of the following Python code?

import sys
sys.stdout.write(' Hello\n')
sys.stdout.write('Python\n')

a) Compilation Error
b) Runtime Error
c) Hello Python
d) Hello
Python

23. Which of the following mode will refer to binary data?


a) r
b) w
c) +
d) b

24. What is the pickling?


a) It is used for object serialization
b) It is used for object deserialization
c) None of the mentioned
d) All of the mentioned

25. What is unpickling?


a) It is used for object serialization
b) It is used for object deserialization
c) None of the mentioned
d) All of the mentioned

26. What is the correct syntax of open() function?


a) file = open(file_name [, access_mode][, buffering])
b) file object = open(file_name [, access_mode][, buffering])
c) file object = open(file_name)
d) none of the mentioned.

27. What will be the output of the following Python code?

fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name
fo.flush()
fo.close()

74 | P a g e
www.aktutor.in

a) Compilation Error
b) Runtime Error
c) No Output
d) Flushes the file when closing them

28. Correct syntax of file.writelines() is?


a) file.writelines(sequence)
b) fileObject.writelines()
c) fileObject.writelines(sequence)
d) none of the mentioned

29. Correct syntax of file.readlines() is?


a) fileObject.readlines( sizehint );
b) fileObject.readlines();
c) fileObject.readlines(sequence)
d) none of the mentioned.

30. In file handling, what does this terms means “r, a”?
a) read, append
b) append, read
c) write, append
d) none of the mentioned

31. What is the use of “w” in file handling?


a) Read
b) Write
c) Append
d) None of the mentioned

32. What is the use of “a” in file handling?


a) Read
b) Write
c) Append
d) None of the mentioned

33. Which function is used to read all the characters?


a) Read()
b) Readcharacters()
c) Readall()
d) Readchar()

34. Which function is used to read single line from file?


a) Readline()
b) Readlines()
c) Readstatement()
d) Readfullline()

35. Which function is used to write all the characters?


a) write()
b) writecharacters()
c) writeall()
d) writechar()

75 | P a g e
www.aktutor.in

36. Which function is used to write a list of string in a file?


a) writeline()
b) writelines()
c) writestatement()
d) writefullline()

37. Which function is used to close a file in python?


a) Close()
b) Stop()
c) End()
d) Closefile()

38. Is it possible to create a text file in python?


a) Yes
b) No
c) Machine dependent
d) All of the mentioned

39. Which of the following are the modes of both writing and reading in binary format in file?
a) wb+
b) w
c) wb
d) w+

40. Which of the following is not a valid mode to open a file?


a) ab
b) rw
c) r+
d) w+

41. What is the difference between r+ and w+ modes?


a) no difference
b) in r+ the pointer is initially placed at the beginning of the file and the pointer is at the end for w+
c) in w+ the pointer is initially placed at the beginning of the file and the pointer is at the end for r+
d) depends on the operating system

42. How do you get the name of a file from a file object (fp)?
a) fp.name
b) fp.file(name)
c) self.__name__(fp)
d) fp.__name__()

43. Which of the following is not a valid attribute of a file object (fp)?
a) fp.name
b) fp.closed
c) fp.mode
d) fp.size

44. How do you close a file object (fp)?


a) close(fp)
b) fclose(fp)

76 | P a g e
www.aktutor.in

c) fp.close()
d) fp.__close__()

45. How do you get the current position within the file?
a) fp.seek()
b) fp.tell()
c) fp.loc
d) fp.pos

46. How do you rename a file?


a) fp.name = ‘new_name.txt’
b) os.rename(existing_name, new_name)
c) os.rename(fp, new_name)
d) os.set_name(existing_name, new_name)

47. How do you delete a file?


a) del(fp)
b) fp.delete()
c) os.remove(‘file’)
d) os.delete(‘file’)

48. How do you change the file position to an offset value from the start?
a) fp.seek(offset, 0)
b) fp.seek(offset, 1)
c) fp.seek(offset, 2)
d) none of the mentioned

49. What happens if no arguments are passed to the seek function?


a) file position is set to the start of file
b) file position is set to the end of file
c) file position remains unchanged
d) error

 MULTIPLE CHOICE QUESTIONS ANSWERS

1. How many except statements can a try-except block have?


a) zero
b) one
c) more than one
d) more than zero

2. When will the else part of try-except-else be executed?


a) always
b) when an exception occurs
c) when no exception occurs
d) when an exception occurs in to except block

3. Is the following Python code valid?


try:
# Do somethingexcept:
# Do somethingfinally:
# Do something
a) no, there is no such thing as finally

77 | P a g e
www.aktutor.in

b) no, finally cannot be used with except


c) no, finally must come before except
d) yes

4. Is the following Python code valid?


try:
# Do somethingexcept:
# Do somethingelse:
# Do something
a) no, there is no such thing as else
b) no, else cannot be used with except
c) no, else must come before except
d) yes

5. Can one block of except statements handle multiple exception?


a) yes, like except TypeError, SyntaxError [,…]
b) yes, like except [TypeError, SyntaxError]
c) no
d) none of the mentioned

6. When is the finally block executed?


a) when there is no exception
b) when there is an exception
c) only if some condition that has been specified is satisfied
d) always

7. What will be the output of the following Python code?


def foo():
try:
return 1
finally:
return 2
k = foo()print(k)
a) 1
b) 2
c) 3
d) error, there is more than one return statement in a single try-finally block

8. What will be the output of the following Python code?


def foo():
try:
print(1)
finally:
print(2)
foo()
a) 1 2
b) 1
c) 2
d) none of the mentioned

9. What happens when ‘1’ == 1 is executed?


a) we get a True
b) we get a False

78 | P a g e
www.aktutor.in

c) an TypeError occurs
d) a ValueError occurs

10. The following Python code will result in an error if the input value is entered as -5.
assert False, 'Spanish'
a) True
b) False

11. What will be the output of the following Python code?


x=10
y=8assert x>y, 'X too small'
a) Assertion Error
b) 10 8
c) No output
d) 108

12. What will be the output of the following Python code?


#generatordef f(x):
yield x+1
g=f(8)print(next(g))
a) 8
b) 9
c) 7
d) Error

13. What will be the output of the following Python code?


def a():
try:
f(x, 4)
finally:
print('after f')
print('after f?')
a()
a) No output
b) after f?
c) error
d) after f

14. What will be the output of the following Python code?


def f(x):
for i in range(5):
yield i
g=f(8)print(list(g))
a) [0, 1, 2, 3, 4]
b) [1, 2, 3, 4, 5, 6, 7, 8]
c) [1, 2, 3, 4, 5]
d) [0, 1, 2, 3, 4, 5, 6, 7]
Explanation: The output of the code shown above is a list containing whole numbers in the range (5).
Hence the output of this code is: [0, 1, 2, 3, 4].

15. The error displayed in the following Python code is?


import itertools
l1=(1, 2, 3)

79 | P a g e
www.aktutor.in

l2=[4, 5, 6]
l=itertools.chain(l1, l2)print(next(l1))
a) ‘list’ object is not iterator
b) ‘tuple’ object is not iterator
c) ‘list’ object is iterator
d) ‘tuple’ object is iterator

16. Which of the following is not an exception handling keyword in Python?


a) try
b) except
c) accept
d) finally

17. What happens if the file is not found in the following Python code?
a=Falsewhile not a:
try:
f_n = input("Enter file name")
i_f = open(f_n, 'r')
except:
print("Input file not found")
a) No error
b) Assertion error
c) Input output error
d) Name error

18. What will be the output of the following Python code?


lst = [1, 2, 3]
lst[3]
a) NameError
b) ValueError
c) IndexError
d) TypeError
19. What will be the output of the following Python code?
t[5]
a) IndexError
b) NameError
c) TypeError
d) ValeError

20. What will be the output of the following Python code, if the time module has already been
imported?
4 + '3'
a) NameError
b) IndexError
c) ValueError
d) TypeError

21. What will be the output of the following Python code?


int('65.43')
a) ImportError
b) ValueError
c) TypeError
d) NameError

80 | P a g e
www.aktutor.in

22. What will be the output of the following Python code if the input entered is 6?
valid = Falsewhile not valid:
try:
n=int(input("Enter a number"))
while n%2==0:
print("Bye")
valid = True
except ValueError:
print("Invalid")
a) Bye (printed once)
b) No output
c) Invalid (printed once)
d) Bye (printed infinite number of times)

23. Identify the type of error in the following Python codes?


Print(“Good Morning”)print(“Good night)
a) Syntax, Syntax
b) Semantic, Syntax
c) Semantic, Semantic
d) Syntax, Semantic

24. Which of the following statements is true?


a) The standard exceptions are automatically imported into Python programs
b) All raised standard exceptions must be handled in Python
c) When there is a deviation from the rules of a programming language, a semantic error is thrown
d) If any exception is thrown in try block, else block is executed

25. Which of the following is not a standard exception in Python?


a) NameError
b) IOError
c) AssignmentError
d) ValueError

26. Syntax errors are also known as parsing errors.


a) True
b) False

27. An exception is ____________


a) an object
b) a special function
c) a standard module
d) a module

28. _______________________ exceptions are raised as a result of an error in opening a


particular file.
a) ValueError
b) TypeError
c) ImportError
d) IOError

29. Which of the following blocks will be executed whether an exception is thrown or not?
a) except

81 | P a g e
www.aktutor.in

b) else
c) finally
d) assert

1.d 2.c 3.b 4.d 5.a 6.d 7.b 8.a 9.b 10.a 11.c 12.b
13.c 14.a 15.b 16.c 17.a 18.c 19.b 20.d 21.b 22.d
23.b 24.a 25.c 26.a 27.a 28.d 29.c

 MULTIPLE CHOICE QUESTIONS ANSWERS

1. Which of these definitions correctly describes a module?


a) Denoted by triple quotes for providing the specification of certain program elements
b) Design and implementation of specific functionality to be incorporated into a program
c) Defines the specification of how it is to be used
d) Any program that reuses code

2. Which of the following is not an advantage of using modules?


a) Provides a means of reuse of program code
b) Provides a means of dividing up tasks
c) Provides a means of reducing the size of the program
d) Provides a means of testing individual parts of the program

3. Program code making use of a given module is called a ______ of the module.
a) Client
b) Docstring
c) Interface
d) Modularity

4. ______ is a string literal denoted by triple quotes for providing the specifications of certain
program elements.
a) Interface
b) Modularity
c) Client
d) Docstring

5. Which of the following is true about top-down design process?


a) The details of a program design are addressed before the overall design
b) Only the details of the program are addressed
c) The overall design of the program is addressed before the details
d) Only the design of the program is addressed

6. In top-down design every module is broken into same number of submodules.


a) True
b) False

7. All modular designs are because of a top-down design process.


a) True
b) False

82 | P a g e
www.aktutor.in

8. Which of the following isn’t true about main modules?


a) When a python file is directly executed, it is considered main module of a program
b) Main modules may import any number of modules
c) Special name given to main modules is: __main__
d) Other main modules can import main modules
.
9. Which of the following is not a valid namespace?
a) Global namespace
b) Public namespace
c) Built-in namespace
d) Local namespace

10. Which of the following is false about “import modulename” form of import?
a) The namespace of imported module becomes part of importing module
b) This form of import prevents name clash
c) The namespace of imported module becomes available to importing module
d) The identifiers in module are accessed as: modulename.identifier

11. Which of the following is false about “from-import” form of import?


a) The syntax is: from modulename import identifier
b) This form of import prevents name clash
c) The namespace of imported module becomes part of importing module
d) The identifiers in module are accessed directly as: identifier

12. Which of the statements about modules is false?


a) In the “from-import” form of import, identifiers beginning with two underscores are private and
aren’t imported
b) dir() built-in function monitors the items in the namespace of the main module
c) In the “from-import” form of import, all identifiers regardless of whether they are private or public
are imported
d) When a module is loaded, a compiled version of the module with file extension .pyc is
automatically produced

13. What will be the output of the following Python code?


from math import factorialprint(math.factorial(5))
a) 120
b) Nothing is printed
c) Error, method factorial doesn’t exist in math module
d) Error, the statement should be: print(factorial(5))

14. What is the order of namespaces in which Python looks for an identifier?
a) Python first searches the global namespace, then the local namespace and finally the built-in
namespace
b) Python first searches the local namespace, then the global namespace and finally the built-in
namespace
c) Python first searches the built-in namespace, then the global namespace and finally the local
namespace
d) Python first searches the built-in namespace, then the local namespace and finally the global
namespace

83 | P a g e
www.aktutor.in

1.b 2.c 3.a 4.d 5.c 6.b 7.b 8.d 9.b 10.a 11.b 12.c
13.d 14.b

 MULTIPLE CHOICE QUESTIONS ANSWERS

1. _____ represents an entity in the real world with its identity and behaviour.
a) A method
b) An object
c) A class
d) An operator

2. _____ is used to create an object.


a) class
b) constructor
c) User-defined functions
d) In-built functions

3. What will be the output of the following Python code?


class test:
def __init__(self,a="Hello World"):
self.a=a

def display(self):
print(self.a)
obj=test()
obj.display()
a) The program has an error because constructor can’t have default arguments
b) Nothing is displayed
c) “Hello World” is displayed
d) The program has an error display function doesn’t have parameters

4. What is setattr() used for?


a) To access the attribute of the object
b) To set an attribute
c) To check if an attribute exists or not
d) To delete an attribute

5. What is getattr() used for?


a) To access the attribute of the object
b) To delete an attribute
c) To check if an attribute exists or not
d) To set an attribute

6. What will be the output of the following Python code?


class change:
def __init__(self, x, y, z):
self.a = x + y + z

x = change(1,2,3)
y = getattr(x, 'a')setattr(x, 'a', y+1)print(x.a)

84 | P a g e
www.aktutor.in

a) 6
b) 7
c) Error
d) 0

7. What will be the output of the following Python code?


class test:
def __init__(self,a):
self.a=a

def display(self):
print(self.a)
obj=test()
obj.display()
a) Runs normally, doesn’t display anything
b) Displays 0, which is the automatic default value
c) Error as one argument is required while creating the object
d) Error as display function requires additional argument

8. Is the following Python code correct?


>>> class A:
def __init__(self,b):
self.b=b
def display(self):
print(self.b)>>> obj=A("Hello")>>> del obj
a) True
b) False

9. What will be the output of the following Python code?


class test:
def __init__(self):
self.variable = 'Old'
self.Change(self.variable)
def Change(self, var):
var = 'New'
obj=test()print(obj.variable)
a) Error because function change can’t be called in the __init__ function
b) ‘New’ is printed
c) ‘Old’ is printed
d) Nothing is printed

10. What is Instantiation in terms of OOP terminology?


a) Deleting an instance of class
b) Modifying an instance of class
c) Copying an instance of class
d) Creating an instance of class

11. What will be the output of the following Python code?


class fruits:
def __init__(self, price):
self.price = price
obj=fruits(50)

85 | P a g e
www.aktutor.in

obj.quantity=10
obj.bags=2
print(obj.quantity+len(obj.__dict__))
a) 12
b) 52
c) 13
d) 60

12. The assignment of more than one function to a particular operator is _______
a) Operator over-assignment
b) Operator overriding
c) Operator overloading
d) Operator instance

13. Which of the following is not a class method?


a) Non-static
b) Static
c) Bounded
d) Unbounded

14. Which of the following Python code creates an empty class?


a)
class A:
return
b)
class A:
pass
c)
class A:
d) It is not possible to create an empty class

15. What are the methods which begin and end with two underscore characters called?
a) Special methods
b) In-built methods
c) User-defined methods
d) Additional methods

16. Special methods need to be explicitly called during object creation.


a) True
b) False

17. Is the following Python code valid?


class B(object):
def first(self):
print("First method called")
def second():
print("Second method called")
ob = B()
B.first(ob)
a) It isn’t as the object declaration isn’t right
b) It isn’t as there isn’t any __init__ method for initializing class members
c) Yes, this method of calling is called unbounded method call

86 | P a g e
www.aktutor.in

d) Yes, this method of calling is called bounded method call

18. What is hasattr(obj,name) used for?


a) To access the attribute of the object
b) To delete an attribute
c) To check if an attribute exists or not
d) To set an attribute
19. What is delattr(obj,name) used for?
a) To print deleted attribute
b) To delete an attribute
c) To check if an attribute is deleted or not
d) To set an attribute

20. __del__ method is used to destroy instances of a class.


a) True
b) False

21. What does print(Test.__name__) display (assuming Test is the name of the class)?
a) ()
b) Exception is thrown
c) Test
d) __main__

22. What will be the output of the following Python code?


class stud:
def __init__(self, roll_no, grade):
self.roll_no = roll_no
self.grade = grade
def display (self):
print("Roll no : ", self.roll_no, ", Grade: ", self.grade)
stud1 = stud(34, ‘S’)
stud1.age=7print(hasattr(stud1, 'age'))
a) Error as age isn’t defined
b) True
c) False
d) 7

ANSWERS :

1.b 2.b 3.c 4.b 5.a 6.b 7.c 8.a 9.c 10.d 11.c 12.c
13.a 14.b 15.a 16.b 17.c 18.c 19.b 20.a 21.c 22.a

 MULTIPLE CHOICE QUESTIONS ANSWERS

1. Which of the following best describes inheritance?


a) Ability of a class to derive members of another class as a part of its own definition
b) Means of bundling instance variables and methods in order to restrict access to certain class
members
c) Focuses on variables and passing of variables to functions

87 | P a g e
www.aktutor.in

d) Allows for implementation of elegant software that is well designed and easily modified

2. Which of the following statements is wrong about inheritance?


a) Protected members of a class can be inherited
b) The inheriting class is called a subclass
c) Private members of a class can be inherited and accessed
d) Inheritance is one of the features of OOP

3. What will be the output of the following Python code?


class Demo:
def __new__(self):
self.__init__(self)
print("Demo's __new__() invoked")
def __init__(self):
print("Demo's __init__() invoked")class Derived_Demo(Demo):
def __new__(self):
print("Derived_Demo's __new__() invoked")
def __init__(self):
print("Derived_Demo's __init__() invoked")def main():
obj1 = Derived_Demo()
obj2 = Demo()
main()
a) Derived_Demo’s __init__() invoked
Derived_Demo's __new__() invoked
Demo's __init__() invoked
Demo's __new__() invoked
b) Derived_Demo's __new__() invoked
Demo's __init__() invoked
Demo's __new__() invoked
c) Derived_Demo's __new__() invoked
Demo's __new__() invoked
d) Derived_Demo’s __init__() invoked
Demo's __init__() invoked

4. What will be the output of the following Python code?


class Test:
def __init__(self):
self.x = 0class Derived_Test(Test):
def __init__(self):
self.y = 1def main():
b = Derived_Test()
print(b.x,b.y)
main()
a) 0 1
b) 0 0
c) Error because class B inherits A but variable x isn’t inherited
d) Error because when object is created, argument must be passed like Derived_Test(1)

5. What will be the output of the following Python code?


class A():
def disp(self):
print("A disp()")class B(A):

88 | P a g e
www.aktutor.in

pass
obj = B()
obj.disp()
a) Invalid syntax for inheritance
b) Error because when object is created, argument must be passed
c) Nothing is printed
d) A disp()

6. All subclasses are a subtype in object-oriented programming.


a) True
b) False

7. When defining a subclass in Python that is meant to serve as a subtype, the subtype Python
keyword is used.
a) True
b) False

8. Suppose B is a subclass of A, to invoke the __init__ method in A from B, what is the line of
code you should write?
a) A.__init__(self)
b) B.__init__(self)
c) A.__init__(B)
d) B.__init__(A)

9. What will be the output of the following Python code?


class Test:
def __init__(self):
self.x = 0class Derived_Test(Test):
def __init__(self):
Test.__init__(self)
self.y = 1def main():
b = Derived_Test()
print(b.x,b.y)
main()
a) Error because class B inherits A but variable x isn’t inherited
b) 0 0
c) 0 1
d) Error, the syntax of the invoking method is wrong

10. What will be the output of the following Python code?


class A:
def __init__(self, x= 1):
self.x = xclass der(A):
def __init__(self,y = 2):
super().__init__()
self.y = ydef main():
obj = der()
print(obj.x, obj.y)
main()
a) Error, the syntax of the invoking method is wrong
b) The program runs fine but nothing is printed
c) 1 0
d) 1 2

89 | P a g e
www.aktutor.in

11. What does built-in function type do in context of classes?


a) Determines the object name of any value
b) Determines the class name of any value
c) Determines class description of any value
d) Determines the file name of any value

12. Which of the following is not a type of inheritance?


a) Double-level
b) Multi-level
c) Single-level
d) Multiple

13. What does built-in function help do in context of classes?


a) Determines the object name of any value
b) Determines the class identifiers of any value
c) Determines class description of any built-in type
d) Determines class description of any user-defined built-in type

14. What will be the output of the following Python code?


class A:
def one(self):
return self.two()

def two(self):
return 'A'
class B(A):
def two(self):
return 'B'
obj1=A()
obj2=B()print(obj1.two(),obj2.two())
a) A A
b) A B
c) B B
d) An exception is thrown

15. What type of inheritance is illustrated in the following Python code?


class A():
passclass B():
passclass C(A,B):
pass
a) Multi-level inheritance
b) Multiple inheritance
c) Hierarchical inheritance
d) Single-level inheritance

16. What type of inheritance is illustrated in the following Python code?


class A():
passclass B(A):
passclass C(B):
pass
a) Multi-level inheritance
b) Multiple inheritance

90 | P a g e
www.aktutor.in

c) Hierarchical inheritance
d) Single-level inheritance

17. What does single-level inheritance mean?


a) A subclass derives from a class which in turn derives from another class
b) A single superclass inherits from multiple subclasses
c) A single subclass derives from a single superclass
d) Multiple base classes inherit a single derived class

18. What will be the output of the following Python code?


class A:
def __init__(self):
self.__i = 1
self.j = 5

def display(self):
print(self.__i, self.j)class B(A):
def __init__(self):
super().__init__()
self.__i = 2
self.j = 7
c = B()
c.display()
a) 2 7
b) 1 5
c) 1 7
d) 2 5

19. Which of the following statements isn’t true?


a) A non-private method in a superclass can be overridden
b) A derived class is a subset of superclass
c) The value of a private variable in the superclass can be changed in the subclass
d) When invoking the constructor from a subclass, the constructor of superclass is automatically
invoked

20. What will be the output of the following Python code?


>>> class A:
pass>>> class B(A):
pass>>> obj=B()>>> isinstance(obj,A)
a) True
b) False
c) Wrong syntax for isinstance() method
d) Invalid method for classes

21. Which of the following statements is true?


a) The __new__() method automatically invokes the __init__ method
b) The __init__ method is defined in the object class
c) The __eq(other) method is defined in the object class
d) The __repr__() method is defined in the object class

22. Method issubclass() checks if a class is a subclass of another class.


a) True
b) False

91 | P a g e
www.aktutor.in

23. What will be the output of the following Python code?


class A:
def __init__(self):
self.__x = 1class B(A):
def display(self):
print(self.__x)def main():
obj = B()
obj.display()
main()
a) 1
b) 0
c) Error, invalid syntax for object declaration
d) Error, private class member can’t be accessed in a subclass

24. What will be the output of the following Python code?


class A:
def __init__(self):
self._x = 5 class B(A):
def display(self):
print(self._x)def main():
obj = B()
obj.display()
main()
a) Error, invalid syntax for object declaration
b) Nothing is printed
c) 5
d) Error, private class member can’t be accessed in a subclass

25. What will be the output of the following Python code?


class A:
def __init__(self,x=3):
self._x = x class B(A):
def __init__(self):
super().__init__(5)
def display(self):
print(self._x)def main():
obj = B()
obj.display()

main()
a) 5
b) Error, class member x has two values
c) 3
d) Error, protected class member can’t be accessed in a subclass
View Answer
26. What will be the output of the following Python code?
class A:
def test1(self):
print(" test of A called ")class B(A):
def test(self):
print(" test of B called ")class C(A):
def test(self):

92 | P a g e
www.aktutor.in

print(" test of C called ")class D(B,C):


def test2(self):
print(" test of D called ")
obj=D()
obj.test()
a)
test of B called
test of C called
b)
test of C called
test of B called
c) test of B called
d) Error, both the classes from which D derives has same method test()

27. What will be the output of the following Python code?


class A:
def test(self):
print("test of A called")class B(A):
def test(self):
print("test of B called")
super().test() class C(A):
def test(self):
print("test of C called")
super().test()class D(B,C):
def test2(self):
print("test of D called")
obj=D()
obj.test()

a) test of B called
test of C called
test of A called
b) test of C called
test of B called
c) test of B called
test of C called
d) Error, all the three classes from which D derives has same method test()

1.a 2.c 3.b 4.c 5.d 6.b 7.b 8.a 9.c 10.d 11.b 12.a 13.c 14.b
15.b 16.a 17.c 18.c 19.c 20.a 21.c 22.a 23.d 24.c 25.a 26.c
27.a

93 | P a g e
www.aktutor.in

UNIT V

 MULTIPLE CHOICE QUESTIONS ANSWERS

1. Which is the most appropriate definition for recursion?


a) A function that calls itself
b) A function execution instance that calls another execution instance of the same function
c) A class method that calls another class method
d) An in-built method that is automatically called

2. Only problems that are recursively defined can be solved using recursion.

94 | P a g e
www.aktutor.in

a)True
b) False

3. Which of these is false about recursion?


a) Recursive function can be replaced by a non-recursive function
b) Recursive functions usually take more memory space than non-recursive function
c) Recursive functions run faster than non-recursive function
d) Recursion makes programs easier to understand

4. Fill in the line of the following Python code for calculating the factorial of a number.
def fact(num):
if num == 0:
return 1
else:
return _____________________
a) num*fact(num-1)
b) (num-1)*(num-2)
c) num*(num-1)
d) fact(num)*fact(num-1)

5. What will be the output of the following Python code?


def test(i,j):
if(i==0):
return j
else:
return test(i-1,i+j)
print(test(4,7))

a) 13
b) 7
c) Infinite loop
d) 17

6. What is tail recursion?


a) A recursive function that has two base cases
b) A function where the recursive functions leads to an infinite loop
c) A recursive function where the function doesn’t return anything and just prints the values
d) A function where the recursive call is the last thing executed by the function

7. Which of the following statements is false about recursion?


a) Every recursive function must have a base case
b) Infinite recursion can occur if the base case isn’t properly mentioned
c) A recursive function makes the code easier to understand
d) Every recursive function must have a return value

8. What will be the output of the following Python code?


def fun(n):
if (n > 100):
return n - 5
return fun(fun(n+11));
print(fun(45))

a) 50

95 | P a g e
www.aktutor.in

b) 100
c) 74
d) Infinite loop

9. Recursion and iteration are the same programming approach.


a)True
b) False

10. What happens if the base condition isn’t defined in recursive programs?
a) Program gets into an infinite loop
b) Program runs once
c) Program runs n number of times where n is the argument given to the function
d) An exception is thrown

11. Which of these is not true about recursion?


a) Making the code look clean
b) A complex task can be broken into sub-problems
c) Recursive calls take up less memory
d) Sequence generation is easier than a nested iteration

12. Which of these is not true about recursion?


a) It’s easier to code some real-world problems using recursion than non-recursive equivalent
b) Recursive functions are easy to debug
c) Recursive calls take up a lot of memory
d) Programs using recursion take longer time than their non-recursive equivalent

13. What will be the output of the following Python code?


def a(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return a(n-1)+a(n-2)
for i in range(0,4):
print(a(i),end=" ")

a) 0 1 2 3
b) An exception is thrown
c) 0 1 1 2 3
d) 0 1 1 2

14. Suppose the first fibonnaci number is 0 and the second is 1. What is the sixth
fibonnaci number?
a) 5
b) 6
c) 7
d) 8

96 | P a g e
www.aktutor.in

15. Which of the following is not a fibonnaci number?


a) 8
b) 21
c) 55
d) 14

16. Which of the following option is wrong?


a) Fibonacci number can be calculated by using Dynamic programming
b) Fibonacci number can be calculated by using Recursion method
c) Fibonacci number can be calculated by using Iteration method
d) No method is defined to calculate Fibonacci number.

17. Which of the following recurrence relations can be used to find the nth fibonacci
number?
a) F(n) = F(n) + F(n – 1)
b) F(n) = F(n) + F(n + 1)
c) F(n) = F(n – 1)
d) F(n) = F(n – 1) + F(n – 2)

18. How many times will the function fibo() be called when the following code is executed?
int fibo(int n){
if(n == 1)
return 0;
else if(n == 2)
return 1;
return fibo(n - 1) + fibo(n - 2);}int main(){
int n = 5;
int ans = fibo(n);
printf("%d",ans);
return 0;}
a) 5
b) 6
c) 8
d) 9\
19. What is the output of the following code?
int fibo(int n){
if(n == 1)
return 0;
else if(n == 2)
return 1;
return fibo(n - 1) + fibo(n - 2);}int main(){
int n = 10;
int ans = fibo(n);
printf("%d",ans);
return 0;}
a) 21
b) 34
c) 55
d) 13

97 | P a g e
www.aktutor.in

20. What is the output of the following code?


int fibo(int n){
if(n == 1)
return 0;
else if(n == 2)
return 1;
return fibo(n - 1) + fibo(n - 2);}int main(){
int n = 5;
int ans = fibo(n);
printf("%d",ans);
return 0;}
a) 1
b) 2
c) 3
d) 5

21. What is the objective of tower of hanoi puzzle?


a) To move all disks to some other rod by following rules
b) To divide the disks equally among the three rods by following rules
c) To move all disks to some other rod in random order
d) To divide the disks equally among three rods in random order

22. Which of the following is NOT a rule of tower of hanoi puzzle?


a) No disk should be placed over a smaller disk
b) Disk can only be moved if it is the uppermost disk of the stack
c) No disk should be placed over a larger disk
d) Only one disk can be moved at a time

23. The time complexity of the solution tower of hanoi problem using recursion is _________
a) O(n2)
b) O(2n)
c) O(n log n)
d) O(n)

24. Recurrence equation formed for the tower of hanoi problem is given by _________
a) T(n) = 2T(n-1)+n
b) T(n) = 2T(n/2)+c
c) T(n) = 2T(n-1)+c
d) T(n) = 2T(n/2)+n

25. Minimum number of moves required to solve a tower of hanoi problem with n disks is
__________
a) 2n
b) 2n-1
c) n2
d) n2-1

26. Space complexity of recursive solution of tower of hanoi puzzle is ________


a) O(1)
b) O(n)
c) O(log n)

98 | P a g e
www.aktutor.in

d) O(n log n)

27. Recursive solution of tower of hanoi problem is an example of which of the


following algorithm?
a) Dynamic programming
b) Backtracking
c) Greedy algorithm
d) Divide and conquer

28. Tower of hanoi problem can be solved iteratively.


a) True
b) False

29. Minimum time required to solve tower of hanoi puzzle with 4 disks assuming one
move takes 2 seconds, will be __________
a) 15 seconds
b) 30 seconds
c) 16 seconds
d) 32 seconds

ANSWERS :

1.b 2.b 3.c 4.a 5.d 6.d 7.d 8.b 9.b 10.a 11.c 12.b
13.d 14.a 15.d 16.d 17.d 18.d 19.b 20.c 21.a 22.c
23.b 24.c 25.b 26.b 27.d 28.a 29.b

 MULTIPLE CHOICE QUESTIONS ANSWERS

1. Where is linear searching used?


a) When the list has only a few elements
b) When performing a single search in an unordered list
c) Used all the time
d) When the list has only a few elements and When performing a single search in an unordered list.

2. What is the best case for linear search?


a) O(nlogn)
b) O(logn)
c) O(n)

99 | P a g e
www.aktutor.in

d) O(1)

3. What is the worst case for linear search?


a) O(nlogn)
b) O(logn)
c) O(n)
d) O(1)

4. What is the best case and worst case complexity of ordered linear search?
a) O(nlogn), O(logn)
b) O(logn), O(nlogn)
c) O(n), O(1)
d) O(1), O(n)

5. Which of the following is a disadvantage of linear search?


a) Requires more space
b) Greater time complexities compared to other searching algorithms
c) Not easy to understand
d) Not easy to implement
6. Is there any difference in the speed of execution between linear serach(recursive)
vs linear search(lterative)?
a) Both execute at same speed
b) Linear search(recursive) is faster
c) Linear search(Iterative) is faster
d) Cant be said

7. Is the space consumed by the linear search(recursive) and linear search(iterative)


same?
a) No, recursive algorithm consumes more space
b) No, recursive algorithm consumes less space
c) Yes
d) Nothing can be said

8. What is the worst case runtime of linear search(recursive) algorithm?


a) O(n)
b) O(logn)
c) O(n2)
d) O(nx)

9. Linear search(recursive) algorithm used in _____________


a) When the size of the dataset is low
b) When the size of the dataset is large
c) When the dataset is unordered
d) Never used

10. The array is as follows: 1,2,3,6,8,10. At what time the element 6 is found? (By

100 | P a g e
www.aktutor.in

using linear search(recursive) algorithm)


a) 4th call
b) 3rd call
c) 6th call
d) 5th call

11. The array is as follows: 1,2,3,6,8,10. Given that the number 17 is to be searched.
At which call it tells that there’s no such element? (By using linear search(recursive)
algorithm)
a) 7th call
b) 9th call
c) 17th call
d) The function calls itself infinite number of times

12. What is the best case runtime of linear search(recursive) algorithm on an


ordered set of elements?
a) O(1)
b) O(n)
c) O(logn)
d) O(nx)

13. Can linear search recursive algorithm and binary search recursive algorithm be
performed on an unordered list?
a) Binary search can’t be used
b) Linear search can’t be used
c) Both cannot be used
d) Both can be used

14. What is the recurrence relation for the linear search recursive algorithm?
a) T(n-2)+c
b) 2T(n-1)+c
c) T(n-1)+c
d) T(n+1)+c

15. What is the advantage of recursive approach than an iterative approach?


a) Consumes less memory
b) Less code and easy to implement
c) Consumes more memory
d) More code has to be written

16. Given an input arr = {2,5,7,99,899}; key = 899; What is the level of recursion?
a) 5
b) 2
c) 3
d) 4

101 | P a g e
www.aktutor.in

17. Given an array arr = {45,77,89,90,94,99,100} and key = 99; what are the mid
values(corresponding array elements) in the first and second levels of recursion?
a) 90 and 99
b) 90 and 94
c) 89 and 99
d) 89 and 94

18. What is the worst case complexity of binary search using recursion?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)

19. What is the average case time complexity of binary search using recursion?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)

20. Which of the following is not an application of binary search?


a) To find the lower/upper bound in an ordered sequence
b) Union of intervals
c) Debugging
d) To search in unordered list.

21. Binary Search can be categorized into which of the following?


a) Brute Force technique
b) Divide and conquer
c) Greedy algorithm
d) Dynamic programming

22. Given an array arr = {5,6,77,88,99} and key = 88; How many iterations are done
until the element is found?
a) 1
b) 3
c) 4
d) 2

23. Given an array arr = {45,77,89,90,94,99,100} and key = 100; What are the mid
values(corresponding array elements) generated in the first and second iterations?
a) 90 and 99
b) 90 and 100
c) 89 and 94
d) 94 and 99

24. What is the time complexity of binary search with iteration?

102 | P a g e
www.aktutor.in

a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)

ANSWERS :

1.d 2.d 3.c 4.d 5.b 6.c 7.a 8.a 9.a 10.a 11.a 12.a 13.a 14.c
15.b 16.c 17.a 18.b 19.b 20.d 21.b 22.d 23.a 24.b

 MULTIPLE CHOICE QUESTIONS ANSWERS

1.What is an in-place sorting algorithm?


a) It needs O(1) or O(logn) memory to create auxiliary locations
b) The input is already sorted and in-place
c) It requires additional storage
d) It requires additional space

2. In the following scenarios, when will you use selection sort?


a) The input is already sorted
b) A large file has to be sorted
c) Large values need to be sorted with small keys
d) Small values need to be sorted with large keys

3. What is the worst case complexity of selection sort?


a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)

4. What is the advantage of selection sort over other sorting techniques?


a) It requires no additional storage space
b) It is scalable
c) It works best for inputs which are already sorted
d) It is faster than any other sorting technique

5. What is the average case complexity of selection sort?


a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)

6. What is the disadvantage of selection sort?


a) It requires auxiliary memory
b) It is not scalable

103 | P a g e
www.aktutor.in

c) It can be used for small keys


d) It takes linear time to sort the elements

7. The given array is arr = {3,4,5,2,1}. The number of iterations in bubble sort and
selection sort respectively are,
a) 5 and 4
b) 4 and 5
c) 2 and 4
d) 2 and 5

8. The given array is arr = {1,2,3,4,5}. (bubble sort is implemented with a flag
variable)The number of iterations in selection sort and bubble sort respectively are,
a) 5 and 4
b) 1 and 4
c) 0 and 4
d) 4 and 1

9. What is the best case complexity of selection sort?


a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)

10. Merge sort uses which of the following technique to implement sorting?
a) backtracking
b) greedy algorithm
c) divide and conquer
d) dynamic programming

11. What is the average case time complexity of merge sort?


a) O(n log n)
b) O(n2)
c) O(n2 聽 log n)
d) O(n log n2)
12. What is the auxiliary space complexity of merge sort?
a) O(1)
b) O(log n)
c) O(n)
d) O(n log n)

13. Merge sort can be implemented using O(1) auxiliary space.


a) true
b) false

14. What is the worst case time complexity of merge sort?


a) O(n log n)

104 | P a g e
www.aktutor.in

b) O(n2)
c) O(n2log n)
d) O(n log n2)

15. Which of the following method is used for sorting in merge sort?
a) merging
b) partitioning
c) selection
d) exchanging

16. What will be the best case time complexity of merge sort?
a) O(n log n)
b) O(n2)
c) O(n2 log n)
d) O(n log n2)

17. Which of the following is not a variant of merge sort?


a) in-place merge sort
b) bottom up merge sort
c) top down merge sort
d) linear merge sort

18. Choose the incorrect statement about merge sort from the following?
a) it is a comparison based sort
b) it is an adaptive algorithm
c) it is not an in place algorithm
d) it is stable algorithm

19. Which of the following is not in place sorting algorithm?


a) merge sort
b) quick sort
c) heap sort
d) insertion sort

20. Which of the following is not a stable sorting algorithm?


a) Quick sort
b) Cocktail sort
c) Bubble sort
d) Merge sort

21. Which of the following stable sorting algorithm takes the least time when applied
to an almost sorted array?
a) Quick sort
b) Insertion sort
c) Selection sort
d) Merge sort

105 | P a g e
www.aktutor.in

22. Merge sort is preferred for arrays over linked lists.


a) true
b) false

23. Which of the following sorting algorithm makes use of merge sort?
a) tim sort
b) intro sort
c) bogo sort
d) quick sort

24. Which of the following sorting algorithm does not use recursion?
a) quick sort
b) merge sort
c) heap sort
d) bottom up merge sort

ANSWERS :
1.a 2.c 3.d 4.a 5.d 6.b 7.a 8.d 9.d 10.c
11.a 12.c 13.a 14.a 15.a 16.a 17.a 18.b 19.a
20.a 21.d 22.b 23.a 24.d

106 | P a g e

You might also like