Cute FTP Pro

Answers and Explanations to the Quiz-I

1.     (b) Depends on compiler
2.     (c) 9 (sure), 10 (mostly)
3.     (d) -128
4.     (b) False

5a.    (a) 5
5b.    (d) Illegal
5c.    (c) 8
5d.    (d) Illegal
5e.    (a) 1
5f.    (c) Illegal

Q.) What do you think is the output???

int main( )
{
int a=3,b;
b=a++ + a++;
cout<<endl<<"b value = "<<b;
a = 3;
b=++a + ++a;
cout<<endl<<"b value = "<<b;
return 0;
}

  1. The output will (probably) be:

b value = 6
b value = 10

When we say a++, the computer will not increment the variable value first. Thus it will only add 3 + 3 and b will become 6. Whereas in ++a, we will have to increment the variable first.

Note: We've said probably because you may find the result of 7,10 on some systems.

It all depends on the compiler. Thanks to a teacher Malin for pointing out the discrepancy.

On a SPARC machine, the compiler gave the result of 7,10. Why? Some compilers, add up the original value of 'a' and then increment 'a' twice. Equivalent low level code for b = a++ + a++ on some compilers is:
-Add 'a' with 'a' and store in a temporary area.
-Move value from temporary area to 'b'.
-Increment value of 'a' twice.

This would yield the result as 6.

On the SPARC system, the equivalent low-level code created was:
-Store 'a' in temporary area.
-Increment 'a' (now 'a' is 4).
-Add temporary area value with 'a' and store in another temporary area.
-Move value from second temporary area to 'b'.
-Increment 'a'.

Here the value for 'b' will be 7!

The next question explores this issue further.


Q.) The following coding:

a=3;
int c = ++a;
int d = ++a;
int b = c + d;

gives b the value of 9 but the following code:

a=3;
b = ++a + ++a;

Gives b a value of 10. Why?

  1. There are some tricky points to note in these scenarios.

    a=3;
    b = ++a + ++a;

What do you think the compiler will do? As you know, a unary operator will have a higher precedence than a binary operator. The compiler evaluates the expression from left to right. First it increments ‘a’ to 4. Binary operator will operate on two variables. Thus it needs to first find out what is the value of the leftmost ++a + ++a. It will increment ‘a’ once again (from 4 to 5). The common assumption would be that the resultant should be: 5+4 but actually it will be 5+5 (in the first step, it increments ‘a’ from 3 to 4 and then for the second half of the expression it increments ‘a’ from 4 to 5. ‘a’ is the same variable thus the value of ‘a’ is now 5 for both cases). The expression becomes:

    b = 5 + 5;

or it would be

    b = 10;

The overall sequence of events will be:

b = ++a + ++a;
b = 4 + ++a;
b = 5 + 5; (4 becomes 5 because ‘a’ is now 5)
b = 10;

If we extend the problem as:

b=3;
b = ++a + ++a + ++a;

and try it on different compilers; you might end up with different results. Compilers will convert these C++ statements we write into machine code.

CASE I.) Some compilers do the following:

When there is a prefix operator in a statement, apply the prefix operator first throughout the statement and then perform other operations.

i.e. in our example: Increment 'a' three times and then then use this value of 'a' in the statement:

b = 6 + 6 + 6; //and b will be 18.

CASE II.) But on some compilers you'll get the result as 16. Why? When they encounter:

a=3;
b = ++a + ++a + ++a;

they'll do the following:

b = 4 + ++a + ++a;
b = 5 + 5 + ++a;        //when the second instance becomes 5 the first also will be 5 (because it is the same variable)
b = 10 + ++a;
b = 10 + 6;
b = 16;

In these compilers (the second case which we saw just now), the placement of parentheses can change the output. Consider the statement:

    a = 3;
    b = ++a + ++a + ++a + ++a;

will give ‘b’ a value of 23. But

    b=(++a + ++a) + (++a + ++a);

will give ‘b’ a value of 24.

The latest compilers seem to work as explained in our second case. But it is better to avoid writing such code (it gets really confusing and a later time no one will be able to predict the output). Generally in tests the question would be:

    b = ++a + ++a;

in which case both types of the compiler will produce the same result.

Tip: Best is to avoid testing candidates/ students with such questions and if you happen to be the candidate and are asked this in an interview you should be able to convince the interviewer that this question has no one right answer. If it's a written test then best of luck!

The conclusion from this is to avoid using the increment operator twice on the variable in an expression.

And the point to remember regarding ++ is: When the prefix operator is used in a larger expression it will return the incremented value while the postfix operator will return the original value of the variable (as was before incrementing).


Q.) In the following piece of coding what do you think is the value of j?

char i=127;
int j;
i=i+1;
j=i;
cout<<j;

A.) If you predict 128, then you’re wrong. Note that ‘i’ and ‘j’ are signed quantities. A character has a maximum of 1 byte and thus the maximum positive value it can store is +127. When you increment ‘i’ beyond +127, it will become –128. If you increment it further it will go on as –127,-126 etc. The reason for this was discussed in the 2nd chapter onn data types.


Q.) Are ‘cout’ and ‘cin’ keywords?

  1. No. ‘cout’ and ‘cin’ are pre-defined objects. They are not keywords. They are defined in the iostream header file.

Q.) For each of the subdivisions assume:

int i = 2;
int j = 5;
int k;

Find out the value of ‘k’ (not all will have values because some are illegal statements).

  1. k= (j++);
  2. k= 2++;
  3. k= i++ + ++j;
  4. k = i+++++j;
  5. k= i = (j = 1);
  6. 1=2;

    A.     I'll leave this to the reader!!!


Go back to the Contents Page


Copyright © 2004 Sethu Subramanian All rights reserved. Sign my guestbook.