0% found this document useful (0 votes)
68 views

Python Questions

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Python Questions

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 18

Q1.

What is the output for −


'python' [-3]?

A - 'o'
B - 't'
C - 'h'
D - Negative index error.

Answer: C

Q2. Which of the following is false statement in python

A - int(144)==144
B - int('144')==144
C - int(144.0)==144
D - None of the above

Answer : D

Q3. Select the option for following code −

s = 0
for d in range(0, 5, 0.1):
… s += d
… print(s)

A - Syntax Error
B - Type Error
C - Runtime Error
D - Both b & c

Answer : D

Q4. The str(x) Converts object x to a string representation.

A. True
B. False

Answer : A

Q5. The float(x) Converts x to a floating-point number.

A. True
B. False

Answer : A

Q6. The float(x) Converts x to an integer.

A. True
B. False

Answer : B
Q7. c -= a is equivalent to

A. c = a - c
B. c = c - a
C. a = c - a
D. a = a - c

Answer : B

Q8. c *= a is equivalent to

A. c = c * a
B. a = c * a
C. c = c / a
D. a = c / a

Answer : A

Q9. c /= a is equivalent to

A. c = c / a
B. c = c * a
C. a = c * a
D. a = a / c

Answer : A

Q10. c %= a is equivalent to

A. c = c % a
B. a = c % a
C. a = a % c
D. c = a % c

Answer : A

Q11. c += a is equivalent to

A. c = c + a
B. a = c + a
C. a = a + c
D. c = a + c

Answer : A,D

Q12. Who created Python

A. James Gosling
B. Denis Richie
C. Guido Van Rossum
D. Ramus Lerdorf

Answer : C

Q13. Is the a do while loop in python?

A. Yes
B. No

Answer: B

Q14. Which of the following function convert a string to a float in python?

A. int(x)
B. long(x)
C. float(x)
D. flaot(x)
E. str(x)

Answer: C

Q15. In python 3 what does // operator do ?

A. Float division
B. Integer division
C. returns the remainder
D. same as a**b

Answer: B

Q16. In python which keyword is used to start function?

A. function
B. try
C. def
D. import

Answer: C

Q17. Which function is used to open the file for reading in python?

A. fopen(filename,mode)
B. open(filename,mode)
C. openfile(filename,mode)
D. opne_file(filename,mode)

Answer: B

Q18. Out of list and tuples which are mutable?

A. tuples
B. list
C. both are mutable
D. none of above

Answer: B

Q19. What is the name of data type for character in python?

A. chr
B. char
C. character
D. python do not have any data type for characters they are treated as string

Answer: D
Q20. In python which is the correct method to load a module?

A. include sqlite3
B. import sqlite3
C. #include sqlite3
D. using sqlite3

Answer: B

Q21. Python is Case sensitive.

A. True
B. Flase

Answer: A

Q22. Are nested if-else allowed in python?

A. Yes
B. No

Answer: A

Q23. What will be the output of 19 % 2 in python?

A. 2
B. 17
C. 0
D. None above

Answer: D

Q24. What will be the output of 19 % 2 in python?

A. 2
B. 17
C. 19
D. 1

Answer: D

Q25. Which of the following is an invalid variable?

A. my_string_1
B. 1st_string
C. foo
D. _

Answer: B

Q26. Which of the following is an ivalid statement?

A. abc = 1,000,000
B. a b c = 1,000,000
C. a,b,c = 1,000,000
D. a_b_C = 1,000,000

Answer: B
Q27. Which one of these is floor division?

A. /
B. //
C. %
D. None of the mentioned

Answer: B

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

A. 7
B. 1
C. 0
D. 5

Answer: B

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

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

Answer: C

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

A. True
B. False

Answer: A

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

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

A. he
B. lo
C. olleh
D. hello

Answer: A

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

Q33. What data type is the object below?

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

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

Answer: A

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

>>>"Hello"*2

A. 'Hello*2'
B. 'Hello2'
C. 'HelloHello'
D. 'Hel'

Answer: C

Q35. 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. 85

Answer: A

Q36. Select all options that print.

hello-how-are-you

A. print(‘hello’, ‘how’, ‘are’, ‘you’)


B. print(‘hello’, ‘how’, ‘are’, ‘you’ + ‘-‘ * 4)
C. print(‘hello-‘ + ‘how-are-you’)
D. print(‘hello’ + ‘-‘ + ‘how’ + ‘-‘ + ‘are’ + ‘you’)

Answer: C

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

A. int
B. bool
C. float
D. None
Answer: A

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

4 + 3 % 5

A. 4
B. 7
C. 2
D. 0

Answer: B

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

Answer: B

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

A. /
B. %
C. //
D. |

Answer: C

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

Answer: A

Q42. What is the output for −


'python'[:3]?

A - 'pyt'
B - 'thon'
C - 'h'
D - Index error.

Answer: A

Q43. What is the output for −


'python'[3:]?

A - 'pyt'
B - 'hon'
C - 'h'
D - Index error.

Answer: B

Q44. What is the output for −


'python'[3:5]?

A - 'py'
B - 'ho'
C - 'tho'
D - Index error.

Answer: B

Q45. What is the output for −


'python'[-5]?

A - 'p'
B - 'y'
C - 'ython'
D - Index error.

Answer: B

Q46. What is the output for −


'python'[:-5]?

A - 'p'
B - 'y'
C - 'ython'
D - Index error.

Answer: A

Q47. What is the output for −


'python'[-5:]?

A - 'p'
B - 'y'
C - 'ython'
D - Index error.

Answer: C

Q48. What is the output for −


'python'[-3]?

A - 'p'
B - 'y'
C - 'ython'
D - 'h'

Answer: D
Q49. Python is Case Insensitive.

A. True
B. Flase

Answer: B

Q50. What will be the output of 20 % 2 in python?

A. 10
B. 5
C. 4
D. None above

Answer: D

Q51. What will be the output of 20 % 2 in python?

A. 2
B. 10
C. 4
D. 0

Answer: D

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

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

A. 85.0
B. 85
C. 84.0
D. 84

Answer: B

Q53. Python is not Case Insensitive.

A. True
B. Flase

Answer: B

Q54. What will be the output of 25 % 2 in python?

A. 10
B. 5
C. 4
D. None above

Answer: D
Q55. What will be the output of 25 % 2 in python?

A. 2
B. 10
C. 5
D. 1

Answer: D

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


4 + 6 % 5

A. 5
B. 7
C. 9
D. 10

Answer: A

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


40//6*5

A. 1
B. 30
C. 20
D. 10

Answer: B

Q58. What is the output for −


'Saturday'[:3]?

A - 'Sat'
B - 'turday'
C - 't'
D - 'day'

Answer: A

Q59. What is the output for −


'Saturday'[3:]?

A - 'Sat'
B - 'day'
C - 't'
D - 'tur'

Answer: B

Q60. What is the output for −


'Saturday'[3:5]?

A - 'Sa'
B - 'tu'
C - 'urda'
D - 'rday'

Answer: B

Q61. What is the output for −


'Saturday'[-5]?

A - 'd'
B - 'u'
C - 'r'
D - Index error.

Answer: B

Q62. What is the output for −


'Saturday'[:-5]?

A - 'Sat'
B - 'Satur'
C - 'urday'
D - Index error.

Answer: A

Q63. What is the output for −


'Saturday'[-5:]?

A - 'Sat'
B - 'Satur'
C - 'urday'
D - Index error.

Answer: C

Q64. What is the output for −


'Saturday'[-5:]?

A - 'u'
B - 'S'
C - 't'
D - 'd'

Answer: D

Q65. What will be the output of 15 % 2 in python?

A. 2
B. 10
C. 5
D. 1

Answer: D

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


4 + 2 % 5

A. 6
B. 1
C. 9
D. 4

Answer: A

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


6//6*5

A. 1
B. 0
C. 5
D. 6

Answer: C

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


'3'+'4'

A. 7
B. 34
C. 43
D. '3+4'

Answer: B

Q69. Which of the following symbols are used for comments in Python?

A. //
B. ''
C. /**/
D. #

Answer: D

Q70. Which predefined Python function is used to find length of string?

A. length()
B. len()
C. strlen()
D. stringlength()

Answer: B

Q71. Python allows string slicing. What is the output of below code:

s='Breakfast'
print(s[3:5])

A. 'st'
B. 'ak'
C. 'ea'
D. 'fa'
Answer: B

Q72. How to find the last element of list in Python? Assume `bikes` is the name of
list.

A. bikes[0]
B. bikes[-1]
C. bikes[lpos]
D. bikes[:-1]

Answer: B

Q73. If a='IPMC', b='GHANA' then which of the following operation would show
'IPMCGHANA' as output?

A. a+b
B. a+''+b
C. a+""+b
D. All of the above

Answer: D

Q74. If a='IPMCGHANA', b='GHANA' then what is the output of:


c = a-b
print(c)

A. IPMC
B. IPMCGHANA-GHANA
C. TypeError: unsupported operand
D. None of the above

Answer: C

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


a = 8.6
b = 2
print a//b

A. 4.3
B. 4.0
C. 4
D. compilation error

Answer: B

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

A. Error
B. 25
C. None
D. 2

Answer: B

Q77. To open a file c:\data\cppbuzz.txt for writing, we use


A. outfile = open(file = "c:\data\cppbuzz.txt", "o")
B. outfile = open("c:\data\cppbuzz.txt”, "r")
C. outfile = open("c:\data\cppbuzz.txt”, "w")
D. outfile = open("c:\data\cppbuzz.txt”, "r")

Answer: C

Q.78 How following evaluates in Python?


round(0.5) – round(-0.5)

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

Answer: C

Q79. ind out output of following code snippet

>>>str="ipmcghana"
>>>str[:4]

A. pmcg
B. ghan
C. ipmc
D. hana

Answer: C

Q80. What is the data type of X in X = [12.12, 13, 'ipmcghana']

A. Tuple
B. Array
C. Dictionary
D. List

Answer: D

Q81. Which of the following data type is used to store values in Key & Value
format?

A. Class
B. List
C. Tuple
D. Dictionary

Answer: D

Q82. Select the correct code to print


ipmc-ghana

A. print('ipmc-' + 'ghana');
B. print('ipmc' + '-ghana');
C. print('ipmc' + '-' + 'ghana');
D. All of the above

Answer: D

Q83. Is Python case sensitive?

A. Yes
B. No
C. Depends on Python Version

Answer: A

Q84.Which of the following statment is False?

A. Variable names can be arbitrarily long.


B. They can contain both letters and numbers.
C. Variable name can begin with underscore.
D. Variable name can begin with number.

Answer: D

Q85. Which of the following is a valid variable?

A. var@
B. 32var
C. def
D. abc_a_c

Answer : D

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

A. if a>=2 :
B. if (a >= 2)
C. if (a => 22)
D. if a >= 22

Answer: A

Q87. What keyword would you use to add an alternative condition to an if statement?

A. else if
B. elseif
C. elif
D. None of these

Answer: C

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

A. Yes
B. No

Answer: A

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

Answer : B

Q90. Does python have switch case statement?

A. True
B. False

Answer: B

Q91. Suppose a list with name arr, contains 5 elements. You can get the 2nd element
from the list using:

A. arr[-2]
B. arr[2]
C. arr[1]
D. arr[-1]

Answer: C

Q92. How to get last element of list in python? Suppose we have list with name arr,
contains 5 elements.

A. arr[0]
B. arr[5]
C. arr[last]
D. arr[-1]

Answer: D

Q93. Which keyword is use for function?

A. define
B. fun
C. def
D. function

Answer: C

Q94. What gets printed?


x = 4.5
y = 2
print(x//y)

A. 2.0
B. 2.25
C. 9.0
D. 20.25
E. 21

Answer: A

Q95. What gets printed?


nums = set([1,1,2,3,3,3,4])
print(len(nums))

A. 1
B. 2
C. 4
D. 5
E. 7

Answer: C

Q96. What gets printed?

x = True
y = False
z = False

if x or y and z:
print("yes")
else:
print("no")

A. yes
B. no
C. fails to compile

Answer: A

Q97. What gets printed?

x = True
y = False
z = False

if not x or y:
print(1)
elif not x or not y and z:
print(2)
elif not x or y or not y and x:
print(3)
else:
print(4)

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

Answer: C

Q98. Suppose you run the following code:

n = [[9, 8, 7], [6, 5, 4], [3, 2, 1]]


print n[2]

What gets printed?

A. [6, 5, 4]
B. 2
C. 7
D. [3, 2, 1]

Answer: D

Q99. What is the Python prompt?


A. >>
B. >>>
C. #
D. $

Answer: B

Q100. What is the result of the expression 15/2?


A. 1
B. 7
C. 7.5
D. 8

Answer: C

You might also like