OA Exams

  • web.groovymark@gmail.com
  • December 8, 2024

Question 41

How do you inherit a class in Python?

a) class SubClass inherits SuperClass:
b) class SubClass(SuperClass):
c) class SubClass inherit SuperClass:
d) class SubClass derives SuperClass:

Correct Answer: b) class SubClass(SuperClass):

Explanation: In Python, inheritance is achieved by specifying the parent class in parentheses after the subclass name.

Question 42

What will this Python code output?

class Parent:

    def __init__(self):

        self.value = 5

class Child(Parent):

    def __init__(self):

        Parent.__init__(self)

        self.value += 5

obj = Child()

print(obj.value)

a) 5
b) 10
c) 0
d) Error

Correct Answer: b) 10

Explanation: The Child class inherits from Parent and modifies the value attribute, adding 5 to it.

Question 43

How can you override a method in a subclass in Python?

a) By using def method_name(self):
b) By using super().method_name()
c) By defining a method with the same name in the subclass
d) All of the above

Correct Answer: c) By defining a method with the same name in the subclass

Explanation: You can override a method in a subclass by defining a method with the same name as in the parent class.

Question 44

What will this Python code output?

class MyClass:

    def __init__(self):

        self.value = 5

    def method(self):

        print(self.value)

obj = MyClass()

obj.value = 10

obj.method()

a) 5
b) 10
c) None
d) Error

Correct Answer: b) 10

Explanation: The value of the value attribute is changed to 10 before calling the method, so it prints 10.

Question 45

What is the result of this code?

class MyClass:

    def __init__(self):

        self.value = 5

obj = MyClass()

obj.new_value = 10

print(obj.new_value)

a) 5
b) 10
c) None
d) Error

Correct Answer: b) 10

Explanation: You can dynamically add new attributes to an object in Python.

Question 46

What is the correct syntax to call a parent class method in a subclass?

a) super().method_name()
b) parent_class_name.method_name()
c) this.method_name()
d) superclass_name.super().method_name()

Correct Answer: a) super().method_name()

Explanation: The super() function allows you to call methods from a parent class in a subclass.

Question 47

How do you define a private attribute in Python?

a) By prefixing the attribute with one underscore (_)
b) By prefixing the attribute with two underscores (__)
c) By using the private keyword
d) Python does not support private attributes

Correct Answer: b) By prefixing the attribute with two underscores (__)

Explanation: Python does not have strict private attributes, but prefixing an attribute with two underscores makes it harder to access from outside the class (name mangling).

Question 48

What is the result of this Python code?

class MyClass:

    def __init__(self):

        self.__value = 5

obj = MyClass()

print(obj.__value)

a) 5
b) Error
c) None
d) 0

Correct Answer: b) Error

Explanation: The attribute __value is private and cannot be accessed directly from outside the class.

Question 49

What is method overloading in Python?

a) When multiple methods with the same name are defined with different arguments
b) When a method has multiple return statements
c) When a method calls another method
d) Python does not support method overloading

Correct Answer: d) Python does not support method overloading

Explanation: Python does not support method overloading. If multiple methods with the same name are defined, the last one will override the previous ones.

Question 50

What is the difference between is and == in Python?

a) is checks for value equality, while == checks for reference equality
b) is checks for reference equality, while == checks for value equality
c) Both are the same
d) is checks for integer equality, while == checks for string equality

Correct Answer: b) is checks for reference equality, while == checks for value equality

Explanation: The is operator checks whether two references point to the same object, while == checks if their values are equal.

Complete the Captcha to view next question set.

Tags

Prev Post
WGU D522 Practice Exam Questions – Set 4 – Part 2
Next Post
WGU D522 Practice Exam Questions – Set 5 – Part 1