Function Board Questions


  1. State True or False : [1 MARK] [2024]

    While defining a function in Python, the positional parameters in the function header must always be written after the default parameters.
  2. False

  3. Observe the given Python code carefully : [1 MARK] [2024]

    a = 20
    def convert(a) :
        b = 20
        a = a+b
    convert(10)
    print(a)
    

    Select the correct output from the given options :

    (a) 10
    (b) 20
    (c) 30
    (d) Error
    
  4. (b) 20

  5. The code given below accepts five numbers and displays whether they are even or odd :
    Observe the following code carefully and rewrite if after removing all syntax and logical errors :
    Underline all the corrections made. [2 MARKS] [2024]

    def EvenOdd() 
        for i in range(5) :
            num = int(input("Enter a number ")
            if num/2==0 :
               print("Even")
            else:
            print("Odd")
    EvenOdd()
    
  6. def EvenOdd() :
        for i in range(5) :
            num = int(input("Enter a number "))
            if num%2==0 :
                print("Even")
            else:
                print("Odd")
    EvenOdd()
    

  7. Write a user defined function in Python named Puzzle(W, N) which takes the argument W as an English word and N as an integer and returns the string where every Nth alphabet of the word W is replaced with an underscore ("_"). For Example : if W contains the word "TELEVISION" and N is 3, then the function should return the string "TE_EV_SI_N". Likewise for the word "TELEVISION" if N is 4, then the function should return "TEL_VIS_ON". [2 MARKS] [2024]
  8. def puzzle(W,N):
        result=""
        for i in range(len(W)):
            if (i+1)%N==0:
                result+="_"
            else:
                result+=W[i]
        return result
    
    x=puzzle("TELEVISION",3)
    print(x)
    

  9. Write a user defined function in Python named showGrades(S) which takes the dictionary S as an argument. The dictionary, S contains Name : [Eng, Math, Science] as key : value pairs. The function displays the corresponding grade obtained by the students according to the following grading rules : [2 MARKS] [2024]

    Average of Eng, Math, Science Grade
    >=90 A
    <90 but >=60 B
    <60 C


    For example :
    Consider the following dictionary
    S={"AMIT" : [92,86,64], "NAGMA" : [65,42,43], "DAVID" : [92,90,88]}

    The output should be :
    AMIT - B
    NAGMA - C
    DAVID - A
  10. def showGrades(S):
        for i,j in S.items():
            avg=(j[0]+j[1]+j[2])/3
            if avg>=90:
                print(i,"- A")
            elif avg>=60 and avg<90:
                print(i,"- B")
            elif avg<60:
                print(i,"- C")
    
    
    D={"Amit":[92,86,64],"Nagma":[65,42,43],"David":[92,90,88]}
    showGrades(D)
    

  11. Predict the output of the following code : [2 MARKS] [2024]

    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)
    
  12. 300 # 100
    300 @ 200
    210 # 200
    300 @ 210

  13. Atharva is a Python programmer working on a program to find and return the maximum value from the list. The code written below has syntactical errors. Rewrite the correct code and underline the corrections made. [2 MARKS] [2023]
    
    def max_num(L):
          max=L(0)
          for a in L:
                if a>max
                max=a
          return max
    
  14. def max_num(L):
          max=L[0]
          for a in L:
                if a>max:
                      max=a
          return max
    

  15. Write the output of the code given below: [2 MARKS] [2023]
    
    def short_sub(lst,n):
          for i in range(0,n):
                if len(lst)>4:
                      lst[i]=lst[i]+lst[i]
                else:
                      lst[i]=lst[i]
    subject=['CS','HINDI','PHYSICS','CHEMISTRY','MATHS']
    short_sub(subject,5)
    print(subject)
    
  16. ['CSCS', 'HINDIHINDI', 'PHYSICSPHYSICS', 'CHEMISTRYCHEMISTRY', 'MATHSMATHS']

  17. Write the output of the code given below : [2 MARKS] [2023]
    
    a=30
    def call(x):
          global a
          if a%2==0:
                x+=a
          else:
                x-=a
          return x
    x=20
    print(call(35),end="#")
    print(call(40),end="@")
    
  18. 65#70@

  19. Predict the output of the code given below : [2 MARKS] [2023]
    
    def makenew(mystr):
        newstr = ""
        count = 0
        for i in mystr:
            if count%2 != 0:
                newstr = newstr + str(count)
            else:
                if i.lower():
                    newstr = newstr + i.upper()
                else:
                    newstr = newstr + i
            count += 1
        print(newstr)
    
    makenew("No@1")
    
  20. N1@3

  21. Write a function EOReplace( ) in Python, which accepts a list L of numbers. Thereafter, it increments all even numbers by 1 and decrements all odd numbers by 1. [3 MARKS] [2023]

    Example : 
    
    If Sample Input data of the list is :
    L= [10, 20, 30, 40, 35, 55]
    
    Output will be : 
    L= [11, 21, 31, 41, 34,54]
  22. def EOReplace(L):
          for i in range(len(L)):
                if L[i]%2==0:
                      L[i]=L[i]+1
                else:
                      L[i]=L[i]-1
    print(L)
    
    or
    
    def EOReplace():
          L=[]
          ch = 'y'
          while ch == 'y' or ch == 'Y':
                x = int(input('give item'))
                L.append(x)
                ch= input('do you want to enter more y/n ')
          for i in range(len(L)):
                if L[i]%2==0:
                      L[i]=L[i]+1
                else:
                      L[i]=L[i]-1
    print(L)