Exercise: Python

Questions for: Functions

There is a error in the below program. Which statement will you add to remove it?
#include<stdio.h>

int main()
{
    int a;
    a = f(10, 3.14);
    printf("%d\n", a);
    return 0;
}
float f(int aa, float bb)
{
    return ((float)aa + bb);
}
A:
Add prototype: float f(aa, bb)
B:
Add prototype: float f(int, float)
C:
Add prototype: float f(float, int)
D:
Add prototype: float f(bb, aa)
Answer: B

The correct form of function f prototype is float f(int, float);

Which of the following statements are correct about the program?
#include<stdio.h>

int main()
{
    printf("%p\n", main());
    return 0;
}
A:
It prints garbage values infinitely
B:
Runs infinitely without printing anything
C:
Error: main() cannot be called inside printf()
D:
No Error and print nothing
Answer: B

In printf("%p\n", main()); it calls the main() function and then it repeats infinetly, untill stack overflow.

Point out the error in the program
#include<stdio.h>

int main()
{
    int a=10;
    void f();
    a = f();
    printf("%d\n", a);
    return 0;
}
void f()
{
    printf("Hi");
}
A:
Error: Not allowed assignment
B:
Error: Doesn't print anything
C:
No error
D:
None of above
Answer: A

The function void f() is not visible to the compiler while going through main() function. So we have to declare this prototype void f(); before to main() function. This kind of error will not occur in modern compilers.

Point out the error in the program
#include<stdio.h>
int f(int a)
{
  a > 20? return(10): return(20);
}
int main()
{
    int f(int);
    int b;
    b = f(20);
    printf("%d\n", b);
    return 0;
}
A:
Error: Prototype declaration
B:
No error
C:
Error: return statement cannot be used with conditional operators
D:
None of above
Answer: C

In a ternary operator, we cannot use the return statement. The ternary operator requires expressions but not code.

Point out the error in the program

f(int a, int b)
{
    int a;
    a = 20;
    return a;
}
A:
Missing parenthesis in return statement
B:
The function should be defined as int f(int a, int b)
C:
Redeclaration of a
D:
None of above
Answer: C

f(int a, int b) The variable a is declared in the function argument statement.

int a; Here again we are declaring the variable a. Hence it shows the error "Redeclaration of a"

Ad Slot (Above Pagination)
Quiz