Su Swagatam

The Rose flower is for someone special
Please don't touch it.



If u want
S----->Smiling
M----> Magnificient
A---->Awesome
R----> Refreshing
T---->Technical

STUFF

Then U R AT THE RIGHT PLACE


Wednesday, March 25, 2009

Test Ur C Skills

Chapter 1
Declarations and Initializations


Q 1.1
What would be the output of the following program?
main( )
{
char far *s1, *s2 ;
printf ( "%d %d", sizeof ( s1 ), sizeof ( s2 ) ) ;
}

Q 1.2
What would be the output of the following program?
int x = 40 ;
main( )
{
int x = 20 ;
printf ( "\n%d", x ) ;
}

Q 1.3
What would be the output of the following program?
main( )
{
int x = 40 ;
{
int x = 20 ;
printf ( "\n%d ", x ) ;
}
printf ( "%d", x ) ;
}

Q 1.4
Is the following statement a declaration or a definition?
extern int i ;

Q 1.5
What would be the output of the following program?
main( )
{
extern int i ;
i = 20 ;
printf ( "%d", sizeof ( i ) ) ;
}

A. 2
B. 4
C. Would vary from compiler to compiler
D. Error, i undefined

Q 1.6
Is it true that a global variable may have several declarations, but only one definition?
Q 1.7
Is it true that a function may have several declarations, but only one definition?
Q 1.8
In the following program where is the variable a getting defined and where is it getting declared?
main( )
{
extern int a ;
printf ( "%d", a ) ;
}
int a = 20 ;

Q 1.9
What would be the output of the following program?
main( )
{
extern int a ;
printf ( "\n%d", a ) ;
}
int a = 20 ;

A. 20
B. 0
C. Garbage value
D. Error

Q 1.10
What's the difference between a defintion and declaration of a variable?
Q 1.11
If the definition of an external variable occurs in the source file before its use in a particular function, then there is no need for an extern declaration in the function.
Q 1.12
Suppose a program is divided into three files f1, f2 and f3, and a variable is defined in the file f1 but used in the files f2 and f3. In such a case would we need the external declaration for the variables in the files f2 and f3?
Q 1.13
When we mention the prototype of a function are we defining the function or declaring it?
Q 1.14
What's the difference between the following declarations?
extern int fun( ) ;
int fun( ) ;

Q 1.15
Why does the following program report a redeclaration error of function display( )?
main( )
{
display( ) ;
}
void display( )
{
printf ( "\nCliffhanger" ) ;
}

Q 1.16
What would be the output of the following program?
main( )
{
extern int fun ( float ) ;
int a ;
a = fun ( 3.14 ) ;
printf ( "%d", a ) ;
}

int fun ( aa )
float aa ;
{
return ( ( int ) aa ) ;
}

A. 3
B. 3.14
C. 0
D. Error

Q 1.17
Point out the error, if any, in the following program.
struct emp
{
char name[20] ;
int age ;
}

/* some more code may go here */

fun ( int aa )
{
int bb ;
bb = aa * aa ;
return ( bb ) ;
}

main( )
{
int a ;
a = fun ( 20 ) ;
printf ( "\n%d", a ) ;
}

Q 1.18
If you are to share the variables or functions across several source files how would you ensure that all definitions and declarations are consistent?
Q 1.19
How would you rectify the error in the following program?
f ( struct emp ) ;
/* any other prototypes may go here */
struct emp
{
char name[20] ;
int age ;
} ;
main( )
{
struct emp e = { "Soicher", 34 } ;
f ( e ) ;
}

f ( struct emp ee )
{
printf ( "\n%s %d", ee.name, ee.age ) ;
}

Q 1.20
Global variables are available to all functions. Does there exist a mechanism by way of which I can make it available to some and not to others.
Q 1.21
What do you mean by a translation unit?
Q 1.22
What would be the output of the following program?
main( )
{
int a[5] = { 2, 3 } ;
printf ( "\n%d %d %d", a[2], a[3], a[4] ) ;
}

A. Garbage values
B. 2 3 3
C. 3 2 2
D. 0 0 0

Q 1.23
What would be the output of the following program?
main( )
{
struct emp
{
char name[20] ;
int age ;
float sal ;
} ;
struct emp e = { "Tiger" } ;
printf ( "\n%d %f", e.age, e.sal ) ;
}

A. 0 0.000000
B. Garbage values
C. Error
D. None of the above

Q 1.24
Some books suggest that the following definitions should be preceded by the word static. Is it correct?
int a[ ] = { 2, 3, 4, 12, 32 } ;
struct emp e = { "sandy", 23 } ;

Q 1.25
Point out the error, if any, in the following program.
main( )
{
int ( *p )( ) = fun ;
( *p )( ) ;
}
fun( )
{
printf ( "\nLoud and clear" ) ;
}

Q 1.26
Point out the error, if any, in the following program.
main( )
{
union a
{
int i ;
char ch[2] ;
} ;
union a z = 512 ;
printf ( "%d %d", z.ch[0], z.ch[1] ) ;
}

Q 1.27
What do you mean by scope of a variable? What are the 4 different types of scopes that a variable can have?
Q 1.28
What are the different types of linkages?
Answers
A 1.1
4 2
A 1.2
20. Whenever there is a conflict between a local variable and a global variable it is the local variable which gets a priority.
A 1.3
20 40. In case of a conflict between local variables, the one which is more local that gets the priority.
A 1.4
Declaration
A 1.5
D. extern int i is a declaration and not a definition, hence the error.
A 1.6
Yes
A 1.7
Yes
A 1.8
extern int a is the declaration whereas int a = 20 is the definition.
A 1.9
A
A 1.10
In the defintion of a variable space is reserved for the variable and some initial value is given to it, whereas a declaration only identifies the type of the variable for a function. Thus definition is the place where the variable is created or assigned storage whereas declaration refers to places where the nature of the variable is stated but no storage is allocated.
A 1.11
True
A 1.12
Yes
A 1.13
We are declaring it. When the function alongwith the statements belonging to it are mentioned we are defining the function.
A 1.14
There is no difference except for the fact that the first one gives a hint that the function fun() is probably in another source file.
A 1.15
Here display( ) is called before it is defined. In such cases the compiler assumes that the function display() is declared as
int display( ) ;
That is, an undeclared function is assumed to return an int and accept an unspecified number of arguments. Then when we define the function the compiler finds that it is returning void hence the compiler reports the discrepancy.
A 1.16
D. The error occurs because we have mixed the ANSI prototype with K & R style of function definition. When we use ANSI protptype for a function and pass a float to the function it is promoted to a double. When the function accepts this double into a float a type mismatch occurs hence the error.The remedy for this error could be to define the function as:
int fun ( float aa )
{
..
}
A 1.17
Because of the missing semicolon at the end of the structure declaration (the intervening comment further obscures it) the compiler believes that fun() would return something of the the type struct emp, whereas in actuality it is attempting to return an int. This causes a mismatch, hence an error results.
A 1.18
The best arrangement is to place each definition in a relevant .c file. Then, put an external declaration in a header file (.h file) and use #include to bring in the declaration wherever needed.
The .c file which contains the definition should also include the header file, so that the compiler can check that the definition matches the declaration.
A 1.19
Declare the structure before the prototype of f( ).
A 1.20
No. The only way this can be achieved is to define the variable locally in main( ) instead of defining it globally and then passing it to the functions which need it.
A 1.21
A translation unit is a set of source files seen by the compiler and translated as a unit: generally one .c file, plus all header files mentioned in #include directives.
A 1.22
D. When an automatic array is partially initialised, the remaining array elements are initialised to 0.
A 1.23
A. When an automatic structure is partially initialised, the remaining elements of the structure are initialised to 0.
A 1.24
Pre-ANSI C compilers had such a requirement. Compilers which conform to ANSI C standard do not have such a requirement.
A 1.25
Here we are initialising the function pointer p to the address of the function fun(). But during this initialisation the function has not been defined. Hence an error.
To eliminate this error add the prototype of the fun() before declaration of p, as shown below:
extern int fun( ) ;
or simply
int fun( ) ;

A 1.26
In a pre-ANSI compiler a union variable cannot be initialised. However, ANSI C permits initialisation of first member of the union.
A 1.27
Scope indicates the region over which the variable's declaration has an effect. The four kinds of scopes are: file, function, block and prototype.
A 1.28
There are three different types of linkages: external, internal and none. External linkage means global, non-static variables and functions, internal linkage means static variables and functions with file scope, and no linkage means local variables.

No comments:

Post a Comment