Output of C++ Programs | Set 49

  1. What is the output of the following program?
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using std::cout;
    int main()
    {
        int i = 0;
        cout << (i = 0 ? 1 : 2 ? 3 : 4);
        return 0;
    }

    chevron_right

    
    

    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?
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    int main()
    {
        printf("%.3lf", sizeof "Geeks" / sizeof "forGeeks");
        return 0;
    }

    chevron_right

    
    

    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?
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using std::cout;
    int main()
    {
        cout << 5 ["GeeksforGeeks"];
        return 0;
    }

    chevron_right

    
    

    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?
    filter_none
    edit close
    play_arrow
    link brightness_4 code
    #include <iostream>
    using std::cout;
    int main()
    {
        int i = 0, j = 0;
        ++++j = ++++i + i++;
        cout << i;
        cout << j;
        return 0;
    }
    chevron_right
    
    

    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?
    filter_none
    edit close
    play_arrow
    link brightness_4 code
    #include <iostream>
    using std::cout;
    int main()
    {
        int i = 0, j = 0, k;
        i++;
        j++ ++;
        k = (i++) + j;
        cout << i;
        cout << j;
        cout << k;
        return 0;
    }
    chevron_right
    
    

    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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


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