17 Functions
17 Functions
1) #include<stdio.h>
int fun(int a,int b)
{
#define NUM 5
return a+b;
return(0);
}
int main()
{
printf("Sum=%d\n",fun(NUM,NUM));
return (0);
}
out put
(a) Compile-Time Error
(b) Run-Time Error
(c) Sum=10
(d) None of these
Answer: --c
2)
int f()
void main()
{
f(1);
f(1,2);
f(1,2,3);
}
f(int i,int j,int k)
{
printf("%d %d %d",i,j,k);
}
#include <stdio.h>
display()
{
printf ("\n Hello World");
return 0;
}
void main (void)
{
int (* func_ptr) ();
func_ptr = display;
printf ("\n %u",func_ptr);
(* func_ptr)();
}
Answer:-- it prints the address of the function display and prints Hello World
on the screen
4)
#include <stdio.h>
main()
{
int a=10;
void f();
a=f();
printf("\n%d",a);
}
void f()
{
printf("\nHi");
}
Answer :- In spite of de fining the func lionf() as returni ng void, the program is
trying to collect the value returned by f() in the variable a.
f(int a,int b)
{
int a;
a=10;
return a;
}
Answer:
Redeclaration of a
Answer:-- true
Ans:--False.
Because Any function including main( ) can we called recursively.
8) main()
{
int b ;
b=f ( 20 );
printl ( '%d', b ) ;
}
intf ( int a)
{
a >20 ? return (10) : return ( 20);
}
Ans :-
return statement cannot be used as shown with the conditional A
operators. Instead the following statement may be used:
10) #include<stdio.h>
int swap(int a,int b);
main() {
int temp;
int a=5,b=6;
temp=a;
b=a;
a=temp;
printf("%d%d%d\n",a,b,temp);
}
out put
a) 6 6 6
b) 5 5 5
c) 5 6 6
d) 5 6 5
Answer :-- b
11)
#include<stdio.h>
#define swap1(a,b) a=a+b;b=a-b;a=a-b;
#define swap2(a,b)
main()
{
int x=5,y=10;
swap1(x,y);
printf("%d %d\n",x,y);
swap2(x,y);
printf("%d %d\n",x,y);
}
out put
a) 10 10
b) 10 5
c) 5 10
d) 5 5
Answer:-- b
12) #include<stdio.h>
int main()
{
int a=5,b=6.5,c=7;
printf("%d %d ",a,b,c);
return(0);
}
out put
(a) Compile-Time Error
(b) 5 6.5
(c) 5 6
(d)5 6 7
answer:-- c
13) #include<stdio.h>
int add(int a,int b)
{
return a+b;
}
int main()
{
int a=1,b=2;
printf("%d",add(add(add(a,b),add(a,b)),add(add(a,b),add(a,b))));
return(0);
}
out put
(a) Garbage Value
(b) Compile-Time Error
(c) Run-Time Error
(d) 12
Answer:--- d
14) #include<stdio.h>
int check(int ,int );
int main()
{
int c;
c=check(10,20);
printf("c=%d\n",c);
return 0;
}
int check(int i,int j)
{
int *p,*q;
p=&i;
q=&j;
i>=45?return(*p):return(*q);
}
out put
a) compile error
b) print 10
c) print 20
d) print 1
Answer :-- a
15)
#include<stdio.h>
int main()
{
int i=3,j=4,k,l;
k=addmult(i,j);
l=addmult(i,j);
printf("%d %d\n",k,l);
return 0;
}
int addmult(int ii,int jj)
{
int kk,ll;
kk=ii+jj;
ll=ii*jj;
return(kk,ll);
}
out put
a) 12 12
b) compile error
c) Runtime error
d) No output
Answer: a
return doesn’t take two values at a time.return takes right to left precidency order so kk
value replaced by ll
16)The keyword used to transfer control from a function back to the function calling is
()
out put
a) goback
b) return
c) go to
d) switch
Answer :--b
17)
#include<stdio.h>
int main()
{
int fun(int);
int i=fun(10);
printf("%d\n",--i);
return 0;
}
int fun(int i)
{
return (i++);
}
out put
a) 7
b) 10
c) 9
d) 8
Answer : c
18) #include<stdio.h>
int main()
{
int i=1;
if(!i)
printf("india");
else
{
i=0;
printf("srilanka");
main();
}
return 0;
}
out put
19) #include<stdio.h>
int fun(char ch)
{
ch=ch+1;
return ch;
}
void main()
{
int a=127;
printf("%d %d",a,fun(a));
}
out put
a) compile error
b) 127 -128
c) 127 128
d) none of the above
Answer: b
20) #include<stdio.h>
char fun(int val)
{
char ch=val;
return ch;
}
void main()
{
float a=256.25;
printf("%d",fun(a));
}
out put
a) 256
b) 0
c) 256.25
d) None of the above
Answer:-- b
21) #include<stdio.h>
char fun(int val)
{
char ch=val;
return ch;
}
void main()
{
float a=256.25;
printf("%d",fun(a));
}
out put
a) 17
b) 0
c) compile-Time error
d) None of the above
Answer:-- a
Answer:-- d
23) A function may have any number of return statements each returning different
values.
A. True
B .False
Answer: A
Because of values and each return statements will not occur successively.
24) Names of functions in two different files linked together must be unique
A. True
B. False
Answer :--A
because of :- If two function are declared in a same name, it gives "Error: Multiple
declaration of function_name())".
25) #include<stdio.h>
double minimum(double, double);
int main()
{
printf("%f\n", minimum(1.23, 4.56));
return 0;
}
double minimum(double x, double y)
{
if (x > y)
return x;
else
return y;
}
out put
a) 4.56
b) 1.23
c) Error
d) No output
Answer :-- a
26)main( )
{
int a=1, b=2, c=3, sum ;
sum = calsum ( a, b, c ) ;
printf ( "\nSum = %d", sum ) ;
}
calsum ( x, y, z )
int x, y, z ;
{
int d ;
d=x+y+z;
return ( d ) ;
}
out put
a) 6
b) 10
c) 3
d) All the above
Answer:-- a
}
out put
A) a
B) ch
c) 32
d) Error
Answer :--d
28) main( )
{
int a = 10, b = 20 ;
swapv ( a, b ) ;
printf ( "\na = %d b = %d", a, b ) ;
}
swapv ( int x, int y )
{
int t ;
t=x;
x=y;
y=t;
printf ( "\nx = %d y = %d", x, y ) ;
}
out put
a) x=20 ,y=10
a=10 , b=20
b) x=10 , y=20
a=20, b=10
c) x=10 , y=10
a= 20, b=20
d) None of the above
Answer: -- a
29) main( )
{
int a=4, fact ;
fact = factorial ( a ) ;
printf ( "Factorial value = %d", fact ) ;
}
factorial ( int x )
{
int f = 1, i ;
for ( i = x ; i >= 1 ; i-- )
f=f*i;
return ( f ) ;
}
out put
a) 4,20
b) 20
c) 24
d) Error
Answer :--c
30) main( )
{
int i = 45, c ;
c = check( i * 1000 ) ;
printf ( "\n%d", c ) ;
}
check ( int ch )
{
if ( ch >= 40000 )
return ( ch / 10 ) ;
else
return ( 10 ) ;
}
out put
a) 10
b) 40000
c) 4500
c) 4000
Answer :--c
Answer:--b
out put
a) return 0
b) program()
c)system()
d)start()
e) main()
Answer:-- e
33)main()
void fun(int t);
main(){
f1();
}
f1()
{
fun(3);}
void fun(int t){
int s;
switch(t){
case 2:s=3;
case 3:s=4;
case 4:s=5;
case 5:s=6;
default:s=0;
}
printf("%d",s--);
}
out put
a) syntax error
b) 5
c)0
d)4
Ans:-- 4
34) main is itself a.------------------------.
Answer :-- function
35)
int funct1(int a){
{;}{{;}return a;}}
void main()
{
int a=17;
printf("%d",funct1(a));
}
out put
a. 0
b. compile-time error
c. 17
d. noneof these
Answer:-- c
36)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i=0;
i++;
if(i<=5)
{
printf("IndiaBIX");
exit(1);
main();
}
return 0;
}
out put
A. Prints "IndiaBIX" 5 times
B. Function main() doesn't calls itself
C. Infinite loop
D. Prints "IndiaBIx"
Answer: Option D
37).
#include<stdio.h>
int main()
{
printf("%p\n", main());
return 0;
}
out put
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: Option B
Explanation:
In printf("%p\n", main()); it calls the main() function and then it repeats infinetly,
untill stack overflow.
38).
#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);
}
out put
Answer: Option B
Explanation:
This program will create an error "Type mismatch in redeclaration of f". To overcome
this error, we have to add function prototype of f.
The correct form of function f prototype is float f(int, float);
38).
f(int a, int b)
{
int a;
a = 20;
return a;
}
out put
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: Option C
Explanation:
39).
Is it true that too many recursive calls may result into stack overflow?
A. Yes
B. No
Answer: Option A
Explanation:
Yes, too many recursive calls may result into stack overflow. because when a
function is called its return address is stored in stack.
40).{
char string[]="Hello World";
display(string);
}
void display(char *string)
{
printf("%s",string);
}
Answer:
Compiler Error : Type mismatch in redeclaration of function display
Explanation :
In third line, when the function display is encountered, the compiler doesn't know
anything about the function display. It assumes the arguments and return types to be
integers, (which is the default type). When it sees the actual function display, the
arguments and type contradicts with what it has assumed previously. Hence a compile
time error occurs.
50).
What will be the output of the following program :
void main()
{
int add(int,int);
int a=7,b=13;
printf("%d",add(add(a,b),add(a,b)));
}
int add(a,b)
int a,b;
{
return (a+b);
}
out put
(a) Compile-Time error
(b) 20
(c) 40
(d) None of these
Answer. --(c)
51).
Answer. --(a)
52).
Answer. --(a)
53).
Answer. (c)
53).
Answer. --(c)
54).
Answer. (a)
55).
What will be the output of the following program :
auto int a;
void changeval(int x)
{
a=x;
}
void main()
{
a=15;
printf("%d",a);
changeval(75);
printf("%d",a);
}
out put
(a) Compile-Time Error
(b) 15 75
(c) 15 15
(d) None of these
Answer. --(a)
56).
What will be the output of the following program :
static int count=1;
void funct3(void)
{
printf("%d",++count);
}
void funct2(void)
{
printf("%d",count);
funct3();
}
void funct1(void)
{
printf("Counting...%d",count++);
funct2();
}
void Main()
{
funct1();
}
out put
(a)Compile-Time Error
(b)Counting...123
(c)Counting...111
(d)Counting...112
Answer. (a) Since main() is different from Main() i.e. linker error
57).
58).
Answer. (c)
59).
What will be the output of the following program :
static int funct(int val)
{
static int sum;
sum+=val;
return sum;
}
void main()
{
int i,n=9;
for (i=1; i<n--; i++)
funct(i*2);
printf("%d",funct(0));
}
out put
(a) 20
(b) 0
(c) 30
(d) None of these
Answer. (a)
60).
What will be the output of the following program :
int val;
static int funct()
{
return val*val;
}
void main()
{
val=5;
funct();
val++;
printf("%d",funct());
}
out put
Answer. (c)
R091799
R091793
R091742