Cbsepython - In-Term 1 Practice Questions Class 12 Computer Science 083
Cbsepython - In-Term 1 Practice Questions Class 12 Computer Science 083
083
cbsepython.in/term-1-practice-questions-class-12-computer-science
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.
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 yyyymm-dd format?
a) x[0:9]
b) x[0:10]
c) x[1:10]
d) x[1:11]
b) x[0:10]
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?
1/21
a) graphically representing the contents of any Python object on the screen
c) storing the data in any Python object for later reconstruction or use
d) compressing a large dataset so that it can be saved in less than a tenth of the space
c) storing the data in any Python object for later reconstruction or use
5. Which of these Python data structures cannot have duplicate items and does not support
ordering?
a) list
b) tuple
c) dictionary
d) set
d) set
6. Which of these Python data structures would be most suited to store the list of Indian states
and
a) list
b) tuple
c) dictionary
d) set
c) dictionary
a) p = 5
b) p = -5
c) p = p – 5
d) p = 5 – p
c) p = p – 5
2/21
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?
b) logfile = open(‘internet_access.log’,’w’)
10. Which of these functions can be used to set a file’s current position?
a) seek()
b) set()
c) tell()
d) open()
a) seek()
3/21
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
a) is a text file
4/21
a) is a text 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
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.
c) An HTML file is an example of a text file while a CSV file is an example of a binary file.
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
6 * 3 ** 2 ** 2 / 2
5/21
a) *
d) /
4 / 2 ** 3 * 2
a) 16.0
b) 1.0
c) 1/4
d) 1/16
c) 1/4
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.
a) A for loop usually run a given number of times; a while loop runs while a condition is met.
20. How does a Python program know where a FOR block ENDS?
6/21
21. Sonal wrote the following program print_students.py to print the number of students in her
class.
class = 5
section = “A”
students = 30
However, when she ran the program, she got the following output:
class = 5
Which of these changes will make the program run without error?
d) adding a colon (‘:’) at the end of the statement (begin indented block)
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
a) Comma (,)
b) Colon (:)
c) Double-colon(::)
d) Hyphen (-)
7/21
b) Colon (:)
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)
d) file.seek(-135, SEEK_START)
24. The advantage of opening a file using the with clause instead of the open() function is that:
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] ?
a) e Py
b) ve
c) evol
d) ython
a) e Py
8/21
26. Identify the error in the program below:
import pickle
fileobject=open(“mydata.dat”,”r”)
objectvar=pickle.load(fileobject)
fileobject.close()
print(objectvar)
p = None
q=0
r = “”
s = “None”
if (p == q):
elif (p == r):
elif (p == s):
else:
9/21
b) None is the same as empty string
In Python, lists are mutable and tuples immutable. See the program and
answer questions 28 and 29:
list_items = [“Sachin”, “Dravid”, “Kapil”, “Dhoni”, “Sourav”]
list_items[4] = “Harbhajan”
tuple_items[4] = “Friday”
print(list_items[4], tuple_items[4])
a) memory-efficient
b) fixed
c) changeable
d) sequential
c) changeable
a) Sourav Thursday
b) Harbhajan Friday
30. What will be the output of this program? (All quotes shown are double quotes)
print(message[23:])
10/21
a) 3 Idiots
b) 3 Idiots””
x = txt.find(“,”)
y = txt.find(“?”)
print(txt[x+2:y])
c) Ravi
d) Ravi?
c) Ravi
m=1
n = “1”
print (str(m) + n)
a) 1
b) 2
c) 11
d) Syntax Error
c) 11
11/21
print(“This is Delhi. # Delhi is the capital of India.”) # This is a comment.
a) This is Delhi.
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
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
for x in range(1,i+1):
12/21
b) Print the total of all numbers from 1 to 10
c) For each number till 10, print the sum of numbers between 1 and the number.
d) For each number till 10, print the sum of the number and its previous number
c) For each number till 10, print the sum of numbers between 1 and the number.
if (i % 3 == 0) or (i % 4 == 0):
continue
print(i)
38. See the following program snippet which lists out the names of all students:
number_of_students = len(student_list)
i=0
13/21
print(student_list[i])
i += 1
When it was run, it gave the following error on the last line
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
file.close()
40. What will be the value of p and r at the end of this program?
p = 40
r = 50
p=p+r
14/21
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
c) p = 50, r = 40
fp = open(“RoadNotTaken.txt”, ‘r’)
poem = fp.readlines()
print (len(poem))
fp.close()
a) string
b) list
c) tuple
d) dictionary
b) list
a) 2
15/21
b) 3
c) 8
d) 22
b) 3
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.
print (score[2][2])
a) (1, 3, 2)
b) (2, 1)
c) 3
d) 8
a) (1, 3, 2)
c) 9876500001 | +1 203-607-1232
16/21
45. The choice() method of Python’s random module returns a random element from a list. See
the program below.
import random
cards = (“Ace”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “Jack”, “Queen”, “King”)
print(card_picked)
a) Hearts Ace
b) Diamonds of Queen
c) 7 of King
d) Jack of Clubs
d) Jack of Clubs
46. Study the manual entry for the split() method given below.
Return a list of the words in the string, using sep as the delimiter string. If maxsplit is
given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1
elements). If maxsplit is not specified or -1, all possible splits are made.
parts = string.split(“::”, 2)
print(parts)
17/21
47. What will be the output of this program?
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
c) 6 6
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
18/21
c) bc
d) bccc
b) abc
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]
a) [101112][SML]1234
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:
Nagesh Rao,12342,,85,92,96
Ravi Gulati,43234,,76,97,81
import csv
print(“Enter your name and roll number to see your subject marks…\n”)
line_count = 0
match_found = False
19/21
line_count += 1
if line_count == 1:
(2)
else:
match_found = True
eng_marks = row[3]
maths_marks = row[4]
cs_marks = row[5]
(3)
if match_found == False:
print (“\nNo matching name and roll number found. Please check and re-enter”)
a) w
b) a
c) wb
51. What statements, respectively, should come in blanks 2 and 3? Choose the BEST answer.
a) bytes
20/21
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?
d) <empty string>
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
21/21