Sample Question
Sample Question
Sample Question
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”)
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
x= 'ba'
y= 'na'
print ( x + y * 2 )
print ( ( y * 2 ) [ : 3 ] + x )
Output
banana
nanba
s = [None] * 10
print(s)
Output
[None, None, None, None, None, None, None, None, None, None]
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
def Interest(P,R,T=7):
I = (P*R*T)/100
print(I)
Output:
Line 2: Positional Argument should be before keyword argument
Line 4: Required positional argument (R) missing ; required argument cannot be missed
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()
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’+
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)
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()
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
>>>
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