Programming and Problem Solving Through Python Language Theory Question

Functions - Set 3

  1. What will be the output of the following Python code ?
     def printMax(a, b): 
     if a > b: 
    	 print(a, 'is maximum') 
     elif a == b: 
    	 print(a, 'is equal to', b) 
     else: 
     	print(b, 'is maximum') 
     printMax(3, 4) 
    
    1. 3
    2. 4
    3. 4 is maximum
    4. None of the mentioned
    Answer : C
    Explain : The codition of else is true so, print(b,'is maximum' ) which is 4 is maximum.
  2. What is the output of the following code ?
     x = 50 
     def func (x) : 
     x = 2 
     func (x) 
     print ('x is now', x)
     
    1. x is now 50
    2. x is now 2
    3. x is now 100
    4. Error
    Answer : A
    Explain : x= is global variable where as x=2 is local variable and in function the x value is 2 but when outside function x value is 50.
  3. What is the output of the following code ?
     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 these
    Answer : A
    Explain : TypeError: disp() got an unexpected keyword argument 'name' - this show when we run this. To run this program just remove the name & age. Then run this you will see- Rajat
    20.
  4. What is the return type of following function ?
    def func1():
    return 'mnp',22
    1. List
    2. dictionary
    3. string
    4. tuple
    Answer : D
    Explain : It is tuple value. For list we need to use [] brackets.
  5. What is the output of the 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 : By default the value of b is 6. But in this the value of change to 4. So a+b=a=9.
  6. An algorithm that calls itself directly or indirectly is called as __________.
    1. Sub Function
    2. Recursion
    3. Reverse Polish Notation
    4. Reverse Polish Notation
    Answer : B
    Explain : The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function.
  7. Function defined to achieve some task as per the programmer‟s requirement is called a __________.
    1. user defined function
    2. library function
    3. built in functions
    4. All of the above.
    Answer : A
    Explain : The function which is defined to achieve some task as per the programmer's requirement is called a user defined function.
  8. Which of the following is not a keyword ?
    1. eval
    2. nonlocal
    3. assert
    4. finally
    Answer : A
    Explain : eval is a built-in- function used in python, which is used to evaluates the specified expression.
  9. What will be the output of the following ?
     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 : This is recursive fucntion, which call itself automatically. 1st- value (3,6), 2nd time - (2,9), 3rd time- (1,11) and Last time (0,12). After that a==0, return b which is now 12.
  10. What will be the output of the following Python code ?
     def C2F(c) : 
     return c*9/5+32 
     print (C2F(100)) 
     print (C2F(0))
     
    1. 212.0
      32.0
    2. 212
      32
    3. 314
      32.0
    4. 567
      32.0
    Answer :
    Explain :
  11. Which of the following statement will execute in last ?
     def s(n1): #Statement 1 
     	print(n1) #Statement 2 
     n2=4 #Statement 3 
     s(n2) #Statement 4
    
    1. Statement 1
    2. Statement 2
    3. Statement 3
    4. Statement 4
    Answer :
    Explain :
  12. What is the output of following code ?
    def func(a, b=5, c=10) : 
     print('a is', a, 'and b is', b, 'and c is', c) 
     func(3, 7) 
     func(25, c = 24) 
     func(c = 50, a = 100) 
    
    1. a is 7 and b is 3 and c is 10
      a is 25 and b is 5 and c is 24
      a is 5 and b is 100 and c is 50
    2. a is 3 and b is 7 and c is 10
      a is 5 and b is 25 and c is 24
      a is 50 and b is 100 and c is 5
    3. a is 3 and b is 7 and c is 10
      a is 25 and b is 5 and c is 24
      a is 100 and b is 5 and c is 50
    4. None of the mentioned
    Answer : C
    Explain : By default the value of b=5 and c=10. But when function call the value given is change according to it.
  13. What will be the output of the following Python code ?
     x = 50 
     def func(x): 
     	print('x is', x) 
    	 x = 2 
     	print('Changed local x to', x) 
     func(x) 
     print('x is now', x)
     
    1. x is 50
      Changed local x to 2
      x is now 50
    2. x is 50
      Changed local x to 2
      x is now 2
    3. x is 50
      Changed local x to 2
      x is now 100
    4. None of Above.
    Answer : A
    Explain : The value of x=2 - in function because it is local variable. For outside the function x value is 50.
  14. Which one of the following is the correct way of calling a function
    1. function_name()
    2. call function_name()
    3. ret function_name()
    4. function function_name()
    Answer : A
    Explain : When we call a function, use fun_name()
  15. If a function does not have a return statement, which of the following does the function return ?
    1. int
    2. null
    3. An exception is thrown without return
    4. None
    Answer : D
    Explain :If a function doesn't specify a return value, it returns None

Next Set

1 2