Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Prepared By:-

Namita Ranjan
PGT Computer Science
KV AFS Bareilly
MCQs
1. Which of the following is not a keyword?
a. eval
b. assert
c. nonlocal
d. pass
2. Which of these is not a core data type?
a. List
b. Class
c. Dictionary
d. Tuple
3. What would be the output of the following: -
>>> 25>50<80
a. True
b. False
c. None
d. No output
4. Predict the output of the following piece of code, if the value entered by user is (10 + 10)

myval=eval(input("Enter any number of your choice:"))


print(myval)
print(type(myval))

a. 10 + 10
b. 1010
c. 10
d. 20
5. Which of the following expressions results in an error?
a. float (’11.5’)
b. int (‘11’)
c. float (‘11’)
d. int (’11.5’)
6. The shortcut to remember precedence of operators in Python is:
a. BODMAS
b. PREMDAS
c. PEMDAS
d. DEVDAS
7. What will be the value of the expression
>>>19%11//3
a. 1
b. 2
c. 0
d. Error
8. What will be the output of the following code snippet:
A= [1,2,3,4,5]
print (A [4:0: -1])
a. [5,4,3,2,1]
b. [1,2,3,4,5]
c. [5,4,3,2]
d. [2,3,4]
9. Which type of error occurs when the content of the object being assigned is not of required data type.
a. SyntaxError
b. NameError
c. ValueError
d. TypeError
10. What is the order of resolving scope of a name in a Python program?

B: Built-in; E: Enclosing; G: Global; L: Local

a. BEGL
b. LGEB
c. GELB
d. LEGB
11. The numbered position of a letter in a string is called ________.
a. location
b. index
c. position
d. pointer
12. A token is also called a ___________ .
a. lexical unit
b. non-lexical unit
c. keyword
d. None of these
13. The keys of a dictionary must be of which type.
a. integer
b. float
c. mutable
d. immutable
14. What data type is the object below:
A= 1, 2, ‘hi’, 12.7
a. List
b. Tuple
c. Dictionary
d. array
15. What data type is the object below:
A={1,2,3,’hi’,9}
a. Set
b. Array
c. Dictionary
d. None of these
16. Which of the following is not a valid identifier in Python.
a. Radian
b. _semi1
c. #myfile
d. FOR
17. What will be the output of the code below:
>>> int (“5” + “8.9”)
a. “13.9”
b. “58.9”
c. TypeError
d. ValueError
18. Dictionaries are also called _________ .
a. mappings
b. hashes
c. associative arrays
d. all of these
19. Which of the given functions cannot be used with nested tuples?
a. sum ()
b. index ()
c. count ()
d. max ()
20. What would be the data type of variable “data” in the following statement:
data=f.readline()
a. String
b. List
c. Tuple
d. Set
21. Which of the following random module functions generates an integer?
a. random ()
b. randint ()
c. uniform ()
d. Both (b) and (c)
22. Which statement is NOT true about docstring.
a. Multi line comment is possible.
b. Starts and ends with triple quote.
c. Are completely ignored by the Python Interpreter.
d. All are true.
23. The help <module> statement displays ____________ from a module.
a. constants
b. classes
c. docstrings
d. functions
24. What will be printed when the following code executes:
def test (x, y=10):
print (x, y)
test (-5)
a. x, y
b. x, -5
c. -5, 10
d. -5 10
25. 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
26. Which of the following is the use of function in python?
a. Functions are reusable pieces of programs
b. Functions don’t provide better modularity for your application
c. you can’t also create your own functions
d. All of the mentioned
27. What is the output of the below program?
def func(a, b=5, c=10):
print('a is', a, 'and b is', b, 'and c is', c)
func(3, 7)
func(25, c = 24)
func(c = 50, a = 100)
a. a is 7 and b is 3 and c is 10
a is 25 and b is 5 and c is 24
a is 5 and b is 100 and c is 50
b. a is 3 and b is 7 and c is 10
a is 5 and b is 25 and c is 24
a is 50 and b is 100 and c is 5
c. a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50
d. None of the mentioned
28. What possible output(s) are expected to be displayed on screen at the time of execution of the program
from the following code? Also specify the minimum values that can be assigned to each of the variables BEGIN
and LAST.
import random
VALUES = [10, 20, 30, 40, 50, 60, 70, 80]
BEGIN = random.randint (1, 3)
LAST = random.randint(2, 4)
for I in range (BEGIN, LAST+1):
print (VALUES[I], end = "-")
a. 30-40-50
BEGIN=1
LAST=2
b. 30-40-50-60
BEGIN=0
LAST=2
c. 10-20-30-40
BEGIN=2
LAST=3
d. None of these
29. What possible outputs(s) are expected to be displayed on screen at the time of execution of the program
from the following code?
from random import randint
LST=[5,10,15,20,25,30,35,40,45,50,60,70]
first = randint(3,8)
second = randint(4,9)
third = randint(6,11)
print(LST[first],"#", LST[second],"#", LST[third],"#")
a. 20#25#25#
b. 30#40#70#
c. 15#60#70#
d. 35#40#60#
30. Which of the following functions helps us to randomize the items of a list?
a. seed
b. randomize
c. shuffle
d. uniform
31. What is the interval of the value generated by the function random.random(), assuming that the random
module has already been imported?
a. (0,1)
b. (0,1]
c. [0,1]
d. [0,1)
32. In which year was the Python 3.0 version developed?
a. 2008
b. 2010
c. 2018
d. 2020
33. What will be the output of this statement?
>>> print(ord('h') - ord('z'))
a. 18
b. -18
c. 17
d. -17
34. Find the correct output of the following code:
x = 'pqrs'
for i in range(len(x)):
x[i].upper()
print (x)
a. PQRS
b. PQRs
c. pQRS
d. pqrs
35. What will be the output of this program?
int1 = 10
int2 = 6
if int1 != int2 :
int2 = ++int2
print(int1 - int2)
a. 2
b. 4
c. 6
d. None
36. What will be the output of this program?
int1 = 0b0011
print(int1)
a. 0b0011
b. 3
c. Error
d. No output
37. Predict the output of the following code :
Word=”hello”
print(*Word)
a. hello
b. hello
c. Error
d. None
38. Consider the code block below. What happens when you run this program?
repeat_lyrics()
def repeat_lyrics():
print_lyrics()
print_lyrics()
def print_lyrics():
print("I'm a lumberjack, and I'm okay.")
print('I sleep all night and I work all day.')
a. The lyrics print like normal
b. We get a TypeError.
c. We get a NameError.
d. The program compiles but nothing prints.
39. What will be returned after calling modulus(100,95) and modulus(95.3,100.5) and modulus(12,12)?
def modulus(num1, num2):
answer = num1 % num2
return answer

a. 5 and 5.2 and 1


b. 5 and 95.3 and 0
c. 100 and 95.3 and 12
d. 95 and 100.5 and 0

40. After running the following code, what will the output be?
def multiplication_one(num1, num2):
num1 * num2

print(multiplication_one(5, 10))

def multiplication_two(num1, num2):


return num1 * num2

multiplication_two(5, 10)

a. None will be outputted after printing and calling multiplication_one(5, 10). Nothing will be outputted
after calling multiplication_two(5, 10).
b. Nothing will be outputted after printing and calling multiplication_one(5, 10). None will be outputted
after calling multiplication_two(5, 10).
c. 50 will be outputted after printing and calling multiplication_one(5, 10) and after calling
multiplication_two(5, 10).
d. None will be outputted after printing and calling multiplication_one(5, 10). 50 will be outputted after
calling multiplication_two(5, 10).

41. What would be outputted after running the code below? (Note: Ignore whitespaces.)
def addition(num1, num2):
return(num1 + num2)

def subtraction(num1, num2):


print(num1 - num2)

def main():
add_answer = addition(2, 4)
new_add_answer = addition(add_answer, 105)
print(subtraction(new_add_answer, 200))

a. None and -89


b. None
c. -89
d. -89 and None

42. After how many iterations will this code execute the break?
def divide_by_two_until_one(num):
count = 0
while (True):
num = num/2
count = count + 1
if (num <= 1):
break
return count

print(divide_by_two_until_one(50))

a. 20
b. 25
c. 5
d. 6

43. How many times does the following code print and in what pattern?
lst1 = [1, 3, 5, 7]
lst2 = [2, 4, 6, 8, 10]

for x in lst1:
for y in lst2:
print(x * y)

a. It prints 19 times and it skip counts by the current value in lst2


b. It prints 20 times and it skip counts by the current value in lst1.
c. It prints 20 times and it skip counts by the current value in lst2.
d. It prints 19 times and it skip counts by the current value in lst1.

44. Select which is true for Python function

a. A Python function can return only a single value


b. A function can take an unlimited number of arguments.
c. Python function doesn’t return anything unless and until you add a return statement
d. All are true

45. What is the output of the following display() function call:

def display(**kwargs):
for i in kwargs:
print(i)
display(emp="Karan", salary=8000)

a. TypeError
b. Karan
8000
c. (’emp’, ‘Karan’)
(‘salary’, 8000)
d. emp
salary

46. What is the output of the following display() function call:

def display(**kwargs):
for i in kwargs:
print(i)
display("Karan", 8000)

a. TypeError
b. Karan
8000
c. (’emp’, ‘Karan’)
(‘salary’, 8000)
d. emp
salary

47. What is the output of the following display() function call:

def display(**kwargs):
for i in kwargs:
print(i)
display(emp, salary)

a. TypeError
b. NameError
c. emp
salary
d. SyntaxError

48. What is the output of the following function call:

def outerFun(a, b):


def innerFun(c, d):
return c + d
return innerFun(a, b)
return a

result = outerFun(5, 10)


print(result)
a. 5
b. 15
c. 5, 15
d. Syntax Error

49. What is the output of the following displayPerson() function call:

def displayPerson(*args):
for i in args:
print(i)

displayPerson(name="Rima", age=25)

a. TypeError
b. NameError
c. name
age
d. Rima
25

50. What is the output of the following displayPerson() function call:

def displayPerson(*args):
for i in args:
print(i)

displayPerson("Rima", 25)

a. TypeError
b. NameError
c. name
age
d. Rima
25

51. What is the output of the following displayPerson() function call:

def displayPerson(*args):
for i in args:
print(i)

displayPerson(name, age)

a. TypeError
b. NameError
c. name
age
d. Rima
25

52. To generate a random float number between 20.5 to 50.5, which function of a random module I need to
use

a. random.random(20.5, 50.5)
b. random.uniform(20.5, 50.5)
c. random.randrange(20.5, 50.5)
d. random.randfloat(20.5, 50.5)

53. Choose the correct function from the following list to get the random integer between 99 to 200, which is
divisible by 3.

a. random.randrange(99, 200, 3)
b. random.randint(99, 200, 3)
c. random.random(99, 200, 3)
d. None of these

54. Select the correct ways to get the value of marks key.

student = {
"name": "Emma",
"class": 9,
"marks": 75
}

a. m = student.get(2)
b. m = student.get('marks')
c. m = student['marks'])
d. both b and c

55. Select the correct way to access the value of “history” subject:
sampleDict = {
"class":{
"student":{
"name":"Mike",
"marks":{
"physics":70,
"history":80
}
}
}
}
a. sampleDict['class']['student']['marks']['history']
b. sampleDict['class']['student']['marks'][1]
c. sampleDict['class'][0]['marks']['history']
d. All of the above

56. CSV files are ______, ______ files.


a. relational, text
b. relational, spreadsheet
c. flat, text
d. flat, spreadsheet

57. Which one of the following can be used as a delimiter in CSV?


a. Tab
b. Pipe (|)
c. Tilde(~)
d. All of these

58. What is the alternate method of writing the following code?


F=open (“c:\\temp\data.txt”, “r”)

a. F=open(r “c:\temp\data.txt”, ”r”)


b. F=open(“c:\temp\data.txt”, ”r”)
c. F=open(r, “c:\temp\data.txt”, ”r”)
d. None of these

59. Which mode create new file if the file does not exist?

a. write mode

b. append mode

c. Both of the above

e. None of the above

60. readlines() method return _________ .

a. List
b. String
c. Dictionary
d. Tuple

61. Which of the following options can be used to read the first line of a text file data.txt?

a. f = open(‘data.txt’)
f.read()
b. f = open(‘data.txt’,’r’)
f.read(n)
c. myfile = open(‘data.txt’)
f.readline()
d. f = open(‘data.txt’)
f.readlines()

62. File in python is treated as sequence of ________________

a. Characters
b. Bytes
c. Bits
d. Words

63. Which function is used to write data in binary mode?

a. write
b. writelines
c. pickle
d. dump

64. Which function is used to force transfer of data from buffer to file?

a. flush()
b. move()
c. save()
d. buffer()

65. Let the file pointer is at the end of 3rd line in a text file named “data.txt”. Which of the following option
can be used to read all the remaining lines?

a. f.read( )
b. f.read(all)
c. f.readline( )
d. f.readlines( )

66. ____________________ module is used for serializing and de-serializing any Python object structure.

a. pickle
b. unpickle
c. load
d. dump

67. Which of the following error is returned when we try to open a file in write mode which does not exist?

a. FileNotFoundError
b. FileFoundError
c. FileNotExistError
d. None of the above
68. The syntax of seek() is:
file_object.seek(offset [, reference_point])
What is reference_point indicate?

a. reference_point indicates the starting position of the file object


b. reference_point indicates the ending position of the file object
c. reference_point indicates the current position of the file object
d. None of the above.

69. Fill in the blank

import pickle
f=open("data.dat",'rb')
d=_____________________.load(f)
f.close()

a. unpickle
b. pickle
c. pickling
d. pick

70. Which statement will return error?


import pickle
f=open("data.dat",'rb')
d=pickle.load(f)
f.end()

a. Statement 1
b. Statement 2
c. Statement 3
d. Statement 4

71. Which of the following function takes two arguments?

a. load()
b. dump()
c. Both a and b
d. None

72. Almost all the files in our computer stored as _______ File.

a. Binary
b. Text
c. CSV
d. None of the above
73. The syntax of seek() is:
file_object.seek(offset [, reference_point])
What all values can be given as a reference point?

a. 0
b. 1
c. 2
d. All of the above

74. Fill in the blanks in the following code of writing data in binary files. Choose the answer for statement 1, 2,
3 and 4 respectively.

import ___________ # Statement 1


rec = [ ]
while True:
rn = int(input("Enter"))
nm = input("Enter")
temp = [rn, nm]
rec.append(temp)
ch = input("Enter choice (Y/N)")
if ch.upper == "N":
break
f = open("stud.dat", "____________") #statement 2
__________ .dump(rec, f) #statement 3
_______.close( ) # statement 4

a. pickle, wb, pickle, f


b. csv, w+, write, file
c. load, w, pickle, rec
d. csv, wb, unpickle, file

75. _______ function returns the current position of file pointer.

a. get()
b. tell()
c. cur()
d. seek()

76. f.seek(10,0) will move 10 bytes

a. forward
b. backward
c. end
d. None of the above

77. Which statement will move file pointer 10 bytes backward from current position.
a. f.seek(-10, 0)
b. f.seek(10, 0)
c. f.seek(-10, 1)
d. None of the above
78. When we open file in append mode the file pointer is at the _________ of the file.

a. End
b. Beginning
c. Anywhere in between the file
d. Second line of the file

79. Write the output of the First, Second, Third and Fourth Print statements respectively:

f=open("data.txt",'w')
f.write("Hello")
f.write("Welcome to my Blog")
f.close()
f=open("data.txt",'r')
d=f.read(5)
print(d) # First Print Statement
f.seek(10)
d=f.read(3)
print(d) # Second Print Statement
f.seek(13)
d=f.read(5)
print(d) # Third Print Statement
d=f.tell()
print(d) # Fourth Print Statement

a. ello, om, e to m, 16
b. Hello, co, e to my, 17
c. Hello, me, to my, 18
d. Hell, me, to my, 19

80. Sumit opened a file in python using open( ) function but forgot to specify the mode. In which mode the file
will open?

a. Write
b. Append
c. Read
d. Both read and write

81. Ram opened a file in a certain mode. After opening the file, he forgot the mode. The interesting facts
about that mode are ” If the file doesn’t exist, then a new file will be created” and “After opening file in that
mode the file handle will be at the end of the file” Help him to identify the correct mode.
a. Write
b. Append
c. Read
d. Both read and write

82. Which of the following is the valid way to open the file?

a. with open (file_name, access_mode) as f:


b. f = open (file_name, access_mode)
c. Both
d. None

83. Rehaan opened the file “myfile.txt” by using the following syntax. His friend told him few advantages of
the given syntax. Help him to identify the correct advantage.

with open ("myfile.txt", "a") as file_object:

a. In case the user forgets to close the file explicitly the file will closed automatically.
b. file handle will always be present in the beginning of the file even in append mode.
c. File will be processed faster
d. None of the above

84. Sanjana jotted down few features of the “write mode”. Help her to identify the valid features.

a. If we open an already existing file in write mode, the previous data will be erased.
b. In write mode, the file object will be positioned at the beginning of the file.
c. In write mode, if the file does not exist then the new file will be created.
d. All of the above

85. Which of the following error is returned by the given code:

>>> f = open("mytest.txt","w")
>>> f.write(345)

a. Syntax Error
b. Type Error
c. String Error
d. Run Time Error

86. Which of the following method does not return the number of characters written in the file.

a. Write
b. Writelines
c. Both
d. None

87. Fill in the blank in the given code:


fo = open("myfile.txt",'w')
lines = ["Hello \n", "Writing strings\n", "third line"]
fo._____________(lines)
myfile.close()

a. Write()
b. Writeline()
c. Writelines()
d. Dump()

88. Write the output of the following:

f=open("test.txt","w+")
f.write("File-Handling")
a=f.read(5)
print(a)

a. File-
b. File-H
c. File
d. No output
89. Write the output of the following:

f=open("test.txt","w+")
f.write("File-Handling")
f.seek(0)
a=f.read(5)
print(a)

a. File-
b. File-H
c. File
d. No output

90. Write the output of the following:

f=open("test.txt","w+")
f.write("FileHandling")
f.seek(5)
a=f.read(5)
print(a)

a. andli
b. FileH
c. Handli
d. No output
91. Write the output of the following:

f=open("test.txt","w+")
f.write("FileHandling")
f.seek(0)
a=f.read(-1)
print(a)

a. File
b. Handling
c. FileHandling
d. No output

92. Select the correct statement about the code given below:

myobj=open("myfile.txt", 'r')
print(myobj.readlines())

a. This code will read one line from the file.


b. This code will read all the lines from the file.
c. This code will read only last line from the file.
d. None of the above

93. Pankaj has written a program which is showing an error. As a friend of Pankaj, help him to identify the
wrong statement.
f=open("test.txt","w") #Statement 1
L = ["My name\n", "is\n", "Amit"] #Statement 2
f.writeline(L) #Statement 3
f.close() #Statement 4

a. Statement 1
b. Statement 2
c. Statement 3
d. Statement 4

94. Ravina wants to move the file object 5 bytes from the current position of the file object. As a friend of
Ravina, help her to write the code.

a. file_object.seek(5, 1)
b. file_object.seek(1, 5)
c. file_object.seek(5, 0)
d. file_object.seek(5, 2)
95. Which of the following statement open the file “marker.txt” as a blank file?

a. f = open(“marker.txt”, “r”)
b. f = open(“marker.txt”, “rb”)

c. f = open(“marker.txt”, “w”)

d. None of the above

96. Write the output of the following code:

import csv
f = open(“data.csv”, ‘r’)
row = csv.reader(f)
print(row)

a. It prints the memory address where csv.reader object is stored


b. It prints the first record of data.csv
c. It prints all the records of data.csv
d. None of the above

97. Write the output of the second print statement of the given code:
f=open("test.txt","r")
print(f.read())
f.seek(7)
print(f.read())

Output of first print statement is : MynameisNamita

a. eisNamita
b. isNamita
c. sNamita
d. Namita

98. Ananya was writing a program of reading data from csv file named “data.csv”. She writes the incomplete
code. As a friend of Ananya help her to complete the code.

________ csv #Statement1


f=open("data.csv", '_______') #Statement2
d=csv.___________(f) #Statement3
for __________ in d: #Statement4
print(row)

Identify the suitable option for Statement 1, 2, 3 and 4 respectively

a. import, a, read, d
b. import, r, reader, row
c. create, rb, readline, csv
d. create, w, readlines, row
99. Parth is writing a program to add/insert records in file “data.csv”. He has written the following code. As a
programmer, help him to execute it successfully.

import csv
field = [“Roll no” , “Name” , “Class”]
f = open(“_________” , ‘w’) #Statement1
d=__________.writer(f) #Statement2
d.writerow(field)
ch=’y’
while ch==’y’ or ch==’Y’:
rn=int(input(“Enter Roll number: “))
nm = input(“Enter name: “)
cls = input(“Enter Class: “)
rec=[_________] #Statement3
d.writerow(rec)
ch=input(“Enter more record??(Y/N)”)
f.close()

Identify the correct statement for Statement 1, 2 and 3 respectively

a. data, csv, (rollno, name, class)


b. data, csv_, (rollno, name, class)
c. data.csv, csv, (rn, nm, cls)
d. data.csv, csv_, (rn, nm, cls)

100. A text file stores information in ASCII or UNICODE characters as per:

a. the compiler
b. the interpreter
c. the programming platform
d. None of these
ANSWERS
1. a 2. b 3. b 4. d 5. d 6. c 7. b 8. c 9. c 10. d
11. b 12. a 13. d 14. b 15. a 16. c 17. d 18. d 19. a 20. a
21. b 22. c 23. c 24. d 25. c 26. a 27. c 28. a 29. d 30. c
31. d 32. a 33. b 34. d 35. b 36. b 37. a 38. c 39. b 40. a
41. d 42. d 43. c 44. b 45. d 46. a 47. b 48. b 49. a 50. d
51. b 52. b 53. a 54. d 55. a 56. c 57. d 58. a 59. c 60. a
61. c 62. b 63. d 64. a 65. c 66. a 67. d 68. a 69. b 70. d
71. b 72. a 73. d 74. a 75. b 76. a 77. c 78. a 79. c 80. c
81. b 82. c 83. a 84. d 85. b 86. b 87. c 88. d 89. a 90. a
91. c 92. b 93. c 94. a 95. c 96. a 97. c 98. b 99. c 100. c

You might also like