Sample Question

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

COMPUTER SCIENCE

PYTHON – SAMPLE QUESTION PAPER

I. Objective type questions: [30x1=10]


1. What is the value of x?
>>>x = int (8/3+5)
a) 7.3333 b) 1 c) 7 d) 1.0

2. What data type is the object below?


Val=1, ‘start’, 2, ‘end’
a) List b) tuple c) dictionary d) string

3. Which two operators can be used on string values in Python?


a) % b) * c) + d) //

4. What is the value of this expression?


>>>4*3**2
a) 144 b) 108 c) 36 d) 24

5. Which of the following statement prints the shown output below?


c:\newfolder\python\test.py
a) print(“c:\newfolder \python\test.py”)
b) print(“c:\\newfolder\python\\test.py”)
c) print(“c:”\” newfolder”\”python”\”test.py”)
d) print(“c:\” newfolder \”python\test.py”)

6. What is the output of the following code?


>>>print(“Good”, “Morning”)
a) GoodMorning b) Good Morning c) “GoodMorning “ d) Good morning

7. What will be the output of following code?


>>>a = [[[4,5],[1,0],9],[6,7]]
>>>a[0][1][1]
a) 1 b) 4 c) 0 d) 7

8. What will be the output of following code?


>>> s= [6,5,4,7,8,5]
>>>a.remove(5)
>>>a
a) [6,4,7,8] b) [6,5,4,7,8] c) [6,4,7,8,5] d) [6,5,4,7,8,5]
9. What will be the output of following program?
>>>my_list = ['p', 'r', 'o', 'b', 'l', 'e', 'm']
>>>print('p' not in my_list)
a) True b) False c) ['p', 'r', 'o', 'b', 'l', 'e', 'm'] d) ‘p’

10. What will be the output of following program?


>>>dict={"Sania":1,"Priya":2}
>>>dict.update({"Bhavya":2})
>>>dict
a) {' Sania ': 1, ' Priya ': 2} b) {' Sania ': 1, ' Bhavya ': 2}
c) {' Sania ': 1, ' Priya ': 2, ' Bhavya ': 2} c) {' Sania ': 2, ' Bhavya ': 2}

11. What will be the output of following program:


my_list = ('a', 'b', 'c', 'b', 'e', 'f', 'g')
my_list*3+= ‘d’
print(my_list)
a) ('a', 'b', 'c', 'b', 'e', 'f', 'g') b) ('a', 'b', 'c', 'd', 'e', 'f', 'g')
c) ['a', 'b', 'c', 'd', 'e', 'f', 'g'] c) Error

12. What will be the output of following program?


>>>empl1={"salary":1000, "dept":"Sales", "Age":25, "name":"John"}
>>>empl1.get("dept")
Sales

13. Write statement to call the function.


def sum(p, q):
r=p+q
print(r)
_____________ # Function call statement
sum(10,20)

14. Which of the following is the use of function in python?


a) Functions are reusable pieces of programs
b) Functions don’t provide better modularity for your application
c) you can’t create your own functions
d) All of the mentioned
15. Which line number code will never be executed?
def check(num): #Line 1
if num%5==0: #Line 2
print(“Hello”) #Line 3
return True #Line 4
print (“Bye”) #Line 5
else: #Line 6
return False #Line 7
c=check(26) #Line 8
print(c) #Line 9

Line 5, is unreachable, because it is after the return statement;


A function will end with ‘return’ statement and control will never reach this statement.

16. What are the two main types of functions?


a) Custom function
b) Built-in function & User defined function
c) User function
d) System function

17. What is called when a function is defined inside a class?


a) Module b) Class c) Another function d) Method

18. What is the output of below program?


def call(x):
return x , x+2, x+3
x = call(3)
print (x)
a) [3, 5, 6] b) 27 c) 3, 5, 6 d) (3, 5, 6)

19. Which of the following module functions generates an integer?


a) randint() b) uniform() c) random() d) all of these

20. What would be the output produced by the following code?


import math
import random
print (math.ceil ( random.random() )
a) 0 b) 0.00011 c) 1 d) it can be a) or b) or c)
21. __________ file must be a part of the folder to be used as a Python package.
__init__.py

22. Which of the following cannot be returned by random.randrange(5)?


a) 0 b) 3 c) 5 d) 2

23. Which function is used to write all the characters?


a) writeall() b) writefullline() c) write() d) writelines()

24. Which of the following command is used to open a file “g:\testfile.txt” in append-mode?
a) outfile = open(“g:/ testfile.txt”, “rb”)
b) outfile = open(“g:\\ testfile.txt”, “rw”)
c) outfile = open(“g:\ testfile.txt”, “w+”)
d) outfile = open(“g:\\ testfile.txt”, “a”)

25. Name the python library for the following functions:


(i) sin() (ii) dump()
Ans
(i) sin() – math module
(ii) dump() – pickle module

26. The default file open mode is ___________ mode.


Read mode

27. Stack is a data structure that follows __________ order.


a) FILO b) LIFO c) FIFO d) both a) & b)

28. The Enqueue operation adds item at the __________ end of the queue.
a) front b) rear c) both d) none

29. Choose the correct output for the following sequence of operation (bold signifies top).
push(6), push(4), pop, push(7), pop push(2)

a) 6 4 7 2 b) 6 2 c) 6 4 7 d) 6 7 2

30. Insertion and deletion operations in Queues are known as _________.


a) Push and Pop b) Enqueue and Dequeue
c) Insert and Delete d) None
II. Answer the following questions: [11x2=22]

1. What will be the output of following code?

x= 'ba'
y= 'na'
print ( x + y * 2 )
print ( ( y * 2 ) [ : 3 ] + x )

Output
banana
nanba

2. What will be the output of following program?

s = [None] * 10
print(s)

Output
[None, None, None, None, None, None, None, None, None, None]

3. What will be the output of following Python code?

x=10
y=0
while (x>y):
print(x,y)
x=x-1
y=y+1

Output
10 0
9 1
8 2
7 3
6 4

4. Differentiate between list and tuple.

List is mutable Tuple is immutable


List is enclosed in [] bracket Tuple is enclosed with () bracket
Eg: [1,2,3] Eg: (1,2,3)
5. Which line number of code(s) will not work and why?

def Interest(P,R,T=7):
I = (P*R*T)/100
print(I)

Interest ( 20000, 0.08, 15) # Line 1


Interest ( T=10, 20000, 0.075) # Line 2
Interest ( 50000, 0.07) # Line 3
Interest ( 80000, T=10) # Line 4

Output:
Line 2: Positional Argument should be before keyword argument
Line 4: Required positional argument (R) missing ; required argument cannot be missed

6. Convert the following expression into its equivalent postfix expression:


A*(B+(C+D)/G)*H

A B C D + G / +* H *

7. Rewrite the following code in python after removing all syntax error(s).
Underline each correction done in the code.
10=A
for S in range (0, A)
if S%2=0:
print (S*2)
Else:
print (S+3)

A=10
for S in range (0, A) :
if S%2==0:
print (S*2)
else:
print (S+3)
8. What is Default argument in function? Illustrate with example.
A parameter having default value in the function header is known as a default
parameter.
Eg:
def sq(a=5):
s=a*a
return s
sqof5=sq()

9. Write a code to write data and read the same in a binary file using pickle module.
import pickle
f=open('new.bin', 'wb')
text = 'hai'
pickle.dump(text, f)
f.close()

f=open('new.bin', 'rb')
text1 = pickle.load(f)
print(text1)
f.close()

10. What will be the output of following Python code?


d={'Name':'Raj','Age':17}
d2=d.copy()
print ( len (d2) + 2 * len (d) )
2 + 2*2
6

11. What is the difference between readline() and readlines() function?

readline() will read a line at a time


readlines() reads all the lines from the file and returns it in the form of a list
III. Answer the following questions: [6x3=18]

1. Find and write the output of following python code:


box={}
jars={}
crates={}
box['biscuit']=3
box['cake']=4
print (box)
jars['jam']=4
print (jars)
crates['box']=box
crates['jars']=jars
print(crates)

Output:
{'biscuit': 3, 'cake': 4}
{'jam': 4}
{'box': {'biscuit': 3, 'cake': 4}, 'jars': {'jam': 4}}

2. Write a function in Python to display the elements of list thrice if it is a number and
display the elements terminated with # if it is not a number.
For example, if the content of list is as follows:
List=*‘10’, ‘one’, ‘20’, ‘two’, ‘30’, ‘three’+

The output should be


101010
One#
202020
Two#
303030
Three#

Code:

' ' ' Python code to display the elements of list thrice if it is a number and display the
elements terminated with # if it is not a number. ' ' '
def disp(l):
for i in l:
if i.isdigit():
print(i*3)
else:
print(i+'#')
print()
List1=eval(input("Enter the list :"))
disp(List1)

Output
Enter the list :['10', 'one', '20', 'two', '30', 'three']
101010
one#
202020
two#
303030
three#
>>>

3. Write a function in python to count the number of lines in “POEM.txt” that begins with
Upper case character.

Python code:
''' To write a function in python to count the number of lines in “POEM.txt” that begins
with Upper case character '''
def disp():
n=0
file=open("C:\\Users\\admin\\Desktop\\Poem.txt",'r')
line=file.readlines()
for i in line:
if i[0].isupper():
n+=1
file.close()
return n
count=disp()
print(count)

Sample file Poem.txt:


The Queen of Hearts
she made some tarts,
All on a summer's day;
the Knave of Hearts
He stole those tarts,
And took them clean away.

Output:
4
>>>
OR

Write a function in python to read lines from file “POEM.txt” and count how many times
the word “INDIA” exists in file.

Python code:
''' To write a function in python to read lines from file “POEM.txt” and count how many
times the word “India” exists in file '''
def disp():
n=0
file=open("C:\\Users\\admin\\Desktop\\India.txt",'r')
line=file.readlines()
for i in line:
if 'India' in i:
n+=1
file.close()
return n
count=disp()
print(count)

Sample file:
India is my country.
India is a great country where people speak different languages.
India is full of different castes, creeds, religion, and cultures but they live together.
That’s the reasons India is famous for the common saying of “unity in diversity“.
India is the seventh-largest country in the whole world.

Output:
4
>>>
4. Read the given program and answer the questions:

import random
mylist = ['Red', 'Green', 'Blue', ‘Yellow’, ‘Orange’, ‘White’]
while True:
BEGIN = random.randint (1 , 3)
END = random.randint (2, 5)
for i in range (BEGIN, END+1):
print (mylist [i])
input()

a) What are the possible output(s)?


b) Specify the maximum values that can be assigned to BEGIN and END.

ANSWER
b) Maximum values assigned to Begin = 3 (1, 2, 3)
Maximum values assigned to End = 5 (2, 3, 4, 5)

a)
Possible outputs:
Output (1, 2)
Green
Blue
>>>

Output (1, 3)
Green
Blue
Yellow
>>>

Output (1, 4)
Green
Blue
Yellow
Orange
>>>

Output (1, 5)
Green
Blue
Yellow
Orange
White
>>>
Output (2, 2)
Blue
>>>

Output (2, 3)
Blue
Yellow
>>>

Output (2, 4)
Blue
Yellow
Orange
>>>

Output (2, 5)
Blue
Yellow
Orange
White
>>>

Output (3, 2)
>>>

Output (3, 3)
Yellow
>>>

Output (3, 4)
Yellow
Orange
>>>

Output (3, 5)
Yellow
Orange
White
>>>
5. Find and write the output of following python code:
def Alter(M,N=50):
M=M+N #200 + 100 M=150
N=M–N #300 – 100 N=100
print(M,"@",N)
return M
A=200
B=100
A = Alter(A,B) #A=300
print(A,"#",B) # 300 # 100
B = Alter(B)

Output:
300 @ 200
300 # 100
150 @ 100
>>>

6. Find and write the output of following python code:


def Shuffle(mystr):
L = len(mystr)
str2=''
str3=''
for i in range(0,L,2):
str2=str2 + mystr[i] + mystr[i+1].lower()
print(str2)
for ch in mystr:
if ch=='R' or ch=='N':
str3 = str3 + ch + 'i'
else:
str3 = str3 + ch + 'a'
return str3
mystr="SSRRGGMMPPDDNNSS"
mystr=Shuffle(mystr)
print(mystr)

Output:
SsRrGgMmPpDdNnSs
SaSaRiRiGaGaMaMaPaPaDaDaNiNiSaSa
x-x-x-x-x-x
ANSWERS FOR OBJECTIVE TYPE QUESTIONS
1. c) 7
2. b) tuple
3. b) * c) +
4. c) 36
5. b) print(“c:\\newfolder\python\\test.py”)
6. b) Good Morning
7. c) 0
8. c) [6,4,7,8,5]
9. b) False
10. c) {' Sania ': 1, ' Priya ': 2, ' Bhavya ': 2}
11. c) Error
12. Sales
13. sum(10,20)
14. a) Functions are reusable pieces of programs
15. Line 5, because it is after the return statement
16. b) Built-in function & User defined function
17. d) Method
18. d) (3, 5, 6)
19. a) randint()
20. c) 1
21. __init__
22. c) 5
23. c) write()
24. d) outfile = open(“g:\\ testfile.txt”, “a”)
25. (i) sin() – math module
(ii) dump() – pickle module
26. Read mode
27. FILO b) LIFO
28. b) rear
29. b) 6 2
30. b) Enqueue and Dequeue

x-x-x-x-x-x

You might also like