Thursday 7 April 2022

Python if-else Interview questions

What are the three main conditional statements in Python?
Answer       if, elif, and else


. What will be the output of the following Python code?

names_1 = ["Narendra", "Boyina", "raj", "Mahalakshmi"]

if  'Raj' in names_1:

print(1)

else:

print(2)

a) None

b) 1

c) 2

d) Error


Answer: c

Explanation: Python is case sensitive, "if" condition failed because "Raj" is not available in that list, "raj" is available, so else block statement is executed.

========================================================================

A company decided to give a bonus of 5% to the employee if his/her year of service is more than 5 years.
Ask users for their salary and year of service and print the net bonus amount.

Answer:


salary = int(input("Enter salary"))

yos = int(input("Enter year of service"))

if yos>5:

  print "Bonus is",.05*salary

else:

  print "No bonus"

========================================================================

 Accept the kilometers covered  for the rented car and calculate the bill according to the following criteria:

First 10 Km              Rs11/km

Next 90Km               Rs 10/km

After that               Rs9/km

Answer
.

kmc = int(input("Enter the kilometer covered"))

if kmc <=10 :

    amt = kmc * 11

elif kmc > 10 and kmc <= 100:

    amt = 110 + (kmc - 10)*10

elif kmc > 100:

    amt = 1010 + (kmc - 100)*9

print("Total amount to pay is ", amt)

========================================================================



========================================================================

Write a program to calculate the electricity bill (accept the number of units from user) according to the following criteria :

             Unit                                                     Price  

First 100 units                                               no charge

Next 100 units                                              Rs 5 per unit

After 200 units                                             Rs 10 per unit

(For example, if the input unit is 350 then the total bill amount is Rs2000)


logic.

amt=0

numer_units=int(input("Enter number of electric unit"))

if numer_units<=100:

     amt=0

if numer_units>100 and numer_units<=200:

     amt=(numer_units-100)*5

if numer_units>200:

     amt=500+(numer_units-200)*10

print("Amount to pay :",amt)

========================================================================

Accept any city from the user and display a monument of that city.

  City                                 Monument

  Delhi                               Red Fort

  Agra                                Taj Mahal

  Jaipur                              Jal Mahal


 Answer

 

city = input("Enter name of the city")

if city.lower()=="delhi":

    print("Monument name is : Red Fort")

elif city.lower()=="agra":

    print("Monument name is : Taj Mahal")

elif city.lower()=="jaipur":

    print("Monument name is : Jal Mahal")

else:

print("Enter correct name of city")


========================================================================

Write a program to find the largest number out of three, excepted by the user.

 Answer


num1 = int(input("Enter first number"))
num2 = int(input("Enter second number"))
num3 = int(input("Enter third number"))
if num1 > num2 and num1 > num3:
      print("Greatest number is ", num1)
if num2 > num1 and num2 > num3:
      print("Greatest number is ", num2)
if num3 > num2 and num3 > num1:
     print("Greatest number is ", num3)
print("Enter correct name of city")

========================================================================

Accept the following from the user and calculate the percentage of class attended:

a.     Total number of working days

b.     Total number of absent days 

After calculating the percentage show that, If the percentage is less than 75 then the student will not be able to sit in the exam.

 Answer

 

nd = int(input("Enter total number of working days"))

na = int(input("Enter number of days  absent"))

per=(nd-na)/nd*100

print("Your attendance is ",per)

if per <75 :

     print("You are not eligible for exams")

else:

     print("You are eligible for writing exam")

========================================================================


                                Request: Please provide your valuable comments

No comments:

Post a Comment