Practice Questions - Class: XII Computer Science (Code 083)
Practice Questions - Class: XII Computer Science (Code 083)
Session: 2021-22
Computer Science (Code 083)
(Theory: Term-1)
2. The main difference between the modes 'w' and 'a' when opening a file in Python is that
a. 'w' is used for writing a file and 'a' for reading a file.
b. 'w' over-writes an existing file while 'a' writes to the end of the file.
c. 'w' is used with text files while 'a' is used with binary files.
d. 'w' gives an error if the file does not exist while 'a' creates it with no error.
3. Sushma gets the current date and time as a string x. Its value is "2021-10-30 12:49:44.216062".
Sushma prints the value of x[11:16] and gets "12:49". Which of these will contain the date in yyyy-
mm-dd format?
a. x[0:9]
b. x[0:10]
c. x[1:10]
d. x[1:11]
4. Python's Pickle module is used for serializing and de-serializing any Python object structure and
dumping / loading it to or from a binary file. Which of these is a case where it can be useful?
a. list
b. tuple
c. dictionary
d. set
6. Which of these Python data structures would be most suited to store the list of Indian states and
their corresponding capitals?
a. list
b. tuple
c. dictionary
d. set
a. p = 5
b. p = -5
c. p = p - 5
d. p = 5 – p
8. Which output lines of the following program will print the same results?
tup1 = (10, 20, 30, 40, 50, 60, 70, 80, 90)
print(tup1[5:-1]) # 1
print(tup1[5]) # 2
print(tup1[5:]) # 3
print(tup1[-4:8]) # 4
9. Every time Raj's python program accesses the internet, it records the time and website accessed in
a log file 'internet_access.log'. This is one of the sample lines in the log file:
Which of these lines would be used by the program to open the file?
a. seek()
b. set()
c. tell()
d. open()
11. When setting the current position of a file, the position CANNOT be set with reference to which
of these?
12. If a function is defined by the line "def calculate(p, q=100, r=10):", which of the following is
true?
a. p is an optional parameter
b. q and r are optional parameters
c. q will always have value 100 in the function
d. the above line will cause a syntax error
a. is a text file
b. can store images
c. is a type of python program
d. is a Computer Software Validation file
15. Python's abs() function returns the absolute value of number passed to it. For example abs(5) is
equal to 5 and abs(-3.1) = 3.1. What will be the value of
abs(3 – abs(-10))
a. 13
b. -13
c. -7
d. 7
16. Which of these statements about text and binary files is true?
a. A text file has the same number of characters in each line unlike a binary file.
b. A text file has a special End Of File character unlike a binary file.
c. An HTML file is an example of a text file while a CSV file is an example of a binary file.
d. Every character in a text file can occur in a binary file but the reverse is not true.
The exponentiation operator in Python (**) has higher precedence than the division (/) and multiplication (*)
operators, meaning that ** will be evaluated before / and * in an expression. Operators / and * have the same
precedence.
Further, ** evaluates from right to left, meaning that if an expression has multiple operators of the same
precedence of **, the operator on the right will be evaluated before the operator on the left. On the other
hand, / and * evaluate from left to right.
6 * 3 ** 2 ** 2 / 2
a. *
b. the first ** from the left
c. the second ** from the left
d. /
4 / 2 ** 3 * 2
a. 16.0
b. 1.0
c. 1/4
d. 1/16
19. Which of these statements about for and while loops in Python is TRUE?
a. A for loop usually run a given number of times; a while loop runs while a condition is met.
b. Statements in a for loop are always run at least once; those in a while loop may never be run.
c. A for loop cannot contain another for loop; a while loop can contain another while loop.
d. A for loop always has to have a loop counter; a while loop never uses a loop counter.
20. How does a Python program know where a FOR block ENDS?
a. when it finds a closing bracket (}) character
b. when it finds the keyword ENDFOR
c. when it finds a line with matching indentation
d. when it finds a colon (:) character
21. Sonal wrote the following program print_students.py to print the number of students in her class.
class = 5
section = "A"
students = 30
print ("There are", students, "students in class", class, section)
However, when she ran the program, she got the following output:
Which of these changes will make the program run without error?
A log file records the time, userid, record number and number of bytes read from a certain database. Each of
the fields uses a fixed number of bytes. The last 3 lines of the file were as follows at some time:
2021-08-09 10:21:20::0014::06733628::00001024
2021-08-09 10:22:03::0443::06384626::00001024
2021-08-09 10:22:52::0014::00549374::00001024
23. Which of these lines will return the latest entry from the file? (Assume the file handler is file and
SEEK_START and SEEK_END represent the offsets from the beginning and end of the file respectively)
a. file.seek(-45, SEEK_END)
b. file.seek(45, SEEK_END)
c. file.seek(135, SEEK_START)
d. file.seek(-135, SEEK_START)
24. The advantage of opening a file using the with clause instead of the open() function is that:
a. the access mode does not have to be specified
b. the file does not have to explicitly be closed
c. the filename does not have to be specified
d. the file gets opened for reading and writing
25. What should appear in the place of the '?' symbol in the table below?
Expression Value
mystring[-1] n
mystring[-3:] hon
mystring[5:-4] ?
e. e Py
f. ve
g. evol
h. ython
import pickle
print("The data that were stored in file are: ")
fileobject=open("mydata.dat","r")
objectvar=pickle.load(fileobject)
fileobject.close()
print(objectvar)
p = None
q = 0
r = ""
s = "None"
if (p == q):
print ("None is the same as 0")
elif (p == r):
print ("None is the same as empty string")
elif (p == s):
print ("None is the same as the string 'None'")
else:
print ("None of the above")
30. What will be the output of this program? (All quotes shown are double quotes)
a. 3 Idiots
b. 3 Idiots""
c. Syntax error on line 1
d. Syntax error on line 2
m = 1
n = "1"
print (str(m) + n)
a. 1
b. 2
c. 11
d. Syntax Error
33. What will be the output of this Python line?
a. This is Delhi.
b. This is Delhi. # Delhi is the capital of India. # This is a comment.
c. This is Delhi. # Delhi is the capital of India.
d. This is Delhi. This is a comment.
p = "12"
q = "5"
r = 10
s = 8
print(p+q, r+s)
a. 17 18
b. 125 108
c. 17 108
d. 125 18
Study the following program which has an unintended error and answer questions 35 and 36. [Recall
that range (m, n) is all integers from m to n-1]
i = 1
while (i <= 10): # For every number i from 1 to 10
sum = 0 # Set sum to 0
for x in range(1,i+1):
sum += x # Add every number from 1 to i
38. See the following program snippet which lists out the names of all students:
i = 0
while i <= number_of_students:
print(student_list[i])
i += 1
When it was run, it gave the following error on the last line
a. Replacing i = 0 with i = 1
b. Replacing i <= number_of_students with i < number_of_students
c. Replacing student_list[i] with student_list[i-1]
d. Replacing i += 1 with i = i + 1
39. See the code below using Python's pickle module to STORE data into file 'Latlong data':
import pickle
p = 40
r = 50
p = p + r
r = p - r
p = p - r
print (p, r)
a. p = 40, r = 40
b. p = 50, r = 50
c. p = 50, r = 40
d. p = 40, r = 50
43. The score of a student in a test is stored as a Python tuple. The test had 3 questions, with some
questions having subparts whose scores are recorded separately.
45. The choice() method of Python's random module returns a random element from a list. See the
program below.
import random
a. Hearts Ace
b. Diamonds of Queen
c. 7 of King
d. Jack of Clubs
46. Study the manual entry for the split() method given below.
parts = string.split("::", 2)
print(parts)
p = 1
q = 6
def change_values():
global p
q = 5
p = p + q
return (p)
change_values()
print(p, q)
a. 6 5
b. 1 5
c. 6 6
d. 1 6
In Python and other languages, a 'regular expression' is a pattern of characters in which some characters have a special
meaning. Specifically:
1. Any characters enclosed within square brackets ([]) represent any one of those characters
2. A * following a character represents zero or more of those characters.
3. All letters and digits represent themselves
Thus:
ab* could represent 'a', 'ab', 'abb', 'abbb' and so on but NOT 'bbb' or 'aba'. (Rules 2 and 3)
star[ekt] could represent 'stare', 'stark' or 'start' but NOT 'star' or 'are' (Rules 1 and 3)
a[bcd]* could represent 'a', 'ab', 'abc', 'acdd' and so on but not 'aba' or 'aa' (Rules 1-3)
a. ac
b. abc
c. bc
d. bccc
49. A factory codes its products using a series of letters and numbers. Each code starts with 10, 11 or 12
(representing different production locations), followed by an S, M or L (for Small, Medium or
Large), followed by a 4 digit number. Which of the following would represent this?
a. [101112][SML]1234
b. 1[012]SML[123456789]
c. 1[012][SML][0123456789][0123456789][0123456789][0123456789]
d. [012][012][SML][0123456789][0123456789][0123456789][0123456789]
Nagesh and Simran have written this Python program. It asks for a student's name and roll no. If both
match the records in a CSV file, it displays the student's English, Maths and Computer Science marks.
The marks data is stored in a file student_marks.csv whose first 3 rows are as shown below:
Name,Roll No,Comments,English,Maths,Computer Science
Nagesh Rao,12342,,85,92,96
Simran Shah,14324,Absent for English test,A,87,99
Ravi Gulati,43234,,76,97,81
import csv
print("Enter your name and roll number to see your subject marks...\n")
name = input("Enter Name exactly as in Hall Ticket: ")
rollno = input("Enter Roll No.: ")
51. What statements, respectively, should come in blanks 2 and 3? Choose the BEST answer.
a. continue and continue
b. break and break
c. break and continue
d. continue and break
52. What is the data type of the variable match_found?
a. bytes
b. str
c. int
d. bool
53. The file student_marks.csv contains a comment field in every row. Which of these comments may
cause the program to fail to read that row properly?
55. For the names shown in the sample data, assuming the roll number is entered correctly, which of
these will NOT find a match?
a. Nagesh Rao
b. Simram
c. Ravi Gulati
d. Simran shah