Computer Science - Class 12
Solutions to CBSE Sample Paper - Computer Science Class 12

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=' ')

 

Answer:

Question 24 Choice 1 -Teachoo.png

Output: 22 # 40 # 9 # 13 #

Explanation:

  • The function Diff( ) takes 2 numbers as parameters and finds the difference by subtracting the smaller number from the larger number and returns the difference.
  • NUM is an array of 5 numbers.
  • The for loop iterates from 4 to 0 by decrementing by 1.
  • A and B are assigned 2 consecutive numbers from the array NUM and the difference is calculated and printed. This is repeated for all 2 consecutive numbers in the array.
Go Ad-free
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science and Computer Science at Teachoo