0% found this document useful (0 votes)
10 views26 pages

17 Functions

This document discusses various objective bits related to functions in C programming. It contains examples of code snippets and questions about functions. Some key points covered include passing arguments to functions, return types, recursion, and function pointers.

Uploaded by

devika pattem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views26 pages

17 Functions

This document discusses various objective bits related to functions in C programming. It contains examples of code snippets and questions about functions. Some key points covered include passing arguments to functions, return types, recursion, and function pointers.

Uploaded by

devika pattem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

OBJECTIVE BITS BELONGS TO FUNCTIONS CHAPTER

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);
}

Answer ::-- None

because we don’t put the semicolon to the prototype.


3)

#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.

5) what error would the following function give on compliation?

f(int a,int b)
{
int a;
a=10;
return a;
}
Answer:
Redeclaration of a

6) In a function two “return” statements should never occur successively.


a) True
b) False

Answer:-- true

7) In C all functions except main( ) can be called recursively.


a) True b)False

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:

return (a >20? 10 : 20) ;

9)When an array is passed as parameter to a function, which of the following statement


is correct choice:
a) The function can change values in the original array
b) In C parameters are passed by value. The funciton cannot change the original value
in the array
c) It results in compilation error when the function tries to access the
elements in the array
d) Results in a run time error when the funtion tries to access the elements in
the array

Answer: a) The function................

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

a) prints “srilanka infinetly”


b) Error:main()should not inside else statement
c) No output
d) prints “india”,srilanka infinetly
Answer:-- a

because we declare main( ) as the calling function

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

22) #include <stdio.h>


int mul(int, int);
int main()
{
int a = 0, b = 3, c;
c = mul(a, b);
printf("c = %d\n", c);
return 0;
}
int mul(int a, int b)
{
if(a == 0 || b == 0)
{
return 1;
}
else
{
return (a * b);
}}
out put
a) Error
b) c=3
c) c=0
d) c=1

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

27) fun(char a);


main(){
char ch ;

printf ( "\nEnter any alphabet " ) ;


scanf ( "%c", &ch ) ;
}
fun(char ch){
if ( ch >= 65 && ch <= 90 ){
return (ch);}
else{
return (ch+32);}

}
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

31) In a function two return statements should never occur.


Out put
a) TRUE b) FALSE

Answer:--b

32)what is the function in all C programs must contain?.

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

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 & Explanation

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:

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"

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

What will be the output of the following program :


int funct2(int b)
{
if (b == 0)
return b;
else
funct1(--b);
}
int funct1(int a)
{
if (a == 0)
return a;
else
funct2(--a);
}
void main()
{
int a=7;
printf("%d",funct1(a));
}
out put
(a) 0
(b) Compile-Time Error
(c) Infinite Loop
(d) 7

Answer. --(a)

52).

What will be the output of the following program :


int compute(int a,int b)
int c;
{
c=a+b;
return c;
}
void main()
{
int a=7,b=9;
printf("%d",compute(a,b));
}
out put
(a) Compile-Time Error
(b) 16
(c) None of these

Answer. --(a)
53).

What will be the output of the following program :


int a=10;
void compute(int a)
{
a=a;
}
void main()
{
int a=100;
printf("%d ",a);
compute(a);
printf("%d",a);
}
out put
(a)10 10
(b) Compile-Time Error
(c) 100 100
(d) 100 10

Answer. (c)
53).

What will be the output of the following program :


int funct(char ch)
{
ch=ch+1;
return ch;
}
void main()
{
int a=127;
printf("%d %d",a,funct(a));
}
out put
(a) Compile-Time Error
(b) 127 128
(c) 127 -128
(d) None of these

Answer. --(c)

54).

What will be the output of the following program :


char funct(int val)
{
char ch=val;
return ch;
}
void main()
{
float a=256.25;
printf("%d",funct(a));
}
out put
(a) 0
(b) 256.25
(c) 256
(d) None of these

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

What will be the output of the following program :


void print(int *);
void print(int *);
void main()
{
int x=100;
print(&x);
}
void print(int *a)
{
printf("%d",*a);
}
out put
(a) Compile-Time Error
(b) Run-Time Error
(c) 100
(d) None of these
Answer. (c)

58).

What will be the output of the following program :


void print(int a[],...)
{
while (*a != -1)
printf("%d",*a++);
}
void main()
{
int a[]={1,2,3,4,5,-1};
print(a,5,6,7,8,9,-1);
}
out put
(a) Compile-Time Error
(b) Run-Time Error
(c) 12345
(d) 56789

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

(a) Compile-Time Error


(b) 25
(c) 36
(d) None of these

Answer. (c)

R091799
R091793
R091742

You might also like