PYTHON QUIZ 8(Solution with the Answer)


PYTHON QUIZ 8

Note :- I have tried my best to give you the correct answer, I do not Guarantee that all the answers given by me are correct, so do it at your own risk, I just want to help you a little , so that you can understand the question.

Thankyou !

1. In python, a class is created by ____ keyword, and to create ____ of the class, we will have to use constructor of class.

·         def, file

·         class, object

·         self, .py file

·         None of these

Ans(b): class, object

Reason:  We start the class definition with the class keyword followed by the constructor of class.

2. What will be the output of the following python code?

class Roll:

    def __init__(self, id):

        self.id = id

        id = 231

        return id

 

val = Roll(321)

print (val.id)

·         TypeError

·         231

·         321

·         None of these

Ans(a): TypeError

Reason:  _init_ should return none.

 

3.  What will be the output of following python code?

class X:

    def __init__(self):

         self.a = 10

         self._b = 20

         self.b = 20

    def getB(self):

         return self.b

 

x = X()

x._b = 60

print(x.getB())

·         20

·         60

·         Error

·         Program runs but does not print anything

Ans(a): 20

Reason: at last self ._b=20  and def getB(self) Print(x.getB())

4. Private method in python starts with ____ while protected methods starts with ____ .

·         #, @

·         double underscore, single underscore

·         single underscore,double underscore

·         None of these

Ans(b): double underscore, single underscore

Reason: A double underscore prefixed to a variable makes it private.

5. What will be the output of the following code. Code is saved in a file named 'code.py' :

f = open('file.txt')

f.readlines()

print(f.name)

print ( f.closed )

f.close()

print ( f.closed )

1.Error in code

2.true

     false

     true

3.code.py

     False

     True

4.file.txt

     False

     True

Ans(a): Error in code

Reason: Because ‘file.text’ doesn’t in system database.


Hope you guys have liked this again and hey stay connected in this blog for more Information, Stay Safe, See you soon.

Till then Jai Hind and Vande Mataram ðŸ˜‡.

Comments

Popular Posts