Programming and Problem Solving Through Python Language Theory Question

Functions

  1. Which keyword is used for function ?
    1. fun
    2. define
    3. def
    4. Function
    Answer : C
    Explain :To define a function in python 'def' is reserved keyword.
  2. Which of the following items are present in the function header ?
    1. function name
    2. parameter list
    3. return value
    4. Both A and B
    Answer : D
    Explain : function header consists of function name and parameter list. Syntax : def function_name (parameter):
  3. What is called when a function is defined inside a class ?
    1. Class
    2. Function
    3. Method
    4. Module
    Answer : C
    Explain : When a function define inside a class it is called mehtod. Module- A python module is file contaning python definations and statements.
  4. If return statement is not used inside the function, the function will return:
    1. None
    2. 0
    3. Null
    4. Arbitary value
    Answer : A
    Explain : By default return value is None.
  5. What will be the output of following code ?
    def mes():
    	print( "hello")
    mes()
    
    1. hello
    2. HELLO
    3. hello hello
    4. Hello
    Answer : A
    Explain : When mes() it means function calling. Then the def mes() function use and print('hello').
  6. Which of the following is true about a function in Python ?
    1. A function can only be defined once in a program.
    2. A function can be defined within another function.
    3. A function cannot have parameters.
    4. A function can only return strings.
    Answer : B
    Explain : Function can be defined with in another function. You can see in next question.
  7. What will be the output of following code ?
    a= 5
    def he():
        b = a*a
        print(b)
        
    def sq():
        print('We get' )
        he()
        
    sq()
        
    
    1. We get
    2. We get 25
    3. We get 5
    4. None of above
    Answer : B
    Explain : In sq() function we call another function name he(). So first print We get then 25 in new line.
  8. What is the purpose of a function docstring in Python ?
    1. To define the function's name.
    2. To indicate the function's return value.
    3. To provide a brief description of the function's purpose.
    4. To specify the function's parameters.
    Answer : C
    Explain : Python documentation strings (or docstrings) provide a convenient way of associating documentation with Python modules, functions, classes, and methods. Remember one thing that comment does not show but docstring display on screen. To display docsring we use .__doc__ . Like print(function_name.__doc__) don't us ().
  9. What is the term used to describe variables declared inside a function ?
    1. Global variables
    2. Primary variables
    3. Secondary variables
    4. Local variables
    Answer : D
    Explain : When we describe variable inisde function it is Local Variable because it use inside that function not outside.Global Varibale is used outside function.
  10. What is the purpose of a return statement in a function ?
    1. It terminates the function execution.
    2. It defines the function's name.
    3. It provides a description of the function.
    4. It specifies the value to be returned by the function.
    Answer : D
    Explain : When a return statement is reached, Pyhton will stop the execution of the current function, sending a value out where the function was called.
  11. What will be the output of following code ?
    def calculate_square(number):
        result = number ** 2
        return result
    
    x = calculate_square(4)
    print(x)
    
    
    1. 8
    2. 16
    3. 4
    4. None
    Answer : B
    Explain : parameter set to 4 so when fucntion call result = 4**2 meanse 4*4 = 16. print(x) wants to print the value of x which is 16.
  12. What will be the output of following code ?
    def find_max(a, b):
        if a > b:
            return a
        else:
            return b
    
    result = find_max(8, 5)
    print(result)
    
    
    1. 8
    2. 5
    3. 8,5
    4. 5,8
    Answer : A
    Explain : 8 is greater than 5. So it return 8.
  13. What will be the output of following code ?
    x= 20
    def lo():
        x=10
        print("Local variable is ", x)
    
    lo()    
    print("Global Variable is ", x)
    
    
    1. Local variable is 10
      Global Variable is 20
    2. Local variable is 20
      Global Variable is 20
    3. Local variable is 10
      Global Variable is 10
    4. Global Variable is 20
      Local variable is 10
    Answer : A
    Explain : Here lo() function is called first, So Local variable is 10 first come. After that Global variable is 20
  14. Which of following is local variable ?
    y=20
    def va():
        y=5
    
    
    1. y=5
    2. y=20
    3. Both are local variable
    4. Non of them are local variable
    Answer : A
    Explain : Local variable are those which is write inside the function. So, here y=5 is written inside the function.
  15. What is a recursive function ?
    1. A function that calls other function.
    2. A function which calls itself.
    3. A function that is reserve.
    4. Both A and B
    Answer : B
    Explain : Python also accepts recursion, which means a defined function can call iteself.

Next Set

2 3