Computer Science Class 12 Sample Question
Computer Science Class 12 Sample Question
General Instructions:
Section-A
This section consists of 25 Questions (1 to 25). Attempt any 20 questions from this section.
Choose the best possible option.
1. A _____________ is a sequence of one or more characters used to provide a name for a given
program element.
a. Identifier
b. Variable
c. String
d. Character
2. The functionality of seek() function is :
a. ab
b. r+
c. w+
d. rw
4. What might be the output of the following code :
A = ("hello") * 3
print(A)
a. Operator Error
b. ('hello','hello','hello')
c. 'hellohellohello'
d. None of these
Page 1 of 12
5. The ________ function returns Boolean True value if all the keys in the dictionary are True else
returns False.
a. all()
b. sorted()
c. len()
d. any()
6. Predict the output of the following code :
>>> dic = {}
>>> dic.fromkeys([1,2,3], "check")
a. Syntax error
b. {1: 'check', 2: 'check', 3: 'check'}
c. 'check'
d. {1:None, 2:None, 3:None}
7. The readline() method returns
a. str
b. a list of lines
c. a list of single characters
d. a list of integers
8. Assume tuple_1 = (7,8,9,10,11,12,13) then the output of tuple_1[1:-1] is :
a. Error
b. (8,9,10,11,12)
c. [8,9,10,11,12]
d. None
9. Infer the output of the following code :
count = {}
count[(1, 2, 4)] = 5
count[(4, 2, 1)] = 7
count[(1, 2)] = 6
count[(4, 2, 1)] = 2
tot = 0
for i in count:
tot = tot + count[i]
print(len(count)+tot)
a. 25
b. 17
c. 16
d. Error
10. The syntax to write to a CSV file is :
a. CSV.DictWriter(filehandler)
b. CSV.reader(filehandler)
c. CSV.writer(filehandler)
Page 2 of 12
d. CSV.write(filehandler)
11. What will be the output of the following code snippet ?
numbers = {}
letters = {}
comb = {}
numbers[1] = 56
numbers[3] = 7
letters[4] = "B"
comb["Numbers"] = numbers
comb["Letters"] = letters
print(comb)
a. print(air_force[2])
b. air_force[2] = 42
c. print(max(air_force))
d. print(len(air_force))
13. The method that returns the value for the key present in the dictionary and if the key is not
present then it inserts the key with default value into the dictionary.
a. update()
b. fromkeys()
c. setdefault()
d. get()
14. What will be the output of the following ?
data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
print(data[1][0][0])
a. 1
b. 2
c. 4
d. 5
15. Assuming b to be a dictionary, what does any(b) do ?
for i in range(5):
with open("data.txt", "w") as f:
Page 3 of 12
if i > 0:
break
print(f.closed)
a. True
b. False
c. None
d. Error
17. What is the data type of (3) ?
a. Tuple
b. List
c. None
d. Integer
18. Assume dic is a dictionary with some key:value pairs. What does dic.popitem() do ?
s="a@b@c@d"
a=list(s.partition("@"))
print(a)
b=list(s.split("@",3))
print(b)
a) [‘a’,’b’,’c’,’d’]
[‘a’,’b’,’c’,’d’]
b) [‘a’,’@’,’b’,’@’,’c’,’@’,’d’]
[‘a’,’b’,’c’,’d’]
c) [‘a’,’@’,’b@c@d’]
[‘a’,’b’,’c’,’d’]
d) [‘a’,’@’,’b@c@d’]
[‘a’,’@’,’b’,’@’,’c’,’@’,’d’]
a. 0
b. 2
Page 4 of 12
c. Error
d. 1
21. Which of the following statements are true ?
a. When you open a file for reading in ‘r’ mode, if the file does not exist, an error
occurs
b. When you open a file for writing in ‘w’ mode, if the file does not exist, a new
file is created
c. When you open a file for writing in ‘w’ mode, if the file exists, the existing file
is overwritten with the new file
d. All of the mentioned
22. Guess the output of the following code.
grades = {90: "S", 80: "A"}
del grades
a. \t\ntweet\n
b. \t\ntweet
c. tweet\n
d. 'tweet'
24. Which of the following cannot be used as a key in Python dictionaries ?
a. Strings
b. Lists
c. Tuples
d. Numerical values
25. What is the output of the following code ?
a) 3
[44, 2, 3]
b) 1
[1,2,3]
Page 5 of 12
c) 3
[1,2,3]
d) 1
[44,2,3]
Section-B
This section consists of 24 Questions (26 to 49). Attempt any 20 questions.
file1=open("notes.txt","a")
ch=input("enter the text")
file1.________(ch+"\n")
file1.close()
(i) writelines
(ii) write
(iii) read
(iv) append
27. Given below a program to found the occurrence of word “To” in a file notes.txt. Which function
should be used to read the file word by word ?
count=0
file1=open("notes.txt","r")
s=file1.read()
s1=_____________
for i in s1:
if(i==ch):
count+=1
print(ch," word exists ", count, " number of times")
(i) s.read(10)
(ii) s. split()
(iii) s.index()
(iv) s.readline()
28. Which types of files stores information in the form of a stream of ASCII or Unicode Characters
?
(i) String
(ii) A List of integers
Page 6 of 12
(iii) A list of characters
(iv) A List of Lines
31. What error is returned by following statement, if file “try.txt” does not exist ?
f = open(“try.txt”)
32. In text file each line is terminated by a special character called _______
(i) EOL
(ii) END
(iii) Full stop
(iv) EOF
33. Which of the following functions do you use to write data in the binary format ?
A. write
B. load
C. dump
D. send
34. Data=F.read(10).
Which of the following statement is True regarding variable Data :
a) 8
b) 12
c) 16
d) 32
Page 7 of 12
36. Name the function to read from CSV file :
a. read()
b. csv.reader()
c. csv.read()
d. reader()
37. In which file, no delimiters are used for line and no translations occur ?
a. Text file
b. Binary file
c. csv file
d. None of the above
a) (‘cd’)
b) (”)
c) (‘cd’, ”, ”)
d) (”, ‘cd’, ”)
a) range(0, 5)
b) 0, 1, 2, 3, 4
c) [0, 1, 2, 3, 4]
d) None
a) Hell
b) he
c) Hello
d) Error
a) Syntax Error
b) Value Error
c) Type Error
a) 1.0
Page 8 of 12
b) 0.0
c) Any floating point number
d) zero
43. Observe the code carefully and write answer of questions given below :
a=10
b=12
def change():
a=9
global b
b=7
print(a,b)
y=b
change()
x=a
z=b
what is the output of the above program ?
a) 9 7
b) 10 7
c) 10 12
d) None of the above
values = [1, 2, 3]
print(increment_items(values, 2))
print(values)
a) None
[3, 4, 5]
b) None
[1, 2, 3]
c) [3, 4, 5]
[1, 2, 3]
d) [3, 4, 5]
None
45. Observe the code carefully and write answer of questions given below :
a=10
def F():
a=5
Page 9 of 12
b=20
print(a)
print(a)
What is the output of the above code?
a) 5
b) 10
c) 20
d) None of the above
47. Which of the following function calls will cause error while invoking the function header
def test(a,b)
(a ) test(10,20)
(b) test(a=10, b=20)
(c) test(a=10, b)
(d) test(10, b=20)
48. If return statement is not used inside the function, the function will return :
a) 0
b) None object
c) An arbitrary integer
d) error
49. Suppose there is a list such that: l=[2,3,4]. If we want to print this list in reverse order, which of
the following methods should be used ?
a) reverse(l)
b) list(reverse[(l)])
c) reversed(l)
d) list(reversed(l))
Section-C
Case Study based Questions
This section consists of 6 Questions (50 - 55) Attempt any 5 questions.
Amal, a student of class 12, is learning BINARY File Module in Python. During examination, he has
been assigned an incomplete Python code (shown below) to create a Binary File ‘emp.dat'. Help
him in completing the code.
Page 10 of 12
The binary file “emp.dat” has structure (Emp_id, Emp_name, Emp_Salary). Write a function in
Python countsal() in Python that would read contents of the file “emp.dat” and display the details of
those employee whose salary is greater than 20000.
import pickle
def ____________: Line1
f = open (“emp.dat”, “________”) Line 2
n=0
try:
while True:
rec = ____________ Line 3
if rec[2] > 20000:
print(rec[0], rec[1], rec[2], sep=”\t”)
num = num + _______ Line 4
except:
f.close() Line 5
a. countsal()
b. Countsal()
c. Sal()
d. count()
a. ab
b. rb
c. wb
d. None of the above
a. pickle.dump()
b. pickle.read()
c. pickle.load()
d. None of the above
a. -1
b. 2
c. 0
d. 1
a. f.close()
b. f.exit()
Page 11 of 12
c. f.stop()
d. None of the above
Page 12 of 12