Output of C++ Programs | Set 49

  1. What is the output of the following program?

    a. 1
    b. 2
    c. 3
    d. 4
    e. compile error
     
    Answer : C
    Explanation: Ternary operator is right to left associative. So the expression becomes (i = 0 ? 1 : (2 ? 3 : 4)) which evaluates as (i = 0 ? 1 : 3). Note the expression “i = 0” assigns 0 to i and returns 0. So 3 is returned and printed.

  2. What is the output of the following program?

    a. 0.000
    b. 1.000
    c. 0.667
    d. Compile error
    Answer : a
    Explanation: The operator sizeof returns an integer and not a double. In case of strings it returns the length of the string including the \0. The sizeof of “Geeks” is 6, and the sizeof the string “forGeeks” is 9. In terms of intgers, 6/9 is equal to 0.

  3. What is the output of the following program?

    a. Geeks
    b. f
    c. forGeeks
    d. compile error

    Answer : b.
    Explanation: In C/C++, the statement X[Y] is identical to *(X+Y). In this case the statement is equal to *(5+"GeeksforGeeks"). The + operator is commutative, so we can write the exprasion as *("GeeksforGeeks"+5). This statement means 5th charcter of the string from the beginning of the string. So finally we print f.
  4. What is the output of the following program?

    a. 12
    b. 34
    c. 22
    d. compile dependent

    Answer : d
    Since ++ operator does not define a sequence point, applying it multiple times on same variable in an expression causes undefined behavior.
  5. What is the output of the following program?

    a. 123
    b. 124
    c. 224
    d. Compile Error

    Answer is: d.
    Explanation: Unlike ++++j, the statement j++ ++; is illegal.



My Personal Notes arrow_drop_up

Recommended Posts:


    Article Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.