Search This Blog

Saturday, August 14, 2010

Chapter-4 Floating Point Issues

Chap-4  FLOATING POINT ISSUES



1.O/P?
Main()
{
  float a=0.7;
if(a<0.7)
  printf(“C”);
else
  printf(“C++”);  
}
a. C
b.C++
c. Error
d.None of above.

Ans: A.
--------------------------------------------------------------

2.O/p?
main()
{
 float a=0.7;
 if(a<0.7f)
   printf(“C”);
else
  printf(“C++”);
}

a.C
b. C++
c. Error
e. None of above

Ans: B.
-------------------------------------------------------------

3.O/p?
main()
{
  printf(“%f”,sqrt(36.0));
}
a.6.0
b.6
c.6.000000
d.Some absurd result

ans: D


4. Would this program give proper results ?
main()
{
   printf(“%f”,log(36.0));
}

ans:No. since math.h is not included.
-------------------------------------------------------------

5.Would the following printf()s print the same values for any value of a ?
main()
{
  float a;
  scanf(“%f”,&a);
  printf(“%f”,a+a+a);
  printf(“%f”,3*a);
}

ans: No.
-------------------------------------------------------------

6. We want to round off x, a float, to an int value. The correct way to do so would be
a.   y=(int)(x+0.5);
b.y=int(x+0.5);
c.  y=(int) x+0.5;
d. y=(int)((int)x+0.5);


ans: A.
-------------------------------------------------------------


7. Which error are you likely to get when you run the following program.
Main()
{
  struct emp
   {
      char name[20];
      float sal;
   };
 struct emp e[10];
 int x;
 for(x=0;x<9;x++)
     scanf(“%s %f”, e[x].name,e[x].sal);
}

a. Suspicious pointer conversion.
b.Floating point formats not linked
c. Can not use scanf() for structure
d.Strings can not be nested inside structure


Ans: B

-------------------------------------------------------------

8. What causes the error of problem 7  above to occur and how  would you rectify the error in the above program.

Ans: What causes the “floating point format not linked” error to occur? When the complier encounters a referance to the address of the foloat , it sets a flag to have the linker link in the folating point emulator a floating point emulator is used to manipulate floating point numbers in runtime library functions like scanf() and atof(). There are some cases in which the reference to a float is a bit obscure and the complier does not detect the need for the emulator.
   These situations usually occur during the initial stages of program development. Normally, once the program is fully developed, the emulator is used in such a fashion that the complier can accurately determine when to link in the emulator.
    To force linking of the floating point emulator into an application  just include the following function in your program

Void LinkFloat(void)
 {
     float a=0, b=&a; /* cause the emulator to be linked. */
     a=*b;    /* suppress warning “var not used” */
 }

There is no need to call this function from your program.
-------------------------------------------------------------


9. Which are the three different types of real data types available in C and what are the  format specifiers used for them?

Ans:
       Float      4 bytes    %f
       Double   8 bytes    %lf
    Long double   10 bytes    %Lf
--------------------------------------------------------------

10.By default any real number is treated as
  a    float
  b    double
  c     long double
  b     depends on the memory model that you are using


ans: B.
-------------------------------------------------------------

11.What should you do to treat the constant 3.14 as a float?

Ans: Use 3.14f

--------------------------------------------------------------

12. What should you do to treat the constant 3.14 as long double?


Ans: Use 3.14l

--------------------------------------------------------------


13. O/p?
main()
{
  printf(“%d %d %d”,sizeof(3.14f),sizeof(3.14),sizeof(3.14l));
}
a. 4 4 4
b.4 garbage value garbage value
c. 4 8 10
d.error


ans: C
-------------------------------------------------------------


14.The binary equivalent of 5.375 is
a     101.101110111
b     101.011
c      101011
d      none of above


ans: B.
--------------------------------------------------------------


15.How floats are stored in  binary form?


Ans: Floating points numbers are represented in IEEE format. The IEEE format for floating point storage uses a sign bit, a mantissa and an exponent for representing the power of two(2).The sign bit denotes the sign of the number (0- positive) and (1- negative) the mantissa is represented in binary after converting it into its normalized form. The normalized form results in a mantissa whose most significant bit is always 1. The IEEE format takes the advantage of this by not storing this bit at all. The exponent is an integer stored in an unsigned binary format after adding a positive integer bias. This ensures that the stored exponent is always positive. The value of bias is 127 for floats and 1023 for doubles
--------------------------------------------------


16. A float occupies 4 bytes. If the hexadecimal equivalent of each of these bytes is A, B, C, and D, then when this float is stored in memory these bytes get stored in the order.

a.     ABCD
b.    DCBA
c.     0xABCD
d.    0xDCBA


ans: B.
--------------------------------------------------


17. If the binary equivalent of 5.375 in normalized form is 0100 0000 1010 1100 0000 0000 0000 0000, what would be the output of the following program?
Main()
{
  float a=5.375;
  char *p;
  int x;
  p=(char*)&a;
  for(x=0;x<=3;x++)
     printf(“%02x”,(unsigned char)p[x]);
}
a    40 AC 00 00
b    04 CA 00 00
c    00 00 AC 40
d    00 00CA 04


ans: C.
----------------------------------------------------------------------------------------------------------------------------

Chapter-3 Expressions

CHAP-3        EXPRESSIONS


1.O/p?
main()
{
 static int a[20];
int x=0;
a[x]=x++;
printf(“\n %d  %d  %d”, a[0],a[1],x);
}

ans: 0  0  1
This is what some compliers give others may give a different answer. The reason is that the same statement causes the same object to be modified or to be modified and then inspected, the behavior is undefined.
---------------------------------------------------

2.O/p?
Main()
{
  int x=3;
  x=x++;
  printf(“%d”,x);
}

ans: 4 but basically the behavior is undefined because of the same reason as above.


3.the expressions on the right hand side of the && and || operator does not get evaluated if the left hand side determines the output.

Ans: True. It is called short circuited mode of evaluating the logical expressions.

---------------------------------------------------
4.O/p?
Main()
{
int x=2;
printf(“%d  %d”,++x,++x);
}
a 3  4
b 4  3
c 4  4
d o/p may vary from complier to complier

ans:d the order of evaluation of the arguments to a function call is unspecified.
---------------------------------------------------

5.O/p?
Main()
{
  int x=10,y=20,z=5,a
  a=x
  printf(“%d”,a);
}
a 1
b 0
c error
d none of above.

Ans:a

---------------------------------------------------------------

6.Are the following statements same?
 a<=20?b=30:c=30;
 (a<=20)?b:c=30;

ans:No.
--------------------------------------------------------------


7.can you suggest the other way of writing the following expression such that 30 is used only once
a<=20?b=30:c=30;

ans: *((a<=20)?&b:&c)=30;
--------------------------------------------------

8. How come the c standards says that the expression
x=y++ * y++;
is undefined, whereas the expression
x=y++ && y++;
 is perfectly legal.

Ans: According to C standards an object’s stored value can be modified only once (by evaluation of the expression ) between two sequence points. A sequence point occurs:
-       At the end of full expression ( expression which is not a sub-expression in a larger expression).
-       At the && , || , and ? : operator.
-       At a function call (after the evaluation of all arguments, just before the actual call)
Since the first expression y is getting modified twice between two sequence points the expression is undefined. The second expression is legal because a sequence point is occurring at && and y is getting modified once after this sequence point.
---------------------------------------------------

9.If a[x]=x++ is undefined, then by the same reason x=x+1 should also be undefined. But it is not so. Why?

Ans:The standard says that if an object is to get modified within an expression then all accesses to it within the same expression must be for computing the value to be stored in the object. The expression a[x]=x++ is disallowed because one of the accesses of x (the one in a[x]) has nothing to do with the value that ends up being stored in x. In this case the complier may not know whether the access should take place before or after the incremented value is stored. Since there is no good way to define it , the standard declares it to be undefined. As against this the expression x=x+1 is allowed because x is accessed to determine x’s final value.
---------------------------------------------------

10.Would the expression *p++ = c be disallowed by the complier.

Ans:No. Because even the value of p is accessed twice it is used to modify two different objects p and *p.
---------------------------------------------------

11.In the following code in which order the functions would be called.
A=f1(23,65) * f2(12/4) + f3();
a. f1,f2,f3.
b.f3,f2,f1.
c. The order may vary from complier to complier.
d.None of above.

Ans:C. Here the multiplication will happen before the addition, but in which order the function would be called is undefined.

---------------------------------------------------
12. In the following code in which order the functions would be called
a=(f1(23,65) * f2(12/4) ) + f3();
a.  f1,f2,f3.
b.  f3,f2,f1.
c.  The order may vary from complier to complier.
d.None of above.

Ans:C. Here the multiplication will happen before the addition, but in which order the function would be called is undefined. In an arithmetic expression the parentheses tell the complier which operands go with which operator but do not force the complier to evaluate everything within the parenthesis first.
---------------------------------------------------

13. What would be the output of the following program?
Main()
{
 int x=-3,y=2,z=0,m;
 m=++x && ++y || ++z;
 printf(“\n %d  %d  %d  %d”,x,y,z,m);
}

ans:-2  3  0  1
---------------------------------------------------

14 What would be the output of the following program?
Main()
{
 int x=-3,y=2,z=0,m;
 m=++y && ++x || ++z;
 printf(“\n %d  %d  %d  %d”,x,y,z,m);.
}

ans:-2  3  0  1
---------------------------------------------------

15. O/p?
main()
{
 int x-3,y=2,z=0,m;
m=++x || ++y && ++z;
printf(“\n %d %d %d %d”,x,y,z,m)
}

ans: -2 2 0 1
---------------------------------------------------

16. O/p?
main()
{
 int x-3,y=2,z=0,m;
m=++x && ++y && ++z;
printf(“\n %d %d %d %d”,x,y,z,m)
}

ans:-2 3 1 1
------------------------------------------------------------------------------------------------------------------------------

Friday, July 9, 2010

Chapter-2 Control Instructions

CHAPTER- 2
CONTROL INSTRUCTIONS.
1.what would be the o/p of the following program
main()
{
int i=4;
switch(i)
{
default
printf(“\n a mouse”);
case 1:
printf(“\n a rabbit”);
break;
case 2:
printf(“\n a tiger’);
break;
case 3:
printf(“\n a lion”);
}
}


ans>a mouse
a rabbit


2. point out error in for loop if any-
main()
{
int i=0;
for( ; ; )
{
printf(“\n %d”,i++);
if(i>10)
break;
}
}

a.the condition in the for loop is must.
b.the two semicolons should be dropped
c.the for loop should replaced by a while loop
d.no error.

Ans:d

3. point error if any in the while loop
main()
{
int i=1;
while()
{
printf(“\n %d”, i++);
if(i>10)
break;
}
}

a.the condition in the while is must.
b.there should be at least one semicolon in the while()
c.the while loop must be replaced by a for loop.
d.no error.

Ans:a

4.point out error if any
main()
{
int x=1;
while(x<=5) { printf(“%d”,x); if(x>2)
goto here;
}
}
fun()
{
here:
printf(“\n Nilesh”);
}

ans:goto can not take control to different function.

5.point error if any
main()
{
x=4,y=2;
switch(x)
{
case 1:
printf(“\n To error is human”);
break;
case y:
printf(“\n don’t do it here’);
break;
}
}

ans:constant expression required in second case we cant use y.

6.point error if any
main()
{
int x=1;
switch(x)
{
case 1:
printf(“\n Hellow”);
break;
case 1*2+4:
printf(“\n the rock”);
break;
}
}

ans:no error constand expression like 1*2+4 are acceptable in cases of switch.


7.point out error if any
main()
{
int a=1;
switch(a)
{
{
printf(“\n Programmers don’t die. They just lost in the procressing”);
}

ans: no error but switch with no case is not required.

8.point out error if any.
Main()
{
int x=1;
switch(x)
{
printf(“Hellow’);
case 1:
printf(“\n Nilesh”);
break;
case 2;
printf(“\n Vivek’);
break;
}
}

ans:though there is no error ,the first printf statement can never be executed irrespective of the value of x . In other words all the statements in the switch have to belong to some case or other.

9.Rewrite the following set of statements using conditional operator.
Int a=1,b;
If(a>10)
B=20;

Ans: int a,b,dummy;
a>10?b=20:dummy=1;
note that the following would not work
a>10?b=20: ; ;

10.point out error if any.
Main()
{
int a=10,b;
a>=5?b=100:b=200;
printf(“%d”,b);
}

ans: lvalue required in function main().The second assignment should be written in the paranthesis as follows
a>=5?b=100b=200);


11. O/p?
main()
{
char str[]=”part-time musicians are semiconductors”;
int a=5;
printf(a>10?”%50s”:”%s”,str);
}

a. part-time musicians are semiconductors
b. part-time musicians are semiconductors
c. error
d. none of above

ans: a


12. What is more efficient a switch statement or an if-else chain?

Ans: There is hardly any difference in efficiency in both cases. But one should use switch where it can be because it is a cleaner way to program.


13.Can we us switch statement to switch between strings.

Ans:No. cases in switch must be either integer constants or constant expressions.


14. We want to test whether the value lies between 2 to 4 or 5 to 7. can we do this using switch.?

Ans:Yes. But the way is not practical if the ranges are bigger. It is as shown bellow:-
switch(a)
{
case 2:
case 3:
case 4:
/* some statements */
break;
case 5:
case 6:
case 7:
/* some statements */
break;
}


15. The way break is used to take control out of switch can continue be used to take the control to the beginning of the switch.

Ans:No. continue can work only with loops and not with switch.

Thursday, June 10, 2010

CHAPTER -1 Declaratization and Initialization

---------------------------------------TEST YOUR "C" SKILLS---------------------

CHAP 1: DECLARATIONS AND INITIALIZATION:-


1. Q> O/P?
main()
{
char far *s1,*s2;
printf(""%d%d",sizeof(s1),sizeof(s2));
}
a>4 2
----------------------------------------------------------------------------------
2. q>
o/p?
int x=40;
main()
{
int x=20;
printf("%d",x);
}
a>20
----------------------------------------------------------------------------------
3.q>o/p?
main()
{
int x=40;
{
int x=20;
printf("%d",x0;
}
printf("%d",x);
}
a>20 40
----------------------------------------------------------------------------------
3. q>is the following statement declaration or defination
extern int x;
a>declaration
----------------------------------------------------------------------------------


4.q>
o/p?
main()

{
extern int i;
i=20;
printf("%d",sizeof(i));
}
a> error,i undefined
because extern int i is a declaration and not defination
----------------------------------------------------------------------------------


5.q>is it true that the global variable have many declarations but only one
defination?
a>yes
----------------------------------------------------------------------------------


6.q>is it true that the function may have many decalaratins but only one
defination?
a>yes
----------------------------------------------------------------------------------


7.q>in the following program where the variable a is geting defined and where it
is declared
main()
{
extern int a; /* declaration*/
printf("%d",a0;
}
int a=12; /* defination*/

8.q>wht will be the o/p of above program/
a>20
---------------------------------------------------------------------------------


9.q>what is the difference between declaration and defination of a variable
a>declaration:-only gives the type,status and nature of variable without
reserving any space for
the variable
defination;-actual space is reserverd for the variable and some initial value
is given.
----------------------------------------------------------------------------------
10.q>if the defination of the external variable occurs in the source file before
it's use in a
perticular function then there is no need for an external declaration in the
function

a>true
----------------------------------------------------------------------------------


11.q>suppose the program is devided in three source files f1,f2,f3 and the variable
is defined in file f1 but used in f2 and f3. In such a casewould we need the
external declaration for
for the variable in files f2 and f3?
a>yes
----------------------------------------------------------------------------------


12.q>when we mention the prototype of the function ,we are definig it or declaring
it?
a>declaring it
----------------------------------------------------------------------------------


13.q>what is the difference between following declarations
extern int fun()
int fun();
a>nothing except that that the firat one gives us hint that function fun is
probally in another
file.
----------------------------------------------------------------------------------


14.q>why does the following programreports the redeclaration error of function
display()
main()
{
dispaly();
}
void dispaly()
{
printf("fggagaetaertrt");
}
a>here the function dispay() is called before it is declared .That is why the
complier assumes it to be declared as
int display();
that accept unspecified no of arguments.i.e. undeclared function assumes to
return int
on appering the declaration the fun shows that it returns void
hence the error
----------------------------------------------------------------------------------


15.q>o/p?
main()
{
extern int fun(float);

int a;
a=fun(3.14);
printf("%d",a);
}
int fun(aa) /* K & R style of function defenation*/
float aa
{
return((int)aa);
}
a>error
because we have mixed the ansi prototype with k & r style of function
defenation
If we use an ANSI prototype and pass float to the function then it is
promoted to double
the funcction accepts it in to variable of type float hence the type mismatch
occurs
To remady the situation define the function as
int fun(float aa)
{
.....
}
----------------------------------------------------------------------------------


16.q>point error if any
struct emp
{
char name[20];
int age;
}
fun(int aa)
{
int bb;
bb=aa*aa;
return(bb);
}
main()
{
int a;
a=fun(20);
printf("%d",a);
}
a>missing semicollon at the end of struct
due to which the function fun assumed to be returning vsr of type struct emp.
but it returns an int hence the error
----------------------------------------------------------------------------------


17.q> If youare to share the variables or functions across several source files how
would you enshore that all definications

and declarations are consistant?
a>The best arrangement is to place each defination in a revelent .c file , then
put an external declaration in a header
file (.h file) and use #includeto briang the declaration wherever needed
The .c file which contains the definations should also include the header
file, so that the complier can check that
the defination matches the declaration.
----------------------------------------------------------------------------------


18.q>Correct the error
f(struct emp0;
struct emp
{
char name[20];
int age;
};
main()
{
struct empe={"Vivek",21}
f(e);
}
f(struct emp ee)
{
printf("\n %s %d",ee.name,ee.age);
}
a> declare the structure before the prototype of f.
----------------------------------------------------------------------------------


19.q> 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.
a>NO.
----------------------------------------------------------------------------------


20.q>What do you mean by a translation unit
a>A trnslation unit is a set of source files as seen by the complier and
translated as a unit. Generally one .c file
plus all header files mentioned in the #include directives
q>What wouldbe the output of the following program
main()
{
int a[5]={2,3}
printf("\n %d %d %d",a[2],a[3],a[4]);
}

a> 0 0 0
if a automatic array is partially initialised then remaiing elements are
initialised by 0
----------------------------------------------------------------------------------


21.q>o/p ?
main()
{
struct emp
{
char name[20];
int age;
float sal;
};
struct emp e={"vivek"}
printf("\n %d %f",e.age, e.sal);
}
a>0 0.000000
if an automatic structure is partially initialised then remaining elements are
initialised bu 0.
----------------------------------------------------------------------------------


22.q>Some books sugget that the fillowing definations should be preceded by the
word static. Is it correct?
int a[]={2,3,4,12,32}
struct emp e={"vinod",23}
a>pre ANSI compilers has such requirment but compliers confirming to ANSI
standard does not have such requirment.
----------------------------------------------------------------------------------


23.q>point out error
main()
{
int(*p)()=fun;
(*p)();
}
fun()
{
printf("\n Loud and clear");
}
a>Here we are initialising function pointer to address of the function fun() but
during the time of initialisation the function has not been defined. Hence an
error
To eliminate the error add the prototype of function fun() before the
declaration of p, as shown bellow;

extern int fun(); or simply
int fun();
----------------------------------------------------------------------------------


24.q> point error if any
main()
{
union a
{
int i;
char ch[2];
};
union a z=512;
printf("%d %d",z.ch[0],z.ch[1]);
}
a>In pre-ANSI complier union vriable can not be initialised . ANSI complier
permits initialisation of first member of the union
----------------------------------------------------------------------------------


25.q>What do you mean by the scope of the variable? what are the 4 differet types
of scopes that a variables can have?
a>Scope indicates the region over which the variable's declaration has an
effect. The four kinds of scopes are: file
function,block,prototype.
----------------------------------------------------------------------------------

26.q> what are different types of linkages?
a>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.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------