Python MCQ

Python MCQ Questions & Answers

What is the output:
class Student:
def __init__(self):
print(“”Sikander”” , end = “” “”)

obj1 = Student()
obj2 = obj1

  • Sikander
  • Sikander Sikander
  • No Output
  • Error

Sikander

How many instance members are there in Student class
class Student:
regno = 1
name = “”Sikander””
marks = 98

obj1 = Student()

  • 3
  • 2
  • 0
  • 1

0

How many instance members are there in Student class
class Student:
regno = 1
name = “”Sikander””
marks = 98

obj1 = Student()

  • 3
  • 2
  • 0
  • 1

0

How does defaultdict work?

  • defaultdict will automatically create a dictionary for you that has keys which are the integers 0-10.
  • If you try to access a key in a diction that doesn’t exist, defaultdict will create a new key for you instead of throwing a KeyError.
  • defaultdict forces a dictionary to only accept keys that are the data type specified when you created the defaultdict.
  • defaultdict stores a copy of a dictionary in memory that you can default to if the original gets unintentionally modified.

If you try to access a key in a diction that doesn't exist, defaultdict will create a new key for you instead of throwing a KeyError.

Which statement about static methods is true?

  • Static methods serve mostly as utility or helper methods, since they cannot access or modify a class’s state.
  • Static methods can be bound to either a class or an instance of a class
  • Static methods can access and modify the state of a class or an instance of a class.
  • Static methods are called static because they always return None.

Static methods serve mostly as utility or helper methods, since they cannot access or modify a class's state.

What does calling namedtuple on a collection type return?

  • a tuple subclass with iterable named fields.
  • a tuple subclass with non-iterable parameter fields.
  • a generic object class with iterable parameter fields.
  • a generic object class with non-iterable named fields

a tuple subclass with iterable named fields.

According to PEP 8 coding style guidelines, how should constant values be named in Python?

  • in lowercase with underscores to seperate words – eg., max_value = 255
  • in all caps with underscores seperating words – eg., MAX_VALUE = 255
  • in mixed case without using underscores to seperate words – e.g., MaxValue = 255
  • in camel case without underscores to seperate words – eg., maxValue = 255

in all caps with underscores seperating words - eg., MAX_VALUE = 255

Describe the functionality of a deque.

  • A deque adds items at either or both ends, and remove items at either or both ends.
  • A deque adds items only to the top, but remove items from either or both sides.
  • A deque adds items to one side and remove items from the other side.
  • A deque adds items to either or both sides, but only removes items from the top.

A deque adds items only to the top, but remove items from either or both sides.

What is the correct syntax for calling an instance method on a class named Game?

  • my_game = Game()self.my_game.roll_dice()
  • my_game = Game(self)
    self.my_game.roll_dice()
  • my_game = Game(self) my_game.roll_dice(self)
  • my_game = Game()my_game.roll_dice()

my_game = Game() my_game.roll_dice()

Which statement about class methods is true?

  • A class method holds all of the data for a particular class.
  • A class method is similar to a regular function, but a class method does not take any arguments.
  • A class method is regular function that belongs to a class, but it must return None.
  • A class method can modify the state of the class, but it cannot directly modify the state of an instance that inherits from that class.

my_game = Game() my_game.roll_dice()

Complete the code to return the output
fun = lambda _____ x + 2
print(fun(2))
print(fun(-5))

4
-3

  • x:
  • x =
  • x
  • (x)

x:

Complete the code to return the output
[’10 strawberries’, ‘2 apples’]

import re
recipe = “”I need 10 strawberries and 2 apples.””
print(re ____(“”\d+ [a-z]+””, recipe))

  • findall
  • finditer
  • find
  • search

findall

import re

print( len ( re.findall(“”[a-r]”” , “”varsity””) ) )

  • 2
  • 3
  • 18
  • 17

3

import re
sequence = “”india is my country””
res = re.findall(“”abc”” , sequence)
print(res)

Options:

  • [‘a’,’c’]
  • [‘a is my c’]
  • []
  • None of the above
[]

import re
sequence = “”sikander””
res = re.findall(“”[^abcde]”” , sequence)
print(res)

  • [‘s’, ‘i’, ‘k’, ‘n’, ‘r’]
  • [‘d’,’e’]
  • []
  • None of the above
['s', 'i', 'k', 'n', 'r']

address = [“”82″” , “”3rd floor”” , “”presidency building”” ,””st marks rd””, “”560001″”]

for word in address:
res = re.match(“”[^0-9]””, word)
if res != None:
print(word)

  • “82” , “3rd floor” , “560001”
  • “presidency building”, “st marks rd”
  • “82”, “560001”
  • None of the above

"presidency building", "st marks rd"

import re
sequence = “”6yrs@hkbk-16yrs@cranes””
print( re.findall(“”\w”” , sequence) )

  • [‘6’, ‘y’, ‘r’, ‘s’, ‘h’, ‘k’, ‘b’, ‘k’, ‘1’, ‘6’, ‘y’, ‘r’, ‘s’, ‘c’, ‘r’, ‘a’, ‘n’, ‘e’, ‘s’]
  • [‘y’, ‘r’, ‘s’, ‘h’, ‘k’, ‘b’, ‘k’, ‘y’, ‘r’, ‘s’, ‘c’, ‘r’, ‘a’, ‘n’, ‘e’, ‘s’]
  • [‘@’,’-‘,’@’]
  • [“yrs”,”hkbk”,”yrs”,”cranes”]
['6', 'y', 'r', 's', 'h', 'k', 'b', 'k', '1', '6', 'y', 'r', 's', 'c', 'r', 'a', 'n', 'e', 's']

class Student:
def Student(self):
print(“”Constructor””)
def display(self):
print(“”display method””)

obj = Student( )
obj.display()

  • Constructor display method
  • display method
  • Error
  • Constructor

Constructor display method

class Student:
def __init__(self, regno, name = “”Arshiya”” ):
self.regno = regno
self.name = name
print(“”Object Created “”)

def __del__(self):
print(“”Object Destroyed “”)

def __str__(self):
return str(self.regno) + “” “” + self.name

def fun( ):
s1 = Student(1, “”Sikander””)
return s1

x = fun()
print(“”Back in main “”)
print(x)

  • Object Created
    Object Destroyed
    1 Sikander
  • Object Created
    1 Sikander
    Object Destroyed
  • Object Created
    Object Destroyed
    1 Arshiya 
  • Object Created
    1 Arshiya
    Object Destroyed

Constructor display method

Want to know more about our courses?

Enquire Now

Enquire Now

Please Sign Up to Download

Please Sign Up to Download

Enquire Now

Please Sign Up to Download

Enquiry Form