You are on page 1of 61

KJ's Educational Institute

TRINITY ACADEMY OF ENGINEERING, PUNE


(Approved by AICTE, New Delhi, Govt. of Maharashtra & affiliated to SPPU, DTE Code: EN6634)
(Accredited by NAAC with ‘A’ Grade)

Multiple Choice Questions

Department: Computer Engineering Year: F.E. (Semester I)

Subject: Programming & Problem Solving Unit: II

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

[a] TRUE
[b] FALSE

Correct Option:[a]

[2] Checking multiple conditions in Python requires elif statements.

[a] TRUE
[b] FALSE

Correct Option:[a]
[3] If the condition is evaluated to true, the statement(s) of if block will be executed

Otherwise the statements(s) in else block(if else is specified) will be executed

[a] TRUE
[b] FALSE

Correct Option:[a]

[4] What will be the output of given Python code?

n=7
c=0
while(n):
if(n>5):

c=c+n-1

n=n-1

else:

Break
print(n)
print(c)
[a] 2
[b] 652
[c] 3
[d] 52

Correct option:[a]

[5] 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

Correct option: [d]

3
[6] Which of the following Python code will give different output from the others?

[a] for i in range(0,5):

print(i)

[b] for j in [0,1,2,3,4]:

print(j)

[c] for k in [0,1,2,3,4,5]:

print(k)

[d] for l in range(0,5,1):

print(l)

Correct option:[c]

[7] What will be the output of the following Python code?

str1=”learn python”

str2=””
str3=””

for x in str1:
if(x==”r” or x==”n” or x==”p”):

str2+=x

pass

if(x==”r” or x==”e” or x==”a”):

str3+=x

print(str2,end=” “)

print(str3)

[a] rnpn ea
[b] rnpn ear
[c] rnp ea
[d] rnp ear

Correct Option:[b]

[8] What will be the output of the following Python code?

for i in range(0,2,-1):

print(“Hello”)

[a] Hello

5
[b] Hello Hello

[c] No Output

[d] Error

Correct option:[c]

[9] Which of the following is a valid for loop in Python?

[a] for(i=0; i < n; i++)

[b] for i in range(0,5):

[c] for i in range(0,5)

[d] for i in range(5)

Correct Option:[b]

[10] Which of the following sequences would be generated bt the given line of code?

range (5, 0, -2)

[a] 5 4 3 2 1 0 -1

[b] 543210
[c] 531

[d] None of the above

Correct answer:[c]

[11] A while loop in Python is used for what type of iteration?

[a] indefinite

[b] discriminant

[c] definite

[d] indeterminate

Correct answer:[a]

[12] When does the else statement written after loop executes?

[a] When break statement is executed in the loop

7
[b] When loop condition becomes false

[c] Else statement is always executed

[d] None of the above

Correct Option:[b]

[13] What will be the output of the following code?

x = “abcdef”

i = “i”

while i in x:

print(i, end=” “)

[a] abcdef

[b] abcdef
[c] i i i i i…..

[d] No Output

correct answer:[d]
[14] What will be the output of the following code?

x = “abcd”

for i in range(len(x)):

print(i)
[a] abcd
[b] 0123
[c] 1234
[d] abcd

Correct Option:[b]

[15] What will be the output of the following code?

x = 12
for i in x:
print(i)
[a] 12
[b] 12
[c] Error

[d] None of the above

Correct answer:[c]

9
[16] 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] error

[d] none of the mentioned

Correct Option:[b]

[17] What will be the output of given Python code?

i=0

while i < 3:

print(i)
i += 1
else:

print(0)
[a] 01230
[b] 0120
[c] 012
[d] error

Correct Option:[b]

[18] What will be the output of given Python code?

x = “abcdef”

while i in x:

print(i, end=” “)

[a] abcdef

[b] abcdef
[c] iiiiii…
[d] error

Correct option:[d]

[19] What will be the output of given Python code?

x = “abcdef”

i = “i”

while i in x:

print(i, end=” “)

11
[a] no output

[b] iiiiii…

[c] abcdef

[d] abcdef

Correct option:[a]

[20] What will be the output of given Python code?

x = “abcdef”

i = “a”

while i in x[:-1]:

print(i, end = ” “)

[a] aaaaa

[b] aaaaaa

[c] aaaaaa…

[d] Correct answer:[c]

[21] What will be the output of given Python code?

x = “abcdef”

i = “a”

while i in x:

x = x[1:]
print(i, end = ” “)

[a] aaaaaa

[b] a

[c] no output

[d] error

Correct Option:[b]

[22] What will be the output of given Python code?

x = “abcdef”

i = “a”

while i in x[1:]:

print(i, end = ” “)

[a] aaaaaa

[b] a

[c] no output

[d] error

Correct option:[c]

[23] What will be the output of given Python code?

x = 123
for i in x:

13
print(i)
[a] 123
[b] 123
[c] error

[d] none of the mentioned

Correct option:[c]

[24] What will be the output of given Python code?

d = {0: ‘a’, 1: ‘b’, 2: ‘c’}

for i in d:
print(i)
[a] 012
[b] abc

[c] 0a 1b 2c

[d] none of the mentioned

Correct option:[a]

[25] What will be the output of given Python code?

d = {0: ‘a’, 1: ‘b’, 2: ‘c’}


for x, y in d:

print(x, y)

[a] 012
[b] abc
[c] 0a 1b 2c

[d] none of the mentioned

Correct option:[d]

[26] What will be the output of given Python code?

d = {0: ‘a’, 1: ‘b’, 2: ‘c’}

for x, y in d.items():

print(x, y)

[a] 012
[b] abc

[c] 0a 1b 2c

[d] none of the mentioned

Correct anser:[C]

15
[27] What will be the output of given Python code?

d = {0: ‘a’, 1: ‘b’, 2: ‘c’}

for x in d.keys():

print(d[x])

[a] 012
[b] abc

[c] 0a 1b 2c

[d] none of the mentioned

Correct Option:[b]

[28] What will be the output of given Python code?

d = {0: ‘a’, 1: ‘b’, 2: ‘c’}

for x in d.values():

print(x)

[a] 012
[b] abc

[c] 0a 1b 2c
[d] none of the mentioned

Correct Option:[b]

[29] What will be the output of given Python code?

d = {0: ‘a’, 1: ‘b’, 2: ‘c’}

for x in d.values():

print(d[x])

[a] 012
[b] abc

[c] 0a 1b 2c

[d] none of the mentioned

Correct option:[d]

[30] What will be the output of given Python code?

d = {0, 1, 2}

17
for x in d.values():

print(x)

[a] 012

[b] None None None

[c] error

[d] none of the mentioned

Correct option:[c]

[31] d = {0, 1, 2}

for x in d:

print(x)

[a] 012

[b] {0, 1, 2} {0, 1, 2} {0, 1, 2}

[c] error

[d] none of the mentioned

Correct option:[a]

[32] What will be the output of given Python code?


d = {0, 1, 2}

for x in d:

print(d.add(x))

[a] 012

[b] 012012012…

[c] None None None

[d] None of the mentioned

Correct option:[c]

[33] What will be the output of given Python code?

for i in range(0):

print(i)
[a] 0

[b] no output

[c] error

[d] none of the mentioned

Correct Option:[b]

19
34 Which of the followings is not a nested loop?

[a] for i in range(0,9):

for j in range(1,i+2)

[b] for i in range(0,10):

print (i)

for j in range(1,i+2):

print (j)

[c] for i in range(0,10):

while j<=i+1:

print (j)

j=j+1

Correct option:b
35 Which of the following statements about the while loop in Python is not true?

a The while loop is a posttest loop.

b Testing condition is made before each iteration.

c The while loop exits if the condition to evaluate is not true.

Correct option:a

36 The purpose of using a loop is to

21
a repeat operation(s) many times

b make decisions

correct option:a

37 The continue command is used to exit a loop.

a. True
b. False

38 What would be printed from the following Python code segment?

for i in range(20,0,-2):

print (i)

a) 20

18
16
14
12
10
8
6
4
2
39 What would be the output from the following Python code segment?

x=0

while (x < 100):

x+=2
print(x)
a.100
b.99
c.101

d.None of the above,infinte loop

ans:a

40 What is the output of the following nested loop

numbers = [10, 20]

items = [“Chair”, “Table”]

for x in number:

for y in items:

print(x,y):

a 10 chair
23
10table
20 chair
20 table

Answer:a

41 Select which is true for for loop(multiple correct answers)

a) Python’s for loop used to iterates over the items of list, tuple, dictionary, set, or
string

b) else clause of for loop is executed when the loop terminates naturally

c) else clause of for loop is executed when the loop terminates abruptly
d) We use for loop when we want to perform a task indefinitely until a particular
condition is met

ans:a,b,d

42 What is the output of the following if statement

a, b = 12, 5

if a + b:

print(‘True’)

else:

print(‘False’)

a) True
b) False
ans: a

43 One IF statement can have multiple ___________

a) IF
b) ELSIF

25
c) ELSE
d) CASE
ans:b

If the condition of IF statement is an expression, then what should be the type of


44
the result of the expression?

a) Bit

b) Std_logic

c) Boolean

d) Integer

Ans:c

45 In a Python program, a control structure:

a)Directs the order of execution of the statements in the program


b)Dictates what happens before the program starts and after it terminates

c)Defines program-specific data structures

d)Manages the input and output of control characters

ans:a

46 what is output?

if False:

print(“Nissan”)

elif True:

print(“Ford”)

elif True:

print(“BMW”)

27
else:

print(“Audi”)

a) Nissan
b) Ford
C) BMW
d) Audi
Ans:b

47 What signifies the end of a statement block or suite in Python?

a)A comment

b) }

c) A line that is indented less than the previous line

d) end
ans: c

48 What is the output of C Program.?

int main()

{
int a=5;
while(a==5)

printf(“RABBIT”);

break;

return 0;

A)RABBIT is printed unlimited number of times

B) RABBIT

C) Compiler error

D) None of the above.

ans:B

49 Which of the following would give an error?

A. list1=[]

B. list1=[]*3

29
C. list1=[2,8,7]

D. None of the above

Ans : D

Explanation: None of the above will result in error.

50 Which of the following will give output as [23,2,9,75] ?

If list1=[6,23,3,2,0,9,8,75]

A. print(list1[1:7:2])

B. print(list1[0:7:2])

C. print(list1[1:8:2])

D. print(list1[0:8:2])

Ans : C
The elements of a list are arranged in descending order.Which of the

51

following two will give same outputs?

i. print(list_name.sort())

ii. print(max(list_name))

iii. print(list_name.reverse())

iv. print(list_name[-1])

A. i, ii B. i, iii C. ii, iii D. iii, iv

ans:b

52 Which of the following creates a tuple?

31
A. tuple1=(“a”,”b”)

B. tuple1[2]=(“a”,”b”)

C. tuple1=(5)*2

D. None of the above

Ans : A

53 2. Choose the correct option with respect to Python.

A. Both tuples and lists are immutable.

B. Tuples are immutable while lists are mutable.

C. Both tuples and lists are mutable.


D. Tuples are mutable while lists are immutable.

Ans:B

54 What will be the output of below Python code?

tuple1=(5,1,7,6,2)

tuple1.pop(2)

print(tuple1)

A. (5,1,6,2)

B. (5,1,7,6)

C. (5,1,7,6,2)

D. Error
Ans : D

Which of the following options will not result in an error when performed on
55
tuples in Python

33
where tupl=(5,2,7,0,3)?

A. tupl[1]=2

B. tupl.append(2)

C. tupl1=tupl+tupl

D. tupl.sort()

Ans:C

56 What will be the output of below Python code?

tupl=([2,3],”abc”,0,9)

tupl[0][1]=1

print(tupl)

A. ([2,3],”abc”,0,9)

B. ([1,3],”abc”,0,9)

C. ([2,1],”abc”,0,9)

D. Error
Ans:c
57 . 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

Ans:d

Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john” what command
58
do we use?

a) d.delete(“john”:40)

35
b) d.delete(“john”)

c) del d[“john”]

d) del d(“john”:40)

Answer: c

59 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

Answer: b
60 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

Answer: b

61 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

37
d) When duplicate keys encountered, the last assignment wins

Answer: c

62 If expression.

The expression can be of which type?

a) True

b) Any number

c) Any string

d) All of the mentioned

Answer: d

63 What is the output of the given code?

if 79>78:

print( “True”.upcase)

if 9>8:

print( “True”.reverse)
v if 7==7:

print( “equal”.downcase)

a) True

b) True eurt

c) TRUE
eurT
equal
d) equal

Answer: c

64 If statement inside if statement is called Nested if statements.

a) True
b) False

Answer: a

65 What is the output of the given code?

x=1
if x > 2

39
print(“x is greater than 2”)

elsif x <= 2 and x!=0

print( “x is 1”)

else

print( “I can’t guess the number”)

a) x is greater than 2

b) x is 1

c) I can’t guess the number

d) None of the mentioned

Answer: b

66 It’s a good habit to give two spaces between if statement and condition.

a) True
b) False

Answer: a

67 What is the use of else statement?

a) When the if condition is false then the next else condition will get executed

b) When the if condition is false then the elsif condition will get executed

c) When the if condition is false and if else condition is true then only it will get
executed

41
d) None of the mentioned

Answer: c

68 Is the following syntax correct?

if conditional:

code…

elsif conditional:

code..
else:
code
a) True
b) False

Answer: a

69 What is the output of the given code?

if 1>2:

print( “false”)

else:

print( “True”)

a) False
b) True

c) Syntax error

d) None of the mentioned


Answer: b

70 Which of the following is valid conditional statement?

a) else
b) els
c) if else

d) None of the mentioned

Answer: a

71 It is necessary that always if should come with else block?

a) True
b) False

Answer: b

72 What is the output of the code?

variable=true

if !variable:

print( “true”)

else:

43
print( “false”)

a) False
b) True

c) Syntax error

d) None of the mentioned

Answer: a

73 Which of the following is valid conditional statement in Python?

a) elseif
b) elif
c) else if
d) elseiff

Answer: b

74 The elif conditional statement is written with an expression.

a) True
b) False

Answer: a
75 The elif statement can add many alternatives to if/else statements.

a) True
b) False

Answer: a

76 What is the output of the given code?

x=7
y=9
if x==y:

print “equal”

elif x>y:

print “greater”

else:

print “less”

a) equal

b) greater

c) less

d) none of the mentioned

Answer: c

45
77 What is the output of the given code?

a=true
b=false
if a && b:

print( “False”)

elsif a || b:

print( “True”)

else:

print( “neither true nor false”)

a) false
b) true

c) neither true nor false

d) none of the mentioned

Answer: b

78 What is the output of the given code?

a=true
b=false
if a && b:

print( “False”)
elif a || b:

print( “True”)

elif a || b and a && b:

print(“True and False Both”)

else:

print( “neither true nor false”)

a) false
b) true

c) neither true nor false

d) none of the mentioned

Answer: b

79 Which of I, II, and III below gives the same result as the following nested if?

x = -10
if x < 0:

47
print(“The negative number “, x, ” is not valid here.”)

else:
if x > 0:

print(x, ” is a positive number”)

else:

print(x, ” is 0″)

I
if x < 0:

print(“The negative number “, x, ” is not valid here.”)

else x > 0:

print(x, ” is a positive number”)

else:

print(x, ” is 0″)

II.

if x < 0:
print(“The negative number “, x, ” is not valid here.”)

elif x > 0:

print(x, ” is a positive number”)

else:

print(x, ” is 0″)

III.

if x < 0:

print(“The negative number “, x, ” is not valid here.”)

if x > 0:

print(x, ” is a positive number”)

else:

print(x, ” is 0″)

a)I only
b) II only

c)I and II onlyy

49
d) All of the above

Answer:b

80 Which of the following will evaluate to true?

I. True AND False

II. False or True

III. False AND (True or False)

a)I only
b) II only
a)I only
b) II only

c)I and II onlyy

d) All of the above

Answer:b

81 What will be the output of below Python code?

tuple1=(5,1,7,6,2)

tuple1.pop(2)
print(tuple1)

A.(5,1,6,2)

B. (5,1,7,6)

C. (5,1,7,6,2)

D. Error
Ans : D

What will be the output of below Python code?

82
tuple1=(2,4,3)

tuple3=tuple1*2

print(tuple3)

A.(4,8,6)

B. (2,4,3,2,4,3)

C. (2,2,4,4,3,3)

D. Error
Ans : B

83 What will be the output of below Python code?

51
tupl=(“annie”,”hena”,”sid”)

print(tupl[-3:0])

A. (“annie”)

B. ()
C. None

D. Error as slicing is not possible in tuple

Ans : B

84 Which of the following is a Python tuple?

a) [1, 2, 3]

b) (1, 2, 3)

c) {1, 2, 3}

d) {}

Answer: b

85 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))

Answer: b

86 What will be the output of the following Python code?

1. >>>t=(1,2,4,3)

2. >>>t[1:3]

a) (1, 2)

b) (1, 2, 4)

c) (2, 4)

d) (2, 4, 3)

Answer: c

87 What will be the output of the following Python code?

53
1. >>>t = (1, 2, 4, 3, 8, 9)

2. >>>[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)

Answer: c

88 What will be the output of the following Python code?

1. >>>t = (1, 2)

2. >>>2 * t

a) (1, 2, 1, 2)

b) [1, 2, 1, 2]

c) (1, 1, 2, 2)

d) [1, 1, 2, 2]

Answer: a
89 What will be the output of the following Python code?

1. >>>t1 = (1, 2, 4, 3)

2. >>>t2 = (1, 2, 3, 4)

3. >>>t1 < t2

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

Answer: b

90 What will be the output of the following Python code?

1. numberGames = {}

2. numberGames[(1,2,4)] = 8

55
3. numberGames[(4,2,1)] = 10

4. numberGames[(1,2)] = 12

5. sum = 0

6. for k in numberGames:

7. sum += numberGames[k]

8. print len(numberGames) + sum

a) 30
b) 24
c) 33
d) 12

Answer: c

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

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

Answer: c
92 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]

Answer: a

Suppose list1 is [4, 2, 2, 4, 5, 2, 1, 0], Which of the following is correct syntax for
93
slicing operation?

a) print(list1[0])

b) print(list1[:2])

c) print(list1[:-2])

57
d) all of the mentioned

Answer:d

94 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)

Answer:c

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

a) 1
b) 9
c) 15
d) Error
Answer:c
96 suppose list1 is[1,5,9] what is max(list1)?

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

97 suppose list1 is[1,5,9] what is min(list1)?

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

Answer:a

98 suppose list1 =[12,13,14,15]

print(list1*2)

a)[12,13,14,15]

b)[15,14,13,12]

c)[12,13,14,15,12,13,14,15]

d) all of the above

Answer:c

59
99 What is True about datatype list?

A) Items in list are mutable

B)Items ain list are immutable

C)Items are present is form of key-values pair

D)List is not Datatype

Answer:A

100 suppose tuple1 =(12,13,14,15)

print(tuple1*2)

a)(12,13,14,15)

b)(15,14,13,12)

c)[(2,13,14,15,12,13,14,15)

d) all of the above

Answer:c
61

You might also like