Function Practice Questions


  1. Predict the output of the following code:
    
    def Changer(P, Q=10):
        P=P/Q
        Q=P%Q
        return P
    
    A=200
    B=20
    A=Changer(A, B)
    print(A, B, sep='$')
    B=Changer(B)
    print(A, B, sep='$', end='###')
    
  2. 10.0$20
    10.0$2.0###

  3. Rao has written a code to input a number and check whether it is prime or not. His code is having errors. Rewrite the correct code and underline the corrections made.
    
    def prime():
        n=int(input("Enter number to check :: ")
        for i in range (2, n//2):
            if n%i=0:
                print("Number is not prime \n")
            break
            else:
                print("Number is prime \n')
    
  4. def prime():
        n=int(input("Enter number to check :: "))
        for i in range (2, n//2):
            if n%i==0:
                print("Number is not prime \n")
                break
            else:
                print("Number is prime \n")
    

  5. 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=' ')
    
  6. 22 # 40 # 9 # 13 #

  7. Write the output of the code given below:
    
    p=5
    def sum(q,r=2):
        global p
        p=r+q**2
        print(p, end= '#')
    
    a=10
    b=5
    sum(a,b)
    sum(r=5,q=1)
    
  8. 105#6#

  9. Consider the code given below :

    b=100
    def test(a):
          ______________  #missing statement
          b=b+a
          print(a, b)
    test(10)
    print(b)
    

    Which of the following statements should be given in the blank for #Missing Statement, if the output produced is 110?

    a. global a
    b. global b=100
    c. global b
    d. global a=100
    
  10. (c) global b

  11. Rewrite the following code in Python after removing all syntax error(s). Underline each correction done in the code.
    
    Value=30
    for VAL in range(0,Value)
        If val%4==0:
            print (VAL*4)
        Elseif val%5==0:
            print (VAL+3)
        else
            print(VAL+10)
    
  12. Value=30
    for VAL in range(0,Value):
        if VAL%4==0:
            print (VAL*4)
        elif VAL%5==0:
            print (VAL+3)
        else:
            print(VAL+10)
    

  13. Find and write the output of the following Python code:
    
    def Display(str):
        m=""
        for i in range(0,len(str)):
            if(str[i].isupper()):
                m=m+str[i].lower()
            elif str[i].islower():
                m=m+str[i].upper()
            else:
                if i%2==0:
                    m=m+str[i-1]
                else:
                    m=m+"#"
        print(m)
            
    Display('Fun@Python3.0')
    
  14. fUN#pYTHONn#.

  15. Find and write the output of the following python code:
    
    def Change(P ,Q=30):
        P=P+Q
        Q=P-Q
        print( P,"#",Q)
        return (P)
    
    R=150
    S=100
    R=Change(R,S)
    print(R,"#",S)
    S=Change(S)
    
  16. 250 # 150
    250 # 100
    130 # 100

  17. Find and write the output of the following python code:
    
    a=10
    def call():
        global a
        a=15
        b=20
        print(a)
        
    call()
    
  18. 15