0% found this document useful (0 votes)
82 views7 pages

Holiday Homework Grade XII Computer Science

Uploaded by

vani agarwal
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)
82 views7 pages

Holiday Homework Grade XII Computer Science

Uploaded by

vani agarwal
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/ 7

Manav Rachna International School

Grade XII
Question Bank
Functions in Python
General Instructions:

Highlight the correct answers with yellow for MCQs.


For subjective questions, solve them in any IDLE and write them in your notebooks.
Also save them for your Practical file work.

Q1. Functions created by user is called _________ function


a) Inbuilt
b) Library
c) User defined
d) logical
Q2. Division of large programs into smaller programs. These smaller programs are
called _______.
a) Functions
b) Operators
c) logical programs
d) specific programs
Q3. Inbuilt functions makes the programming ________
a) Difficult
b) Easy
Q4. We have to import a specific module for using built-in functions?
a) True
b) False
Q5. A function can be called _____ times in a program.
a) 3
b) 7
c) 4
d) any number of
Q6. Which of the following is not a type conversion functions?
a) int( )
b) str( )
c) input( )
d) float( )
Q7. Write the output of the following.
print(int())
a) Error
b) Any random number
c) 0
Q8. Which of the following statement will return error?
print(int("a"))
print(str(123))
a) Statement1
b) Statement2
c) Both Statement
d) None of the above
Q9. Write the output of the following:
print(float())
a) 1.0
b) 0.0
c) Any floating point number
d) Zero
Q10. What type of error is returned by following statement?
print(int("a"))
a) Syntax error
b) ValueError
c) TypeError
Q11. Write the output of the following: if user entered 7
a=input("Enter age")
print(type(a))
a) str
b) int
c) float
Q12. eval(x) function returns error if x is integer.(T/F)
a) False
b) True
Q13. Write the output of the following:
print(max(2,5,4,8,29,24))
a) Error as there should be math.max( )
b) 29
c) 2
d) 2 5 4 8 29 24
Q14. Write the output of the following
print(min("Hell","Hello","he"))
a) Hell
b) he
c) Hello
d) Error
Q15. abs( ) function do not work for float number.(T/F)
a) True
b) False
Q16. Write the output of the following.
print(abs(-8))
a) -8
b) 8
c) 8.0
Q17. len() function returns the length of string only.(T/F)
a) True
b) False
Q18. Write the output of the following :
print(round(34.5671,2))
print(round(34.3631,2))
a) 34.57
34.36
b) 34.56
34.37
c) 34.57
35.36
d) 36.00
34.36
Q19. Write the output of the following:
print(list(i*2 for i in range(4)))
print(list(range(4)))
a) [0, 2, 4, 6]
[3]
b) [0, 2, 4, 6]
[0, 1, 2, 3]
c)[0, 1, 2, 3]
[0, 1, 2, 3]
Q20. Write the output of the following:
print((range(5)))

a) range(0, 5)
b) 0, 1, 2, 3, 4
c) [0, 1, 2, 3, 4]
Q21. The process of dividing a computer program into separate independent blocks
of code with different names and specific functionalities is known as ______________.

a) Mode Programming
b) Modular Programming
c) Division Programming
d) None of the above
Q22. Advantage of using function is/are
a) Reusability of Code
b) Divide a complex problem into simpler ones.
c) Reduces chances of error.
d) All of the above
Q23. _____________ can be defined as a named group of instructions that accomplish
a specific task when it is invoked.
a) Function
b) Variable
c) Global Variable
Q24. A function can be called ________ from anywhere in the program.
a) Single time
b) Two times
c) repeatedly
d) None of the above
Q25. What is the purpose of following statement?
def check():

a) Calling a function
b) Defining a function
c) None of the above
d) Both of the above
Q26. Use of functions in a program makes it __________
a) more organised
b) easy to read
c) both of the above
d) None of the above
Q27. Functions which can be used directly in a program are stored in ________
library.
a) Standard
b) Main
c) Sub
d) Union
Q28. A function defined to achieve some task as per the programmer's requirement is
called a __________
a) Standard function
b) User defined Function
c) Built in Function
d) None of the above
Q29. A function definition begins with ___________
a) def
b) define
c) create
d) New
Q30. Function header always ends with a __________
a) comma
b) semicolon
c) colon
d) period

Q31. Determine the output:

import math
def python_lib():
s = “Computer Science with Python”
print(s.swapcase())
print(s.title())
print(s.split())
print(s.replace(‘with’,’-‘))

Q32. Write a Python function that accepts a hyphen-separated sequence of words as


parameter and prints the words in a hyphen-separated sequence after sorting them
alphabetically.
Sample Items: green-red-yellow-black-white
Expected Result: black-green-red-white-yellow
QUESTION BANK (Topic-File Handling)
Q-1. Which of the following command is used to open a file “c:\temp.txt” in read-
mode only?
A. infile = open(“c:\temp.txt”, “r”)
B. infile = open(“c:\\temp.txt”, “r”)
C. infile = open(file = “c:\temp.txt”, “r+”)
D. infile = open(file = “c:\\temp.txt”, “r+”)
Q-2. Which of the following command is used to open a file “c:\temp.txt” in write-
mode only?
A. outfile = open(“c:\temp.txt”, “w”)
B. outfile = open(“c:\\temp.txt”, “w”)
C. outfile = open(file = “c:\temp.txt”, “w+”)
D. outfile = open(file = “c:\\temp.txt”, “w+”)

Q-3. Which of the following command is used to open a file “c:\temp.txt” in append-
mode?
B. outfile = open(“c:\\temp.txt”, “rw”)
C. outfile = open(“c:\temp.txt”, “w+”)
D. outfile = open(“c:\\temp.txt”, “r+”)
E. outfile = open(“c:\\temp.txt”, “a”)

Q-4. Which of the following statements are true regarding the opening modes of
a file?
A. When you open a file for reading, if the file does not exist, an error occurs.
B. When you open a file for writing, if the file does not exist, an error occurs.
C. When you open a file for reading, if the file does not exist, the program will open an
empty file.
D. When you open a file for writing, if the file does not exist, it is searched in parent
folder.

Q-5. Which of the following commands can be used to read “n” number of characters
from a file using the file object <file>?
A. file.read(n)
B. n = file.read()
C. file.readline(n)
D. file.readlines()
Q-6. Which of the following commands can be used to read the entire contents of a
file as a string using the file object <tmpfile>?
A. tmpfile.read(n)
B. tmpfile.read()
C. tmpfile.readline()
D. tmpfile.readlines()

Q-7. Which of the following commands can be used to read the next line in a file
using the file object <tmpfile>?
A. tmpfile.read(n)
B. tmpfile.read()
C. tmpfile.readline()
D. tmpfile.readlines()

Q-8. Which of the following commands can be used to read the remaining lines in a
file using the file object <tmpfile>?
A. tmpfile.read(n)
B. tmpfile.read()
C. tmpfile.readline()
D. tmpfile.readlines()

Q-9. What does the <readlines()> method returns?


A. str
B. a list of lines
C. list of single characters
D. list of integers

Q-10. Which of the following command is used to open a file “c:\temp.txt” for writing
in binary format only?
A. outfile = open(“c:\temp.txt”, “w”)
B. outfile = open(“c:\\temp.txt”, “wb”)
C. outfile = open(“c:\temp.txt”, “w+”)
D. outfile = open(“c:\\temp.txt”, “wb+”)

Q-11 What will be the output of following code: (2)


file1 = open("myfile.txt","w")
L = ["This is Delhi \n","This is Paris \n","This is London \n"]

file1.write("Hello \n")
file1.writelines(L)
file1.close() #to change file access modes

file1 = open("myfile.txt","r+")
print(file1.read(10))
A. This is De
B. Hello
This
C. Hello
This is
D. Hello
This is Delhi

Subjective Questions
1. Write a program to read a file line by line and print it. (3)
2. Write a python program to read a file and print the contents after removing
all punctuation marks. (3)
3. What is the following code doing? (3)
file =open(“contacts.csv”,”a”)
name=input(“enter name”)
ph=input(“enter ph no.”)
file.write(name+”,”+ph+”\n”)

4. Write a program to display the size of file in bytes. (3)


5. What is the difference between read() and readlines() function. (2)
6. What is the difference between readline() and readlines() function. (2)
7. Write a program to read the content from a file and write in another file. (3)
8. Write a program to count the words “to” and “the” present in a text file
“Article.txt”,(4)
9. Write a program that counts uppercase characters present in text file
“Poem.txt”. (3)
10.Write a program to remove all the lines that contain the character `a' in a file
and write it to another file. (4)
11.Write a program to read a text file and show the longest line from it. (4)
12.Write a program to copy the lines which are starting from uppercase
character to another file. Take the two filenames from user. (4)
A text file contains alphanumeric text. Write a program to print only digits or
numbers from the file. (3)

You might also like