Python MCQ Questions Set-10

Here is an interesting Python Multiple choice questions Quiz. Attempting this MCQ will help you to evaluate your knowledge and skills.

A. What will be the output of the following Python function?

re.findall(“hello world”, “hello”, 1)

  1. [“hello”]
  2. [ ]
  3. hello
  4. hello world

B. Which of the following functions results in a case insensitive matching?

  1. re.A
  2. re.U
  3. re.I
  4. re.X

C. Choose the function whose output can be: <_sre.SRE_Match object; span=(4, 8), match=’aaaa’>

  1. re.search(‘aaaa’, “alohaaaa”, 0)
  2. re.match(‘aaaa’, “alohaaaa”, 0)
  3. re.match(‘aaa’, “alohaaa”, 0)
  4. re.search(‘aaa’, “alohaaa”, 0)

D. What will be the output of the following Python code?

x = 50

def func(x):

    x = 2

func(x)

print(‘x is now’, x)

  1. x is now 50
  2. x is now 2
  3. x is now 100
  4. None of the mentioned

E. What will be the output of the following Python code?

x = 50

def func():

    global x

    print(‘x is’, x)

    x = 2

    print(‘Changed global x to’, x)

func()

print(‘Value of x is’, x)

a)

x is 50

Changed global x to 2

Value of x is 50

b)

x is 50

Changed global x to 2

Value of x is 2

c)

x is 50

Changed global x to 50

Value of x is 50

d) None of the mentioned

F. What will be the output of the following Python code?

def a(b):

    b = b + [5]

 c = [1, 2, 3, 4]

a(c)

print(len(c))

  1. 4
  2. 5
  3. 1
  4. An exception is thrown

G. What will be the output of the following Python code?

a=10

b=20

def change():

    global b

    a=45

    b=56

change()

print(a)

print(b)

a)

10

56

b)

45

56

c)

10

20

d) Syntax Error

H. How do you get the name of a file from a file object (fp)?

  1. fp.name
  2. fp.file(name)
  3. self.__name__(fp)
  4. fp.__name__()

I. How do you rename a file?

  1. fp.name = ‘new_name.txt’
  2. os.rename(existing_name, new_name)
  3. os.rename(fp, new_name)
  4. os.set_name(existing_name, new_name)

J. Which function is used to read a single line from a file?

  1. Readline()
  2. Readlines()
  3. Readstatement()
  4. Readfullline()

K. Which function is used to write a list of strings in a file?

  1. writeline()
  2. writelines()
  3. writestatement()
  4. writefullline()

L. What will be the output of the following Python code? (If entered name is dbms)

import sys

print ‘Enter your name: ‘,

name = ”

while True:

   c = sys.stdin.read(1)

   if c == ‘\n’:

      break

   name = name + c

print ‘Your name is:’, name

  1. dbms
  2. dbms, dbms
  3. dbm
  4. None of the mentioned

M. Correct syntax of file.writelines() is?

  1. file.writelines(sequence)
  2. fileObject.writelines()
  3. fileObject.writelines(sequence)
  4. none of the mentioned

N. What will be the output of the following Python code?

elements = [0, 1, 2]

def incr(x):

    return x+1

print(list(map(elements, incr)))

  1. [1, 2, 3]
  2. [0, 1, 2]
  3. error
  4. none of the mentioned

O. What will be the output of the following Python code?

x = [12.1, 34.0]

print(len(‘ ‘.join(list(map(str, x)))))

  1. 6
  2. 8
  3. 9
  4. error

Leave a Comment