Important Python Mcqs

What will be the output of the following Python code?

class Name:
    def __init__(self, firstName, mi, lastName):
        self.firstName = firstName
        self.mi = mi
        self.lastName = lastName


firstName = "John"
name = Name(firstName, 'F', "Smith")
firstName = "Peter"
name.lastName = "Pan"
print(name.firstName, name.lastName)

a) Peter Pan

b) John Pan

c) Peter Smith

d) John Smith

Answer:

b) John Pan

Explanation:

In the constructor, firstname, mi, and lastname of the object are assigned as John, F, and Smith respectively. Then, we changed firstname to Peter outside the class so it did not change the name object. Finally, we used using the name.lastname = “Pan” which changed lastname of the object to Pan, thus answer is b.

Related MCQs

The main method header is written as?

a. public static void main(string[] args) b. public static void Main(string[] args) c. public static void main(Strings[] args) d. public static void main(String[] args) Answer:

A Java variable is the name of a

a. data value stored in memory that can change its value but cannot change its type during the program’s execution b. data type stored in

Aniket Malik

Aniket Malik

CERTIFIED TUTOR/TRAINER WITH 300+ REVIEWS

Facing difficulty with

this concept?