[Type B] Unit – 5 Class 12 CS – Sumita Arora Assignment | Q/A

Here is class 12 computer science Unit 5 [Type B] solutions for Sumita Arora back exercise assignment. Below includes both textual and video solutions wherever required. View all the answers in assignment for chapter 5 and for all chapters here.

Watch all tutorials for chapter 5.

Q1: How are the following codes different from one another?
(a)
my_file = open(‘poem.txt’, ‘r’)
my_file.read()

(b)
my_file = open(‘poem.txt’, ‘r’)
my_file.read(100)
(a) this will read the entire content of the file poem.txt.
(b) this will read at most 100 bytes from the file even if it has more content.
Q2: If the file ‘poemBTH.txt’ contains the following poem (by Paramhans Yoganand)?
God made the Earth;
Man made confining countries
And their fancy-frozen boundaries.
But with unfound boundless love
I behold borderLand of my India
Expanding into the world.
Hail, mother of religions, lotus, scenic beauty, and sages!


Then what output will be produced by both the code fragments given in question1.
(a)
my_file = open('poemBTH.txt', 'r')
my_file.read()

#output:
God made the Earth;
Man mas=de confining countries
And their fancy-frozen boundaries.
But with unfound boundless love
I behold borderLand of my India
Expanding into the world.
Hail, mother of religions, lotus, scenic beauty, and sages!

(b)
my_file = open('poemBTH.txt', 'r')
my_file.read(100)

#output:
God made the Earth;
Man mas=de confining countries
And their fancy-frozen boundaries.
But with unfound 

Q3: Consider the file poemBTH.txt given above (in the previous question). What output will be produced by the following code fragment?

obj1 = open(“poemBTH.txt”, “r”)
s1 = obj1.readline()
s2.readline(10)
s3 = obj1.read(15)
print(s3)
print(obj1.readline())
obj1.close()
This code will generate error because of undefined s2.

NameError: name 's2' is not defined
Q4: Consider the file “poemBTH.txt” and predict the outputs of following code fragments if the file has been opened in file pointer file1 with code:

file1 = open(“E:\\mydata\\poemBTH.txt”, “r+”)
Chapter 5 Type-B Sumita Arora
Question 4

Solution:

(a)
A. Output1
God made the Earth;
Man made confining countries
And their fancy-frozen boundaries.
But with unfound boundless love
I behold borderLand of my India
Expanding into the world.
Hail, mother of religions, lotus, scenic beauty, and sages!

(b)
B. Output 2
God made the Earth;


(c)
C. Output 3
God made

(d)
D. Output 4
God made

(e)
E. Output of Readlines function is
['God made the Earth;\n', 'Man made confining countries\n', 'And their fancy-frozen boundaries.\n', 'But with unfound boundless love\n', 'I behold borderLand of my India\n', 'Expanding into the world.\n', 'Hail, mother of religions, lotus, scenic beauty, and sages!']
Q5: What is the following code doing?

file = open(“contacts.csv”, “a”)
name = input(“Please enter name. “)
phno = input(“Please enter phone number. “)
file.write(name+ “,”+ phno+”\n”)
This code will open a file object named file and open contacts.csv in append mode for writing.
In 2nd and 3rd line two input will be taken.
In 4th line the both input will be appended in the open file object.
Q6: Write code to open file created in previous question and print it in the following form:

Name: <name> Phone:<phone>
file = open("contact.csv", "r")

for i in file:
    print("Name:",i.split(',')[0] , "\t", "Phone",i.split(',')[1])

file.close()
Q7: Consider the file “contacts.csv” created in the above question and figure out what the following code is trying to do?
name = input("Enter name :")
file = open("contact.csv", "r")
for line in file:
    if name in line:
        print(line)

Solution:

This code is just implementing a searching program i.e. if the entered name is in the document than the whole found line is displayed.
Q8: Consider the file poemBTH.txt and predict the output of the following code fragment. What exactly is the following code fragment doing?
f = open("poemBTH.txt", "r")
nl = 0
for line in f:
    nl +=1
print(nl)

Solution:

Output:
7

The above code fragment if counting the total numbers of line in the poemBTH.txt file.
Q9: If you use the code of Q.8 with pl.txt created in solved problem 14, what would be its output?
Output:
1

as the filep1.txt just contain one line.
Q10: Write a method in python to read the content from a text file diary.txt line by line and display the same on-screen.
file = open("diary.txt", "r")
for line in file:
    print(line)
file.close()

Q11: Write a method in python to write multiple lines of text contents into a text file mylife.txt.line.
file = open("mylife.txt", "w")
file.write("This is the first line\n")
file.write("This is the second line\n")
file.write("This is the third line")
file.close()

Clear Doubts with Computer Tutor
In case you’re facing problems in understanding concepts, writing programs, solving questions, want to learn fun facts | tips | tricks or absolutely anything around computer science, feel free to join CTs learner-teacher community: students.computertutor.in

You cannot copy content of this page