Multiple Choice Questions And Programs in C functions
Posted by
Ravi Kumar at Friday, September 16, 2011
Share this post:
|
Objective Questions, Multiple Choice Questions And Programs:
1) There is a mistake in the following code, add a statement
in it to remove it.
main()
{
int a;
a=f(10,3.14)
printf("%d",a);
}
f(int aa,float bb)
{
return((float)aa+bb);
}
Ans: Add the function prototype in main()
float f(int,float);
2) What error would the following function give on
compilation?
f(int a, int b)
{
int a;
a=20;
return a;
}
A. Missing parentheses in return statement
B. The function should be defined as int
f(int a, int b)
C.Redeclaration of a
D.None of the above
Ans: C
3) In a function two return statements should never
occur successfully.
TRUE or FALSE
Ans: TRUE
4) In C all functions except main() can be called
recursively. TRUE / FALSE
Ans: FALSE
5) Point out the error in the following code.
main()
{
int a=10;
void f();
a=f();
printf("\n %d",a);
}
void f()
{
printf("\n Hi");
}
Ans: Inspite of defining the function f() as returning
void, the program is trying to collect the value returned
by f() in variable a.
6) Point out the error, if any in the following program.
main()
{
int b;
b=f(20);
printf("%d",b);
}
int f(int a)
{
a>20 ? return(10) : return(20);
}
Ans: Return statement cannot be used as shown with the
conditional operators. Instead the following statement
may be used.
return(a>20 ? 10 : 20)
7) A function cannot be defined inside another function.
TRUE / FALSE
Ans: TRUE
8) Will the following function work? YES/NO
f1(int a, int b)
{
return(f2(20));
f2(int a)
{
return(a*a);
}
Ans: YES
9) In a function two return statements should never occur.
TRUE / FALSE
Ans: FALSE
10) Usually recursion works slower than loops.
TRUE / FALSE
Ans: TRUE
11) Is it true that too many recursive calls may result
into stack overflow? YES / NO
Ans: YES
12) What will be the output of the following program.
main()
{
C()
{
c()
{
printf(" C is a C......\n");
}
printf(".......... is a c..\n");
}
printf(".......... is a sea afterall!");
}
Output: Error message: Statement missing ; in function
main The compiler reports an error saying there is a
missing semicolon. But where is it missing? After C().
But suppose we only want to define the function C(),
logically we shouldn't be required to give a semi-colon
after C().That's the point. At the most you can call a
function from within the body of another function. you
are not allowed to define another function within the
body of another function. And that is what the above
program is attempting to do. It is trying to define C()
and c() in main(), which ia not acceptable,hence the error
message.
13)
main()
{
int z=4;
printf("%d",printf("%d%d",z,z);
}
Output: 4 4 3
Here the inner printf() is executed first, and it
prints out a 4, a space and another 4. Thus it totally
prints out 3 characters. Whenever the printf() function
is called it returns the number of characters it has
successfully managed to print.
14)
main()
{
int i;
printf("In the year of lord \n");
for(i=1;i40)
return(!m++);
else
return(!++m);
}
output: Error message: Redeclaration of 'm'in function
check The variable m used in check() has not been
defined before the opening brace. Therefore it is
assumed to be an integer variable.
22) main()
{
int i=45;
float c;
c=check(i);
printf("c=%f", c);
}
check(ch)
int ch;
{
ch>=45 ? return(3.14) : return(6.28);
}
Output: c=3.000000
Here, the condition ch>=45 is satisfied hence 3.14
gets returned. Does it really? No, because by default
any function is capable of returning only an int. Hence
3.14 gets truncated to 3 while returning the value.
23)C supports modular programming.
24)A function is a self contained program which is meant
to do some specific well defined task.
25)Return statement is used to exit from the function.
26)A function can receive many values, but it can return
only one value.
27)A function prototype tells the compiler the type of data
arguments and their order,the type of data function returns,
number of arguments.
28)Parameters are used to communicate data between the
calling and called function.
29)The four storage classes in c are automatic, static,
external and register.
30)Recursion is a process of calling a function within the
same function.