Python Week 9 Quiz Answers



Disclaimer for Pruto-ai

If you require any more information or you have any problem regarding Copyright or have any questions about our site’s disclaimer, please feel free to contact us by email at contact@prutor-ai.com.

All the information on this website is published in good faith and for general information and educational purpose only. Prutor-ai does not make any warranties about the completeness, reliability, and accuracy of this information. Any action you take upon the information you find on this website (Prutor-ai.blogspot.com), is strictly at your own risk. will not be liable for any losses and/or damages in connection with the use of our website.

In this students get weekly assignments that they have to complete every week. Here we will show you or give you all the answers for the python quiz, These answers are for all those who have confusion in the questions or they don’t want to submit wrong answers.

1.  If we have a file object as fileobj, then What does the attribute 'closed' will tell on this file obj ?

·        tells (True of False) whether theFile is closed or not

·        Reads and return ‘something’

·        read a single line from theFile, including the newline character

·        modify the file object for theFile so that the next read will be from

Ans:  tells (True of False) whether theFile is closed or not

Reason:  Closed attribute is an inbuilt attribute of file object. In python it can be used to check whether a file object is closed or not. It will return a Boolean value (true if file is closed and false if not closed).

2. In python, _____ method gets the position of file handle while ____ method changes the position of file handle to the specific position.

·        seek, tell

·        tell, seek

·        read, tell

·        tell, change

Ans: tell, seek

Reason: Tell method is used to get the position of the file handle while seek method changes the position of file handle to specific position.

 

3.  What will be the output of following python code?

with open("myfile.txt", "w") as f:

    f.write("Hello World Python Programming")

   

with open('myfile.txt', 'w+') as f:

    f.write("Hello")

    f.seek(0)

    data = f.readlines()

    for line in data:

        words = line.split()

        print (words)

·        ['Hello']

·        ['Hello World Python Programming']

·        ['Hello', 'World', 'Python', 'Programming']

·        Runtime Error

Ans: ['Hello']

Reason: The output of the following program will be [Hello] only.

4. In the readlines() method -

·        it returns a string.

·        it returns a list of lines.

·        an optional parameter sizehint can be used to limit the number of bytes to read from a file.

·        Both 2 and 3

Ans: Both 2 and 3

Reason: The readlines() method returns a list containing each line in the file as a list item.

5. In python, try-except has an optional else clause, which will execute -

·        always.

·        when try clause does not raise exception.

·        when OSError happens.

·        All of the above.

Ans: All of the above.

Reason: Since in python try-except has an optional else clause, which will execute always, when try clause does not raise exception and when OSError.