Computer Science Class XII Python File Handling Test Series Part 5


Computer Science Class XII Python File Handling Test Series Part 5

Q1. Reading and writing opeation can be done after close() function(T/F)

Q2.         f = open("test.txt", "r")

Write the output f the following

1. print(f.name)

2. print(f.mode)

3. print(f.closed)

4. prin(f.readable())

Q3. Write a program to accept five numbers from the user and write in a file "num.txt"

Q4. What type of error is shown by the following line of code.

                f.write(3)

where f is file handle associated with text file named "try.txt"

Q5. write() function takes integer argument. (T/F)

Q6. Write a proram to accept 10 numbers from the user. if the number is even than write that number in "even.txt", otherwise, write in file "odd.txt"

Q7. Write a program to create a list of five subjects and write them in a file named "subject.txt"


Q8. What type of error is shown by the following line of code? 

    f.write(try)

Where f is file object and try is a an object of list type.

Q9. What type of error is shown by the following line of code? 

    f.writeline(try)

Where f is file object and try is a an object of list type.

Q10. writelines() method does not add any EOL character to the end of String. (T/F)


SOLUTIONS

Q1. Reading and writing opeation can be done after close() function(T/F)

Ans. False

Q2.         f = open("test.txt", "r")

Write the output f the following

1. print(f.name)

2. print(f.mode)

3. print(f.closed)

4. prin(f.readable())

Ans. 

1. test.txt

2. r

3. False

4. True

Q3. Write a program to accept five numbers from the user and write in a file "num.txt"

Ans.

f=open("num.txt","w")
for i in range(5):
n=int(input("Enter number"))
f.write(str(n))
f.close()


Q4. What type of error is shown by the following line of code.

                f.write(3)

where f is file handle associated with text file named "try.txt"

Ans. TypeError

Q5. write() function takes integer argument. (T/F)

Ans. False

Q6. Write a proram to accept 10 numbers from the user. if the number is even than write that number in "even.txt", otherwise, write in file "odd.txt"

Ans. 

f=open("num.txt","w")
f1=open("odd.txt","w")
for i in range(10):
n=int(input("Enter number"))
if n%2==0:
f.write(str(n))
f.write("\n")
else:
f1.write(str(n))
f.write("\n")
f.close()
f1.close()

Q7. Write a program to create a list of five subjects and write them in a file named "subject.txt"

f=open("num.txt","w")
sub=["Englsh\n","Hindi\n","Math\n","Science\n",
"SST\n"]
f.writelines(sub)
f.close()

Q8. What type of error is shown by the following line of code? 

    f.write(try)

Where f is file object and try is a an object of list type.

Ans. TypeError: write() argument must be str, not list

Q9. What type of error is shown by the following line of code? 

            f.writeline(try)

Where f is file object and try is a an object of list type.

Ans. AttributeError

Q10. writelines() method does not add any EOL character to the end of String. (T/F)

Ans. True








No comments:

Post a Comment

Most Recently Published

File Handling Test 6

 File Handling Test 6 Q1 . Do we need to use the close() function when we use 'with' statement to open a file? Q2. Which mode you wi...

CS - IP Assignment/Worksheet