Exercise: Functions

Questions for: Functions

How do you pass a default value to a function parameter?
A:
function(parameter:=default_value)
B:
function(default_value=parameter)
C:
function(parameter, default_value)
D:
function(parameter=default_value)
Answer: D
Default values for function parameters are assigned using the parameter=default_value syntax.
What will be the result of the following code snippet?
def add_numbers(a, b):
    return a + b

result = add_numbers(3, 7)
print(result)
A:
10
B:
21
C:
'37'
D:
Error
Answer: A
The function add_numbers adds the values of a and b (3 + 7), resulting in 10.
What is the purpose of the def keyword?
A:
Defines a variable
B:
Declares a function
C:
Imports a module
D:
Checks the equality of two values
Answer: B
The def keyword is used to define a function in Python.
What will be the output of the following program?
#include<iostream.h> 
class Base
{
    public:
    char S, A, M; 
    Base(char x, char y)
    {
        S = y - y;
        A = x + x; 
        M = x * x;
    }
    Base(char, char y = 'A', char z = 'B')
    {
        S = y;
        A = y + 1 - 1; 
        M = z - 1;
    }
    void Display(void)
    {
        cout<< S << " " << A << " " << M << endl;
    }
};
class Derived : public Base
{
    char x, y, z; 
    public:
    Derived(char xx = 65, char yy = 66, char zz = 65): Base(x)
    {
        x = xx; 
        y = yy;
        z = zz;
    }
    void Display(int n)
    {
        if(n)
            Base::Display(); 
        else
            cout<< x << " " << y << " " << z << endl; 
    }
};
int main()
{
    Derived objDev; 
    objDev.Display(0-1); 
    return 0;
}
A:
A A A
B:
A B A
C:
A B C
D:
Garbage Garbage Garbage
Answer: A
No answer description is available. Let's discuss.
What will be the output of the following program?
#include<iostream.h> 
class Base
{
    public:
    int S, A, M; 
    Base(int x, int y)
    {
        S = y - y;
        A = x + x; 
        M = x * x;
    }
    Base(int, int y = 'A', int z = 'B')
    {
        S = y;
        A = y + 1 - 1; 
        M = z - 1;
    }
    void Display(void)
    {
        cout<< S << " " << A << " " << M << endl;
    }
};
class Derived : public Base
{
    int x, y, z; 
    public:
    Derived(int xx = 65, int yy = 66, int zz = 67): Base(x)
    {
        x = xx; 
        y = yy;
        z = zz;
    }
    void Display(int n)
    {
        if(n)
            Base::Display(); 
        else
            cout<< x << " " << y << " " << z << endl; 
    }
};
int main()
{
    Derived objDev; 
    objDev.Display(-1); 
    return 0;
}
A:
65 65 65
B:
65 66 67
C:
A A A
D:
A B C
Answer: A
No answer description is available. Let's discuss.
Ad Slot (Above Pagination)
Quiz