You are on page 1of 22

PYTHON MCQ WITH ANSWERS

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

a) 31 characters

b) 63 characters

c) 79 characters

d) none of the mentioned

View Answer

Answer: d

Explanation: Identifiers can be of any length.

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

a) X^y

b) X**y

c) X^^y

d) None of the mentioned

View Answer

Answer: b

Explanation: In python, power operator is x**y i.e. 2**3=8.

3.What error occurs when you execute the following Python code snippet?

apple = mango

a) SyntaxError

b) NameError

c) ValueError
d) TypeError

View Answer

Answer: b

Explanation: Mango is not defined hence name error.

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

a) True

b) False

c) Machine dependent

d) Error

View Answer

Answer: b

Explanation: Neither of 0.1, 0.2 and 0.3 can be represented accurately in binary. The round off errors
from 0.1 and 0.2 accumulate and hence there is a difference of 5.5511e-17 between (0.1 + 0.2) and 0.3.

5.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

View Answer

Answer: b
Explanation: The expression shown above is an example of explicit conversion. It is evaluated as
int(43.55+1) = int(44.55) = 44. Hence the result of this expression is 44.

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

a) True

b) False

View Answer

Answer: b

Explanation: The value of the expression (2**2)**3 = 4**3 = 64. When the expression 2**2**3 is
evaluated in python, we get the result as 256, because this expression is evaluated as 2**(2**3). This is
because the associativity of exponentiation operator (**) is from right to left and not from left to right.

7.What will be the output of the following Python code snippet if x=1?

x<<2

a) 8

b) 1

c) 2

d) 4

View Answer

Answer: d

Explanation: The binary form of 1 is 0001. The expression x<<2 implies we are performing bitwise left
shift on x. This shift yields the value: 0100, which is the binary form of the number 4.

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

4^12

a) 2
b) 4

c) 8

d) 12

View Answer

Answer: c

Explanation: ^ is the XOR operator. The binary form of 4 is 0100 and that of 12 is 1100. Therefore,
0100^1100 is 1000, which is equal to 8.

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

if (9 < 0) and (0 < -9):

print("hello")

elif (9 > 0) or False:

print("good")

else:

print("bad")

a) error

b) hello

c) good

d) bad

View Answer

Answer: c

Explanation: The code shown above prints the appropriate option depending on the conditions given.
The condition which matches is (9>0), and hence the output is: good.

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


x = 'abcd'

for i in x:

print(i.upper())

a) a b c d

b) A B C D

c) a B C D

d) error

View Answer

Answer: b

Explanation: The instance of the string returned by upper() is being printed.

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

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

for x, y in d.items():

print(x, y)

a) 0 1 2

b) a b c

c) 0 a 1 b 2 c

d) none of the mentioned

View Answer

Answer: c

Explanation: Loops over key, value pairs.


12.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

View Answer

Answer: b

Explanation: range(int(2.0)) is the same as range(2).

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

string = "my name is x"

for i in string:

print (i, end=", ")

a) m, y, , n, a, m, e, , i, s, , x,

b) m, y, , n, a, m, e, , i, s, , x

c) my, name, is, x,

d) error

View Answer

Answer: a

Explanation: Variable i takes the value of one character at a time.


14.What arithmetic operators cannot be used with strings?

a) +

b) *

c) –

d) All of the mentioned

View Answer

Answer: c

Explanation: + is used to concatenate and * is used to multiply strings.

15.Given a string example=”hello” what is the output of example.count(‘l’)?

a) 2

b) 1

c) None

d) 0

View Answer

Answer: a

Explanation: l occurs twice in hello.

16.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


View Answer

Answer: a

Explanation: Counts the number of times the substring ‘yy’ is present in the given string.

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

print("ccdcddcd".find("c"))

a) 4

b) 0

c) Error

d) True

View Answer

Answer: b

Explanation: The first position in the given string at which the substring can be found is returned.

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

a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]

b) [‘hello’]

c) [‘llo’]

d) [‘olleh’]

View Answer

Answer: a

Explanation: Execute in the shell to verify.

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


a) 5

b) 4

c) None

d) Error

View Answer

Answer: a

Explanation: Execute in the shell and verify.

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

a) 2445

b) 133

c) 12454

d) 123

View Answer

Answer: c

Explanation: Max returns the maximum element in the list.

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

a) 1

b) 9

c) 15

d) Error

View Answer

Answer: c
Explanation: Sum returns the sum of all elements in the list.

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

a) Error

b) None

c) 25

d) 2

View Answer

Answer: c

Explanation: -1 corresponds to the last index in the list.

23.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]

View Answer

Answer: c

Explanation: Execute in the shell and verify.

24. 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)

View Answer
Answer: b

Explanation: We use the function append to add an element to the list.

25.To 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

View Answer

Answer: d

Explanation: In can be used in all data structures.

26. 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

c) 5

d) 8

View Answer

Answer: d
Explanation: + appends all the elements individually into a new list.

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

points = [[1, 2], [3, 1.5], [0.5, 0.5]]

points.sort()

print(points)

a) [[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]]

View Answer

Answer: c

Explanation: Execute in the shell to verify.

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

>>> a=[14,52,7]

>>>> b=a.copy()

>>> b is a

a) True

b) False

View Answer

Answer: b

Explanation: List b is just a copy of the original list. Any copy made in list b will not be reflected in list a.
29.Write the list comprehension to pick out only negative integers from a given list ‘l’.

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]

View Answer

Answer: d

Explanation: To pick out only the negative numbers from a given list ‘l’, the correct list comprehension
statement would be: [x for x in l if x<0].

For example if we have a list l=[-65, 2, 7, -99, -4, 3]

>>> [x for x in l if x<0]

The output would be: [-65, -99, -4].

30.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]

View Answer

Answer: a

Explanation: The list comprehension to print a list of cube of the numbers for the given list is: [x**3 for x
in l].

31. Which of the following is a Python tuple?

a) [1, 2, 3]
b) (1, 2, 3)

c) {1, 2, 3}

d) {}

View Answer

Answer: b

Explanation: Tuples are represented with round brackets.

32. 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))

View Answer

Answer: b

Explanation: Values cannot be modified in the case of tuple, that is, tuple is immutable.

33.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)

View Answer
Answer: c

Explanation: Slicing in tuples takes place just as it does in strings.

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

d = {"john":40, "peter":45}

d["john"]

a) 40

b) 45

c) “john”

d) “peter”

View Answer

Answer: a

Explanation: Execute in the shell to verify.

35.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

View Answer
Answer: b

Explanation: Elements are compared one by one in this case.

36. 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

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

View Answer

Answer: a

Explanation: Since the first element in the sub-tuple of a is larger that the first element in the subtuple
of b, False is printed.

37.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

View Answer
Answer: c

Explanation: In the above case, 3 is the starting value to which the sum of the tuple is added to.

38.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

View Answer

Answer: c

Explanation: The command del a deletes the entire tuple.

39.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

View Answer

Answer: b

Explanation: The variable a has tuples enclosed in a list making it a list of tuples.

40. 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)

View Answer

Answer: c

Explanation: 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.

41.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

View Answer

Answer: b

Explanation: In the above piece of code, the values of the tuples aren’t being changed. Both the tuples
are simply concatenated.
42. 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

View Answer

Answer: c

Explanation: The above piece of code illustrates the unpacking of variables.

43.Is Python case sensitive when dealing with identifiers?

a) yes

b) no

c) machine dependent

d) none of the mentioned

View Answer

Answer: a

Explanation: Case is always significant.

44. Which one of these is floor division?

a) /

b) //
c) %

d) None of the mentioned

View Answer

Answer: b

Explanation: When both of the operands are integer then python chops out the fraction part and gives
you the round off value, to get the accurate answer use floor division. This is floor division. For ex, 5/2 =
2.5 but both of the operands are integer so answer of this expression in python is 2. To get the 2.5
answer, use floor division.

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

a) 7

b) 1

c) 0

d) 5

View Answer

Answer: b

Explanation: Modulus operator gives the remainder. So, 22%3 gives the remainder, that is, 1.

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

a) 27

b) 9

c) 3

d) 1

View Answer

Answer: c
Explanation: First this expression will solve 1**3 because exponential has higher precedence than
multiplication, so 1**3 = 1 and 3*1 = 3. Final answer is 3.

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

>>>str="hello"

>>>str[:2]

>>>

a) he

b) lo

c) olleh

d) hello

View Answer

Answer: a

Explanation: We are printing only the 1st two bytes of string and hence the answer is “he”.

48.What data type is the object below?

L = [1, 23, 'hello', 1]

a) list

b) dictionary

c) array

d) tuple

View Answer

Answer: a

Explanation: List data type can store any values within it.
49.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

View Answer

Answer: d

Explanation: Dictionary stores values in terms of keys and values.

50.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

View Answer

Answer: a

Explanation: Cause a decimal value of 0 to appear as output.

You might also like