Python interview questions part 13

Python interview questions part 13

Take as many assements as you can to improve your validate your skill rating

Total Questions: 20

1. What is the output of the following code? class Demo: def __init__(self): self.a = 1 self.__b = 1   def display(self): return self.__b obj = Demo() print(obj.a)

Correct Answer is : The program runs fine and 1 is printed

2. What is the output of the following code? class Demo: def __init__(self): self.a = 1 self.__b = 1   def display(self): return self.__b   obj = Demo() print(obj.__b)

Correct Answer is : The program has an error because b is private and hence can’t be printed

3. Methods of a class that provide access to private members of the class are called as ______ and ______

Correct Answer is : getters/setters

4. Which of these is a private data field? def Demo: def __init__(self): __a = 1 self.__b = 1 self.__c__ = 1 __d__= 1

Correct Answer is : __b

5. What is the output of the following code? class Demo: def __init__(self): self.a = 1 self.__b = 1   def get(self): return self.__b   obj = Demo() print(obj.get())

Correct Answer is : The program runs fine and 1 is printed

6. What is the output for the following piece of code? class Demo: def __init__(self): self.a = 1 self.__b = 1 def get(self): return self.__b obj = Demo() obj.a=45 print(obj.a)

Correct Answer is : The program runs properly and prints 45

7. Private members of a class cannot be accessed. True or False?

Correct Answer is : FALSE

8. The purpose of name mangling is to avoid unintentional access of private class members. True or False?

Correct Answer is : TRUE

9. What is the output of the following code? class fruits: def __init__(self): self.price = 100 self.__bags = 5 def display(self): print(self.__bags) obj=fruits() obj.display()

Correct Answer is : The program runs fine and 5 is printed

10. What is the output of the following code? class student: def __init__(self): self.marks = 97 self.__cgpa = 8.7 def display(self): print(self.marks) obj=student() print(obj._student__cgpa)

Correct Answer is : The program runs fine and 8.7 is printed

11. Which of the following is false about protected class members?

Correct Answer is : They can be accessed by name mangling method

12. What is the output of the following piece of code? class objects: def __init__(self): self.colour = None self._shape = "Circle"   def display(self, s): self._shape = s obj=objects() print(obj._objects_shape)

Correct Answer is : Error because the member shape is a protected member

13. How many except statements can a try-except block have?

Correct Answer is : more than zero

14. When will the else part of try-except-else be executed?

Correct Answer is : when no exception occurs

15. Is the following code valid? try: # Do something except: # Do something finally: # Do something

Correct Answer is : no, finally cannot be used with except

16. Is the following code valid? try: # Do something except: # Do something else: # Do something

Correct Answer is : yes

17. Can one block of except statements handle multiple exception?

Correct Answer is : yes, like except TypeError, SyntaxError [,…].

18. When is the finally block executed?

Correct Answer is : always

19. What is the output of the following code? def foo(): try: return 1 finally: return 2 k = foo() print(k)

Correct Answer is : 2

20. What is the output of the following code? def foo(): try: print(1) finally: print(2) foo()

Correct Answer is : 45293

Similar Interview Questions

    Search for latest jobs

    Icon
    Icon