Font size
WorksheetsOOPs with Python
Total questions: 59
Worksheet time: 48mins
OOPs
Object Oriented Program
Object Oriented Programming
Object Opted Programming
Objective Oriented Programming
What create objects?
Classes
Functions
Methods
Modules
What are classes?
Blueprints/
Prototypes that defines methods and attributes
A thing which creates objects
A set of attributes and methods
A Blueprint of methods
What are Objects
Instances of a Class
Calling of a class
Identity of a class
Usables of a class
What are Attributes
Functions of a class
Variables of a class
Instance of a class
Variables of a code
What do Objects represent?
Uniqueness
Identity
Instance
Class
What refers to the Behavior of a class?
Attributes
Methods
Constructors
Self
What refers to the behavior of the class
Attribute
Class
Object
Method
what is self?
Self represents the instance of the class in the class
Binds the Attr and methods
A default thing in classes with no use
None of the Above
How to call a method?
Obj.method
Obj.method()
Method(object)
Method.object
Making of a class
class NAME:
class NAME():
class (NAME):
class: Name()
Which of the following is the use of function in python?
Functions are reusable pieces of programs
Functions don’t provide better modularity for your application
you can’t also create your own functions
All of the mentioned
Which keyword is used for function?
Fun
def
Def
fun
_____ represents an entity in the real world with its identity and behaviour.
A method
An object
A class
An operator
What type of inheritance is illustrated in the following Python code?
class X():
pass
class Y():
pass
class Z(X,Y):
pass
Multi-level inheritance
Multiple inheritance
Hierarchical inheritance
Single-level inheritance
What will be the output of the following Python code?
class A:
def __init__(self):
self._x = 7
class B(A):
def display(self):
print(self._x)
def main():
obj = B()
obj.display()
main()
Error, invalid syntax for object declaration
Nothing is printed
7
Error, private class member can’t be accessed in a subclass
What will be the output of the following Python code?
def say(message, times = 1):
print(message * times)
say('Hello')
say('World', 5)
Hello
WorldWorldWorldWorldWorld
Hello
World 5
Hello
World,World,World,World,World
Hello
HelloHelloHelloHelloHello
What will be the output of the following Python code?
def func(a, b=5, c=10):
print('a is', a, 'and b is', b, 'and c is', c)
func(3, 7)
func(25, c = 24)
func(c = 50, a = 100)
a is 7 and b is 3 and c is 10
a is 25 and b is 5 and c is 24
a is 5 and b is 100 and c is 50
a is 3 and b is 7 and c is 10
a is 5 and b is 25 and c is 24
a is 50 and b is 100 and c is 5
a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50
Error
What will be the output of the following Python code?
a=15
b=25
def change():
global b
a=60
b=70
change()
print(a)
print(b)
15
70
60
70
15
25
Error
How are variable length arguments specified in the function heading?
one star followed by a valid identifier
one underscore followed by a valid identifier
two stars followed by a valid identifier
two underscores followed by a valid identifier
What will be the output of the following Python code?
class A:
def test1(self):
print(" test of A called ")
class B(A):
def test(self):
print(" test of B called ")
class C(A):
def test(self):
print(" test of C called ")
class D(B,C):
def test2(self):
print(" test of D called ")
obj=D()
obj.test()
test of B called
test of C called
test of C called
test of B called
test of B called
Error, both the classes from which D derives has same method test()
What is the difference between a class and an object?
A class is a blueprint to create an object
An object is a blueprint to create a class
A blueprint is an object to create a class
Blueprint class is an object to create
Functions as Arguments
What does the code below print?
def sq(func, x):
y = x**2
return func(y)
def f(x):
return x**2
calc = sq(f, 2)
print(calc)
4
8
16
nothing, it will show an error
Function Calls
How many total lines of output will show up if you run the code below?
def add(x, y):
return x+y
def mult(x, y):
print(x*y)
add(5,3)
print(add(3,4))
mult(2,6)
print(mult(4,2))
0
2
4
6
Exceptions
try:
n = int(input("How old are you? "))
percent = round(n*100/80, 1)
print("You've gone through", percent, "% of your life!")
except ValueError:
print("Oops, must enter a number.")
except ZeroDivisionError:
print("Division by zero.")
except:
print("Something went very wrong.")
If the user enters "1" in the code above what does the program do?
prints "You've gone through 1.3 % of your life!"
prints "Something went very wrong."
If the user enters "thirty" in the code below what does the program do?
try:
n = int(input("How old are you? "))
percent = round(n*100/80, 1)
print("You've gone through", percent, "% of your life!")
except ValueError:
print("Oops, must enter a number.")
except ZeroDivisionError:
print("Division by zero.")
except:
print("Something went very wrong.")
prints "You've gone through 37.5 % of your life!"
prints "Division by zero."
Suppose C is a subclass of D, to invoke the __init__ method in D from C, what is the line of code you should write?
D.__init__(self)
C.__init__(self)
D.__init__(C)
C.__init__(D)
Error because class B inherits A but variable x isn’t inherited
0 0
0 1
Error, the syntax of the invoking method is wrong
Error, the syntax of the invoking method is wrong
The program runs fine but nothing is printed
1 0
1 2
Error as age isn’t defined
True
False
7
Old
New
error
Nothing is printed
12
52
13
60
As a Vehicle user, we know how to use it but its internal working are not known. In OOPs Concept, this principle is known as
Encapsulation
Inheritance
Abstraction
Polymorphism
people = ["Alice", "Eve", "Mallory"]
print (people[2])
what would this result be?
Alice
Eve
Mallory
Error
What does __init__ do?
Constructs the instance
Initialize the instance values
Calls the Super class
Creates a circle
What will be the output of the following Python code?
class A:
def __init__(self):
self._x = 7
class B(A):
def display(self):
print(self._x)
def main():
obj = B()
obj.display()
main()
Error, invalid syntax for object declaration
Nothing is printed
7
Error, private class member can’t be accessed in a subclass
Which keyword is used for function?
fun
define
def
function
What will be the output of the following Python code?
>>> lamb = lambda x: x ** 3
>>> print(lamb(7))
21
343
49
Error
Decides if a string only contains numbers
isdigit()
isnumeric()
float()
str()
If you want to check for multiple conditions in your code, what do you use (after if)?
elif
else
ifif
Which of the following blocks will be executed whether an exception is thrown or not?
except
finally
else
raise
The purpose of name mangling is to avoid unintentional access of private class members. True or False?
May be
True
False
None
Which of the following is false about protected class members?
They begin with one underscore
They can be accessed by subclasses
They can be accessed by name mangling method
They can be accessed within a class
Overriding means changing behaviour of methods of derived class methods in the base class. Is the statement true or false?
True
False
None
May be
What is the output of the following code, if the time module has already been imported?
4 + '3'
NameError
IndexError
ValueError
TypeError
The output of the code shown below is:
int("gprec")
ImportError
ValueError
TypeError
NameError
Can one block of except statements handle multiple exception?
yes
no
yes, like except TypeError, SyntaxError [,…].
yes, like except [TypeError, SyntaxError].
When is the finally block executed?
when there is no exception
when there is an exception
only if some condition that has been specified is satisfied
always
Pure OOP can be implemented without using class in a program. (True or False)
True
False
Which Feature of OOP illustrated the code reusability?
Polymorphism
Abstraction
Encapsulation
Inheritance
Which of the following is associated with objects?
State
Behaviour
Identity
All the above
Combining Data and Functions into a single unit is called
Abstraction
Inheritance
Encapsulation
Polymorphism
Advantages of OOPs
Code Reusability
Modular Programming
Easy Maintenance
Security
What is the purpose of the 'self' keyword in Python classes?
To refer to the current instance of the class
To create a new instance of the class
To access class-level variables
To define a private method
What is the output of the following Python code?
class A:
def __init__(self):
self._y = 10
class B(A):
def display(self):
print(self._y)
def main():
obj = B()
obj.display()
main()
Error, invalid syntax for object declaration
Nothing is printed
10
Error, private class member can’t be accessed in a subclass
What is the purpose of the 'finally' block in Python exception handling?
To handle the exception if it occurs
To execute code regardless of whether an exception is thrown or not
To raise a custom exception
To specify the type of exception to catch
