Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Python MCQ

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

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

Identifiers can be of any length.

 31 characters

 63 characters

 79 characters

 Identifiers can be of any length.

Correct Answer:
Identifiers can be of any length.

2. Given a function that does not return any value, What value is thrown by default when executed in shell.

Python shell throws a NoneType object back.

 Int

 bool

 void

 none

Correct Answer:
none

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

// is integer operation in python 3.0 and int(..) is a type cast operator.

 x = 13 // 2

 x = int(13 / 2)

 x = 13 % 2

 All of the mentioned


Correct Answer:
All of the mentioned

4. What is the value of the following expression?


8/4/2, 8/(4/2) The above expressions are evaluated as: 2/2, 8/2, which is equal to (1.0, 4.0).

 (1.0, 4.0)

 (1.0, 1.0)

 (4.0. 1.0)

 (4.0, 4.0)

Correct Answer:
(1.0, 4.0)

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

2**(3**2) (2**3)**2 2**3**3

Explanation: Expression 1 is evaluated as: 2**9, which is equal to 512. Expression 2 is evaluated as 8**2, which is
equal to 64. The last expression is evaluated as 2**(3**2). This is because the associativity of ** operator is from
right to left. Hence the result of the third expression is 512.

 64, 512, 64

 64, 64, 64

 512, 512, 512

 512, 64, 512

Correct Answer:
512, 64, 512

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

example = "snow world"

print("%s" % example[4:7])

Execute in the shell and verify.


 wo

 world

 sn

 rl

Correct Answer:
wo

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

max("what are you")

Max returns the character with the highest ascii value.

 error

 u

 t

 y

Correct Answer:
y
8. Given a string example=”hello” what is the output of example.count(‘l’)?
 l occurs twice in hello.

 2

 1

 None

 0

Correct Answer:
2
9. What will be the output of the following Python code?

example = "helle"
example.rfind("e")

Returns highest index.

 -1

 4

 1

Correct Answer:
4

10. 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')

The tuple ‘v’ is used to generate a list containing only vowels in the string ‘w’. The result is a list containing only
vowels present in the string “hello”. Hence the required list comprehension is: [x for x in w if x in v].

 [x for w in v if x in v]

 [x for x in w if x in v]

 [x for x in v if w in v]

 [x for v in w for x in w]

Correct Answer:
[x for x in w if x in v]

11. 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)]

The list comprehension shown above returns a list of non-prime numbers up to 50. The logic behind this is that the
square root of 50 is almost equal to 7. Hence all the multiples of 2-7 are not prime in this range.
 A list of prime numbers up to 50

 A list of numbers divisible by 2, up to 50

 A list of non prime numbers, up to 50

 Error

Correct Answer:
A list of non prime numbers, up to 50

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

The built-in function pow() can accept two or three arguments. When it takes in two arguments, they are evaluated
as x**y. When it takes in three arguments, they are evaluated as (x**y)%z.

 (x**y)**z

 (x**y) / z

 (x**y) % z

 (x**y)*z

Correct Answer:
(x**y) % z

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

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

The function max() is being used to find the maximum value from among -3, -4 and false. Since false amounts to the
value zero, hence we are left with min(0, 2, 7) Hence the output is 0 (false).

 2

 False

 -4

Correct Answer:
False

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

Tuples are immutable and don’t have an append method. An exception is thrown in this case.

 1

 2

 Error

Correct Answer:
Error

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

l=[2, 3, [4, 5]] l2=l.copy() l2[0]=88 l l2 The code shown above depicts deep copy. In deep copy, the base address of
the objects is not copied. Hence the modification done on one list does not affect the other list.

 [88, 2, 3, [4, 5]] [88, 2, 3, [4, 5]]

 [2, 3, [4, 5]] [88, 2, 3, [4, 5]

 [88, 2, 3, [4, 5]] [2, 3, [4, 5]]

 [2, 3, [4, 5]] [2, 3, [4, 5]]

Correct Answer:
[2, 3, [4, 5]] [88, 2, 3, [4, 5]

16. What will be the output of the following Python code and state the type of copy that is depicted?

l1=[2, 4, 6, 8] l2=[1, 2, 3] l1=l2 l2 The code shown above depicts shallow copy and the output of the code is: [1, 2,
3].

 [2, 4, 6, 8], shallow copy

 [2, 4, 6, 8], deep copy


 [1, 2, 3], shallow copy

 [1, 2, 3], deep copy

Correct Answer:
[1, 2, 3], shallow copy

17. What will be the output?

ef fact(num): if num == 0: return 1 else: return _____________________ Suppose n=5 then, 5*4*3*2*1 is returned
which is the factorial of 5.

 num*fact(num-1)

 (num-1)*(num-2)

 num*(num-1)

 fact(num)*fact(num-1)

Correct Answer:
num*fact(num-1)

18. What will be the output

def f1(x): global x x+=1 print(x) f1(15) print("hello") The code shown above will result in an error because ‘x’ is a
global variable. Had it been a local variable, the output would be: 16

 error

 hello

 16 hello

Correct Answer:
error

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

def f1(a,b=[]):
b.append(a)
return b

print(f1(2,[3,4]))
In the code shown above, the integer 2 is appended to the list [3,4]. Hence the output of the code is [3,4,2]. Both the
variables a and b are local variables.

 [3,2,4]

 [2,3,4]

 Error

 [3,4,2]

View Answer
Correct Answer:
[3,4,2]

20. Program code making use of a given module is called a ______ of the module.

Program code making use of a given module is called the client of the module. There may be multiple clients for a
module.

 Client

 Docstring

 Interface

 Modularity

Correct Answer:
Client

21. Which function overloads the + operator?

Refer documentation.

 __add__()

 __plus__()

 __sum__()
 none of the mentioned

Correct Answer:
__add__()

22. Which operator is overloaded by __invert__()?

__invert__() overloads ~.

 !

 ~

 ^

 –

Correct Answer:
~

23. Let A and B be objects of class Foo. Which functions are called when print(A + B) is executed?

The function __add__() is called first since it is within the bracket. The function __str__() is then called on the
object that we received after adding A and B.

 __add__(), __str__()

 __str__(), __add__()

 __sum__(), __str__()

 __str__(), __sum__()

Correct Answer:
__add__(), __str__()

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

class A:
def __init__(self): self.__x = 1
class B(A):
def display(self):
print(self.__x)
def main():
obj = B()
obj.display()
main()

Private class members in the superclass can’t be accessed in the subclass.

 Error, invalid syntax for object declaration

 Error, private class member can’t be accessed in a subclass

Correct Answer:
Error, private class member can’t be accessed in a subclass

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

t[5]

The expression shown above results in a name error. This is because the name ‘t’ is not defined.

 IndexError

 NameError

 TypeError

 syntaticalError

Correct Answer:
NameError

26. Which of the following is not a keyword in Python

 assert

 eval

 nonlocal

 pass
Correct Answer:
eval

27. What is the output?

print('{:,}'.format(1112223334))

 1122343444

 1222333444

 1112223334

 1332224444

Correct Answer:
1112223334

28. Select the reserved Keywords

 return

 assert

 eval

 class

Correct Answer:
return
assert
class

29. Predict the output

3*1**3

 27

 9

 81
 3

Correct Answer:
3

30. Which of the folowing is a coreect statement with respect to round()

 round(45.8)

 round(6352.898,2,5)

 round()

 round(62.898,6,5)

Correct Answer:
round(45.8)

31. Which of these is not a core data type in python

 class

 list

 tuple

 set

Correct Answer:
class

32. What is the output?

print('{:#}'.format(1112223334))

 1,22,4,222,4

 1112223334
 1,4,2,3

 1234

Correct Answer:
1112223334

33. All the keywords in python is written in

 Upper class

 Lower class

 Captialized

 None of the above

Correct Answer:
None of the above

34. What is the output?

 11

 -11

 5

 -5

Correct Answer:
5

35. Point out the invalid variable

 my_Tutoriallinks
 1Tutorial_links

 _

 _a_

Correct Answer:
1Tutorial_links

36. Is tuple mutuable

 No

 yes

Correct Answer:
No

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

 5

 1

 3

 25

Correct Answer:
1

38. Which command is used to add an element in the list

 list.sum(5)

 list1.add(5)

 list1.append(5)
 list.addelement(5)

Correct Answer:
list1.append(5)

39. Output of 7^10 is

 12

 7

 15

 13

Correct Answer:
13

40. What is the output?

Y=[2,5J,6] Y.sort()

 [2,6,5J]

 [5J,2,6]

 Error

 [6,5J,2]

Correct Answer:
Error

41. What is the output?

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

  [‘h’, ‘e’, ‘l’, ‘l’, ‘o’].

 hello
 hel

 HELLO

Correct Answer:
 [‘h’, ‘e’, ‘l’, ‘l’, ‘o’].

42. In which of the following we can use "in" operator to check the elements

 dictionary

 list

 tuple

 All of the above

Correct Answer:
list

43. Assume the output

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

 6

 36

 8

 4

Correct Answer:
8

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

 [2, 6, 4].
 [1, 3, 2, 3, 2, 1]

 [1, 3, 2, 1, 3]

 [1, 3, 2, 1, 3, 2] .

Correct Answer:
[1, 3, 2, 1, 3, 2] .

45. Which of the following statement is correct

 List is mutable & Tuple is immutable

 List is immutable

 Tuple is mutable

 None of the above

Correct Answer:
List is mutable & Tuple is immutable

46. Assume the output

values = [[3, 4, 5, 1], [33, 6, 1, 2]] v = values[0][0] for lst in values: for element in lst: if v > element: v = element
print(v)

 3

 1

 2

 33

Correct Answer:
1

47. Output is

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


 [0,1,2]

 [1,2,5]

 Error ';"

 None of the above

Correct Answer:
[0,1,2]

48. Predict the output

List = [True, 50, 10] List.insert(2, 5) print(List, "Sum is: ", sum(List))

 [True, 50, 10, 5] Sum is: 66

 [True, 50, 5, 10] Sum is: 65

 [True, 50, 5, 10] Sum is: 66

 TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’

Correct Answer:
[True, 50, 5, 10] Sum is: 66

49. Output is

l = [1,2,6,5,7,8] l.insert(9)

 l=[9,1,2,6,5,7,8]

 Error

 l=[1,2,6,5,9.7,8] (insert randomly at any position)

 l=[1,2,6,5,7,8,9]

Correct Answer:
Error
50. Assume the output

a=list((45,)*4) print((45)*4) print(a)

 180[(45),(45),(45),(45)].

 (45,45,45,45).[45,45,45,45].

 180[45,45,45,45].

 Syntax error

Correct Answer:
180[45,45,45,45].

51. Guess the output

a=[14,52,7]

b=a.copy()

b is a

 False

 True

Correct Answer:
False

52. Output is

a=[1,2,3,4]
b=[sum(a[0:x+1]) for x in range(0,len(a))]

print(b)

 10

 [1,3,4]

 4

 [1,3,6,10]
Correct Answer:
[1,3,6,10]

53. Which of the following data type use Key:pair

 list

 tuple

 dictionary

 set

Correct Answer:
dictionary

54. Output is

print("Hello {0[0]} and {0[1]}".format(('foo', 'bin')))

 Hello foo and bin

 Hello (‘foo’, ‘bin’) and (‘foo’, ‘bin’)

 Error

 None of the above

Correct Answer:
Hello foo and bin

55. Are nested if-else are allowed?

 No

 yes

Correct Answer:
yes

You might also like