Quiz on programmming fundamentals 1

Sir, please explain me answers to question nu. 3,4 and 5. of the quiz.

Hi @ashish_meher, in question 3, you can see that in line no. 5 i.e. in:

signed Nagarro = A;

there is an error. It is that A is not in single or double quotes but it should be so that is causing error.


In question no. 4:

you cannot use short with a floating point value hence the line:

short double Nagarro = 8.8;

is causing error.


In question no. 5:

since for short variables the value assigned is getting out of range/invalid the value assigned is a garbage value. And the garbage value that is assigned has a mechanism.

Consider :

short unsigned BOSS1 = -2018

We know size that is occupied by a short type variable is 2 bytes. Here in short unsigned is meant to hold the value from 0 to 65535. But value given is -2018. So what happens is the value that is computed and assigned to variable in this case is:

2^bits occupied by variable + ( value assigned ).

In this case it becomes:

2^16 - 2018 ;

and hence the result of this value will be printed on output screen. Consider this process by considering a cycle where start point is minimum value(o in this case) and end point i.e. maximum value(65535 in this case) is below minimum value. To select a value we move anticlockwise and select it but what if the value assigned is not in circle.

It will take that value where the given value would have been placed. In th this case on left/above 0 are values greater than 0. So logically values less than 0 will be below/on right of 0. so value -2018 will lie some where here:

-2018 is coinciding with the position 65536 - 2018. i.e. 2^16 - 2018 and hence that is the value that will be asssigned to BOSS1. This is the fashion in which a garbage value is assigned to any variable.

Similar is the case and reason for BOSS2 and BOSS4.

Next, In Nagarro, the variable is an integer so it only takes int value and neglects decimal part.
Lastly in Hackerblocks is stored the numeric value of ‘A’ i.e. ASCII value of ‘A’ which is 65.

Hope this helps :slight_smile: