{"id":23343,"date":"2020-07-11T16:54:34","date_gmt":"2020-07-11T16:54:34","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=23343"},"modified":"2025-05-17T06:21:04","modified_gmt":"2025-05-17T06:21:04","slug":"python-quiz-question","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/python-quiz-question\/","title":{"rendered":"Python Quiz Question"},"content":{"rendered":"<h2>Python Quiz Question<\/h2>\n<p>What is the primary purpose of the <strong>__init__<\/strong> method in Python classes?<\/p>\n<p>a) To define a class variable<br \/>\nb) To create an instance of the class<br \/>\nc) To initialize the instance variables of a class<br \/>\nd) To delete an instance of the class<\/p>\n<h2>Correct Answer<\/h2>\n<p>c) To initialize the instance variables of a class<\/p>\n<h2>Explanation<\/h2>\n<p>The<strong> __init__<\/strong> method in Python is a special method automatically called when a new class instance is created. Its primary purpose is to initialize the class&#8217;s instance variables. This method allows you to set an object&#8217;s initial state by assigning values to the instance variables. It is commonly used to pass values to the object during creation, ensuring it is properly initialized with the required attributes.<\/p>\n<h2>Code Listing<\/h2>\n<pre><code class=\"language-python\" data-line=\"\"># Python Quiz Question\n# Create an instance of the Person class\n# Python Tutorials - www.TestingDocs.com\n\nclass Person:\n    def __init__(self, name, age):\n        self.name = name  # Initialize &#039;name&#039;\n        self.age = age    # Initialize &#039;age&#039;\n\n    def display_info(self):\n        print(f&quot;Name: {self.name}, Age: {self.age}&quot;)\n\n# Create an instance of the Person class\nperson1 = Person(&quot;John&quot;, 30)\n\n# Display the initialized values\nperson1.display_info()<\/code><\/pre>\n<p><strong>Class Definition<\/strong>: The Person class is defined with an<strong> __init__<\/strong> method.<br \/>\n<strong>__init__<\/strong> Method: This method takes self, name, and age as parameters. It initializes the instance variables: name and age.<br \/>\n<strong>Creating an Instance<\/strong>: When person1 = Person(&#8220;John&#8221;, 30) is executed, the __init__ method is called with &#8220;John&#8221; and 30 as arguments, initializing the name and age of person1.<br \/>\n<strong>Displaying Information<\/strong>: The display_info method prints the values of the name and age instance variables.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-23349\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Python-Quiz-Question.png\" alt=\"Python Quiz Question\" width=\"1917\" height=\"1039\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Python-Quiz-Question.png 1917w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Python-Quiz-Question-300x163.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Python-Quiz-Question-1024x555.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Python-Quiz-Question-768x416.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Python-Quiz-Question-1536x833.png 1536w\" sizes=\"auto, (max-width: 1917px) 100vw, 1917px\" \/><\/p>\n<p>Running this code will output:<\/p>\n<p><em>Name: John, Age: 30<\/em><\/p>\n<p>This demonstrates how the <strong>__init__<\/strong> method is used to initialize instance variables of a class.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Quiz Question :<br \/>\nWhat is the primary purpose of the __init__ method in Python classes?<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[211],"tags":[],"class_list":["post-23343","post","type-post","status-publish","format-standard","hentry","category-python","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/23343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=23343"}],"version-history":[{"count":10,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/23343\/revisions"}],"predecessor-version":[{"id":27400,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/23343\/revisions\/27400"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=23343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=23343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=23343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}