Study Material Term-I XII (CS) 2021-22-0
Study Material Term-I XII (CS) 2021-22-0
Study Material Term-I XII (CS) 2021-22-0
CHANDIGARH REGION
Flip Book
SESSION 2021-2022
1
Guidance & Support by:
Dr. P. Devakumar
Deputy Commissioner, KVS (RO) Chandigarh
Ms. T. Rukmani
Assistant Commissioner, KVS (RO) Chandigarh
Subject Coordinator:
Sh. Sanjay Kumar Misra
Principal, Kendriya Vidyalaya 3 BRD AFS Chandigarh
2
INDEX
S.NO TOPIC PAGE NO.
4. Answer Key 21
(Part- I)
8. Answer key 34
(Part- II)
3
PYTHON REVISION TOUR
Q.NO. QUESTIONS
1. Which of the following is not considered a valid identifier in Python ?
print(“100+200”)
x = 123
for i in x:
print(i)
(i) 123 (ii) 1 2 3 (iii) error (iv) infinite
L = 1, 23, ‘hello’,1
AL=[1,2,3,4,5]
Which of the following statement/statements removes the middle element 3 from it so that the
list AL equals [1,2,4,5] ?
10. Identify the invalid logical operator in Python from the following.
4
11. Suppose a tuple T is declared as T = (10, 12, 43, 39), which of the following is Incorrect?
Lst = [1,2,3,4,5,6,8]
code?
name="Computer_Science_with_Python"
print(name[-25:10])
(i) puter_S (ii) hon (iii) puter_Science
14. Can tuple be nested?
19. If the following code is executed, what will be the output of the following code?
Title="Online Teaching 2020"
print(Title[6:10], Title[-2:-4:-1])
(a) Tea 20 (b) Error (c) e Tea 02
20. Write the output of the following python expression:
print((4>5) and (2!=1) or (4<9))
(i) False (ii) True
21. Find the operator which cannot be used with a string in Python from the following:
5
22. Which of the following is not a keyword?
(a) eval
(b) assert
(c) nonlocal
(d) pass
a) Error
b) None
c) 25
d) 2
6
30 To add a new element to a list we use which command?
a) list1.add(5)
b) list1.append(5)
c) list1.addLast(5)
d) list1.addEnd(5)
33 Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.extend ([34, 5])?
a) [3, 4, 5, 20, 5, 25, 1, 3, 34, 5]
b) [1, 3, 3, 4, 5, 5, 20, 25, 34, 5]
c) [25, 20, 5, 5, 4, 3, 3, 1, 34, 5]
d) [1, 3, 4, 5, 20, 5, 25, 3, 34, 5]
34 Suppose listEx is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listEx.pop() ?
a) [3, 4, 5, 20, 5, 25, 1]
b) [1, 3, 3, 4, 5, 5, 20, 25]
c) [3, 5, 20, 5, 25, 1, 3]
d) [1, 3, 4, 5, 20, 5, 25]
35 Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john” what command do we use?
a) d.delete(“john”:40)
b) d.delete(“john”)
c) del d[“john”]
d) del d(“john”:40)
36 Which of the following is not a declaration of the dictionary?
a) {1: ‘A’, 2: ‘B’}
b) dict([[1,”A”],[2,”B”]])
c) {1,”A”,2”B”}
d) { }
37 Which of the following isn’t true about dictionary keys?
a) More than one key isn’t allowed
b) Keys must be immutable
c) Keys must be integers
d) When duplicate keys encountered, the last assignment wins
7
38 What will be the output of the following Python code?
a={}
a[2]=1
a[1]=[2,3,4]
print(a[1][1])
a) [2,3,4]
b) 3
c) 2
d) An exception is thrown
8
45 What will be the output of the following Python code?
print(“abc DEF”.capitalize())
a) abc def
b) ABC DEF
c) Abc def
d) Abc Def
9
PYTHON REVISION TOUR
ANSWER KEY
Q.NO. QUESTIONS
x = 123
for i in x:
print(i)
(i) 123 (ii) 1 2 3 (iii) error (iv) infinite
L = 1, 23, ‘hello’,1
( a) list (b) dictionary (c) array (d) tuple
Ans (d) tuple
Ans (i) 27
AL=[1,2,3,4,5]
Which of the following statement/statements removes the middle element 3 from it so that the list
AL equals [1,2,4,5] ?
10
8. Find the valid identifier from the following
10. Identify the invalid logical operator in Python from the following.
11. Suppose a tuple T is declared as T = (10, 12, 43, 39), which of the following is Incorrect?
Lst = [1,2,3,4,5,6,8]
13. If the following code is executed, what will be the output of the following
code?
name="Computer_Science_with_Python"
print(name[-25:10])
11
16. ………………method of list is used to delete a given element from the list.
19. If the following code is executed, what will be the output of the following code?
21. Find the operator which cannot be used with a string in Python from the following:
Ans (d) //
12
24. The operator _____ tells if an element is present in a sequence or not.
(a) exists
(b) in
(c) into
(d) inside
Ans (b ) in
Ans (c ) immutable
Ans b
31 To insert 5 to the third position in list1, we use which command?
a) list1.insert(3, 5)
b) list1.insert(2, 5)
c) list1.add(3, 5)
d) list1.append(3, 5)
Ans b
13
32 Suppose list1 is [3, 4, 5, 20, 5], what is list1.index(5)?
a) 0
b) 1
c) 4
d) 2
Ans d
Ans d
33 Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.extend([34, 5])?
a) [3, 4, 5, 20, 5, 25, 1, 3, 34, 5]
b) [1, 3, 3, 4, 5, 5, 20, 25, 34, 5]
c) [25, 20, 5, 5, 4, 3, 3, 1, 34, 5]
d) [1, 3, 4, 5, 20, 5, 25, 3, 34, 5]
Ans a
34 Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.pop()?
a) [3, 4, 5, 20, 5, 25, 1]
b) [1, 3, 3, 4, 5, 5, 20, 25]
c) [3, 5, 20, 5, 25, 1, 3]
d) [1, 3, 4, 5, 20, 5, 25]
Ans a
35 Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john” what command do we use?
a) d.delete(“john”:40) b) d.delete(“john”)
c) del d[“john”] d) del d(“john”:40)
Ans c
Ans c
14
38 What will be the output of the following Python code?
a={}
a[2]=1
a[1]=[2,3,4]
print(a[1][1])
a) [2,3,4] b) 3
c) 2 d) An exception is thrown
Ans b
Ans b
Ans c
Ans c
Ans a
Ans a
Ans a
print(‘abcdef12’.replace(‘cd’,’12’))
a) ab12ef12 b) abcdef12
c) ab12efcd d) none of the mentioned
Ans a
Ans D
16
MCQs ON FUNCTIONS IN PYTHON
SNO QUESTIONs
1 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))
2 Which of the following is the use of function in python?
b=20
def change():
global b
a=45
b=56
change()
print(a)
print(b)
a)10 and 56 b)45 and 56
c)10 and 20 d)Syntax Error
6 How are variable length arguments specified in the function heading?
17
8 What is the output of the code shown below?
x=100
def f1():
global x
x=90
def f2():
global x
x=80
print(x)
a) 100 b) 90 c) 80 d) Error
9 What is the value returned by math.floor(3.4)?
a) 3 b) 4 c) 4.0 d) 3.0
a) 6 b) 9
c) error, third argument required d) error, too many arguments
11 Study the following program and select the possible outputs(s) from the option (i) to (iv) following
it.
import random
x=random.random()
y=random.randint(0,4)
Print(int(x0, “ :”, y+int(x))
(i) 0:0 (ii)1 : 6
(ii) 2:4 (iv) 0 : 3
12 If return statement is not used inside the function, the function will return :
a) 0 b) None object
b) An arbitrary iinteger d) error
13 Which of the following keywords marks the beginning of the function?
a) fun b) function
c) def d) definition
14 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)
18
16 What is a variable defined inside a function referred to as?
a) A static variable
b) A global variable
c) A local variable
d) An automatic variable
17 Which of the given argument types can be skipped form a function call?
a) Positional arguments
b) Keyword arguments
c) Named arguments
d) Default arguments
18 A name inside the parentheses of a function header that can receive a value
a) Variable
b) Parameter
c) Argument
d) Keyword
19 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
20 What is the value of Y in the above program?
a) 10 b) 12
b) 7 d) None of the above
21 What is the value of x in the above program?
a) 10 b) 12
b) 9 d) None of the above
22 What is the value of z in the above program?
a) 10 b) 12
b) 7 d) None of the above
23 In Python which is the correct method to load a module math?
a) include math b) import math
c) using math d) from math
19
24 Observe the code carefully and write answer of questions given below:
a=10
def F():
a=5
b=20
print(a)
print(a)
What is the output of the above code?
a) 5 b) 10
b) 20 d) None of the above
25 Observe the code carefully and write answer of questions given below
def sum(a=5,b=6,c=9):
print(a,b,c)
sum(1,2,3)
What is the output of the above code?
a) 123 b) 569
b) 6812 d) None of the above
20
Answer Key
Q. NO ANS Q. NO ANS Q.NO ANS
2 A 12 B 22 C
3 D 13 C 23 B
4 B 14 C 24 B
5 A 15 D 25 A
6 A 16 C 26
7 C 17 D 27
8 A 18 B 28
9 A 19 A 29
10 B 20 B 30
21
Q.NO. QUESTIONS
I. Inbuilt
II. Library
III. User defined
IV. logical
2 Division of large programs into smaller programs. These smaller programs are called________.
a) Functions
b) Operators
c) logical programs
d) specific programs
3 Inbuilt functions makes the programming ________
a) Difficult b) Easy
4 We have to import a specific module for using built-in functions?
a) True b) False
5 A function can be called _____ times in a program.
a) 3
b) 7
c) 4
d) Any no of times
6 Which of the following is not a type conversion functions?
a) Int()
b) Str()
c) Input()
d) Float()
7 Write the output of the following.
print(int())
a) Any random number
b) 0
c) error
8 Which of the following statement will return error?
print(int("a"))
print(str(123))
a) statement1 b) statement2
c).both d) none
9 Write the output of the following:
print(float())
a) 1.0
b) 0.0
c) Any floating point number
d) zero
22
10 What type of error is returned by following statement?
print(int("a"))
a) Syntax Error
b) Value Error
c) Type Error
11 Write the output of the following: if user entered 7
a=input("Enter age")
print(type(a))
a) str
b) int
c) float
12 eval(x) function returns error if x is integer.(T/F)
a) False b) True
13 Write the output of the following:
print(max(2,5,4,8,29,24))
a) Hell
b) he
c) Hello
d) Error
15 abs( ) function do not work for float number.(T/F)
a) False
b) True
16 Write the output of the following.
print(abs(-8))
a) -8
b) 8.0
c) 8
23
a) 34.57
34.36
b) 34.56
34.37
c) 34.57
35.36
d) 36.00
34.36
19 Write the output of the following:
print(list(i*2 for i in range(4)))
print(list(range(4)))
a) [0, 2, 4, 6]
[3]
b) [0, 2, 4, 6]
[0, 1, 2, 3]
c) [0, 1, 2, 3]
[0, 1, 2, 3]
d) none
20 Write the output of the following:
print((range(5)))
a) range(0, 5)
b) 0, 1, 2, 3, 4
c) [0, 1, 2, 3, 4]
d) None
21 The process of dividing a computer program into separate independent blocks of code with
different names and specific functionalities is known as ______________.
a) Mode Programming
b) Modular Programming
c) Division Programming-
d) None
22 Advantage of using function is/are
a) Reusability of Code
b) Divide a complex problem into simpler ones
c) Reduces chances of error
d) All of the above
23 _____________ can be defined as a named group of instructions that accomplish a specific task
when it is invoked.
a) Function
b) Variable
c) Global Variable
d) None
24 A function can be called ________ from anywhere in the program.
a) Single time
b) Two times
c) Repeatedly
d) None
24
25 What is the purpose of following statement?
def check():
a) Calling a function
b) Defining a function
c) None of the above
d) Both of the above
26 Use of functions in a program makes it __________
a) more organized
b) easy to read
c) Both
d) Noner
27 Functions which can be used directly in a program are stored in ________ library.
a) Standard
b) Main
c) Sub
d) Union
28 A function defined to achieve some task as per the programmer's requirement is called a
__________
a) Standard function
b) User defined Function
c) Built in Function
d) None of the above
29 A function definition begins with ___________
a) def
b) define
c) create
d) New
30 Function header always ends with a __________
a) Comma
b) Colon
c) Semicolon
d) period
25
Q.NO. Solution
1 Functions created by user is called _________ function
a) Inbuilt
b) Library
c) User defined
d) logical
2 Division of large programs into smaller programs. These smaller programs are
called________.
a) Functions
b) Operators
c) logical programs
d) specific programs
3 Inbuilt functions makes the programming ________
a) Difficult
b) Easy
4 We have to import a specific module for using built-in functions?
a) True
b) False
5 A function can be called _____ times in a program.
a) 3
b) 7
c) 4
d) Any no of times
6 Which of the following is not a type conversion functions?
a) Int()
b) Str()
c) Input()
d) Float()
7 Write the output of the following.
print(int())
26
9 Write the output of the following:
print(float())
a) 1.0
b) 0.0
c) Any floating point number
d) zero
10 What type of error is returned by following statement?
print(int("a"))
a) Syntax Error
b) Value Error
c) Type Error
a) str
b) int
c) float
12 eval(x) function returns error if x is integer.(T/F)
a) False b) True
13 Write the output of the following:
print(max(2,5,4,8,29,24))
a) Hell
b) he
c) Hello
d) Error
15 abs( ) function do not work for float number.(T/F)
a) False
b) True
16 Write the output of the following.
print(abs(-8))
a) -8
b) 8.0
c) 8
27
17 len() function returns the length of string only.(T/F)
a) False b) True
18 Write the output of the following :
print(round(34.5671,2))
print(round(34.3631,2))
a) 34.57
34.36
b) 34.56
34.37
c) 34.57
35.36
d) 36.00
34.36
19 Write the output of the following:
print(list(i*2 for i in range(4)))
print(list(range(4)))
a) [0, 2, 4, 6]
[3]
b) [0, 2, 4, 6]
[0, 1, 2, 3]
c) [0, 1, 2, 3]
[0, 1, 2, 3]
d) none
20 Write the output of the following:
print((range(5)))
a) range(0, 5)
b) 0, 1, 2, 3, 4
c) [0, 1, 2, 3, 4]
d) None
21 The process of dividing a computer program into separate independent blocks of code with
different names and specific functionalities is known as ______________.
a) Mode Programming
b) Modular Programming
c) Division Programming-
d) None
a) Reusability of Code
b) Divide a complex problem into simpler ones
c) Reduces chances of error
d) All of the above
28
23 _____________ can be defined as a named group of instructions that accomplish a specific
task when it is invoked.
a) Function
b) Variable
c) Global Variable
d) None
24 A function can be called ________ from anywhere in the program.
a) Single time
b) Two times
c) Repeatedly
d) None
25 What is the purpose of following statement?
def check():
a) Calling a function
b) Defining a function
c) None of the above
d) Both of the above
26 Use of functions in a program makes it __________
a) more organized
b) easy to read
c) Both
d) None
27 Functions which can be used directly in a program are stored in ________ library.
a) Standard
b) Main
c) Sub
d) Union
28 A function defined to achieve some task as per the programmer's requirement is called a
__________
a) Standard function
b) User defined Function
c) Built in Function
d) None of the above
29 A function definition begins with ___________
a) def
b) define
c) create
d) New
30 Function header always ends with a __________
a) Comma
b) Colon
c) Semicolon
d) period
29
Data File Handling
SNO QUESTIONs
1. Which of the following functions do you use to write data in the binary format?
A. write
B. output
C. dump
D. send
2. Which of the following file mode will refer to the BINARY mode?
A. binary
B. b
C. bin
D. w
3. The process of converting the structure to a byte stream before writing to the file is known as
______.
a. Pickling
b. Unpickling
c. Dump
d. load
4. The _____ file mode is used when user want to write data into binary file.
a. rb
b. wb
c. r+
d. w+
5. Which one of the following is correct statement?
a) import – pickle b) pickle import
c) import pickle d) All of the above
6. Ms. Sejal is working on the sports.dat file but she is confused about how to read data from the
binary file. Suggest a suitable line for her to fulfill her wish.
a. load()
b. data=f1.load()
c. f1.load(data)
d. data=load(f1)
7 Name the function to read from CSV file.
a. read()
b. csv.reader()
c. csv.read()
d. reader()
30
8 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
9. Priti of class 12 is writing a program to create a CSV file “emp.csv”. She has written the following
code to read the content of file emp.csv and display the employee record whose name begins from
“S‟ also show no. of employee with first letter “S‟ out of total record. As a programmer, help her to
successfully execute the given task.
Consider the following CSV file (emp.csv):
1,Peter,3500
2,Scott,4000
3,Harry,5000
4,Michael,2500
5,Sam,4200
a. pickle
b. csv
c. binary
d. None of the above
b) In above program, choose correct answer for part (b)
a. read mode
b. append mode
c. write mode
d. None of the above
31
c) In above program, choose correct answer for part (c)
a. 'emp.csv'
b. 'emp'
c. 'empcsv'
d. None of the above
d) In above program, choose correct answer for part (d)
a. read
b. writer
c. reader
d. None of the above
e) In above program, choose correct answer for part (e)
a. 2,Scott,4000
5,Sam,4200
b. 2Scott4000
5Sam4200
c. 2,Scott,4000
5,Sam,4200
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. -1 b. 2 c. 0 d. 1
e) In above program , choose correct option for Line 5
a. csv.reader()
b. csv.read()
c. csv.writer()
d. csv.write()
16 In csv files, each field of record is separated by
a. comma
b. colon
c. space
d. None of the above
17 What is true about Binary files
Answer Key
Q. NO ANS Q. NO ANS
1 c 10 (a) a
2 b 10(b) b
3 a 10(c) c
4 b 10(d) d
5 c 10(e) a
6 d 11 a
7 b 12 c
8 b 13 a
9(a) a 14 c
9(b) a 15 a,c
9(c) a 16 a
9(d) c 17 d
9(e) a
34
Q No Question
1 To open a file Myfile.txt ,which is stored at d:\Myfolder, for WRITING , we can use
(i) F=open("d:\Myfolder\Myfile.txt","w")
(ii) F=open(file="d:\Myfolder\Myfile.txt","w")
(iii) F=open(r"d:\Myfolder\Myfile.txt","w")
(iv)F=open("d:\Myfolder\\Myfile.txt","w")
2 If we do not specify file mode while opening a file, the file will open in .............mode
(i) read
(ii) write
(iii) append
(iv)Error occurs
3 To read the remaining lines of the file from a file object F, we use
(i) F.read(2)
(ii)F.read()
(iii) F.readlines()
(iv)F.readline()
(i) fp.seek()
(ii) fp.tell()
(iii) fp.loc()
(iv) fp.pos()
(i) EOL
(ii) END
(iii) Full stop
(iv) EOF
6 In python, default EOL character is _______
(i) \n
(ii) \r
(iii) \d
(iv) \L
35
7 What error is returned by following statement, if file “try.txt” does not exist?
f = open(“try.txt”)
(i) Not found
(ii) FileNotFoundError
(iii) File does not exist
(iv) No error
8 What is the purpose of ‘r’ as prefix in the given statement?
f = open(r “d:\color\flower.txt”)
(i) To make it raw string
(ii) To make it new string
(iii) To make it relative string
(iv) To make it reverse string
9 Another name of file object is _______
(i) Pickle
(ii) CSV
(iii) String
(iv) No module required
11 To open a file Myfile.txt for appending , we can use
(i) F=open("Myfile.txt","a")
(ii) F=open(“Myfile.txt","w+")
(iii) F=open(r"d:\Myfolder\Myfile.txt","w")
(iv)F=open("Myfile.txt","w")
36
14 The readlines() method returns
(i) String
(ii) A List of integers
(iii) A list of characters
(iv) A List of Lines
15 The read() method returns
(i) String
(ii) A List of integers
(iii) A list of characters
(iv) A List of Lines
16 f=None
for i in range(5):
with open("data.txt","w") as f:
if(i>2):
break
print(f.closed)
(i) True
(ii) False
(iii) None
(iv) Error
17 Which method is used to write List of strings in the File
(i) Write
(ii) Writelines
(iii) WriteList
(iv) Write(n)
18 Which method is used to break the link of file object and the file on the disk.
(i) Open
(ii) Close
(iii) Break
(iv) end
19 Which types of files stores information in the form of a stream of ASCII or Unicode Characters
(i) open
(ii) close
(iii) flush
(iv) write
22 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()
23 Choose the name of missing function
file1=open("notes.txt","a")
ch=input("enter the text")
file1.________(ch+"\n")
file1.close()
(i) writelines
(ii) write
(iii) read
(iv) append
24 Choose the correct option for mode
file1=open("notes.txt",______)
ch=file1.read()
print(ch)
file1.close()
(i) w
(ii) a
(iii) r
(iv) All of the above
25 Data=F.read(10).
1 (iii) F=open(r"d:\Myfolder\Myfile.txt","w")
2 (i) read
3 (iii) F.readlines()
4 (ii) fp.tell()
5 (i) EOL
6 (i) \n
7 (ii) FileNotFoundError
11 (i) F=open("Myfile.txt","a")
12 (i) F.read(2)
13 (iv)F.readline()
15 (i) String
16 (i) True
17 (ii) Writelines
18 (ii) Close
20 (iii) w+
21 (iii) flush
22 (ii) s. split()
23 (ii) writ
24 (iii) r
39