Output Questions
#find output for given python code
LS= ["HIMALYAS","NILGIRI","ALASKA","ALPS"]
D={}
for S in LS:
if(len(S)%4==0):
D[S]=len(S)
for k in D:
print(k,D[k],end="# ")
Ans HIMALYAS 8# ALPS 4#
#Predict the output of the following code:
a="msp44@gmail.com"
s=" "
for i in a:
if i.isalpha():
s=s+i.upper()
elif i.isdigit():
s=s+"33"
else:
s=s+"@"
print(s)
#Predict the output of the following code:
#Ans: MK***IN@GMAIL@COM
a="mk168in@gmail.com"
s=""
for i in a:
if i.isalpha():
s=s+i.upper()
elif i.isdigit():
s=s+"*"
else:
s=s+"@"
print(s)
#Predict the output of the following code:
#Ans: M@K@***I@N@@G@M@A@I@L@@C@O@M@
a="mk168in@gmail.com"
s=""
for i in a:
if i.isalpha():
s=s+i.upper()
if i.isdigit():
s=s+"*"
else:
s=s+"@"
print(s)
"""Predict the output of the Python code given below : Ans iIDIA@gGroIiG """
s="India Growing"
n = len(s)
m=""
for i in range (0, n) :
if (s[i] >= 'a' and s[i] <= 'm') :
m = m + s [i].upper()
elif (s[i] >= 'O' and s[i] <= 'z') :
m = m +s [i-1]
elif (s[i].isupper()):
m = m + s[i].lower()
else:
m = m + '@'
print (m)
#Predict the output of the following code:
# Ans:
[12, 15, 10, 24, 32]
[72, 45, 21, 144, 65]
a=[12,15,10,24,32]
print(a)
for i in range( len(a)):
if (a[i]%2==0):
a[i]=a[i]*2
if (a[i]%3==0):
a[i]=a[i]*3
else:
a[i]=a[i]+1
print(a)
""" Predict the output of the following code : Ans : 6$30 """
def Total (Num=10):
Sum=0
for C in range(1,Num+1):
if C%2!=0:
continue
Sum+=C
return Sum
print(Total(4),end="$")
print(Total(),sep="@")
""" Write the Python statement for each of the following tasks using built-in
functions/methods only:
(i)To remove the item whose key is "NISHA" from a dictionary named Students.
For example,if the dictionary Students contains
{"ANITA":90,"NISHA":76,"ASHA":92},
then after removal
the dictionary should contain{"ANITA":90,"ASHA":92}
(ii)To display the number of occurrences of the substring "is" in a string named
message.
For example if the string message contains "This is his book",then the
output will be 3. """
Students ={"ANITA":90,"NISHA":76,"ASHA":92}
Students.pop("NISHA")
print(Students)
# del(Students["NISHA"])
# del Students["NISHA"]
message="This is his book"
print(message.count("is"))
# message.count("is")
""" ANS : {'ANITA': 90, 'ASHA': 92}
3
"""
"""
A tuple named subject stores the names of different subjects.Write the Python
commands to convert the given tuple to a list and there after delete the last element
of the list.
"""
subject=("Hindi","English","Computer Science","Physics")
subject=list(subject)
subject.pop()
print(subject)
"""
# OR
subject=list(subject)
subject.pop(-1)
OR
subject=list(subject)
del(subject[-1])
OR
subject=list(subject)
del subject[-1] """
""" Predict the output of the following code:
ANS:
300 # 100
300 @ 200
210 # 200
300 @ 210
"""
def callon(b=20,a=10):
b=b+a
a=b-a
print(b,"#",a)
return b
x=100
y=200
x=callon(x,y)
print(x,"@",y)
y=callon(y)
print(x,"@",y)
"""
Write the output on execution of the following Python code:
ANS:
R*A*C*E*C*A*R*
C#a#r#
R*A*D*A*R* """
S="Racecar Car Radar"
L=S.split()
for W in L:
x=W.upper()
if x==x[::-1]:
for I in x:
print(I,end="*")
else:
for I in W:
print(I,end="#")
print()
#Predict the output
mySubject = "Computer Science"
print(mySubject[0:len(mySubject)])
print(mySubject[-7:-1])
print(mySubject[::2])
print(mySubject[::-1])
Ans:
Computer Science
Scienc
Cmue cec
ecneicS retupmoC
# Predict the output
x,y=20,60
y, x, y=x,y-10,x+10
print(x,y)
""" Explanation
Ans: 50 30
x, y = 20, 60 ⇒ assigns an initial value of 20 to x and 60 to y.
y, x, y = x, y - 10, x + 10
⇒ y, x, y = 20, 60 - 10, 20 + 10
⇒ y, x, y = 20, 50, 30
First RHS value 20 is assigned to first LHS variable y. After that second RHS value 50 is assigned to second LHS variable x. Finally third RHS value 30 is assigned to third LHS variable which is again y. After this assignment, x becomes 50 and y becomes 30.
print (x, y) ⇒ prints the value of x and y as 50 and 30 respectively."""
#Predict the output
a, b = 12, 13
c, b = a*2, a/2
print (a, b, c)
Ans: 12 6.0 24
#Predict the output
a, b, c = 10, 20, 30
p, q, r = c - 5, a + 3, b - 4
print ('a, b, c :', a, b, c)
print ('p, q, r :', p, q, r)
Ans:
a, b, c : 10 20 30
p, q, r : 25 13 16
#Predict the output
a, b = 12, 13
print (print(a + b))
Ans:
25
None
#Predict the output of the Python code given below:
def Diff(N1,N2):
if N1>N2:
return N1-N2
else:
return N2-N1
NUM= [10,23,14,54,32]
for CNT in range (4,0,-1):
A=NUM[CNT]
B=NUM[CNT-1]
print(Diff(A,B),'#', end=' ')
Ans: 22 # 40 # 9 # 13 #
#Predict the output of the Python code given below:
tuple1 = (11, 22, 33, 44, 55 ,66)
list1 =list(tuple1)
new_list = []
for i in list1:
if i%2==0:
new_list.append(i)
new_tuple = tuple(new_list)
print(new_tuple)
Ans: (22, 44, 66)
#Write the output of the code given below:
my_dict = {"name": "Aman", "age": 26}
my_dict['age'] = 27
my_dict['address'] = "Delhi"
print(my_dict.items())
Ans:
dict_items([('name', 'Aman'), ('age', 27), ('address', 'Delhi')])
#Given is a Python string declaration:
myexam="@@CBSE Examination 2022@@"
#Write the output of:
print(myexam[::-2])
Ans: @20 otnmx SC@
#predict output
a=20
def convert(a):
b=20
a=a+b
convert(10)
print(a)
Ans: 20
#Predict the output
def ChangeVal(M,N):
for i in range(N):
if M[i]%5 == 0:
M[i]//=5
if M[i]%3 == 0:
M[i]//=3
L = [25,8,75,12]
ChangeVal(L,4)
for i in L:
print(i,end="#")
Ans:5#8#5#4#
#Predict the output
c = 10
def add():
global c
c = c + 2
print(c,end='#')
add()
c=15
print(c,end='%')
Ans:
12#15%
Data Structure Questions
""
Write separate user defined functions for the following :
(i)
PUSH(N) This function accepts a list of names, N as parameter.
It then pushes only those names in the stack named OnlyA
which contain the letter 'A'.
(ii) POPA(OnlyA) This function pops each name from the stack
OnlyA and displays it. When the stack is empty, the message
"EMPTY" is displayed.
For example :
If the names in the list N are
['ANKITA', 'NITISH', 'ANWAR', 'DIMPLE', 'HARKIRAT']
Then the stack OnlyA should store
['ANKITA', 'ANWAR', 'HARKIRAT']
And the output should be displayed as
HARKIRAT ANWAR ANKITA EMPTY """
N=['ANKITA', 'NITISH', 'ANWAR', 'DIMPLE', 'HARKIRAT']
ONLYA=[]
def PUSH(N):
for i in N:
if "A" in i:
ONLYA.append(i)
def POPA(ONLYA):
if(len(ONLYA)==0):
print("EMPTY")
else:
ONLYA.pop()
PUSH(N)
print(ONLYA)
POPA(ONLYA)
print(ONLYA)
"""Write the following user defined functions :
(i)
pushEven(N) This function accepts a list of integers named N
as parameter. It then pushes only even numbers into the stack
named EVEN.
(ii) popEven(EVEN) This function pops each integer from the
stack EVEN and displays the popped value. When the stack is
empty, the message "Stack Empty" is displayed.
For example :
If the list N contains
[10,5,3,8,15,4]
Then the stack, EVEN should store
[10,8,4]
And the output should be
4 8 10 Stack Empty """
N= [10,5,3,8,15,4]
EVEN=[]
def pushEven(N):
for i in N:
if(i%2==0):
EVEN.append(i)
def popEven(EVEN):
if(len(EVEN)==0):
print("Stack Empty")
else:
print(EVEN.pop())
pushEven(N)
print(EVEN)
popEven(EVEN)
popEven(EVEN)
popEven(EVEN)
popEven(EVEN)
"""A list contains following record of course details for a University :
[Course_name, Fees, Duration]
Write the following user defined functions to perform given operations on
the stack named 'Univ' :
(i)
Push_element() To push an object containing the
Course_name, Fees and Duration of a course, which has fees
greater than 100000 to the stack.
(ii)
Pop_element() To pop the object from the stack and display it.
also display "under flow " when stack is empty"""
courses=[["MCA",250000,3],["PGDCA",100000,1],["MSC",200000,2]]
Univ=[]
def Push_element():
for i in courses:
if(i[1]>100000):
Univ.append(i)
def Pop_element():
if len(Univ)==0:
print("Under Flow")
else:
print( Univ.pop() )
Push_element()
print(Univ)
Pop_element()
Pop_element()
Pop_element()
""" A dictionary, d_city contains the records in the following format :
{state:city}
Define the following functions with the given specifications :
(i) push_city(d_city): It takes the dictionary as an argument and
pushes all the cities in the stack CITY whose states are of more than
4 characters.
(ii) pop_city(): This function pops the cities and displays "Stack
empty" when there are no more cities in the stack. """
d_city={"Punjab":"Patiala", "Rajasthan":"Jaipur", "Hariyana":"Panipat"}
CITY=[]
def push_city(d_city):
for i in d_city.keys():
if(len(i)>4):
CITY.append(d_city[i])
def pop_city(CITY):
if(len(CITY)==0):
print("Stack empty")
else:
print(CITY.pop())
push_city(d_city)
print(CITY)
pop_city(CITY)
pop_city(CITY)
pop_city(CITY)
pop_city(CITY)
print(CITY)
""" Write a function in Python,
Push(DItem) where , DItem is a dictionary
containing the details of stationary items– {Sname:price}.
The function should push the names of those items in the stack who have price
greater than 75. Also display the count of elements pushed into the stack.
For example:
If the dictionary contains the following data:
Ditem={"Pen":106,"Pencil":59,"Notebook":80,"Eraser":25}
The stack should contain
Notebook
Pen
The output should be:
The count of elements in the stack is 2
"""
Ditem={"Pen":106,"Pencil":59,"Notebook":80,"Eraser":25}
stackItem=[]
def Push(DItem):
count=0
for k,v in DItem.items():
if (v>=75):
stackItem.append(k)
count=count+1
print("The count of elements in the stack is : ",count)
Push(Ditem)
print(stackItem)