Preview

03 - Instantiation

 1. From an object oriented programming perspective, a class is the blueprint for an ………………
Note: The following is OOP in Java - worth a read as you expose yourselves to different languages. 

  object

  archtype

  aptitude variable

  attribute

 2. A class simply describes what an object will look like and how it will behave.

  TRUE

  FALSE

 3. Therefore instantiation is the process of taking a class definition and ………………………………………..
So, at it’s core, instantiation is the process of taking a class, and creating an object from it that you can actually use in your program. (or simply, creating an object from a class)

  creating an object that you can use in a program

  creating attributes and methods that attach to the object

  None of the above

  creating a class that acts as an object

 4. An object is an instance of a class, and may be called a class instance or class object; instantiation is then also known as ………………………

  mechanation

  attribute creation

  construction

  objection

 5. Not all classes can be instantiated – ………………………. cannot be instantiated, while classes that can be instantiated are called concrete classes

  abstract classes

  finite classes

  transparent classes

  None of the above

 6. Like constructors in C++, we’ve got the ……………..in Python. This function gets called every time you instantiate the class. Please refer the below example to view a complete Python class.
class EmployeeData:

    def __init__(self, sal=0, age=0):
        self.sal = sal
        self.age = age

    def getData(self):
        print("{0}+{1}j".format(self.sal,self.age))

  def() method

  __init__() method

  getattribute() method

  construct() method

 7. Which Of The Following Represents A Distinctly Identifiable Entity In The Real World?

  A method

  A class

  An attribute

  An object

 8. Which Of The Following Keywords Mark The Beginning Of The Class Definition?

  All of the above can be used

  class

  return

  def

 9. Which Of The Following Is Required To Create A New Instance Of The Class?

  A 'None' method

  A value returning method

  A class

  A constructor

 10. Which Of The Following Statements Is Most Accurate For The Declaration X = Circle()?

  x contains a reference to a Circle object.

  x contains an int value.

   You can assign an int value to x (e.g radius of circle)

  All of the above are valid

 11. What is the output of the following code snippet?
class Sales:
    def __init__(self, id):
        self.id = id
        id = 100

val = Sales(123)
print (val.id)

  100

  123

  None of the above

  Syntax error - this program will not run

 12. Every object in Python has an attribute denoted by __dict__. This dictionary/dictionary-like object contains all the attributes defined for the object itself. It ……………………

  maps the attribute name to the class

  maps the value to the class

  maps the object to the value as well as to the class methods

  maps the attribute name to its value.

 13. What is the output of the following code that makes use of __dict__?
class Person:
    def __init__(self, id):
        self.id = id

sam = Person(100)

sam.__dict__['age'] = 49

print (sam.age + len(sam.__dict__))

  2

  51

  49

  1

 14. Which Of The Following Statements Can Be Used To Check, Whether An Object “Obj” Is An Instance Of Class A Or Not?

  isinstance(obj, A)

  A.isinstance(obj)

  isinstance(A, obj)

  obj.isinstance(A)

 15. In the following code, where is the object being created and what is the object?
# A simple example class
class Test:
     
    # A sample method 
    def fun(self):
        print("Hello")
 
# Driver code
obj = Test()
obj.fun()

  obj = Test() creates the object named 'obj'

  None of the above

  class Test: creates the 'test' object called 'obj'

  class Test: creates the object 'Test'

 16. What is the output of the following? Note particularly the last line - obj.fun()
# A simple example class
class Test:
     
    # A sample method 
    def fun(self):
        print("Hello")
 
# Driver code
obj = Test()
obj.fun()

  Error

  self

  Hello

  fun

 17. If we have a method which takes no arguments, then we still have to have one argument – the self

  FALSE

  TRUE

 18. The __init__ method is similar to constructors in C++ and Java. It is run as soon as an ……………………………….. is instantiated.
# A Sample class with init method
class Person:
 
    # init method or constructor 
    def __init__(self, name):
        self.name = name
 
    # Sample Method 
    def say_hi(self):
        print('Hello, my name is', self.name)
 
p = Person('Ruth Marvin')
p.say_hi()

  attribtue of an object

  None of the above

  attribute of a class

   object of a class

 19. Here, we define the __init__ method as taking one parameter ……………………………..(along with the usual self)

  Ruth Marvin

  name

  hi

  self

 20. What is the output from the following code?
class fruits:
    def __init__(self, price):
        self.price = price
obj=fruits(50)
 
obj.quantity=10
obj.bags=2
 
print(obj.quantity+len(obj.__dict__))

  12

  52

  13

  10

 21. In the following code, fill in the blanks for d.kind.
class Dog:

    kind = 'canine'         # class variable shared by all instances

    def __init__(self, name):
        self.name = name    # instance variable unique to each instance

>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.kind                  # ................?
'canine'
>>> e.kind                  # shared by all dogs
'canine'
>>> d.name                  # unique to d
'Fido'
>>> e.name                  # unique to e
'Buddy'

  unique to d

  Shared by all dogs

  unique to e

  unique to all cats

 22. It is good practice to define the attributes of an instance right after its creation.

  TRUE

  FALSE

 23. __init__ is a method which is immediately and automatically called after an instance has been created

  TRUE

  FALSE

 24. There is no explicit constructor or destructor method in Python, as they are known in C++ and Java but…(read the following excerpt) and decide whether it is true or false.
The __init__ method can be anywhere in a class definition, but it is usually the first method of a class, i.e. it follows right after the class header

  TRUE

  FALSE

 25. Guido van Rossum has designed the language according to the principle "first-class everything". In fact - everything in Python is a class.

  TRUE

  FALSE