0% found this document useful (0 votes)
64 views26 pages

Cogni Python

The document contains a series of multiple-choice questions related to Python programming concepts, covering topics such as case sensitivity, variable types, operators, expressions, and formatting. Each question is followed by four answer options, with the correct answer indicated. The content serves as a quiz or test for assessing knowledge of Python programming.

Uploaded by

p2004mondal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views26 pages

Cogni Python

The document contains a series of multiple-choice questions related to Python programming concepts, covering topics such as case sensitivity, variable types, operators, expressions, and formatting. Each question is followed by four answer options, with the correct answer indicated. The content serves as a quiz or test for assessing knowledge of Python programming.

Uploaded by

p2004mondal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

1. Is Python case sensitive when dealing with identifiers?


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

2. What is the maximum possible length of an identifier?​


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

3. Which of the following is not allowed in Python?​


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

4. Which of the following is an invalid variable?​


a) my_string_1​
b) 1st_string​
c) foo​
d) _​
Answer: b

5. 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​
Answer: a

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​
Answer: c

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


a) 27​
b) 9​
c) 3​
d) 1​
Answer: c

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​
Answer: a

9. The expression int(x) implies that the value of variable x is converted to integer.​
a) True​
b) False​
Answer: a

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

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


a) True​
b) False​
c) Machine dependent​
d) Error​
Answer: b

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​
Answer: c

13. What is the type of inf?​


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

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


a) +5​
b) -11​
c) +11​
d) -5​
Answer: a

16. In Python, variable types are not explicitly declared—they are inferred at runtime.
Consider the following incomplete operation:

x = 13 ? 2

The objective is to ensure that x has an integer value. Select all options that achieve this
(Python 3.x):​
a) x = 13 // 2​
b) x = int(13 / 2)​
c) x = 13 % 2​
d) All of the mentioned​
Answer: d

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

apple = mango

a) SyntaxError​
b) NameError​
c) ValueError​
d) TypeError​
Answer: b

18. 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​
b) Cannot perform mathematical operation on strings​
c) hello2​
d) hello2hello2​
Answer: a

19. What data type is the object below?

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

a) list​
b) dictionary​
c) array​
d) tuple​
Answer: a

20. In Python, which core data type is used to store values in the form of key–value
pairs?​
a) list​
b) tuple​
c) class​
d) dictionary​
Answer: d

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

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

print(4 + 3 % 5)

a) 4​
b) 7​
c) 2​
d) 0​
Answer: b

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

A % B // A

a) 0.0​
b) 0​
c) 1.0​
d) 1​
Answer: b

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

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

x = int(43.55+2/2)

print(x)

a) 43​
b) 44​
c) 22​
d) 23​
Answer: b

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

print(float(4+int(2.39)%2))

a) 5.0​
b) 5​
c) 4.0​
d) 4​
Answer: c

27. 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​
Answer: a

28. Which of the following expressions results in an error?​


a) float(‘10’)​
b) int(‘10’)​
c) float(’10.8’)​
d) int(’10.8’)​
Answer: d
29. What will be the value of the following Python expression?

print(4+2**5//10)

a) 3​
b) 7​
c) 77​
d) 0​
Answer: b

30. The expression 2**2**3 is evaluated as: (2**2)**3.​


a) True​
b) False​
Answer: b

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

x<<2

a) 8​
b) 1​
c) 2​
d) 4​
Answer: d

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

print(bin(29))

a) 0b10111​
b) 0b11101​
c) 0b11111​
d) 0b11011​
Answer: b

33. What will be the value of x in the following Python expression, if the result of that
expression is 2?

x>>2

a) 8​
b) 4​
c) 2​
d) 1​
Answer: a
34. What will be the output of the following Python expression?

print(int(1011))

a) 1011​
b) 11​
c) 13​
d) 1101​
Answer: a

35. To find the decimal value of 1111, that is 15, we can use the function:​
a) int(1111,10)​
b) int(‘1111’,10)​
c) int(1111,2)​
d) int(‘1111’,2)​
Answer: d

36. 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​
Answer: c

37. Which of the following expressions results in an error?​


a) int(1011)​
b) int(‘1011’,23)​
c) int(1011,2)​
d) int(‘1011’)​
Answer: c

38. Which of the following represents the bitwise XOR operator?​


a) &​
b) ^​
c) |​
d) !​
Answer: b

39. What is the value of the following Python expression?

print(bin(0x8))
a) 0bx1000​
b) 8​
c) 1000​
d) 0b1000​
Answer: d

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

print(0x35 | 0x75)

a) 115​
b) 116​
c) 117​
d) 118​
Answer: c

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

x = ['ab', 'cd']
for i in x:
i.upper()

print(x)

a) [‘ab’, ‘cd’]​
b) [‘AB’, ‘CD’]​
c) [None, None]​
d) none of the mentioned​
Answer: a

42. Which of the following is not a class method?​


a) Non-static​
b) Static​
c) Bounded​
d) Unbounded​
Answer: a

43. 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​
Answer: d

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

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​
Answer: a

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

x = "abcdef"

i = "a"

while i in x:

print(i, end = " ")

a) no output​
b) i i i i i i …​
c) a a a a a a …​
d) a b c d e f​
Answer: c

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

i = 5

while True:

if i%0O9 == 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​
Answer: d

47. 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 …​
Answer: d

48. 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​
Answer: c

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

i = 1

while False:

if i%2 == 0:
break

print(i)

i += 2

a) 1​
b) 1 3 5 7 …​
c) 1 2 3 4 …​
d) none of the mentioned​
Answer: d

50. 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​
Answer: c

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

print('%d %s %g you' %(1, 'hello', 4.0))

a) Error​
b) 1 hello you 4.0​
c) 1 hello 4 you​
d) 1 4 hello you​
Answer: c

52. The output of which of the codes shown below will be: “There are 4 blue birds.”?​
a) ‘There are %g %d birds.’ %4 %blue​
b) ‘There are %d %s birds.’ %(4, “blue”)​
c) ‘There are %s %d birds.’ %[4, “blue”]​
d) ‘There are %d %s birds.’ 4, “blue”​
Answer: b
53. What will be the output of the following Python code?

x=456

print("%-06d"%x)

a) 000456​
b) 456000​
c) 456​
d) error​
Answer: c

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

X=345

print(“%06d”%X)

a) 345000​
b) 000345​
c) 000000345​
d) 345000000​
Answer: b

55. Which of the following formatting options can be used in order to add ‘n’ blank
spaces after a given string ‘S’?​
a) print(“-ns”%S)​
b) print(“-ns”%S)​
c) print(“%ns”%S)​
d) print(“%-ns”%S)​
Answer: d

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

X = -122

print("-%06d"%X)

a) −000122​
b) 000122​
c) −−00122​
d) −00122​
Answer: c

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


x=34

print(“%f”%x)

a) 34.00​
b) 34.0000​
c) 34.000000​
d) 34.00000000​
Answer: c

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

x=56.236

print("%.2f"%x)

a) 56.00​
b) 56.24​
c) 56.23​
d) 0056.236​
Answer: b

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

x=22.19

print("%5.2f"%x)

a) 22.1900​
b) 22.00000​
c) 22.19​
d) 22.20​
Answer: c

60. The expression shown below results in an error.

print("-%5d0",989)

a) True​
b) False​
Answer: b

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

l=list('HELLO')
print('first={0[0]}, third={0[2]}'.format(l))

a) ‘first=H, third=L’​
b) ‘first=0, third=2’​
c) Error​
d) ‘first=0, third=L’​
Answer: a

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

l=list('HELLO')

p=l[0], l[-1], l[1:3]

print('a={0}, b={1}, c={2}'.format(*p))

a) Error​
b) “a=’H’, b=’O’, c=(E, L)”​
c) “a=H, b=O, c=[‘E’, ‘L’]”​
d) Junk value​
Answer: c

63. The formatting method {1:<10} represents the ___________ positional argument,
_________ justified in a 10 character wide field.​
a) first, right​
b) second, left​
c) first, left​
d) second, right​
Answer: b

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

print(hex(255), int('FF', 16), 0xFF)

a) [0xFF, 255, 16, 255]​


b) (‘0xff’, 155, 16, 255)​
c) Error​
d) (‘0xff’, 255, 255)​
Answer: d

65. The output of the two codes shown below is the same.

i. print(bin((2**16)-1))

ii. print('{}'.format(bin((2**16)-1)))
a) True​
b) False​
Answer: a

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

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

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

print('x\97\x98')

a) Error​
b)97

98

c) x\97˜​
d) \x97\x98​
Answer: c

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

str1="helloworld"

print(str1[::-1])

a) dlrowolleh​
b) hello​
c) world​
d) helloworld​
Answer: a

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

print(0xA + 0xB + 0xC)

a) 0xA0xB0xC​
b) Error​
c) 0x22​
d) 33​
Answer: d

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


a) seed()​
b) sqrt()​
c) factorial()​
d) print()​
Answer: d

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

print(round(4.576))

a) 4.5​
b) 5​
c) 4​
d) 4.6​
Answer: b

73. 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​
Answer: c

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

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

a) Error​
b) True​
c) False​
d) 0​
Answer: c

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

print(round(4.5676,2))

a) 4.5​
b) 4.6​
c) 4.57​
d) 4.56​
Answer: c

76. The function complex(‘2-3j’) is valid but the function complex(‘2 – 3j’) is invalid.​
a) True​
b) False​
Answer: a

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

print(list(enumerate([2, 3])))

a) Error​
b) [(1, 2), (2, 3)]​
c) [(0, 2), (1, 3)]​
d) [(2, 3)]​
Answer: c

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

x=3

print(eval('x^2'))

a) Error​
b) 1​
c) 9​
d) 6​
Answer: b

79. What is the output of the function complex()?​


a) 0j​
b) 0+0j​
c) 0​
d) Error​
Answer: a

80. Which of the following functions does not necessarily accept only iterables as
arguments?​
a) enumerate()​
b) all()​
c) chr()​
d) max()​
Answer: c

81. 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​
Answer: d

82. What are the two main types of functions?​


a) Custom function​
b) Built-in function & User defined function​
c) User function​
d) System function​
Answer: b

83. Where is the function defined?​


a) Module​
b) Class​
c) Another function​
d) All of the mentioned​
Answer: d

84. What is called when a function is defined inside a class?​


a) Module​
b) Class​
c) Another function​
d) Method​
Answer: d

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


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​
Answer: a
86. Which of the following refers to mathematical function?​
a) sqrt​
b) rhombus​
c) add​
d) rhombus​
Answer: a

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

def change(one, *two):

print(type(two))

change(1,2,3,4)

a) Integer​
b) <class ‘tuple’>​
c) <class ‘Dict’>​
d) An exception is thrown​
Answer: b

88. If a function doesn’t have a return statement, which of the following does the function
return?​
a) int​
b) null​
c) None​
d) An exception is thrown without the return statement​
Answer: c

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

def display(b, n):

while n > 0:

print(b,end="")

n=n-1

display('z',3)

a) zzz​
b) zz​
c) An exception is executed​
d) Infinite loop​
Answer: a

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

def find(a, **b):

print(type(b))

find('letters',A='1',B='2')

a) <class ‘string’>​
b) <class ‘tuple’>​
c) <class ‘dict’>​
d) An exception is thrown​
Answer: c

91. Which module in Python supports regular expressions?​


a) re​
b) regex​
c) pyregex​
d) none of the mentioned​
Answer: a

92. Which of the following creates a pattern object?​


a) re.create(str)​
b) re.regex(str)​
c) re.compile(str)​
d) re.assemble(str)​
Answer: c

93. What does the function re.match do?​


a) matches a pattern at the start of the string​
b) matches a pattern at any position in the string​
c) such a function does not exist​
d) none of the mentioned​
Answer: a

94. What does the function re.search do?​


a) matches a pattern at the start of the string​
b) matches a pattern at any position in the string​
c) such a function does not exist​
d) none of the mentioned​
Answer: b
95. What will be the output of the following Python code?

import re

sentence = 'we are humans'

matched = re.match(r'(.*) (.*?) (.*)', sentence)

print(matched.groups())

a) (‘we’, ‘are’, ‘humans’)​


b) (we, are, humans)​
c) (‘we’, ‘humans’)​
d) ‘we are humans’​
Answer: a

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

import re

sentence = 'we are humans'

matched = re.match(r'(.*) (.*?) (.*)', sentence)

print(matched.group())

a) (‘we’, ‘are’, ‘humans’)​


b) (we, are, humans)​
c) (‘we’, ‘humans’)​
d) we are humans​
Answer: d

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

import re

sentence = 'we are humans'

matched = re.match(r'(.*) (.*?) (.*)', sentence)

print(matched.group(2))
a) ‘are’​
b) ‘we’​
c) ‘humans’​
d) ‘we are humans’​
Answer: a

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

import re

sentence = 'horses are fast'

regex = re.compile('(?P<animal>\w+) (?P<verb>\w+) (?P<adjective>\w+)')

matched = re.search(regex, sentence)

print(matched.groupdict())

a) {‘animal’: ‘horses’, ‘verb’: ‘are’, ‘adjective’: ‘fast’}​


b) (‘horses’, ‘are’, ‘fast’)​
c) ‘horses are fast’​
d) ‘are’​
Answer: a

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

import re

sentence = 'horses are fast'

regex = re.compile('(?P<animal>\w+) (?P<verb>\w+) (?P<adjective>\w+)')

matched = re.search(regex, sentence)

print(matched.groups())

a) {‘animal’: ‘horses’, ‘verb’: ‘are’, ‘adjective’: ‘fast’}​


b) (‘horses’, ‘are’, ‘fast’)​
c) ‘horses are fast’​
d) ‘are’​
Answer: b

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


import re

sentence = 'horses are fast'

regex = re.compile('(?P<animal>\w+) (?P<verb>\w+) (?P<adjective>\w+)')

matched = re.search(regex, sentence)

print(matched.group(2))

a) {‘animal’: ‘horses’, ‘verb’: ‘are’, ‘adjective’: ‘fast’}​


b) (‘horses’, ‘are’, ‘fast’)​
c) ‘horses are fast’​
d) ‘are’​
Answer: d

101. _____ represents an entity in the real world with its identity and behaviour.​
a) A method​
b) An object​
c) A class​
d) An operator​
Answer: b

102. _____ is used to create an object.​


a) class​
b) constructor​
c) User-defined functions​
d) In-built functions​
Answer: b

103. 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​
Answer: c

104. 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​
Answer: b

105. 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​
Answer: a

106. 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)
a) 6​
b) 7​
c) Error​
d) 0​
Answer: b

107. 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​
Answer: c

108. 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​
Answer: a

109. 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​
Answer: c

110. 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​
Answer: d

You might also like