Header Ads Widget

Responsive Advertisement

Python Programming Week 9 Quiz Solution [Python Programming course on Prutor.ai by IIT kharagpur]

 Here you will get all the assignment and quiz solutions of Python Programming Course by prutor.ai. Prutor is an online smart tutoring platform which provides various MOOC courses for student and organised by by IIT Kharagpur. At the end of each course student gets a certificate for sucessfull  completion of each course but throughout the course students have to pass all the weekly assignments and quizes.

[1] Assume that theFile is a file object, and the operations are all valid operations (i.e., theFile is open in an appropriate mode). Now if we execute – .numWritten = theFile.write(‘something’)

  • (a): It tell whether theFile is open or closed
  • (b): Reads and return ‘something’
  • (c): Stores the number of characters written to theFile in a variable
  • (d): reads a single line from theFile, including the newline character

Answer: (c) Stores the number of characters written to theFile in a variable

[2] Assume that theFile is a file object, and the operations are all valid operations (i.e., theFile is open in an appropriate mode). Now if we execute –

theFile.closed
  • (a): tells (True of False) whether theFile is closed or not
  • (b): Reads and return ‘something’
  • (c): read a single line from theFile, including the newline character
  • (d): modify the file object for theFile so that the next read will be from

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

[3] Assume that theFile is a file object, and the operations are all valid operations (i.e., theFile is open in an appropriate mode). Now if we execute –

theFile.tell()
  • (a): points to the location from where next byte will be read in theFile
  • (b): tell whether theFile is open or closed
  • (c): Reads and return ‘something’
  • (d): Store the number of characters written to theFile in a variable

Answer:(a) points to the location from where next byte will be read in theFile


[4] Assume that theFile is a file object, and the operations are all valid operations (i.e., theFile is open in an appropriate mode). Now if we execute –

theFile.seek(pos, ref)
  • (a): tell whether theFile is open or closed
  • (b): Read and return at most
  • (c): Reads and return ‘something’
  • (d): Store the number of characters written to theFile in a variable

Answer: (d) Store the number of characters written to theFile in a variable

[5] Assume that theFile is a file object, and the operations are all valid operations (i.e., theFile is open in an appropriate mode). Now if we execute-

theFile.readline
  • (a): tell whether theFile is open or closed
  • (b): Read and return at most
  • (c): read a single line from theFile, including the newline character
  • (d): modify the file object for theFile so that the next read will be from




Answer: (c) read a single line from theFile, including the newline character

[6] Which of the following command is used to open a file “C:\desktop\myfile.txt”, In read-mode only?

A. file_name = open(“C:\desktop\myfile.txt”, “r”)
B. file_name = open(“C:\desktop\myfile.txt”, “r”) 
C. file_name = open(file = “C:\desktop \myfile.txt”, “r+”) 
D. file_name = open(file = “C:\desktop\myfile.txt”, “r+”)
  • (a): A
  • (b): B
  • (c): C
  • (d): D

Answer: (b)file_name = open(“C:\\desktop\myfile.txt”, “r”)

[7] What will be the output of the following given code?

with open("myfile.txt", "w") as f:
		    f.write("Hello World Python Programming")
		    
		with open('myfile.txt', 'r') as f:
		    data = f.readlines()
		    for line in data:
		        words = line.split()
		        print (words)
  • (a): Hello
  • (b): Hello World Python Programming
  • (c): [‘Hello’, ‘World’, Python, Programming]
  • (d): Runtime Error

Answer: (c) [‘Hello’, ‘World’, Python, Programming]


[8] Which of the following commands can be used to read the entire contents of a file object (object name myfile)?

  • (a): myfile.read(n)
  • (b): myfile.read()
  • (c): myfile.readline()
  • (d): myfile.readlines()

Answer: (b)  myfile.read()


[9] The readlines() method returns ____________

  • (a): str
  • (b): a list of lines
  • (c): a list of single characters
  • (d): a list of integers

Answer: (b) a list of lines

[10] To read the remaining lines of the file from a file object infile, we use ____________

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

Answer: (c)  infile.readline()




[11] When will the else part of try-except-else be executed?

  • (a): always
  • (b): when an exception occurs
  • (c): when no exception occurs
  • (d): when an exception occurs in to except block

Answer: (c) when no exception occurs

[12] What will be output when ‘1’ == 1 is executed?

  • (a): True
  • (b): False
  • (c): TypeError
  • (d): ValueError

Answer: (b) False

[13] What will be output of following given code?

 try:
		  print(x)
	  except NameError:
		  print("Variable x is not defined")
	  except:
		  print("Something wrong")
  • (a): x
  • (b): Something wrong
  • (c): Variable x is not defined
  • (d): None of these

Answer: (c) Variable x is not defined

[14] How many except statements can a try-except block have?

  • (a): zero
  • (b): one
  • (c): more than one
  • (d): more than zero




Answer: (d) more than zero

[15] What will be the output of the following Python code?

def test():
    try:
        print(1)
    finally:
        print(2)
test()
  • (a): 1 2
  • (b): 1
  • (c): 2
  • (d): None of these

Answer: (a) 1 2



If you have any quetions, please comment down below, we will try to answer with in first 24 hour. Hope you liked this content, We will come with another week’s quiz, Thanks for reading. Good Bye.

Post a Comment

0 Comments