Question and Answers
for the class test 1 15th july – COMPUTER SCIENCE - 12 SC
Q1. Write a function called my_buzz that takes a number.
If the number is
divisible by 3, it should return “Fizz”.
If it is divisible
by 5, it should return “Buzz”.
If it is divisible by
both 3 and 5, it should return “FizzBuzz”.Otherwise, same no.
Ans1. Program:
1. def my_buzz(n):
2. if n % 3==0 and n % 5==0:
3. return 'FizzBuzz'
4. elif n % 3 == 0:
5. return 'Fizz'
6. elif n % 5 == 0:
7. return 'Buzz'
8. else:
9. return n
10.
11. a=int(input('enter a number'))
12. print(my_buzz(a))
Input and Output:
1. ==== RESTART: D:\pythonVirtualEnv\practicePro\examplePro\classTestPro.py ====
2. enter a number15
3. Fizz
4. >>>
5. ==== RESTART: D:\pythonVirtualEnv\practicePro\examplePro\classTestPro.py ====
6. enter a number20
7. Buzz
8. >>>
9. ==== RESTART: D:\pythonVirtualEnv\practicePro\examplePro\classTestPro.py ====
10. enter a number11
11
Q2. Find the output of the following:
a. 1. def increment(n):
2. n.append([4])
3. return n
4. l=[1,2,3]
5. m=increment(l)
6. print(l,m) b.
1. def increment(n):
2. n.append([49])
3. return n[0], n[1], n[2], n[3]
4. l=[23,35,47]
5. m1,m2,m3,m4=increment(l)
6. print(l)
7. print(m1,m2,m3,m4)
8. print(l[3] == m4)
Ans (b). [23, 35, 47, [49]]
23 35 47
[49]
True
Q3. Write a python program to read last 2 lines
of a text file.
Ans3. Program:
1. f=open('newFile.txt','r')
2. ls=f.readlines()
3. print('The Last two lines of the file are')
4. print(str(ls[-2]),end='')
5. print(str(ls[-1]),end='')
Output:
1. ==== RESTART: D:\pythonVirtualEnv\practicePro\examplePro\classTestPro.py ====
2. The Last two lines of the file are
3. so its a rainy day
4. so you get a raincoat
Q4. Write a method in python to write multiple lines of text
contents into a text file mylife.txt.
Ans4. Program:
1. f=open('newFile.txt','a')
2. ls=['but its a nice day\n','so its a fun\n','lets have fun then\n']
3. f.writelines(ls)
4. f.close()
Q5. Write a method in python to read the content from a text
file diary.txt line by line and display the same on screen.
Ans5. Program:
1. f=open('newFile.txt','r')
2. print(f.read())
Output:
1. ==== RESTART: D:\pythonVirtualEnv\practicePro\examplePro\classTestPro.py ====
2. but its a nice day
3. so its a fun
4. lets have fun then
5. but its a nice day
6. so its a fun
7. lets have fun the