Represents an Entity in the Real World That Can Be Distinctly Identified.

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on "Classes and Objects – ane".

ane. _____ represents an entity in the existent world with its identity and behaviour.
a) A method
b) An object
c) A course
d) An operator
View Answer

Answer: b
Explanation: An object represents an entity in the real world that can exist distinctly identified. A grade may define an object.

ii. _____ is used to create an object.
a) class
b) constructor
c) User-divers functions
d) In-built functions
View Reply

Reply: b
Explanation: The values assigned by the constructor to the class members is used to create the object.

3. What volition exist the output of the following Python lawmaking?

                  form                  examination:                  def                  __init__                  (                  self                  ,a=                  "Howdy World"                  ):                  self.a                  =a                  def                  display(                  self                  ):                  print                  (                  cocky.a                  )                  obj=                  test                  (                  )                  obj.brandish                  (                  )                

a) The plan has an fault because constructor can't have default arguments
b) Nix is displayed
c) "Hello World" is displayed
d) The programme has an mistake brandish function doesn't accept parameters
View Answer

Answer: c
Explanation: The program has no fault. "How-do-you-do Globe" is displayed. Execute in python shell to verify.

4. What is setattr() used for?
a) To access the attribute of the object
b) To gear up an aspect
c) To bank check if an attribute exists or not
d) To delete an attribute
View Respond

Respond: b
Explanation: setattr(obj,proper noun,value) is used to set an attribute. If attribute doesn't exist, so information technology would be created.

five. What is getattr() used for?
a) To access the attribute of the object
b) To delete an aspect
c) To check if an attribute exists or non
d) To set an attribute
View Answer

Answer: a
Explanation: getattr(obj,name) is used to become the attribute of an object.

6. What volition be the output of the following Python code?

                  class                  change:                  def                  __init__                  (                  self                  ,                  x,                  y,                  z):                  cocky.a                  =                  10 + y + z   x                  =                  change(                  i                  ,                  2                  ,                  three                  )                  y                  =                  getattr                  (x,                  'a'                  )                  setattr                  (x,                  'a'                  ,                  y+1                  )                  print                  (10.a                  )                

a) 6
b) vii
c) Error
d) 0
View Answer

Reply: b
Caption: First, a=1+two+3=half dozen. So, afterward setattr() is invoked, 10.a=6+one=seven.

7. What volition be the output of the following Python lawmaking?

                  class                  examination:                  def                  __init__                  (                  self                  ,a):                  self.a                  =a                  def                  display(                  self                  ):                  print                  (                  self.a                  )                  obj=                  test                  (                  )                  obj.display                  (                  )                

a) Runs usually, doesn't display anything
b) Displays 0, which is the automatic default value
c) Error equally one argument is required while creating the object
d) Error every bit display function requires additional argument
View Answer

Respond: c
Explanation: Since, the __init__ special method has another argument a other than self, during object cosmos, ane argument is required. For instance: obj=examination("Hello")

8. Is the following Python lawmaking correct?

                  >>>                  grade                  A:                  def                  __init__                  (                  self                  ,b):                  self.b                  =b                  def                  brandish(                  cocky                  ):                  print                  (                  self.b                  )                  >>>                  obj=A(                  "Hello"                  )                  >>>                  del                  obj

a) True
b) False
View Answer

Respond: a
Explanation: It is possible to delete an object of the class. On further typing obj in the python crush, it throws an fault considering the defined object has now been deleted.

ix. What volition be the output of the post-obit Python code?

                  form                  test:                  def                  __init__                  (                  self                  ):                  self.variable                  =                  'Old'                  self.Change                  (                  self.variable                  )                  def                  Alter(                  cocky                  ,                  var):         var                  =                  'New'                  obj=                  test                  (                  )                  impress                  (obj.variable                  )                

a) Error because part alter can't be called in the __init__ function
b) 'New' is printed
c) 'Old' is printed
d) Nothing is printed
View Answer

Respond: c
Explanation: This is because strings are immutable. Hence any change fabricated isn't reflected in the original string.

10. What is Instantiation in terms of OOP terminology?
a) Deleting an instance of class
b) Modifying an example of course
c) Copying an instance of class
d) Creating an instance of class
View Answer

Answer: d
Explanation: Instantiation refers to creating an object/instance for a class.

eleven. What will be the output of the following Python code?

                  class                  fruits:                  def                  __init__                  (                  cocky                  ,                  price):                  self.price                  =                  price obj=fruits(                  50                  )                  obj.quantity                  =                  ten                  obj.bags                  =                  two                  print                  (obj.quantity+len                  (obj.__dict__                  )                  )                

a) 12
b) 52
c) 13
d) 60
View Answer

Answer: c
Caption: In the in a higher place lawmaking, obj.quantity has been initialised to 10. There are a total of three items in the dictionary, price, quantity and bags. Hence, len(obj.__dict__) is iii.

12. What will be the output of the following Python lawmaking?

                  class                  Demo:                  def                  __init__                  (                  cocky                  ):                  pass                  def                  test                  (                  self                  ):                  print                  (__name__)                  obj                  =                  Demo(                  )                  obj.test                  (                  )                

a) Exception is thrown
b) __main__
c) Demo
d) exam
View Answer

Answer: b
Caption: Since the above code is beingness run not equally a result of an import from some other module, the variable volition have value "__main__".

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

Manish Bhojasia - Founder & CTO at Sanfoundry

Manish Bhojasia, a engineering science veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.

nagleaters1960.blogspot.com

Source: https://www.sanfoundry.com/python-questions-answers-classes-objects-1/

0 Response to "Represents an Entity in the Real World That Can Be Distinctly Identified."

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel