ANSWER_KEY_ComputerScience_12 pb 1
ANSWER_KEY_ComputerScience_12 pb 1
SESSION : 2020-21
SET-1
SUBJECT – COMPUTER SCIENCE (ANSWER KEY)
General Instructions:
Answer: 7_class
Answer:
(i) ''
(ii) 4
(iii) True
(iv) 7
(c) Determine the size of following constants in python 2
(i) '\b'
(ii) "it's"
(iii) "ab\
cd"
(iv) """ab
cd"""
Answer:
(i) 1
(ii) 4
(iii) 4
(iv) 5
(d) What is the data type of the following object L ? 1
L = 2 , 72 , 'python' , 9, 8.0
Answer: Tuple
(f) The following code does not give correct output. If 40 is given as input, it should 2
give 120 as output. What is the reason for incorrect output? Correct the code.
Corrected code:
num=int(input("Enter the number:"))
tri=num*3
print(tri)
Immutable means unchangeable. Immutable types are those whose value cannot be
changed in place. If a new value is assigns to such variable, variable’s reference is
changed, not the previous value
print(int("5"+"9"))
Answer: 59
Q2 (a) Write a program that reads a string and checks whether the string is a palindrome 4
or not.
a=str(1234)
b="python"*3
print(a,b)
a="hello"+"world"
b=len(a)
print(b,a)
Output:
1234 pythonpythonpython
10 helloworld
[ 1 mark for 1234 pythonpythonpython,1 mark for 10 (finding length), 1 mark for
helloworld (concatenation)
(c) What will be the output of: 2
i) 12/4
ii) 13//3
iii) 14%4
iv) 13.0//4
Answers
i) 3.0
ii) 4
iii) 2
iv) 3.0
dict1={}
dict1[(1,5,6)]=10
dict1[(6,2,5)]=3
dict1[(1,2)]=2
sum=0
for i in dict1:
sum+=dict1[i]
print(sum)
print(dict1)
Output:
15
{(1, 5, 6): 10, (6, 2, 5): 3, (1, 2): 2}
(e) Does the following code snippet result in error? If so, identify the cause. 1
name="Rahul"
print("GoodMorning")
print("Hai",name,end="@@")
print("see you")
(f) Which among the following operators holds the highest precedence ? 1
*, | , + , - ,**, << , >>, %
Answer: **
Text="Python ForMe"
length=len(Text)
newText=""
for i in range(0,length):
if Text[i].isupper():
newText+=Text[i].lower()
elif Text[i].isalpha():
newText+=Text[i].upper()
else:
newText+="ccc"
print(newText)
Output:
pYTHONcccfORmE
language="Python."
print("I love",language,sep="###",end="")
print("Python is easy")
Answer:
I love###Python.Python is easy
(i) Observe the following Python code very carefully and rewrite it after removing all 2
errors with each correction underlined:
DEF execmain():
x = int(input("Enter a number:"))
if(abs(x)= x):
print("You entered a positive number")
else:
x =* -1
print("Number made positive:"x)
execmain()
Corrected code:
def execmain():
x = int(input("Enter a number:"))
if(abs(x)== x):
print("You entered a positive number")
else:
x *= -1
print("Number made positive:",x)
execmain()
Q3 (a) def add(a,b): 2
c=a+b
print("Sum is",c)
x=20
y=40
add(x,y)
Answer:
(i) Arguments : x,y
(ii) Parameters : a,b
(b) Will the following code to calculate area result in error? If yes, identify the reason 2
for error.
def area(length,breadth):
a=length*breadth
return ar
L=int(input("Enter the length:"))
B=int(input("Enter the breadth:"))
Area=area(L,B)
print("Area of the rectangle is ",ar)
Answer:
Yes, the code will result in error
It is because variable ar has local scope in the function area, it cannot be accessed
outside
(c) Write a program (using function) to calculate the sum and average of n numbers. 4
Read the numbers from user and pass the numbers to the function as a List and
return multiple values ( i.e. sum and average) from function to the invoking
program to display the result.
Answer:
def SumAverage(mylist):
sum=0
for i in mylist:
sum+=i
avg=sum/len(mylist)
return sum,avg
mylist=[]
n=int(input("Enter the count : "))
for i in range(n):
num=int(input("Enter the number : "))
mylist.append(num)
print(mylist)
sum1,avg1=SumAverage(mylist)
print("Sum = ",sum1," Average = ",avg1)
(d) Predict the output of following code 3
def add(x,y,z):
print(x+y+z)
def product(x,y,z):
return(x*y*z)
a=add(3,4,5)
b=product(3,4,5)
print(a,b)
Output:
12
None 60
def hello(name="Ramesh"):
print("Hello", name,"Goodmorning")
hello("Neha")
Answer:
Hello Neha Goodmorning
def changeval(num):
print(num)
num+=10
print(num)
myval=100
print(myval)
changeval(myval)
print(myval)
Output:
100
100
110
100
Mark Distribution
[100 - ½ mark
100 - ½ mark
110 - 1 mark
100 - 1 mark]
(h) What is the output of the following code: 1
a=1
def f():
a = 10
print(a)
Answer:
1
Note: a=10 will not be executed as function f() is not called
Q4 (a) Name the file mode that opens a text file for read and write purpose 1
Answer:
r+
(b) Differentiate between readlines() and read() 1
readlines() reads the file in read mode and it returns a list of all lines in the file,
where as read() reads the file in read mode and returns entire file content as a
string
(c) Write the code to print only the last line of a text file “mybook.txt” 2
Answer:
f=open("mybook.txt")
lines=f.readlines()
print("Last line of file is : ",lines[-1])
(d) If the file book.txt contains “goodmorning” before execution of above code, what 1
will be the content of file after execution?
f=open("book.txt","w")
f.write("hello")
f.close()
Answer:
hello
(e) Consider the file mybook1.txt contains the following text 3
I love python
It is my favourite subject
Output:
I lov
e py
thon
It is my favourite subject
(f) Name the function used to force transfer data from buffer to file? 1
flush()
(g) Quote two differences between text files and binary files 2
Answer:
1. Text file stores information in ASCII OR UNICODE character.
Binary file stores the information in the same format as in the memory .
2. In text file each like is terminated by special character called EOL(‘\n’
or ‘\r’ or combination of both). In binary file there is no such delimiter for a
line.
(h) Name the module to be imported in the python program to use the standard 1
input(stdin), output(stdout) and error(stderr) streams
Answer:
sys
(i) Hima has placed the file mark.xls inside a folder named exam, and the exam folder 2
is placed inside folder main. The folder main contains one more subfolder
attendance other than exam. The folder main is kept in D drive. If her current
working directory is attendance, determine
(i) Absolute path to mark.xls
(ii) Relative path to mark.xls
Answer:
(i) D:\\main\\exam\\mark.xls
(ii) ..\\exam\\mark.xls
(j) What is the opening position of file pointer if a file is opened in following modes? 2
(i) ab+
(ii) rb
(iii) w+
(iv) r+
(i) ab+ -- at the end of file if file exists, otherwise creates a new file
(ii) rb -- at the beginning of file
(iii) w+ -- at the beginning of file ( if file exists, overwrites it)
(iv) r+ -- at the beginning of file
(k) Name two functions provided by pickle module in python for binary file 1
operations(write and read)
Answer:
dump() for write, load() for read