Python MCQ

Python MCQ Questions & Answers

The keyword we used to define a function is:

  • Define
  • call
  • def
  • function
View Answer

def

A function header consists of

  • function name
  • parameter list
  • return value
  • Both A and B
View Answer

Both A and B

Functions are coming under two categories:

  • Nested function and Private function
  • Local function and Private function
  • Built-in function and User-define function
  • Nested function and local function
View Answer

Builtin function user defined function

The function, which is defined under a class, is called

  • Class
  • Function
  • Method
  • Module
View Answer

Method

A function can be executed automatically without calling it

  • True
  • False
  • Depending on the python version
  • Depending on the type of the function
View Answer

False

The variable which is defined inside a function is called

  • Local variable
  • Global variable
  • Both A and B
  • None of the above
View Answer

Local variable

The variable which is defined outside the function is called

  • Local variable
  • Global variable
  • Both A and B
  • None of the above
View Answer

Global variable

The abs function returns the

  • The absolute value of the specific number
  • The average value of some numbers
  • Mean value for the list of numbers
  • None of the above
View Answer

The absolute value of a specified number

The output of bool(‘’) will return

  • True
  • False
  • Error
  • Nan
View Answer

False

The output of bool(‘False’) will return

  • True
  • False
  • Error
  • Nan
View Answer

True

For the given program
def mul(x):
print(3*x)
if we call the function like mul(“ Hello”) the output should be

  • Hello^3
  • Hello Hello Hello
  • Error
  • None of the above
View Answer

Hello Hello Hello

Parameters of a function can be defined

  • During the function creation
  • During the function calling
  • To find the bug in a function
  • None of the above
View Answer

During function creation

The output of int(False) should be

  • 1
  • 0
  • False
  • Error
View Answer

0

For the given program
def add(num1=2,num2=3):
return num1+num2

If we call the function as add(4) the output should be

  • 5
  • 4
  • 7
  • 13
View Answer

7

Which one of the following is the correct way of calling a function

  • function_name()
  • call function_name()
  • ret function_name()
  • function function_name()
View Answer

function_name()

You can also create your own functions, these functions are called

  • In-build function
  • User-defined function
  • Nested function
  • Local function
View Answer

User- defined function

What is a recursive function?

  • A function that calls other function
  • A function which calls itself
  • Both A and B
  • None of the above
View Answer

A function that calls itself

If return statement is not used inside the function, the function will return

  • None
  • 0
  • Null
  • Arbitrary value
View Answer

A function that calls itself

A list can contain ____ ‌elements:

  • Int
  • Float
  • String
  • All
View Answer

None

We can do matrix and array operations in a list:

  • True
  • False
  • True under certain conditions
  • False under certain conditions
View Answer
False

A list is a

  • Mutable collection of data
  • Immutable collection of data
  • Mutable but depend on the elements it contains
  • Immutable but depend on the elements it contains
View Answer
Mutable collection of data

The list is faster than a tuple

  • True
  • False
  • Both are taking a same amount of time
  • Sometimes it is true
View Answer
False

Space consumption of a tuple is lesser than a list

  • True
  • False
  • Can’t be predicted
  • It vary from one system to another
View Answer
True

We can use a negative index in python

  • True
  • False
View Answer
True

Elements of a list are

  • Ordered
  • Unordered
  • It depends on the elements
  • None of the above
View Answer
Ordered

The method to extract the last element of a list is

  • List_name[2:3]
  • List_name[-1]
  • List_name[0]
  • None of the above
View Answer
List_name[-1]

If List=[‘A’,5,6,’C’,8,’L’,’C’,’N’] then the method to extract[‘A’,5,6,’C’] from the list is

  • List[:]
  • List[0:4]
  • List[1:4]
  • List[0:4:-1]
View Answer

List[0:4]

If List=[‘A’,5,6,’C’,8,’L’,’C’,’N’] the method to print every even-index element from the list is

  • List[5:7]
  • List[0:7:-1]
  • List[:]
  • List[::2]
View Answer

List[::2]

If List=[‘A’,5,6,’C’,8,’L’,’C’,’N’] then the output of ‘List [::-1]’ is

  • [‘C’,8,’L’,’C’]
  • [‘A’,’C’,’E’]
  • [‘N’,’C’,’L’,8,’C’,6,5,’A’]
  • [‘A’,’C’,8,’C’]
View Answer

[‘N’,’C’,’L’,8,’C’,6,5,’A’]

To remove an element of a list, we use the attribute

  • add
  • index
  • pop
  • Delete
View Answer

pop

To add an element to a list, we use the attribute

  • append
  • copy
  • reverse
  • sort
View Answer

append

The function that convert a string to a list is

  • Str(string_value)
  • Bool(string_value)
  • Input(string_value)
  • List(string_value)
View Answer

Not available

If List=[[‘Emma’,23],45,{‘grapes’,’lemon’,’apples’},(23,’Music’,True)] then the output of List[0][0] is

  • 45
  • ‘Emma’
  • ‘Music’
  • None of the above
View Answer

Emma

The method we used to attach over one element individually in a list is

  • append
  • pop
  • extend
  • insert
View Answer

extend

Sets is a

  • An unordered collection of data
  • An ordered collection of data
  • Depend on the element it contains
View Answer

An unordered collection of data

To attach an extra element in a set we use

  • copy
  • attach
  • add
  • insert
View Answer

add

We can access an element in a set

  • True
  • False
  • Only for integer elements
  • Only for float elements
View Answer

False

The command we can use to define the length of a set is

  • len(Name_of_the_set_variable) 
  • Size(Name_of_the_set_variable)
  • Length(Name_of_the_set_variable)
  • Count(Name_of_the_set_variable)
View Answer

len(Name_of_the_set_variable)

Method that is used to delete an element from a set is

  • del
  • remove
  • delete
  • erase
View Answer

remove

The method we used to merge two different sets is

  • union
  • intersection
  • difference
  • symmetric_difference
View Answer

union

The attribute we used to figure out common elements between two sets is

  • union
  • intersection
  • difference
  • symmetric_difference
View Answer

intersection

If l1 is {‘jam’,’butter’,’banana’,’cack’,’Patise’} and l2 is {‘banana’,’eggs’,’chocolate’,’jam’,’cack’} then l1.difference(l2) is

  • {‘Patise’, ‘butter’}
  • {‘jam’,’butter’,’banana’}
  • {’banana’,’cack}
  • {‘jam’,’butter’}
View Answer

{‘Patise’,’butter’}

If tuple=(34,[34,’Honey’,True],{‘Pizza’,23.1,’Cloth’}) then value of tuple[1][0] is

  • ’Pizza’
  • 34
  • 23.1
  • [34,’Honey’,True]
View Answer

34

If dictionary = {1:’Rony’,2:’Anshul’,3:’Upasana’,4:’Madhab’,5:’Juhi’} then dictionary.keys() will show us

  • dict_keys([1, 2, 3, 4, 5])
  • dict_values([‘Rony’,’Anshul’,’Upasana’,’Madhab’,’Juhi’])
  • dict_items([(1, ‘a’), (2, ‘b’), (3, ‘c’), (‘d’, 4), (5.1, [‘e1’, ‘e5’, ‘e12’, ‘e11’])])
  • None of the above
View Answer

dict_values([‘Rony’,’Anshul’,’Upasana’,’Madhab’,’Juhi’])

In a dictionary keys and values are separated by

  • Colon
  • Comma
  • Apostopy
  • Dot
View Answer

Colon

In a dictionary every key-value pairs are separated from each other by

  • Colon
  • Comma
  • Apostopy
  • Dot
View Answer

Comma

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

obj1 = Student()
obj2 = obj1

  • Sikander
  • Sikander Sikander
  • No Output
  • Error
View Answer
Sikander

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

obj1 = Student()

  • 3
  • 2
  • 0
  • 1
View Answer
0

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

obj1 = Student()

  • 3
  • 2
  • 0
  • 1
View Answer
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.
View Answer
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.
View Answer
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
View Answer
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
View Answer
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.
View Answer

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()
View Answer
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.
View Answer
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)
View Answer
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
View Answer
findall

import re

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

  • 2
  • 3
  • 18
  • 17
View Answer
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
View Answer
[]

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

  • [‘s’, ‘i’, ‘k’, ‘n’, ‘r’]
  • [‘d’,’e’]
  • []
  • None of the above
View Answer
[‘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
View Answer
“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”]
View Answer
[‘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
View Answer
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
View Answer
Constructor display method

Enquire Now

Enquire Now

Enquire Now

Please Sign Up to Download

Please Sign Up to Download

Enquire Now

Please Sign Up to Download

Enquiry Form