C Programming :: Floating Point Problems

Home > C Programming > Floating Point Problems > General Questions

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

1 / 6

 Member function of a class template should be defined

Awithin the class template definition

Boutside class template definition

Ceither (a) or (b)

Din the derived class

Answer: Option C

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

2 / 6

 What would be the value of c?

void main()
{
int c;
float a,b;

a= 245.05;
b = 40.02;
c =a+ b;
}

A285.07

B285

C2850

D285

Answer: Option D

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

3 / 6

 What would be the value of i and k in below C program?

void main() {
int i,j,k;
j = 5;
i = 2 *j/2;
k = 2 *(j/2);
}

Ai = 5, k = 5

Bi=4,k=4

Ci = 5, k = 4

Di = 4, k = 5

Answer: Option C

Explanation:

at first, glance looks like i value will be 4 and k value will be 4 too. 

but if you see in second statement () has been used. why?

in first statement i = 2 *j/2; three operators have been used i.e = , * , / .
operator = has priority 14 and both operator * & / have priority as 3.
however operator * has high precedence than / so in above statement first multiplication will take place and then division will happen so that i will carry 5. 
Now, in second statement () has been used for j/2. operator () (function call operator has priority 1 and highest precedence in c operator.) so the first j/2 operation will take place and then 2 * result of j/2 (i.e 2). so, k will carry 4. 
An answer will be i = 5 and k = 4.


 


Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

4 / 6

 If a = -11 and b = -3 what is the value of a%b?

A-3

B3

C2

D-2

Answer: Option D

Explanation:

Note: % is a reminder operator in c programming (and programming language). For the remainder operator, the sign of the result is the same as the sign of the dividend.

C define % operator operation for a%b as below:

a = (a/b) * b + a%b

Here dividend is negative so the answer will be -2

Important Points:

=> The answer will be -2 in the scenario where a = -11 and b = 3

=> The answer will be 2 in the scenario when a = 11 and b = -3 or 3



Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

5 / 6

 What is the output of the following 'C' program ?

#include
# pragma pack(2)
struct SIZE {
int i;
char ch ;
double db ;
} ;
main () {
printf ( "%d\n",sizeof(struct SIZE) );
}

A12

B14

C16

D8

 View Answer |  Discuss in Forum |  Workspace | Asked In Global Edge |

Answer: Option B

Explanation:

Pragma pack instructs the compiler to pack structure members with particular alignment. Most compilers, when you declare a struct, will insert padding between members to ensure that they are aligned to appropriate addresses in memory (usually a multiple of the type's size). This avoids the performance penalty (or outright error) on some architectures associated with accessing variables that are not aligned properly. For example, given 4-byte integers and the following struct:

struct SIZE {

int i;

char ch ;

double db ;

} ;

The compiler could choose to lay the struct out in memory like this:


1 byte

2nd byte
3rd byte
4th byte
int i
 i(1)
i(2)
i(3)
i(4)
char ch
ch(1)
padding
padding
padding
double db

db(1)

db(5)

db(2)

db(6)

db(3)

db(7)

db(4)

db(8)

the size of this structure will be 16.

with pragma pack (1)

1 byte

int i(1)

int i(2)

int i(3)

int i(4)

char ch(1)

double db(1)

.

.

double db(8)

 and the size of strut will be 13.

with pragma pack (2)


1st byte        2nd byte

int i(1)             int i(2)

int i(3)             int i(4)

char ch(1)        double db(1)

.                         .

double db(6)    double db(7)

double db(8)    padding


the size of struct will be 14  (Answer). if pragma pack (4) is given then in 32 bit size of sruct will be 16.

pragma pack tells the compiler the boundary to align objects in a structure to in order to improve access times.

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

6 / 6

 #define mysizeof(a) (&a+1) - &a
void main()
{
float d;
printf("%d\n", mysizeof(d) );
}
note: assume sizeof float is 8 bytes

A8

B4

C1

Dcompiler error

 View Answer |  Discuss in Forum |  Workspace | Asked In Global Edge |

Answer: Option C

Explanation:

Tricky ... little bit .. basically in below line &a (address of float variable d) getting subtracted from address of variable + 1 i.e incrementing it with 1 gives the address where the next variable of the type d can be stored, the answer is 1 instead of 8 (as given float size is 8 byte).

#define mysizeof(a) (&a+1) - &a

The difference gives the result that how many variables of type of d can be stored in that amount of memory which will obviously be 1 for the type d. The result of pointer subtraction is in elements and not in bytes. so, that the below expression evaluates to 1. instead of 8.

If you above statement put like below, the answer will be 8.

#define mysizeof(a) (char*)(&a+1) - (char*)&a

typecasting it int char* and taking the difference will tell us how many variables of type char can be stored in the given memory space (the difference). Since each char requires only 1 Byte of memory therefore (amount of memory)/1 will give the number of bytes between two successive memory locations of the type of variable passed on to the macro and hence, the amount of memory that the variable of type d requires i.e 8 bytes.

Workspace