[go: up one dir, main page]

0% found this document useful (0 votes)
115 views9 pages

PT-1 Assignment (CS) AK

This document contains a revision assignment for Class XII Computer Science students covering topics related to functions and file handling in Python. It includes 50 multiple choice questions testing students' understanding of core Python data types, functions, file I/O operations and modes. Some questions test knowledge of built-in functions like open(), read(), write(), as well as concepts like opening files for reading, writing and appending. The assignment aims to help students review and reinforce key Python programming concepts.

Uploaded by

Vaibhav Kalia
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)
115 views9 pages

PT-1 Assignment (CS) AK

This document contains a revision assignment for Class XII Computer Science students covering topics related to functions and file handling in Python. It includes 50 multiple choice questions testing students' understanding of core Python data types, functions, file I/O operations and modes. Some questions test knowledge of built-in functions like open(), read(), write(), as well as concepts like opening files for reading, writing and appending. The assignment aims to help students review and reinforce key Python programming concepts.

Uploaded by

Vaibhav Kalia
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/ 9

Class-XII Subject:- Computer Science-083 Revision Assignment

(Revision Tour, Working with functions & File Handling)

1. Which of these in not a core data type?


a) Lists b) Dictionary c) Tuples d) Class

2. Given a function that does not return any value, What value is thrown by default when
executed in shell.
a) int b) bool c) void d) None

3. Following set of commands are executed in shell, what will be the output?
>>>str="hello"
>>>str[:2]
a) he b) lo c) olleh d) hello

4. Which of the following will run without errors?


a) round(45.8) b) round(6352.898,2,5) c) round() d) round(7463.123,2,1)

5. What is the return type of function id ?


a) int b) float c) bool d) dict

6. In python we do not specify types,it is directly interpreted by the compiler, so consider the
following operation to be performed.
>>>x = 13 ? 2
objective is to make sure x has a integer value, select all that apply (python 3.xx)
a) x = 13 // 2 b) x = int(13 / 2) c) x = 13 % 2 d) All of the mentioned

7. What error occurs when you execute?


apple = mango
a) SyntaxError b) NameError c) ValueError d) TypeError

8. Carefully observe the code and give the answer.


def example(a):
a = a + '2'
a = a*2
return a
>>>example("hello")
a) indentation Error b) cannot perform mathematical operation on strings
c) hello2 d) hello2hello2

9. What data type is the object below?


L = [1, 23, ‘hello’, 1].
a) list b) dictionary c) array d) tuple
10. In order to store values in terms of key and value we use what core data type.
a) list b) tuple c) class d) dictionary

1|Page
11. What is the output of the following?
i=1
while True:
if i%3 == 0:
break
print(i)
i + = 1 # Space not allowed i + = 1
a) 1 2 b) 1 2 3 c) error d) none of the mentioned

12. What is the output of the following?


i=1
while True:
if i%0O7 == 0:
break
print(i)
i += 1
a) 1 2 3 4 5 6 b) 1 2 3 4 5 6 7 c) error d) none of the mentioned

13. What is the output of the following?


x = "abcdef"
i = "i"
while i in x:
print(i, end=" ")
a) no output b) i i i i i i … c) a b c d e f d) abcdef

14. What is the output of the following?


x = 'abcd'
for i in x:
print(i.upper())
a) a b c d b) A B C D c) a B C D d) error

15 . What is the output of the following?


d = {0: 'a', 1: 'b', 2: 'c'}
for i in d:
print(i)
a) 0 1 2 b) a b c c) 0 a 1 b 2 c d) none of the mentioned

16. 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 also create your own functions
d) All of the mentioned

17. What are the two main types of functions?


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

2|Page
18. What is the output of the below program?
def C2F(c):
return c * 9/5 + 32
print C2F(100)
print C2F(0)
a) 212 32 b) 314 24 c) 567 98 d) None of the mentioned

20. What is the output of the below program?


def power(x, y=2):
r=1
for i in range(y):
r=r*x
return r
print power(3)
print power(3, 3)
a) 212 32 b) 9 27 c) 567 98 d) None of the mentioned

21. To open a file c:\scores.txt for reading, we use


a) infile = open(“c:\scores.txt”, “r”)
b) infile = open(“c:\\scores.txt”, “r”)
c) infile = open(file = “c:\scores.txt”, “r”)
d) infile = open(file = “c:\\scores.txt”, “r”)

22. To open a file c:\scores.txt for writing, we use


a) outfile = open(“c:\scores.txt”, “w”)
b) outfile = open(“c:\\scores.txt”, “w”)
c) outfile = open(file = “c:\scores.txt”, “w”)
d) outfile = open(file = “c:\\scores.txt”, “w”)

23. To open a file c:\scores.txt for appending data, we use


a) outfile = open(“c:\\scores.txt”, “a”)
b) outfile = open(“c:\\scores.txt”, “rw”)
c) outfile = open(file = “c:\scores.txt”, “w”)
d) outfile = open(file = “c:\\scores.txt”, “w”)

24. Which of the following statements are true?


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, a new file is created
c) When you open a file for writing, if the file exists, the existing file is overwritten with the new file
d) All of the mentioned

25. To read two characters from a file object infile, we use


a) infile.read(2) b) infile.read() c) infile.readline() d) infile.readlines()

26. To read the entire remaining contents of the file as a string from a file object infile, we use
a) infile.read(2) b) infile.read() c) infile.readline() d) infile.readlines()

3|Page
27. In which mode the file must exist already, otherwise python raises an error?
a. read mode b. write mode c. binary mode d. None of these

28. What is the prefix r stands for in file path?


a. raw string b. read c. write d. append

29. In which mode______ if the file does not exist, then the file is created?
a. read write mode b. read mode c. write mode d. All of these

30. Which option is correct about this program?


f=open(“ss.txt”,”wb”)
print(“Name of the file:”,f.name)
f.flush()
f.close()
a. Compilation error b. Runtime error c. No output d. Flushes the file when closing them

31. What is the output of the following?


import sys
sys.stdout.write(‘Hello\n’)
sys.stdout.write(‘Python\n’)
a. error b. Runtime error c. Hello Python d. Hello
Python
32. Which function is used to read all the characters in text files?
a. read( ) b. readcharacters( ) c. readall( ) d. readchar( )

33. Which function is used to read all the lines?


a. read( ) b. readall( ) c. readlines( ) d. readline( )

34. In which format does the readlines( ) function give the output?
a. Integer type b. list type c. string type d. tuple type

35. In which format does the read( ) function give the output?
a. Integer type b. string type c. list type d. tuple type

36. Which function is used to write a list of strings in a file?


a. writestatement() b. writelines() c. writefulline() d. writeline()

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


a. writechar() b. writecharacters() c. write() d. writeall()

38. What is the correct syntax of open() function?


a. file=open(file_name[,access_mode][,buffering])
b. fileobject=open(file_name[,access_model][,buffering])
c. fileobject=filename.open()
d. none of the mentioned

4|Page
39.In file handling, what does means “r”, “a”?
a. append, read b. read, append c. read, add d. None of the mentioned

40.The default file open mode is….


a. w b. r+ c. w+ d. r

41. What is the difference between r+ and w+ modes?


a. In r+ mode, file length truncates to zero.
b. In w+ mode, file length truncates to zero either file exists or not.
c. No difference
d. Depends on the operating system

42. A file maintains a __________ which tells the current position in the file where writing or
reading will take place.
a. line b. file pointer c. list d. order

43. Which of the following statements is true regarding the opening modes of a file?
a. While opening a file for reading, if the file does not exist, an error occurs.
b. While opening a file for writing ,if the file does not exist, an error occurs.
c. While opening a file for reading, if the file does not exist, a new file is created.
d. None of the above.

44.To force python to write the contents of file buffer on to storage file,........method may be
used.
a. buffer() b. flush() c. close() d. write()

45. Which of the following statements are true?


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, a new file is created.
c) When you open a file for writing, if the file exists, the existing file content is overwritten with the new
content.
d) All of the these

46. Which of the following functions do you use to write data in the binary format?
A. write B. output C. dump D. send

47. Which of the following file mode will refer to the BINARY mode?
A. binary B. b C. bin D. w

48. The process of converting the structure to a byte stream before writing to the file is known
as ______.
a. Pickling b. Unpickling c. Dump d. load

49. The _____ file mode is used when user want to write data into binary file.
a. rb b. wb c. r+ d. w+

5|Page
50. Which one of the following is correct statement?
a) import – pickle b) pickle import c) import pickle d) All of the above

51. Ms. Sejal is working on the sports.dat file but she is confused about how to read data from
the binary file. Suggest a suitable line for her to fulfill her wish.

a. load() b. data=f1.load() c. f1.load(data) d. data=load(f1)

52. Name the function to read from CSV file.


a. read() b. csv.reader() c. csv.read() d. reader()

53. In which file, no delimiters are used for line and no translations occur?
a. Text file b. Binary file c. csv file d. None of the above

6|Page
54. Priti of class 12 is writing a program to create a CSV file “emp.csv”. She has written the
following code to read the content of file emp.csv and display the employee record whose
name begins from “S‟ also show no. of employee with first letter “S‟ out of total record. As a
programmer, help her to successfully execute the given task. Consider the following CSV file
(emp.csv):
1,Peter,3500
2,Scott,4000
3,Harry,5000
4,Michael,2500
5,Sam,4200

(a) In above program, choose correct answer for part (a)


a. pickle b. csv c. binary d. None of the above
(b) In above program, choose correct answer for part (b)
a. read mode b. append mode c. write mode d. None of the above
(c) In above program, choose correct answer for part (c)
a. 'emp.csv' b. 'emp' c. 'empcsv' d. None of the above
(d) In above program, choose correct answer for part (d)
a. read b. writer c. reader d. None of the above
(e) Do it by yourself.
2,Scott,4000
5,Sam,4200
Number of “S” names are 2/5 reader

7|Page
55. Given a binary file “emp.dat” has structure (Emp_id, Emp_name, Emp_Salary). Write a
function in Python countsal() in Python that would read contents of the file “emp.dat” and
display the details of those employee whose salary is greater than 20000.

a) In above program , choose correct option for Line 1


a. countsal() b. Countsal() c. Sal() d. Count()
b). In above program , choose correct option for Line 2
a. ab b. rb c. wb d. None of the above
c) In above program , choose correct option for Line 3
a. pickle.dump() b. pickle.read() c. pickle.load() d. None of the above
d) In above program , choose correct option for Line 4
a. -1 b. 2 c. 0 d. 1
e) In above program , choose correct option for Line 5
a. f.close() b. f.exit() c. f.stop() d. None of the above

56. A binary file “Stu.dat” has structure (rollno, name, marks).


(i) Write a function in Python add_record() to input data for a record and add to Stu.dat.
(ii) Write a function in python Search_record() to search a record from binary file “Stu.dat” on
the basis of roll number.
Ans:- def add_record():
file = open('Stu.dat', 'ab' )
rollno=int(input("Enter rollno : "))
name=input("Enter Name :")
marks = int(input("Enter Marks : "))
rec=[rollno, name, marks]
pickle.dump(rec,fobj)
fobj.close()

def Search_record():
file = open('Stu.dat', 'rb' )
SRoll=input("Enter Roll Number for search: ")
try:
while True:
d=pickle.load(file)
8|Page
if d[0]==SRoll:
print(d)
except EOFError:
print("Done")
file.close()

9|Page

You might also like