Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
Filter by Categories
nmims post
Objective Type Set
Online MCQ Assignment
Question Solution
Solved Question
Uncategorized

Python MCQ Set 1

1. In file handling, what does this terms means “r, a”?
a) read, append
b) append, read
c) all of the mentioned
d) none of the the mentioned

View Answer

Answer: a [Reason:] r- reading, a-appending.

2. What is the use of “w” in file handling?
a) Read
b) Write
c) Append
d) None of the the mentioned

View Answer

Answer: b [Reason:] This opens the file for writing. It will create the file if it doesn’t exist, and if it does, it will overwrite it. fh = open(“filename_here”, “w”).

3. What is the use of “a” in file handling?
a) Read
b) Write
c) Append
d) None of the the mentioned

View Answer

Answer: c [Reason:] This opens the fhe file in appending mode. That means, it will be open for writing and everything will be written to the end of the file. fh =open(“filename_here”, “a”).

4. Which function is used to read all the characters?
a) Read()
b) Readcharacters()
c) Readall()
d) Readchar()

View Answer

Answer: a [Reason:] The read function reads all characters fh = open(“filename”, “r”) content = fh.read().

5. Which function is used to read single line from file?
a) Readline()
b) Readlines()
c) Readstatement()
d) Readfullline()

View Answer

Answer: b [Reason:] The readline function reads a single line from the file fh = open(“filename”, “r”) content = fh.readline().

6. Which function is used to write all the characters?
a) write()
b) writecharacters()
c) writeall()
d) writechar()

View Answer

Answer: a [Reason:] To write a fixed sequence of characters to a file fh = open(“hello.txt”,”w”) write(“Hello World”).

7. Which function is used to write a list of string in a file
a) writeline()
b) writelines()
c) writestatement()
d) writefullline()

View Answer

Answer: a [Reason:] With the writeline function you can write a list of strings to a file fh = open(“hello.txt”, “w”) lines_of_text = [“a line of text”, “another line of text”, “a third line”] fh.writelines(lines_of_text).

8. Which function is used to close a file in python?
a) Close()
b) Stop()
c) End()
d) Closefile()

View Answer

Answer: a [Reason:] f.close()to close it and free up any system resources taken up by the open file.

9. Is it possible to create a text file in python?
a) Yes
b) No
c) Machine dependent
d) All of the mentioned

View Answer

Answer: a [Reason:] Yes we can create a file in python. Creation of file is as shown below. file = open(“newfile.txt”, “w”) file.write(“hello world in the new filen”) file.write(“and another linen”) file.close().

10. Which of the following is modes of both writing and reading in binary format in file.?
a) wb+
b) w
c) wb
d) w+

View Answer

Answer: a [Reason:] Here is the description below “w” Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. “wb” Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. “w+” Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing. “wb+” Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

Python MCQ Set 2

1. Which is/are the basic I/O connections in file?
a) Standard Input
b) Standard Output
c) Standard Errors
d) All of the mentioned

View Answer

Answer: d [Reason:] Standard input, standard output and standard error. Standard input is the data that goes to the program. The standard input comes from a keyboard. Standard output is where we print our data with the print keyword. Unless redirected, it is the terminal console. The standard error is a stream where programs write their error messages. It is usually the text terminal.

2. What is the output of this program?

  1. import sys
  2. print 'Enter your name: ',
  3. name = ''
  4. while True:
  5.    c = sys.stdin.read(1)
  6.    if c == 'n':
  7.       break
  8.    name = name + c
  9.  
  10. print 'Your name is:', name

If entered name is
aidlo
a) aidlo
b) aidlo, aidlo
c) Aid
d) None of the mentioned

View Answer

Answer: a [Reason:] In order to work with standard I/O streams, we must import the sys module. The read() method reads one character from the standard input. In our example we get a promt saying “Enter your name”. We enter our name and press enter. The enter key generates the new line character: n. Output: Enter your name: aidlo Your name is: aidlo

3. What is the output of this program?

  1. import sys
  2. sys.stdout.write(' Hellon')
  3. sys.stdout.write('Pythonn')

a) Compilation Error
b) Runtime Error
c) Hello Python
d) Hello
Python

View Answer

Answer: d [Reason:] None Output: Hello Python

4. Which of the following mode will refer to binary data?
a) r
b) w
c) +
d) b

View Answer

Answer:d [Reason:] Mode Meaning is as explained below: r Reading w Writing a Appending b Binary data + Updating.

5. What is the pickling?
a) It is used for object serialization
b) It is used for object de-serialization
c) None of the mentioned
d) All of the mentioned

View Answer

Answer: a [Reason:] Pickle is the standard mechanism for object serialization. Pickle uses a simple stack-based virtual machine that records the instructions used to reconstruct the object. This makes pickle vulnerable to security risks by malformed or maliciously constructed data, that may cause the deserializer to import arbitrary modules and instantiate any object.

6. What is unpickling?
a) It is used for object serialization
b) It is used for object deserialization
c) None of the mentioned
d) All of the mentioned

View Answer

Answer: b [Reason:] We have been working with simple textual data. What if we are working with objects rather than simple text? For such situations, we can use the pickle module. This module serializes Python objects. The Python objects are converted into byte streams and written to text files. This process is called pickling. The inverse operation, reading from a file and reconstructing objects is called deserializing or unpickling.

7. What is the correct syntax of open() function?
a) file = open(file_name [, access_mode][, buffering])
b) file object = open(file_name [, access_mode][, buffering])
c) file object = open(file_name)
d) none of the mentioned

View Answer

Answer: b [Reason:] Open() function correct syntax with the parameter details as shown below: file object = open(file_name [, access_mode][, buffering]) Here is parameters’ detail: file_name: The file_name argument is a string value that contains the name of the file that you want to access. access_mode: The access_mode determines the mode in which the file has to be opened, i.e., read, write, append, etc. A complete list of possible values is given below in the table. This is optional parameter and the default file access mode is read (r). buffering: If the buffering value is set to 0, no buffering will take place. If the buffering value is 1, line buffering will be performed while accessing a file. If you specify the buffering value as an integer greater than 1, then buffering action will be performed with the indicated buffer size. If negative, the buffer size is the system default(default behavior).

8. What is the output of this program?

  1. fo = open("foo.txt", "wb")
  2. print "Name of the file: ", fo.name
  3. fo.flush()
  4. fo.close()

a) Compilation Error
b) Runtime Error
c) No Output
d) Flushes the file when closing them

View Answer

Answer: d [Reason:] The method flush() flushes the internal buffer. Python automatically flushes the files when closing them. But you may want to flush the data before closing any file.

9. Correct syntax of file.writelines() is?
a) file.writelines(sequence)
b) fileObject.writelines()
c) fileObject.writelines(sequence)
d) none of the mentioned

View Answer

Answer: c [Reason:] The method writelines() writes a sequence of strings to the file. The sequence can be any iterable object producing strings, typically a list of strings. There is no return value. Syntax Following is the syntax for writelines() method: fileObject.writelines( sequence ).

10. Correct syntax of file.readlines() is?
a) fileObject.readlines( sizehint );
b) fileObject.readlines();
c) fileObject.readlines(sequence)
d) none of the mentioned

View Answer

Answer: a [Reason:] The method readlines() reads until EOF using readline() and returns a list containing the lines. If the optional sizehint argument is present, instead of reading up to EOF, whole lines totalling approximately sizehint bytes (possibly after rounding up to an internal buffer size) are read. Syntax Following is the syntax for readlines() method: fileObject.readlines( sizehint ); Parameters sizehint — This is the number of bytes to be read from the file.

Python MCQ Set 3

1. What is the output when following statement is executed ?

  1. >>>chr(ord('A'))

a) A
b) B
c) a
d) Error

View Answer

Answer: a [Reason:] Execute in shell to verify.

2. What is the output when following statement is executed ?

  1. >>>print(chr(ord('b')+1))

a) a
b) b
c) c
d) A

View Answer

Answer: c [Reason:] Execute in the shell to verify.

3. Which of the following statement prints helloexampletest.txt ?
a) print(“helloexampletest.txt”)
b) print(“helloexampletest.txt”)
c) print(“hello”example”test.txt”)
d) print(“hello”example”test.txt”)

View Answer

Answer: b [Reason:] is used to indicate that the next is not an escape sequence.

4. Suppose s is “ttWorldn”, what is s.strip() ?
a) ttWorldn
b) ttWorldn
c) ttWORLDn
d) World

View Answer

Answer: d [Reason:] Execute help(string.strip) to find details.

5. The format function, when applied on a string returns :
a) Error
b) int
c) bool
d) str

View Answer

Answer: d [Reason:] Format function returns a string.

6. What is the output of “hello”+1+2+3 ?
a) hello123
b) hello
c) Error
d) hello6

View Answer

Answer: c [Reason:] Cannot concantenate str and int objects.

7. What is the output when following code is executed ?

  1. >>>print("D", end = ' ')
  2. >>>print("C", end = ' ')
  3. >>>print("B", end = ' ')
  4. >>>print("A", end = ' ')

a) DCBA
b) A, B, C, D
c) D C B A
d) D, C, B, A will be displayed on four lines

View Answer

Answer: d [Reason:] Execute in the shell.

8. What is the output when following statement is executed ?(python 3.xx)

  1. >>>print(format("Welcome", "10s"), end = '#')
  2. >>>print(format(111, "4d"), end = '#')
  3. >>>print(format(924.656, "3.2f"))

a)    Welcome# 111#924.66
b) Welcome#111#924.66
c) Welcome#111#.66
d) Welcome   # 111#924.66

View Answer

Answer: d [Reason:] Execute in the shell to verify.

9. What will be displayed by print(ord(‘b’) – ord(‘a’)) ?
a) 0
b) 1
c) -1
d) 2

View Answer

Answer: b [Reason:] ASCII value of b is one more than a. Hence the output of this code is 98-97, which is equal to 1.

10. Say s=”hello” what will be the return value of type(s) ?
a) int
b) bool
c) str
d) String

View Answer

Answer: c [Reason:] str is used to represent strings in python.

Python MCQ Set 4

1. What is the output of the following?

for i in range(10):
    if i == 5:
        break
    else:
        print(i)
else:
    print("Here")

a) 0 1 2 3 4 Here
b) 0 1 2 3 4 5 Here
c) 0 1 2 3 4
d) 1 2 3 4 5

View Answer

Answer: c [Reason:] The else part is executed if control doesn’t break out of the loop.

2. What is the output of the following?

for i in range(5):
    if i == 5:
        break
    else:
        print(i)
else:
    print("Here")

a) 0 1 2 3 4 Here
b) 0 1 2 3 4 5 Here
c) 0 1 2 3 4
d) 1 2 3 4 5

View Answer

Answer: a [Reason:] The else part is executed if control doesn’t break out of the loop.

3. What is the output of the following?

x = (i for i in range(3))
for i in x:
    print(i)

a) 0 1 2
b) error
c) 0 1 2 0 1 2
d) none of the mentioned

View Answer

Answer: a [Reason:] The first statement creates a generator object.

4. What is the output of the following?

x = (i for i in range(3))
for i in x:
    print(i)
for i in x:
    print(i)

a) 0 1 2
b) error
c) 0 1 2 0 1 2
d) none of the mentioned

View Answer

Answer: a [Reason:] We can loop over a generator object only once.

5. What is the output of the following?

string = "my name is x"
for i in string:
    print (i, end=", ")

a) m, y, , n, a, m, e, , i, s, , x,
b) m, y, , n, a, m, e, , i, s, , x
c) my, name, is, x,
d) error

View Answer

Answer: a [Reason:] Variable i takes the value of one character at a time.

6. What is the output of the following?

string = "my name is x"
for i in string.split():
    print (i, end=", ")

a) m, y, , n, a, m, e, , i, s, , x,
b) m, y, , n, a, m, e, , i, s, , x
c) my, name, is, x,
d) error

View Answer

Answer: c [Reason:] Variable i takes the value of one word at a time.

7. What is the output of the following?

a = [0, 1, 2, 3]
for a[-1] in a:
    print(a[-1])

a) 0 1 2 3
b) 0 1 2 2
c) 3 3 3 3
d) error

View Answer

Answer: b [Reason:] The value of a[-1] changes in each iteration.

8. What is the output of the following?

a = [0, 1, 2, 3]
for a[0] in a:
    print(a[0])

a) 0 1 2 3
b) 0 1 2 2
c) 3 3 3 3
d) error

View Answer

Answer: a [Reason:] The value of a[0] changes in each iteration. Since the first value that it takes is itself, there is no visible error in the current example.

9. What is the output of the following?

a = [0, 1, 2, 3]
i = -2
for i not in a:
    print(i)
    i += 1

a) -2 -1
b) 0
c) error
d) none of the mentioned

View Answer

Answer: c [Reason:] SyntaxError, not in isn’t allowed in for loops.

10. What is the output of the following?

string = "my name is x"
for i in ' '.join(string.split()):
    print (i, end=", ")

a) m, y, , n, a, m, e, , i, s, , x,
b) m, y, , n, a, m, e, , i, s, , x
c) my, name, is, x,
d) error

View Answer

Answer: a [Reason:] Variable i takes the value of one character at a time.

Python MCQ Set 5

1. Which of the following is not a valid mode to open a file?
a) ab
b) rw
c) r+
d) w+

View Answer

Answer: b [Reason:] Use r+, w+ or a+ to perform both read and write operations using a single file object.

2. What is the difference between r+ and w+ modes?
a) no difference
b) in r+ the pointer is initially placed at the beginning of the file and the pointer is at the end for w+
c) in w+ the pointer is initially placed at the beginning of the file and the pointer is at the end for r+
d) depends on the operating system

View Answer

Answer: b [Reason:] none.

3. How do you get the name of a file from a file object (fp)?
a) fp.name
b) fp.file(name)
c) self.__name__(fp)
d) fp.__name__()

View Answer

Answer: a [Reason:] name is an attribute of the file object.

4. Which of the following is not a valid attribute of a file object (fp)?
a) fp.name
b) fp.closed
c) fp.mode
d) fp.size

View Answer

Answer: d [Reason:] fp.size has not been implemented.

5. How do you close a file object (fp)?
a) close(fp)
b) fclose(fp)
c) fp.close()
d) fp.__close__()

View Answer

Answer: c [Reason:] close() is a method of the file object.

6. How do you get the current position within the file?
a) fp.seek()
b) fp.tell()
c) fp.loc
d) fp.pos

View Answer

Answer: b [Reason:] It gives the current position as an offset from the start of file.

7. How do you rename a file?
a) fp.name = ‘new_name.txt’
b) os.rename(existing_name, new_name)
c) os.rename(fp, new_name)
d) os.set_name(existing_name, new_name)

View Answer

Answer: b [Reason:] os.rename() is used to rename files.

8. How do you delete a file?
a) del(fp)
b) fp.delete()
c) os.remove(‘file’)
d) os.delete(‘file’)

View Answer

Answer: c [Reason:] os.remove() is used to delete files.

9. How do you change the file position to an offset value from the start?
a) fp.seek(offset, 0)
b) fp.seek(offset, 1)
c) fp.seek(offset, 2)
d) none of the mentioned

View Answer

Answer: a [Reason:] 0 indicates that the offset is with respect to the start.

10. What happens if no arguments are passed to the seek function?
a) file position is set to the start of file
b) file position is set to the end of file
c) file position remains unchanged
d) error

View Answer

Answer: d [Reason:] seek() takes at least one argument.

Synopsis and Project Report

You can buy synopsis and project from distpub.com. Just visit https://distpub.com/product-category/projects/ and buy your university/institute project from distpub.com