Python Programming Quiz

Quiz on Function in Python

Time left: 300 seconds

Q.1. Which of the following are the two main parts of every function in Python?

Select Your Option

A function signature defines its name and arguments and a function body holds the code that runs whenever this function is called. These two are the main parts of every function in Python.

Q.2. Which of the following is true about user-defined functions in Python?

Select Your Option

There are specific nomenclature rules which a user should follow when defining a function in Python. They are:
1. Function names should be in lower case.
2. Use an underscore to join words in function name.

Q.3. What will be the output of the following Python code?
          
            def sayHello():
                print('Hello World!',end=" ") 
            sayHello()
            sayHello()
            
          
          

Select Your Option

Functions are defined using the def keyword. After this keyword comes an identifier name for the function, followed by a pair of parentheses which may enclose some names of variables, and by the final colon that ends the line. Next follows the block of statements that are part of this function. A function can be called as many times as required by use of its name followed by a pair of parentheses (enclosing parameters if required).

Q.4. Which of the following is not a user-defined function?

Select Your Option

Other than Option C, all other functions are in-built functions.

Q.5. What is the output of the following display_person() function call
          
            def display_person(*args):
                for i in args:
                    print(i, end=" ")

            display_person(name="Emma", age="25")
          
          

Select Your Option

TypeError will occure as display_person() function gets a unexpected arguement name and age. In the given snippet, *args expects to get a variable number of positional arguments but instead received a number of keyword arguments. To create functions that take n number of Keyword arguments we can use **kwargs (prefix a parameter name with a double asterisk ** ). Example: def displayPerson(**kwargs): print(kwargs) displayPerson(name="Emma", age=25)

Q.6. 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)
          
          

Select Your Option

Here, we define a function called printMax that uses two parameters called a and b. We find out the greater number using a simple if..else statement and then print the bigger number.

Q.7. What is the default value of a function?

Select Your Option

There is no default value of a function in Python.

Q.8. What will be output for the folllowing code?
          
            x = 50
            def func(x):
                print('x is', x, end="/ ")
                x = 2
                print('Changed local x to', x, end="/ ")
            func(x)
            print('x is now', x, end="/ ")
          
          

Select Your Option

The first time that we print the value of the name x with the first line in the function’s body, Python uses the value of the parameter declared in the main block(i.e., above the function definition). Next, we assign the value 2 to x. The name x is local to our function. So, when we change the value of x inside the function, the x defined in the main block remains unaffected. With the last print function call, we display the value of x as defined in the main block, thereby confirming that it is actually unaffected by the local assignment within the previously called function.

Q.9. What will be output for the folllowing code?
          
            def adder(x, y):
                print(x + y)
    
            adder("1","2")
          
          

Select Your Option

Since both parameters that are passed are string. Hence, they will be concatinated with each other by the + operator.

Q.10. What will be the output of the following Python code?
          
            def minimum(x, y):
                if x < y:
                    return x
                elif x == y:
                    return 'The numbers are equal'
                else:
                    return y
 
            print(minimum(2, 3))
          
          

Select Your Option

The minimum function returns the minimum of both the the parameters received. The numbers supplied to the function when it is called. The function uses a simple if..else statement to find the smaller value and then returns that value

Q.11. Consider the following function:
          
            def square(num):
                num_squared = num ** 2
                return num_squared
          
          
Which of the following lines of codes is the function’s signature?

Select Your Option

The function signature always starts with the def keyword, followed by the function’s name and any argument in parentheses.

Q.12. What is the output of the following code snippet?
          
            func = lambda x: return x
            print(func(2))
          
          

Select Your Option

A lambda function can’t contain the return statement. In a lambda function, statements like return, pass, assert, or raise will raise a SyntaxError exception.

Q.13. What is the output of the following code snippet?
          
            def say(message, times = 1):
                print(message * times,end=" ")
            say('Hello')
            say('World', 5)
          
          

Select Your Option

For some functions, you may want to make some parameters optional and use default values in case the user does not want to provide values for them. This is done with the help of default argument values. You can specify default argument values for parameters by appending to the parameter name in the function definition the assignment operator (=) followed by the default value. The given function is used to print a string as many times as specified. If we don’t supply a value, then by default, the string is printed just once. We achieve this by specifying a default argument value of 1 to the parameter times. In the first usage of say, we supply only the string and it prints the string once. In the second usage of say, we supply both the string and an argument 5 stating that we want to say the string message 5 times.

Q.14. Python function always returns a value

Select Your Option

If you do not include any return statement in function, it automatically returns None. So, in Python function always returns a value.

Q.15. What is the output of the following function call
          
            def outer_fun(a, b):
                def inner_fun(c, d):
                    return c + d

            return inner_fun(a, b)
            return a

            result = outer_fun(5, 10)
            print(result)
          
          

Select Your Option

Adding multiple return statements doesn’t perform any task. Once function execution is encountered with the return statement, it stops the execution by returning whatever specified by that return statement.

Instructions:-

  1. This Quiz is based on Function in Python
  2. Each correct answer will carry 2 points
  3. Once an option is selected you cannot select any other, So choose wisely
  4. Do not refresh the page as it will reset the quiz
  5. Once the time is up the quiz will automatically get submitted with no of Question responded
  6. Your total score alongwith your name will be shown after submission of the Quiz
  7. Wish you ALL THE BEST 👍👍

START

Results:-

Hi




Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post