Output of C programs | Set 58 (operators)

4

Prerequisite : Operators in C

Q.1 What is the output of this program?

#include <iostream>
using namespace std;
int main()
{
    printf("value is = %d", (10 ++));
    return 0;
}

Options
a) 10
b) 11
c) compile time error
d) run time error

ans: c

Explanation : lvalue required as increment operator operate only on variables and not constant values.

Q.2 What is the output of this program?

#include <iostream>
using namespace std;
int main()
{
    int i = 0, j = 1, k = 2, m;
    m = i++ || j++ || k++;
    printf("%d %d %d %d", m, i, j, k);
    return 0;
}

Options

a) 1 1 2 3
b) 1 1 2 2
c) 0 1 2 2
d) 1 2 3 3

ans:- b

Explanation : Once the value of expression is true in OR, latter expression will not evaluated hence j = 1 is assigned to m .

Q.3 What is the output of this program?

#include <iostream>
using namespace std;
int main()
{
    int y = 10;
    if (y++ > 9 && y++ != 10 && y++ > 11)
        printf("%d", y);
    else
        printf("%d", y);
    return 0;
}

Options
a) 11
b) 12
c) 13
d) none of above

ans: c

Explanation : and operator(&) is used so whole expression is evaluated even if the first part is true.

Q.4 What is the output of this program?

#include <iostream>
using namespace std;
int main()
{
    int i = 10;
    i = !i > 14;
    printf("i=%d", i);
    return 0;
}

Options
a) i=1
b) i=0
c) i=10
d) none of these

ans:- b

Explanation : Not oprerator(!) has more precedence than greater than operator(>) so 0>14 is evaluated false.

Q.5 What is the output of this program?

#include <iostream>
using namespace std;
int main()
{
    int a = 3, b = 5, c, d;
    c = a, b;
    d = (a, b);
    printf("c=%d d=%d", c, d);
    return 0;
}

Options
a) c=3 d=5
b) c=5 d=5
c) can’t be determined
d) none of these

ans : a

Explanation : The precedence of ‘(‘ is greater as compared to ‘, ‘ so firstly a is assigned in c and then b is assigned in d.

Q.6 What is the output of this program?

#include <iostream>
using namespace std;
int main()
{
    int y = 10;
    if (y++ > 9 && y++ != 11 && y++ > 11)
        printf("%d", y);
    else
        printf("%d", y);
    return 0;
}

Options
a) 11
b) 12
c) 13
d) 14

ans: b

Explanation : y++!=11 gets false, so y++>11 will not be evaluated.


This article is contributed by Pragya Singh. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



4 Average Difficulty : 4/5.0
Based on 2 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.