+ All Categories
Home > Documents > Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR....

Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR....

Date post: 11-Feb-2018
Category:
Upload: hatu
View: 241 times
Download: 5 times
Share this document with a friend
30
SOLUTIONS TUTORIAL-1 ( PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR FUNCTIONS,RECURSION,STORAGE CLASSES 1.After the function definition is executed,the control passes to the calling function. 2.A function that calls some other function is called calling function. 3.main() is called by the operating system 4.The inputs that the function takes are known as parameters or arguments. 5.Function that calls itself is known as recursive function 6.Recursion is implemented internally using stack 7.The function float func(); takes zero arguments. 8.Parameters used in function call are called actual Parameters and Parameters used in function header of function definition are called formal parameters 9.By default,the return type of a function is int (integer)
Transcript
Page 1: Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR. FUNCTIONS,RECURSION,STORAGE. CLASSES. 1.After the function definition is executed

SOLUTIONS

TUTORIAL-1 ( PROGRAMMING IN ‘C’)

PREPARED BY: Mr. DILIP KUMAR GANGWAR

FUNCTIONS,RECURSION,STORAGE CLASSES

1.After the function definition is executed,the control passes to the calling function.

2.A function that calls some other function is called calling function.

3.main() is called by the operating system

4.The inputs that the function takes are known as parameters or arguments.

5.Function that calls itself is known as recursive function

6.Recursion is implemented internally using stack

7.The function float func(); takes zero arguments.

8.Parameters used in function call are called actual Parameters and Parameters used in function header of function definition are called formal parameters

9.By default,the return type of a function is int (integer)

10.The execution of program starts at main ( )

11.The recursive function is defined as the function that calls itself.

12.Variables declared inside a function are known as localvariables

13.global variables can be accessed from all the functions in the program.

14.The function that is invoked is known as a)calling function b)called function

Page 2: Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR. FUNCTIONS,RECURSION,STORAGE. CLASSES. 1.After the function definition is executed

15.Function declaration tells these things about a function a)name b)arguments c) data type of return value d)all of these

16.Memory is allocated for a function when a function is a)declared b)called c) defined d)returned

17.The default storage class of global variables is a)auto b)static c)register d)extern

18. The default storage class of local variables is a)auto b)static c)register d)extern

19.Which type of variable retains its value between different function calls a)auto b)static c)register d)extern

20.Which variable cannot return any value to its calling function a)int b)float c)void d)char

True /False

21.The calling function must pass parameters to the called function. F

22.No function can be declared within the body of another function T

23.The function call is necessary to invoke the function. T

24.A C function can return only one value. T

25.Local variables overwrite the values of global variables F

26.Recursion uses divide and conquer technique to solve problems. T

27.A function must have at least one argument. F

28.A function can be defined in main() F

29.Variables name in the function definition must match with those specified in the function declaration. F

30 Specifying variables name in function declaration is necessary. F

31.The main() is a user defined function. T

Page 3: Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR. FUNCTIONS,RECURSION,STORAGE. CLASSES. 1.After the function definition is executed

32.The operating system has the declaration of main() T

33main() return the value to OPERATING FUNCTION.

34.default return type of main() is a) int b)void c) float

35. To return the control back to the calling function we must use the keyword return. F36. The same variable names can be used in different functions without any conflict . T38.Every called function must contain a return statement. F39.. A function may contain more than one return statements. T40. Each return statement in a function may return a different value. T41. A function may be called more than once from any other function. T42. It is necessary for a function to return some value. F43 If the CPU registers are not available, the register storage class variables are treated as static storage class variables. F(AS AUTO CLASS)44. The life of static variable is till the control remains within the block in which it is defined. F (till program doesnot ends)45. If a global variable is to be defined, then the extern keyword is necessary in its declaration. F46.One variable will have only one storage class F (STATIC VARIABLES CAN BE GLOBAL i.e extern as well as static )48.A variable that is defined outside all function can have static storage class. T49. If the variable x is defined as extern and a variable x is also defined as a local variable of some function, then the global variable gets preference over the local variable. F50.We can use any name for a function name. T (ACCORDING TO NAMING CONVENTION OF IDENTIFIERS)51. Any C program

(1) must contain at least one function. (2) need not contain any function. (3) needs input data.

Page 4: Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR. FUNCTIONS,RECURSION,STORAGE. CLASSES. 1.After the function definition is executed

(4) none of the above.

52. Return is a statement used to

(1)Return control back to calling function. (2)Return control and value back to calling function. (3)Return void. (4)Return value to the calling function.

53. The statement used to send back any value to the calling function is

(1) continue. (2)exit. (3)break. (4)return.

54. A ______________ function is one that returns no value.

(1) float. (2)recursive. (3)void. (4)integer.

55. Actual and formal parameters must agree in

(1) names and data types. (2)number of arguments and data types. (3)names and number of arguments. (4)data types.

56.void func( );The above function declaration indicates

(1) it returns nothing and no arguments. (2) it returns nothing and had arguments. (3) it returns a value and had arguments. (4) it returns a value and no arguments.

Page 5: Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR. FUNCTIONS,RECURSION,STORAGE. CLASSES. 1.After the function definition is executed

57.If the number of actual arguments are not matching with formal arguments then

(1) no error. (2)compiler error. (3)logical error. (4)syntax error.

58. Recursion means

(1) fuction calling a same function. (2)fuction with out a return value. (3)function calling a function. (4)passing a function to a function.

59.The names of actual parameters and formal parameters

(1) almost same. (2)should be same. (3)always same. (4)need not be same.

60.Any function can be called from any other function. this statement is

(1)neither true nor false. (2)true. (3)false. (4)true some times.

61. In a program, A function can be called

(1) only one time.

Page 6: Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR. FUNCTIONS,RECURSION,STORAGE. CLASSES. 1.After the function definition is executed

(2)only once. (3)any number of times. (4)only three times.

62.

How many values a function can return at a time

(1)only one (2)depends on the system (3) infinite values (4) 2

63.

which of the below statement's is not used inside an if statement

(1)goto (2)break (3)clrscr() (4)none of the above

64.

The minimum number of times the while loop is executed is

(1)1 (2)0 (3)cannot be predicted (4)2

65. The minimum number of times the do while loop is executed is

(1)1 (2)0 (3)cannot be predicted (4)2

66. A do while loop is useful when we want the statements within the loop must be executed

Page 7: Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR. FUNCTIONS,RECURSION,STORAGE. CLASSES. 1.After the function definition is executed

(1)atleast once (2)more than once (3)exactly once (4)none of the above

66. The minimum number of times the for() loop is executed is

(1)1 (2)0 (3)cannot be predicted (4)2

67. Within a switch statement

(1)Continue can be used but Break cannot be used

(2) Continue cannot be used but Break can be used (3) Both Continue and Break can be used (4) Neither Continue nor Break can be used

68. The break statement causes an exit

(1) from the innermost loop only. (2) only from the innermost switch. (3) from all loops & switches. (4) from the innermost loop or switch.

69.

Which of the following declaration of for statement is syntactically correct?

(1) for(); (2) for(;); (3) for(,); (4) for(;;);

Page 8: Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR. FUNCTIONS,RECURSION,STORAGE. CLASSES. 1.After the function definition is executed

Answer it!!!Question.Why we write the function declaration ?Done in class

Find the output of these codes.If error is /are present ,find the error/errors.Assume header files , getch() and return 0 in main are present.1.int f();main(){printf(“%d”,f() );printf(“%d”,f() );printf(“%d”,f() ); getch();return 0;}Int f(){int c=0;return 0;}Ans:0 0 0

2.int add(int,int);main(){ int a=2,b=4,c;printf(“%d %d”,a,b);

Page 9: Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR. FUNCTIONS,RECURSION,STORAGE. CLASSES. 1.After the function definition is executed

c=add(a,b);printf(“%d “,c);return 0;}int add(int a,int b){ int c=a+b;return c; }ans: 2 4 6

3.

int add(int,int);main(){ Int a=2,b=4,c;printf(“%d %d”,a,b);c=add(a,b);printf(“%d “,c);return 0;}Int add(int a,int b){ int c=a+b;return ; }ans: 2 4 garbage4.int add(int,int);main(){ Int a=2,b=4,c;printf(“%d %d”,a,b);c=add(a,b);printf(“%d “,c);return 0;}Int add(int a,int b){

Page 10: Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR. FUNCTIONS,RECURSION,STORAGE. CLASSES. 1.After the function definition is executed

int c=a+b; }ans: 2 4 garbage

5.Int f(int);Int a=10;main(){ Int a=45; Printf(“%d\n”,a);f(a);printf(“%d “,a);return 0;}void f(int a){ a=30;}Ans: 45 456.void f(char)Main(){ Char ch=256;f(); //error it should be f(ch) then ans=0 why? ask in classreturn 0;}Void f(char a){ Printf(“%d “,a);}

7.main( ){printf ( "\nC is excellent" ) ;

Page 11: Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR. FUNCTIONS,RECURSION,STORAGE. CLASSES. 1.After the function definition is executed

main( ) ;}

Ans:infinite printing till stack is not full8.

main( ) { int a ; a = message( ) ; } message( ) { printf ( "\nViruses are written in C" ) ; return ;

} ans: Viruses are written in C

9.

main( ) { float a = 15.5 ; char ch = 'C' ; printit ( a, ch ) ; } printit ( a, ch ) //error it should be printit (int a,int ch ){ printf ( "\n%f %c", a, ch ) ; }

Now answer is 0.000000 C10.

main( ) { message( ) ;

}message( ) ; { printf ( "\nPraise worthy and C worthy are synonyms" ) ; }

Page 12: Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR. FUNCTIONS,RECURSION,STORAGE. CLASSES. 1.After the function definition is executed

Ans: Praise worthy and C worthy are synonyms

11.main( ) { func( ) { printf ( "\nC is a Cimple minded language !" ) ; printf ( "\nOthers are of course no match !" ) ; } }

Ans:compile time error. a fn cannot be defined inside other function body.

12.#include<stdio.h>#include<conio.h>main( ){int k = 35, z ;z = check ( k ) ;printf ( "\n%d", z ) ;

getch();return 0;}check ( m ) //error if it is check(int m) the also pgm has error{int m ; //error is m redeclaredif ( m > 40 )return ( 1 ) ;elsereturn ( 0 ) ;}13.#include<stdio.h>#include<conio.h>main( )

Page 13: Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR. FUNCTIONS,RECURSION,STORAGE. CLASSES. 1.After the function definition is executed

{int k = 35, z ;z = check ( k ) ;printf ( "\n%d", z ) ;

getch();return 0;}check ( int m ){if ( m > 40 )return ( 1 ) ;elsereturn ( 0 ) ;}Ans: 014. main( ){Int i ;for ( i = 0 ; i <= 500 ; i++ )printf ( "\n%d", i ) ;}

Ans: 0 1 2 3 upto 50015.#include<stdio.h>#include<conio.h>int i = 0 ;main( ){printf ( "\nmain's i = %d", i ) ;i++ ;val( ) ;printf ( "\nmain's i = %d", i ) ;val( ) ;getch();return 0;}

Page 14: Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR. FUNCTIONS,RECURSION,STORAGE. CLASSES. 1.After the function definition is executed

val( ){i = 100 ;printf ( "\nval's i = %d", i ) ;i++ ;}Ans:

main's i = 0val's i = 100main's i = 101val's i = 100

16.void main( ) { int x, y, s = 2 ; s *= 3 ; //s=2*3=6y = f ( s ) ; //y=f(6)x = g ( s ) ; //x=g(6)printf ( "\n%d %d %d", s, y, x ) ;

getch();} int t = 8 ; f ( int a ) { a += -5 ; //a=6-5=1t -= 4 ; //t=8-4=4return ( a + t ) ; //return (1+4) i.e return (5)} g ( int a ) { a = 1 ; t += a ; //t=4+1=5return ( a + t ) ; return(1+5)}

Ans: 6 5 617. #include<stdio.h>#include<conio.h>main( )

Page 15: Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR. FUNCTIONS,RECURSION,STORAGE. CLASSES. 1.After the function definition is executed

{static int count = 5 ;printf ( "\ncount = %d", count-- ) ;if ( count != 0 )main( ) ;getch();}ANS:

count = 5count = 4count = 3count = 2count = 1 (STATIC variables retain their previous values)18. main( ){int i, j ;for ( i = 1 ; i < 5 ; i++ ){j = g ( i ) ;printf ( "\n%d", j ) ;}getch();return 0;}

g ( int x ) { static int v = 1 ; int b = 3 ; v += x ; return ( v + x + b ) ; }

19. #include<stdio.h>#include<conio.h>void main( ){

Page 16: Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR. FUNCTIONS,RECURSION,STORAGE. CLASSES. 1.After the function definition is executed

int a;void f(); // function f() declarationf();getch();}void f(){ int a=4; printf("%d",a);}Ans: 4 (a function can be declared at anywhere before it is called)20.

main( ) { func( ) ; func( ) ; } func( ) { auto int i = 0 ; register int j = 0 ; static int k = 0 ; i++ ; j++ ; k++ ; printf ( "\n %d % d %d", i, j, k ) ; }Ans:

1 1 1 1 1 2

Value of i and j will be initialised to 0 when we enter in func () i.e previous values of I and j will not be remembered.but previous value of k will be used in different calls as k is static.

21.

int x = 10 ; main( )

Page 17: Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR. FUNCTIONS,RECURSION,STORAGE. CLASSES. 1.After the function definition is executed

{ int x = 20 ; { int x = 30 ; printf ( "\n%d", x ) ; } printf ("\n%d", x ) ; }

Ans:30 2022.

main( ) { long num ; num = 2 ; printf ( "\n%ld", num ) ; }

Ans: 223. static int y ;main( ){static int z ;printf ("%d %d", y, z ) ;}Ans: 0 024. main( ){int k, j = 2 ;switch ( k = j + 1 ){case 0 :printf ( "\nTailor") ;case 1 :printf ( "\nTutor") ;case 2 :printf ( "\nTramp") ;default :printf ( "\nPure Simple Egghead!" ) ;

Page 18: Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR. FUNCTIONS,RECURSION,STORAGE. CLASSES. 1.After the function definition is executed

}}Ans: Pure Simple Egghead!Switch(k=2+1)=switch(k=3)= switch(3)=>goto case 3 there is no case 3 ,so goto default25. void main( ){int i = 0 ;switch ( i ){case 0 :printf ( "\nCustomers are dicey" ) ;case 1 :printf ( "\nMarkets are pricey" ) ;case 2 :printf ( "\nInvestors are moody" ) ;case 3 :printf ( "\nAt least employees are good" ) ;}getch();}Ans: Customers are dicey

26. void main( ){float a = 5, b = 2 ;int c ;c = a % b ;printf ( "%d", c ) ;getch();

}Ans: compile time error mod operator ( % ) need both operands as integer type here a is float type.27.

void main( ) {

Page 19: Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR. FUNCTIONS,RECURSION,STORAGE. CLASSES. 1.After the function definition is executed

int i = 10, j ; i >= 5 ? ( j = 10 ) : ( j = 15 ) ; printf ( "\n%d %d", i, j ) ;

getch();}

Ans:10 10

i >= 5 ? ( j = 10 ) : ( j = 15 ) ; 10>=5?( j = 10 ) : ( j = 15 ) ; 1? ( j = 10 ) : ( j = 15 ) ; =>j=10 is executed

28. main( ) { float x = 1.1 ; while ( x == 1.1 ) { printf ( "\n%f", x ) ; x = x – 0.1 ; } }

Ans:loop becomes false at the first time itself as 1.1 is not equal to 1.1 why? Ask in class29.

void main( ) { int i = 45, c ; c = multiply ( i * 1000 ) ; //multiply(45000)printf ( "\n%d", c ) ;

getch();} multiply ( int ch ) { if ( ch >= 40000 ) //if(45000>=40000) =>true=>return(45000/10)=>return (4500)return ( ch / 10 ) ; else return ( 10 ) ; }

Ans:450030. #include<stdio.h>

Page 20: Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR. FUNCTIONS,RECURSION,STORAGE. CLASSES. 1.After the function definition is executed

#include<conio.h>

void main( ){f1();f2();getch();}f1(){ x++; // error, x undeclared

printf("%d ",x);}int x=8;f2(){ x++; printf("%d ",x);}Ans:error, x undeclared 32. #include<stdio.h>#include<conio.h>

void main( ){f1();f2();getch();}f1(){ extern int x; x++; printf("%d ",x);}int x=8;

Page 21: Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR. FUNCTIONS,RECURSION,STORAGE. CLASSES. 1.After the function definition is executed

f2(){ x++; printf("%d ",x);}Ans: 9 10

33. #include<stdio.h>#include<conio.h>register int x;//error register variable caanot be globalvoid main( ){printf("%d ",x);

getch();}34. void main( ){ register int x;printf("%d ",x);

getch();}

35.void main( ){ register int x;scanf("%d ",&x);//errorprintf("%d ",x);

getch();}Ans: error as register do not have addresses so we cannot use &x,we can write direct value in x like register int x=45;

Page 22: Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR. FUNCTIONS,RECURSION,STORAGE. CLASSES. 1.After the function definition is executed

36.Int f();void main( ){ f();

getch();}Int f(){Int a=5; printf(“%d”,a); return a;}Ans: 537.Int f();void main( ){ int k; k=f();printf(“%d”,k);getch();}Int f(){Int a=5; printf(“%d\n”,a); return;}Ans: 5 garbage38.float f();void main( ){float k; k=f();printf(“%f”,k);getch();

Page 23: Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR. FUNCTIONS,RECURSION,STORAGE. CLASSES. 1.After the function definition is executed

}float f(){float a=5.45,b=7.8; printf(“%f\n”,a); return a,b;}Ans:compiler error due to type mismatch. Because we have not wriiten the declaration of f() .IMPORTANT POINT TO NOTE IS –“if we forget to write the declaration of a fn the compiler assumes for example here the declaration as int f() i.e a function with return type int and same number of argument as in function callBUT here return type of func f() was float so type mismatch occurs39.float f();void main( ){int k; k=f();printf(“%d”,k);getch();}float f(){float a=5.45,b=7.8; printf(“%f\n”,a); return a,b;// return statements returns last value i.e b}Ans:5.450000 7.800000

40.float f();void main( ){int k; k=f();printf(“%d”,k);getch();}

Page 24: Web viewSOLUTIONS. TUTORIAL-1 (PROGRAMMING IN ‘C’) PREPARED BY: Mr. DILIP KUMAR GANGWAR. FUNCTIONS,RECURSION,STORAGE. CLASSES. 1.After the function definition is executed

float f(){float a=5.45,b=7.8; printf(“%f\n”,a); return a;return b; //here a will be returned}Ans:5.450000 5 (return a=>return 5.45 but in main() k is int so we get 5


Recommended