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

  attribute

  archtype

  aptitude variable

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

  FALSE

  TRUE

 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 a class that acts as an object

  creating attributes and methods that attach to the object

  creating an object that you can use in a program

  None of the above

 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 ………………………

  construction

  objection

  mechanation

  attribute creation

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

  transparent classes

  finite classes

  None of the above

  abstract classes

 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

  construct() method

  __init__() method

  getattribute() method

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

  A class

  A method

  An object

  An attribute

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

  All of the above can be used

  class

  def

  return

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

  A value returning method

  A 'None' method

  A class

  A constructor

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

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

  All of the above are valid

  x contains a reference to a Circle object.

  x contains an int value.

 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

  Syntax error - this program will not run

  123

  None of the above

 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 its value.

  maps the value to the class

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

  maps the attribute name to the class

 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__))

  49

  51

  1

  2

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

  A.isinstance(obj)

  obj.isinstance(A)

  isinstance(obj, A)

  isinstance(A, obj)

 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()

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

  None of the above

  class Test: creates the object 'Test'

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

 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

  fun

  self

  Hello

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

  TRUE

  FALSE

 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()

   object of a class

  attribute of a class

  attribtue of an object

  None of the above

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

  Ruth Marvin

  hi

  self

  name

 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__))

  13

  52

  10

  12

 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

  unique to all cats

  Shared by all dogs

  unique to e

 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

  FALSE

  TRUE

 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

  FALSE

  TRUE

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

  FALSE

  TRUE