Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

Aastha wants to create a program that accepts a string and display the characters in the reverse in the same line using a Stack. She has created the following code,help her by completing the definitions on the basis of requirements given below:Class mystack :def inin (self):selfe. mystr= # Accept a stringself.mylist= # Convert mystr to a list# Write code to display while removing element from the stack.def display (self) :::

Answer»

class mystack :

def _init_ (self) :

self.myster= rawjnput ("Enter the string”)

self.mylist = list (self.mystr)

def display (self) :

x = len (self. mylist)

if (x > 0) :

for i in range (x) :

print self.mylist.pop (),

else :

print "Stack is empty”

2.

Write a program to input n numbers and to search any number from the list.

Answer»

n=input(“Enter no. of values”)

num=[]

flag=0

for i in range (n):

number=input(“Enter the number”)

num. append(number)

search = input(“Enter number to be searched")

for i in range(n):

if num[i]==search:

print search,“found at position”,i

flag=1

if flag==0:

print search, “not found in list”

3.

Write a program to input n numbers and to reverse the set of numbers without using functions.

Answer»

n=input(“Enter no. of values”)

num=[]

flag=0

for i in range (n):

number=input(“Enter the number”)

num. append(number)

j=n-1

for i in range(n):

if i<=n/2:

num[i],num[j] = num[j],num[i]

j=j-1

else:

break

print num

4.

Write a program to search input any customer name and display customer phone number if the customer name is exist in the list.

Answer»

def printlist(s):

i=0

for i in range(len(s)):

print i,s[i]

i = 0

phonenumbers = [‘9840012345’,‘9840011111’,’ 

9845622222’,‘9850012345’,‘9884412345’]

flag=0

number = raw_input(“Enter the phone number to be searched")

number = number.strip()

try:

i = phonenumbers.index(number)

if i >= 0:

flag=1

except ValueError:

pass

if(flag <>0):

print “\nphone number found in Phonebook at index”, i

else:

print'\iphonenumbernotfoundin phonebook”

print “\nPHONEBOOK”

printlist(phonenumbers)

5.

Write a program to find all the prime numbers up to a given number

Answer»

n = input("Enter the number”)

i = 2

flag = 0

while (i < n):

if (n%i)==0:

flag = 1

print n,“is composite”

break

i = i+ 1

if flag ==0 :

print n,“is prime”

6.

Write a program to convert decimal number to binary.

Answer»

i=1

s=0

dec = int ( raw_input(“Enter the decimal to be converted:”))

while dec>0:

rem=dec%2

s=s + (i*rem)

dec=dec/2

i=i*10

print “The binary of the given number is:”,s raw_input()

7.

Write a program to convert binary to decimal.

Answer»

binary = raw_input(“Enter the binary string”)

decimal=0

for i in range(len(str(binary))):

power=len (str (binary)) - (i+1)

decimal+=int(str(binary)[i])*(2**power)

print decimal

8.

Write a program to input two complex numbers and to find sum of the given complex numbers.

Answer»

areal = input("Enter real part of first complex number”)

aimg = input("Enter imaginary part of first complex number”)

breal = input("Enter real part of second complex number”)

bimg = input("Enter imaginary part of second complex number”)

totreal = areal + breal

totimg = aimg + bimg

print “Sum of the complex numbers=",totreal, “+i”, totimg

9.

Write a program to input two complex numbers and to implement multiplication of the given complex numbers.

Answer»

a = input("Enter real part of first complex number”)

b = input("Enter imaginary part of first complex number”)

c = input("Enter real part of second complex number”)

d = input("Enter imaginary part of second complex number”)

real= a*c - b*d

img= a*d + b*c

print “Product of the complex numbers=",real, “+i”,img

10.

Write a program to find the sum of all digits of the given number.

Answer»

n = inputfEnter the number”)

rev=0

while (n>0):

a=n%10

sum = sum + a

n=n/10

print “Sum of digits=”,sum

11.

Write a program to find the reverse of a number.

Answer»

n = input("Enter the number”)

rev=0

while (n>0):

a=n%10

rev=(rev*10)+a

n=n/10

print “Reversed number=”,rev

12.

Write a generator function generate sqr() that displays the square roots of numbers from 100 to n where n is passed as an argument.

Answer»

import math

def generatesq (n) :

for i in range (100, n) :

yield (math, sqrt (i))

13.

Write a program to print the pyramid. 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5

Answer»

for i in range(1,6):

for j in range(1,i+1):

print i,

print

14.

Write a method in Python to find and display the prime number between 2 to n. Pass n as argument to the method.

Answer»

def prime (N) :

for a in range (2, N):

for I in range (2, a):

if N%i ==0 :

break print a

OR

def prime (N):

for a in range (2, N):

for I in range (2, a) :

if a%1= = 0 :

break

else :

print a

15.

Write a method in Python and display the prime numbers between 2 to N. Pass as argument to the methods.

Answer»

def prime (N) :

for a in range (2, N)

Prime=1

for I in range (2, a):

if a%i= = 0 :

Prime = 0

if Prime = = 1:

print a

OR

def prime (N) :

for a in range (2, N):

for I in range (2, a) :

if a%i = = 0:

break

else :

print a

OR

Any other correct code performing the same

16.

Find and write the output of the following Python code:class Client:def init (self, ID = 1, NM=”Noname”) #constructorself.CID = IDself. Name = NMdef Allocate (self, changelD, Title) :self.CID = self.CID + Changeldself.Name = Title + self. Namedef Display (self) :print (self. CID). "#”, self. Name)C1 = Client ()C2 = Client (102)C3 = Client (205, ‘’Fedrick”)C1 . Display ()C2 . Display ()C3 . Display ()C2 . Allocate (4, "Ms.”)C3 .Allocate (2, "Mr.”)C1. Allocate (1, "Mrs.”)C1. Display ()C2 . Display ()C3 . Display ()

Answer»

CID Name

— Fedrick

102 Mr. Fedrick

205 Mrs. Fedrick

— Mr. & Mrs. Fedrick

17.

Write a program to input ,.any number and to check whether given number is Armstrong or not. (Armstrong 1,153,etc. 13 =1, 13+53 +33 =153)

Answer»

n = inputfEnter the number”)

savedn = n

sum=0

while n > 0:

a = n%10

sum = sum + a*a*a

n = n/10

if savedn == sum:

print savedn,“is an Armstrong Number”

else:

print savedn,”is not an Armstrong Number”

18.

What will be the output of the following Python code considering the following set of inputs?MAYAYour 5 ApplesMine2412Also, explain the try and except used in the code.Count = 0while True :try:Number=int (raw input ("Input a Number :"))breakExcept valueError :Count=Count + 2# For later versions of python, raw_input# Should be consider as inputmethods: – DenCal () # Method to calculate Density as People/Area – Add () # Method to allow user to enter values Dcode, DName, People, Area and Call DenCal () Mehtod – View () # Method to display all the data members also display a message “”High Population” if the Density is more than 8000.

Answer»

Output is below 

2 Re-Enter Number 

10 Re-Enter Number 

5 Input = Number 

3 Input = number 

Try and except are used for handling an exception in the Python code.

19.

Write a program to input username and password and to check whether the given username and password are correct or not.

Answer»

import string

usemame= raw_input(“Enter username”)

password = raw_input(“Enter password”)

if cmp(usemame.strip(),“XXX”)== 0:

if cmp(password,”123”) == 0:

print “Login successful”

else:

print “Password Incorrect”

else:

print “Username Incorrect”

20.

Write a program to input n numbers and to insert any number in a particular position.

Answer»

n=input(“Enter no. of values")

num=[]

for i in range (n):

number=input(“Enter the number") num.append(number)

newno = input(“Enter the number to be inserted”)

pos = input(“Enter position”) num.insert(newno,pos)

print num

21.

Write a program to input any string and to find the number of words in the string.

Answer»

str = “Honesty is the best policy”

words = str.split()

print len(words)

22.

Write a program to input any number and to print all the factors of that number.

Answer»

n = inputfEnter the number")

for i in range(2,n):

if n%i == 0:

print i,“is a factor of’.n

23.

Write a program to calculate the roots of a quadratic equation.

Answer»

import math

a = input(“Enter co-efficient of x^2”)

b = input(“Enter co-efficient of x”)

c = inputfEnter constant term”)

d = b*b - 4*a*c

if d == 0:

print “Roots are real and equal”

root1 = root2 = -b / (2*a)

elif d > 0:

print “Roots are real and distinct”

root1 = (- b + math.sqrt(d)) / (2*a)

root2 = (-b - math.sqrt(d)) / (2*a)

else:

print “Roots are imaginary”

print “Roots of the quadratic equation are”,root1,“and”,root2

24.

Write a program to calculate the area of a rectangle. The program should get the length and breadth ; values from the user and print the area.

Answer»

length = input(“Enter length”)

breadth = input(“Enter breadth”)

print “Area of rectangle =”,length*breadth