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. 

  aptitude variable

  attribute

  archtype

  object

 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 an object that you can use in a program

  None of the above

  creating attributes and methods that attach to the object

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

  attribute creation

  mechanation

  construction

  objection

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

  finite classes

  None of the above

  abstract classes

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

  construct() method

  __init__() method

  def() method

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

  class

  return

  def

  All of the above can be used

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

  A class

  A constructor

  A 'None' method

  A value returning method

 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)

  x contains a reference to a Circle object.

  All of the above are valid

  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)

  123

  None of the above

  100

  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 attribute name to its value.

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

 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

  1

  49

 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(A, obj)

  isinstance(obj, 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'

  class Test: creates the object 'Test'

  None of the above

  class Test: creates the 'test' object called '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()

  fun

  self

  Error

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

  attribtue of an object

  attribute of a class

  None of the above

   object of a class

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

  Ruth Marvin

  self

  hi

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

  12

  10

  13

  52

 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 e

  unique to all cats

  Shared by all dogs

  unique to d

 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

  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.

  TRUE

  FALSE