Programming and Problem Solving Through Python Language Theory Question

Functions - Set 2

  1. Which of the following is the use of id() function in python ?
    1. id() returns the size of object.
    2. id() returns the identity of the object.
    3. Id() return the type of function
    4. None of above
    Answer : B
    Explain : The id() function returns a unique id for the specified object. All objects in Python has its own unique id. The id is assigned to the object when it is created. The id is the object's memory address, and will be different for each time you run the program.
  2. which function in python use to check memory location ?
    1. type()
    2. class()
    3. id()
    4. None of these.
    Answer : C
    Explain : We can see the location of the memory address of that value with the id() function.
  3. How many numbers will be printed by the following code ?
    def fun(a,b):
        for x in range(a,b+1):
            if x%3==0:
                print(x,end=" ")
    fun(100,120)
    
    
    1. 7
    2. 8
    3. 6
    4. 9
    Answer : A
    Explain : When we solve this we get 7 values which is 102 105 108 111 114 117 120.
  4. What will be the output of following statement ?
    def disp(*arg):
        for i in arg:
            print(i)
    disp(name="Rajat", age="20")
    
    
    1. TypeError
    2. Rajat 20
    3. Name age
    4. None of above
    Answer : A
    Explain : Because of arbitrary(*) argument, to make it run we need to use keyword argument which is (**) before arg.
  5. Which part of the memory does the system store the parameter and local variables of a function call
    1. heap
    2. stack
    3. Unintialized data segement
    4. None of these
    Answer : B
    Explain : The local variables and the parameters are always stored in the stack segment, where as the references are created at heap segements.
  6. What is the output of following code ?
    def iq(a,b):
        if (a==0):
            return b
        else:
            return iq(a-1,a+b)
    
    print(iq(3,6))
    
    
    1. 9
    2. 10
    3. 11
    4. 12
    Answer : D
    Explain : At the end it returns value of b which is 12 at last after solve.
  7. What will be the output of following code ?
    def maximum(x,y):
        if x>y:
            return x
        elif x==y:
            return 'The number are equal'
        else:
            return y
    print(maximum(2,3))
    
    
    1. 2
    2. 3
    3. The number are equal
    4. None of the above
    Answer : B
    Explain : In this else condition use which is return y. We get output 3.
  8. Which of the following function headers is correct ?
    1. def fun(x = 2, y = 3, z)
    2. def fun(x = 2, y, z = 3)
    3. def fun(x,y = 2, z = 3)
    4. def fun(x, y, z= 3, d)
    Answer : C
    Explain : In Python, a default parameter is defined with a fallback value as a default argument. Such parameters are optional during a function call.
  9. What will be the output of following code ?
    def  add(a,b):
        return a+5,b+5
    result= add(3,2)
    print(result)
    
    
    1. 15
    2. 8
    3. (8,7)
    4. Error
    Answer : C
    Explain : a=3 b=2 so it return 3+5, 2+5 which is (8,7)
  10. What will be the output of following code ?
    def say(message, times=1):
        print(message*times)
    say('hello')
    say('World',5)
    
    
    1. hello
      WorldWorldWorldWorldWorld
    2. Hello
      World 5
    3. hello
      World,World,Worl,dWorld,World
    4. Hello
      HelloHelloHelloHelloHello
    Answer : A
    Explain : First hello print then in new line World*5 use replicate methon and World come 5 times.
  11. What will be the output of the following Python code ?
    def additem(listParam):
        listParam +=[1]
    mylist = [1,2,3,4]
    additem(mylist)
    print(len(mylist))
    
    
    1. 5
    2. 8
    3. 4
    4. 1
    Answer : A
    Explain : It's important to note that lists in Python are mutable objects, meaning they can be modified in place. When you modify a list inside a function, the changes are reflected outside the function as well. After the function call, mylist is still [1, 2, 3, 4, 1].
  12. What is the output of following code ?
    def fun(a,b=6):
        a=a+b
        print(a)
    fun(5,4)
    
    
    1. 11
    2. 9
    3. 5
    4. 4
    Answer : B
    Explain : In this fun(5,4) b value changes to 4 then a=a+b =5+4 = 9.
  13. How is a function declared in Python ?
    1. def function function_name():
    2. declare function function_name():
    3. def function_name():
    4. declare function_name():
    Answer : C
    Explain : Correct syntax :- def fun_name():, here def is keyword and fun_name is function name which is depend on programmer.
  14. How many numbers will be printed by the following code ?
     def fun(a,b): 
     for x in range(a,b+1): 
     if x%3==0: 
     	print(x, end=" ") 
     fun(100,120)
     
    1. 7
    2. 8
    3. 6
    4. 9
    Answer : A
    Explain : Here a=100 and b=120, so function call for loop start from 100 - 121 (120+1). The value which is divided by 3 is print - 102,105,108,111,114,117,120.
  15. What is the output ?
     
     def calc(x): 
     r=2*x**2 
     return r 
     print(calc(5))
     
    1. Error
    2. 50
    3. 100
    4. 120232
    Answer : B
    Explain : In print, function is calling which give function value. r=2*5**2=2*25=50.

Next Set

1 3