0% found this document useful (0 votes)
12 views10 pages

ANSWER_KEY_ComputerScience_12 pb 1

This document is an answer key for the Mid Term Examination in Computer Science for Class XII, covering various topics related to Python programming. It includes questions on identifiers, data types, expressions, code predictions, and file handling, along with their respective answers. The document serves as a guide for evaluating student responses in the examination.

Uploaded by

19thbatchforever
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views10 pages

ANSWER_KEY_ComputerScience_12 pb 1

This document is an answer key for the Mid Term Examination in Computer Science for Class XII, covering various topics related to Python programming. It includes questions on identifiers, data types, expressions, code predictions, and file handling, along with their respective answers. The document serves as a guide for evaluating student responses in the examination.

Uploaded by

19thbatchforever
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

NAVODAYA VIDYALAYA SAMITI

MID TERM EXAMINATION

SESSION : 2020-21
SET-1
SUBJECT – COMPUTER SCIENCE (ANSWER KEY)

TIME ALLOWED : 3 Hrs CLASS : XII MM : 70

General Instructions:

 All questions are compulsory.


 Read the questions very carefully before writing the answers.
 Write question number of each question
 Programming Language –Python

Q1 (a) Which of the following is invalid name for an identifier? 1


(i) Class_7
(ii) 7_class
(iii) class_Seven
(iii) _7

Answer: 7_class

(b) Predict the value of following expressions 2

(i) "navodaya" and ""


(ii) 4 or 0.0
(iii) (4==4) or (7==9)
(iv) (5+2) or (8-2)

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

(e) Predict the output of following code 2


x, y = 7,2
x, y, x = x+1,y+4,x+20
print("x =", x,"y =",y)

Answer: x = 27 y = 6 (1 mark each for value of x and y)

(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.

num=input("Enter the number:")


tri=num*3
print(tri)

Reason for incorrect output:


Input function returns the value it has read as string type

Corrected code:
num=int(input("Enter the number:"))
tri=num*3
print(tri)

[ 1 mark for reason and 1 mark for corrected code]


(g) What do you understand by the term ‘immutable’? Give two examples of 2
immutable data type in python.

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

Example: Integer, String

[ 1 mark for explanation and one mark for examples]


(h) What is the output of following code? 1

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.

string=input("Enter a string :")


l=len(string)
mid=l//2
rev=-1
for a in range(mid):
if string[a]==string[rev]:
a+=1
rev-=1
else:
print(string,"is not a palindrome")
break
else:
print(string, "is a palindrome")
(b) Predict the output of following code 3

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

(d) Predict the output of following code snippet 2

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")

Yes. Code will result in error


It is due to wrong indentation on third and fourth lines

(f) Which among the following operators holds the highest precedence ? 1
*, | , + , - ,**, << , >>, %

Answer: **

(g) Predict the output of following code 3

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

[ 1 mark for converting to uppercase, 1 mark for converting to lowercase, 1 mark


for inserting ‘ccc’ ]

(h) Predict the output of following code 2

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)

Identify the following in the above code:


(i) Function Arguments
(ii) Function Parameters

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

[ 1 mark each for predicting values 12,None,60)


(e) What is the difference between local variable and global variable? Illustrate with 4
an example program and list the local and global variables in it.
Answer:
A local variable is a variable declared with in a function or within a block. It is
accessible only within the function/block in which it is declared. A global variable
is a variable declared outside all functions, and is accessible throughout the
program (1 mark)
Any example program containing local and global variables (2 marks)
Listing local and global variables ( ½ marks each)
(f) Consider the following function 1

def hello(name="Ramesh"):
print("Hello", name,"Goodmorning")

What is the ouput if the function is invoked as

hello("Neha")
Answer:
Hello Neha Goodmorning

(g) Predict the output of following code 3

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

Predict the output of following code:


myfile1=open("mybook1.txt","r")
str1=myfile1.read(5)
str2=myfile1.read(4)
str3=myfile1.read()
print(str1)
print(str2)
print(str3)
myfile1.close()

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

You might also like