• Study Materials – Company wise
    • Infosys Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Dell Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • TCS Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation – E Books
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements
    • Quant for Placements
  • Register
  • Login
CODE OF GEEKS

We at CODE OF GEEKS, aim at providing best and quality content for our users at no extra cost.

    • Study Materials – Company wise
      • Infosys Archive
      • Accenture Archive
      • AMCAT Archive
      • Capgemini Archive
      • Cisco Archive
      • CoCubes Archive
      • Cognizant(CTS) Archive
      • Dell Archive
      • Deloitte Archive
      • DXC Archive
      • Goldman Sachs Archive
      • Hexaware Technologies Archive
      • LTI Archive
      • MindTree Archive
      • TCS Archive
      • Virtusa Archive
      • Wipro Archive
    • Interview Preparation – E Books
      • C Interview Questions
      • Data Structures Interview Questions
      • DBMS Interview Questions
      • HR Interview Questions
      • Java Interview Questions
      • Operating System Interview Questions
      • Python Interview Questions
      • SQL Query Interview Questions
    • Programming
      • C Programming MCQs
      • C Code Snippets – Output Questions
      • Python Code Snippets – Output Questions
    • Aptitude
      • Verbal Ability for Placements
      • Quant for Placements
    • Register
    • Login
  • [email protected]
  • ..
Login / Register
Apply Now
CODE OF GEEKS
CODE OF GEEKS
  • Study Materials – Company wise
    • Infosys Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Dell Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • TCS Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation – E Books
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements
    • Quant for Placements
  • Register
  • Login

Cart

Practice Python Output Questions : Code Snippets with Explanation

  • April 11, 2021
  • CODE OF GEEKS
  • 3

1. Find the output :

class Glove: 
    def __init__(self,color):
        self.__color = color 
    def get_color(self):
        return self.__color 
    def set_color(self,color):
        self.__color= color 
class Minion: 
    def __init__(self,glove): 
        self.__glove=glove 
        self.__color="Yellow"
    def get_glove(self):
        return self.__glove 
black_glove = Glove("Black")
red_glove = Glove("Red")
bob = Minion(black_glove)
black_glove.set_color(red_glove.get_color())
print(#line)

What should be placed in the place of #line to get the color of bob Minion’s glove ?

a. bob.__glove.__color
b. red_glove.get_color()
c. bob.get_glove().get_color()
d. bob.get_glove().__color

View Answer

Ans. c

Explanation : Option C is correct as bob is the object of Minion class

2. Find the output :

a = 8.3
b = 2
print(a//b)

a. 4.15
b. 4
c. 4.1
d. 4.0

View Answer

Ans. d

Explanation : ‘/’ would have given 4.15 as the answer. However, ‘//’ operator performs floor division.

3. Find the output :

s1={3, 4}
s2={1, 2}
s3=set()
i=0
j=0
for i in s1:
	for j in s2:
		s3.add((i,j))
		i+=1
		j+=1
print(s3)

a. {(3, 4), (1, 2)}
b. Error
c. {(4, 2), (3, 1), (4, 1), (5, 2)}
d. {(3, 1), (4, 2)}

View Answer

Ans. c

Explanation : Above code runs for

i = 3 j = 1
i = 4 j = 2
i = 4 j = 1
i = 5 j = 2
{(3, 1), (4, 1), (4, 2), (5, 2)}

 

4. Find the output :

class A():
def disp(self):
	print("A disp()")
class B(A):
	pass
obj = B()
obj.disp()

a. Invalid syntax for inheritance
b. Error because when object is created, argument must be passed
c. Nothing is printed
d. A disp()

View Answer

Ans. d

Explanation : We created an object of class B that inherits class A and so disp() method (of class A) becomes visible with reference to object obj. Option d is correct

 

5. Consider the following code :

class Customer: 
    def __init__(self,cust_id, cust_name):
        self.__cust_id = cust_id 
        self.__cust_name = cust_name
        self.__bill_amount = 0.0 
class RegularCustomer(Customer): 
    def __init__(self,cust_id, cust_name):
        self.__cust_id = cust_id 
        self.__cust_name = cust_name
        self.__bill_amount = 0.0 
        self.__discount = 5 

Identify the OOP principle(s) that has/have been implemented in the above code :

a. Only Encapsulation
b. Only Inheritance
c. Both Encapsulation and Inheritance
d. Only Polymorphism

View Answer

Ans. b

Explanation : Clearly, RegularCustomer class inherits Customer class, perfect example of Inheritance.

 

Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
  • Previous Hack with Infy 2021 – Expected Cutoff | Results | Maha Survey
  • Next InfyTQ Screening Test Last Year Questions – DBMS

3 comments on “Practice Python Output Questions : Code Snippets with Explanation”

  1. Anubhaba Sabat Anubhaba Sabat says:
    August 1, 2021 at 5:58 pm

    Question No.38 ….I don’t find any reason to tell option “C” is correct can you verify…
    because there will be 3 arguments given instead of 2

    Reply
  2. Anubhaba Sabat Anubhaba Sabat says:
    August 1, 2021 at 6:20 pm

    Question no 45 ….I think the 3rd line “global int” has missed proper indentation some how….

    Reply
  3. Anubhaba Sabat Anubhaba Sabat says:
    August 3, 2021 at 5:06 pm

    Question 61 . I was just wondering why the answer is “d” that is error…

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

CODE OF GEEKS

Subscribe to Newsletter

CODE OF GEEKS

Learn | Code | Achieve

Reach us

[email protected]
We at CODE OF GEEKS, aim at providing quality content to our users at no cost.
CODE OF GEEKS

Important Pages

About us
Advertise
Privacy Policy
Terms and Conditions

Placements – Study Materials

TCS NQT     Wipro     CapGemini
Accenture     MindTree     CTS
DXC     Hexaware Technologies     AMCAT
CoCubes     Goldman Sachs     Dell
Cisco     Deloitte     Virtusa     LTI     Infosys   

Courses

  • Mail Us
  • Login/Register

Recent Posts

return Statement in Python with Example
  • November 27, 2021
assert Statement in Python with Example
  • November 27, 2021

Copyright 2021 CODE OF GEEKS. All Rights Reserved.

  • →