Here is an interesting Python Multiple choice questions Quiz. Attempting this MCQ will help you to evaluate your knowledge and skills.
- Assume that there is a file ‘somefile.txt’ with the given contents.
Contents of somefile.txt:
This is first line.
This is second line.
This is third line.
What will be the output of the following code?
file = open(“somefile.txt”, “r”)
content1 = file.read()
content2 = file.readline()
content3 = file.readlines()
print (“content1: “, content1)
print (“content2: “, content2)
print (“content3: “, content3)
A. content1: This is first line.
This is second line.
This is third line.
content2: This is first line.
content3: [‘This is first line.\n’, ‘This is second line.\n’, ‘This is third line.\n’]
B. content1: This is first line.
This is second line.
This is third line.
content2:
content3: []
C. content1: This is first line.
This is second line.
This is third line.
content2: This is first line.
content3: []
D. content1:
content2: This is first line.
content3: [‘This is first line.\n’, ‘This is second line.\n’, ‘This is third line.\n’]
- What is the output of following line of code?
fileobj = open(“somefile.txt”, “r”)
print (fileobj.tell(10))
A. returns the position of the 10th character in the file
B. places the cursor at 10th character in the file
C. Error
D. No output
- What is the output of following line of code?
fileobj = open(“somefile.txt”, “a”) # assume there is a file somefile.txt with contents
print (fileobj.tell())
A. returns the position of the last character in the file
B. returns the position of the end of file
C. Error
D. No output
- What will be the output of the following Python code?
print(“Hello {name1} and {name2}”.format(name1=’foo’, name2=’bin’))
A. Hello foo and bin
B. Hello {name1} and {name2}
C Error
D. Hello and
- What will be the output of the following Python code?
print(“Hello {2} and {1}”.format(‘bin’, ‘bar’, ‘foo’))
A. Hello foo and bar
B. Hello bin and foo
C Error
D. Hello bar and foo
- What will be the output of the following Python code?
print(“Hello {0!r} and {1!s}”.format(‘foo’, ‘bin’))
A. Hello foo and foo
B. Hello ‘foo’ and bin
C. Hello foo and ‘bin’
D. Error
- What will be the output of the following Python code snippet?
print(‘The sum of {1} and {0} is {2}’.format(2, 10, 12))
A. The sum of 10 and 2 is 12
B. Error
C. The sum of 0 and 1 is 2
D. None of the mentioned
- What will be the output of the following Python code?
print(“abcdef”.find(“cd”) == “cd” in “abcdef”)
A. True
B. False
C. Error
D. None of the mentioned
- What will be the output of the following Python code?
print(“abcdef”.find(“cd”) == “abcdef”.rfind(“cd”))
A. True
B. False
C. Error
D. None of the mentioned
- Which of the following lines of code will not show a match (assume that ‘import re’ is used)?
A. >>> re.match(‘ab‘, ‘a’) B. >>> re.match(‘ab‘, ‘ab’)
C. >>> re.match(‘ab‘, ‘abb’) D. >>> re.match(‘ab‘, ‘ba’)
- What is the output of the following code?
import re
m = re.search(‘a’, ‘The blue umbrella’)
print (m.re.pattern)
A. {}
B. ‘The blue umbrella’
C. a
D. No output
- What is the difference between ‘r+’ and ‘a’ 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 ‘a’
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
- 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
- What happens if no arguments are passed to the file handling ‘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
- Which function of file handling is used to read all the characters of a file?
A. read()
B. readcharacters()
C. readall()
D. readchar()
- Which file handling function is used to write all the characters to a file?
A. write()
B. writecharacters()
C. writeall()
D. writechar()
- Which file handling function is used to write a list of strings to a file?
A. writeline()
B. writelines()
C. writestatement()
D. writefullline()
- What will be the output of the following Python code?
fo = open(“foo.txt”, “r+”)
print (“Name of the file: “, fo.name)
Assuming file has following 5 lines
This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
for index in range(5):
line = fo.readline()
print (“Line No %d – %s” % (index, line))
Close opened file
fo.close()
A. Compilation Error
B. Syntax Error
C. Displays Output
D. None of the mentioned
- What will be the output of the following Python code snippet?
print(‘abcdefcdghcd’.split(‘cd’, 2))
A. [‘ab’, ‘ef’, ‘ghcd’]
B. [‘ab’, ‘efcdghcd’]
C. [‘abcdef’, ‘ghcd’]
D. none of the mentioned
- What will be the output of the following line?
print(‘ab\ncd\nef’.splitlines())
A. [‘ab’, ‘cd’, ‘ef’]
B. [‘ab\n’, ‘cd\n’, ‘ef\n’]
C. [‘ab\n’, ‘cd\n’, ‘ef’]
D. [‘ab’, ‘cd’, ‘ef\n’]
- What will be the output of the following Python code snippet?
print(‘Ab!2’.swapcase())
A. AB!@
B. ab12
C. aB!2
D. aB1@
- What will be the output of the following Python code snippet?
print(‘ab cd ef’.title())
A. Ab cd ef
B. Ab cd eF
C. Ab Cd Ef
D. None of the mentioned
- What will be the output of the following Python code snippet?
print(‘ab cd-ef’.title())
A. Ab cd-ef
B. Ab Cd-ef
C. Ab Cd-Ef
D. None of the mentioned
- What will be the output of the following Python code snippet?
print (‘xyyxyyxyxyxxy’.replace(‘xy’, ’12’, 0))
A. xyyxyyxyxyxxy
B. 12y12y1212x12
C. 12yxyyxyxyxxy
D. xyyxyyxyxyx12
- What will be the output of the following Python code snippet?
print(‘xyyxyyxyxyxxy’.replace(‘xy’, ’12’, 100))
A. xyyxyyxyxyxxy
B. 12y12y1212x12
C. none of the mentioned
D. error