Operators
- Which of the following is the correct output for the program given below?
#include <stdio.h>
int main ( )
{
int a = 9, b, c;
b = --a;
c = a--;
printf ( "%d %d %d\n", a, b, c);
return 0;
}
-
View Hint View Answer Discuss in Forum
Step 1: int a=9, b, c; here variable a, b, c are declared as an integer type and variable a is initialized to 9.
Step 2: b = --a; becomes b = 8; because (--a) is pre-decrement operator.
Step 3: c = a--; becomes c = 8;. In the next step variable a becomes 7, because (a--) is post-decrement operator.
Step 4: printf("%d, %d, %d\n", a, b, c); Hence it prints "7 8 8".Correct Option: D
Step 1: int a=9, b, c; here variable a, b, c are declared as an integer type and variable a is initialized to 9.
Step 2: b = --a; becomes b = 8; because (--a) is pre-decrement operator.
Step 3: c = a--; becomes c = 8;. In the next step variable a becomes 7, because (a--) is post-decrement operator.
Step 4: printf("%d, %d, %d\n", a, b, c); Hence it prints "7 8 8".
- Which of the following statements are correct about the code snippet given below?
int number = 10;
int p = number > 5 ? p = 30;
-
View Hint View Answer Discuss in Forum
Compiler would report an error.
error: expected ':' before ';' tokenCorrect Option: D
Compiler would report an error.
error: expected ':' before ';' token
- Which of the following is the correct output for the program given below ?
#include <stdio.h>
int main ( )
{
int x = 10, y = 20, z;
z = (x == 10 || y > 20);
printf ( "z = %d\n", z);
return 0;
}
-
View Hint View Answer Discuss in Forum
Step 1: int x=10, y=200, z;
Step 2: z = (x == 10 || y > 20);
becomes z = (10 == 10 || 20 > 20);
becomes z = (TRUE || FALSE);
becomes z = (TRUE); (ie. z = 1)Correct Option: C
Step 1: int x=10, y=200, z;
Step 2: z = (x == 10 || y > 20);
becomes z = (10 == 10 || 20 > 20);
becomes z = (TRUE || FALSE);
becomes z = (TRUE); (ie. z = 1)
Step 3: printf("z=%d\n", z); It prints the value of variable z=1
Hence the output of the program is '1'(one).
- Which of the following is the correct output for the program given below ?
#include <stdio.h>
int main ( )
{
int a = 12, b = 7, c;
c = a != 4 || b == 2;
printf ("c = %d\n" , c);
return 0;
}
-
View Hint View Answer Discuss in Forum
Step 1: c = a != 4 || b == 2;
which is equivalent to c = TRUE || FALSECorrect Option: B
Step 1: c = a != 4 || b == 2;
which is equivalent to c = TRUE || FALSE
or c = TRUE
i.e. c = 1
- Which of the following is the correct output for the program given below?
#include <stdio.h>
int main ( )
{
int a = 4, b = -1, c = 0, p, q, r, s;
p = a || b || c;
q = a && b && c;
r = a || b && c;
s = a && b || c;
printf ("%d %d %d %d\n", p, q, r, s);
return 0;
}
-
View Hint View Answer Discuss in Forum
Step 1: int a=4, b=-1, c=0, p, q, r, s;
here variable a, b, c, p, q, r, s are declared as an integer type and the variable a, b, c are initialized to 4, -1, 0 respectively.
Step 2: p = a || b || c; becomes p = 4 || -1 || 0;. Hence it returns TRUE. So, p=1
Step 3: q = a && b && c; becomes q = 4 && -1 && 0; Hence it returns FALSE. So, q=0
Step 4: r = a || b && c; becomes r = 4 || -1 && 0; Hence it returns TRUE. So, r=1
Step 5: s = a && b || c; becomes s = 4 && -1 || 0; Hence it returns TRUE. So, s=1.Correct Option: E
Step 1: int a=4, b=-1, c=0, p, q, r, s;
here variable a, b, c, p, q, r, s are declared as an integer type and the variable a, b, c are initialized to 4, -1, 0 respectively.
Step 2: p = a || b || c; becomes p = 4 || -1 || 0;. Hence it returns TRUE. So, p=1
Step 3: q = a && b && c; becomes q = 4 && -1 && 0; Hence it returns FALSE. So, q=0
Step 4: r = a || b && c; becomes r = 4 || -1 && 0; Hence it returns TRUE. So, r=1
Step 5: s = a && b || c; becomes s = 4 && -1 || 0; Hence it returns TRUE. So, s=1.
Step 6: printf("%d, %d, %d, %d\n", p, q, r, s); Hence the output is "1, 0, 1, 1".