Check The Programming Section
Find the correct output of the code?
void main(){
int ans = 2;
int m = 10;
k = !((ans<2) && (m>2));
printf(“%d”, k);
}
0
1
-1
2
Answer: The LHS operand of logical && operator gets evaluated first and compares 2<2, returns 0. If logical && LHS operands return a false value, then the RHS operand of logical && is not evaluated. Then k = !(0) becomes 1 as the output.
What will be output of the code?
void main(){
int m, j = 3, k;
m = 2 * j/2;//line2
k = 2 * (j/2);//line3
printf(“m = %d, k = %d”, m, k);
}
m = 3, k = 2
m = 3, k = 3
m = 2, k = 2
m = 2, k = 3
Answer: In the second and third line of the code j/2 is a integer division and the operators * and / has equal precedence and left to right associativity. In line2 multiplication is performed first and the value of m becomes 3 (6/2). In line3 division operation will be performed first, because of parentheses and the value of k becomes 2 (2 * (3/2)). So the code will print m = 3, k = 2.
What will be the value of x, y, and z after the execution of the code?
void main(){
int x, y, z;
y = x = 2;
x = 2 * (y++); //line3
z = 2 * (++y); //line4
printf(“x = %d, y = %d, z = %d”,x,y,z);
}
x = 4, y = 4, z = 8
x = 6, y = 4, z = 8
x = 2, y = 4, z = 8
x = 4, y = 4, z = 4
Answer: At line3 y++ gets executed first and because of post increment value of x becomes 4 (2 * 2) and the value of y gets incremented by 1 to 3. At line4 ++y gets executed first value y gets incremented to 4 and value of z is set to 8 (2 *(4)). The final output will be x = 4, y = 4, z = 8.
What will be the value of x after the execution of the following program?
void main(){
int x = !0 * 10;
}
10
1
0
None of the above
Answer: Logical ! (NOT) is a unary operator and it inverts 0 to 1 and 1 gets multiplied with 10 as the value of x.
What will the output after the execution of the following program?
void main(){
int k = 8;
printf("k = %d", k++ - k++);
}
k = -1
k = 8
k = 0
k = 9
Answer: The expression k++ - k++ inside the printf() will evaluted from left to right and will do post increment. At first value k will be placed at LHS k++ as 8 and get incremented by 1 and thereafter right operand of - will evaluted and k++ will be replaced by 9. Finally, output will be (8-9) = -1.
What will be the value of b and k after the execution of the following program?
void main(){
int b, k = 8;
b = (k++ - k++ - k++, k++);
}
11 and 12
11 and 11
12 and 11
12 and 13
Answer: The value of b depeneds on the evalution of comma (,) operator. Comma operator evalutes operand from left side and discard the value of left opernad and evalutes the right operand keep it. At first all the post increment operator k++ of left operand gets executed one by one and in total there are four post increment operation. So the value of k will be 8 + 4 = 12 and the value of b is 11.
The result of the expression (10/3) * 3 + 5 % 3 is
11
10
8
1
Answer: (10/3) is an integer divission and the evalution will be performed from left to right. 10/3 is 3 because of integer division, and then 3 * 3 = 9. Thereafter, modulus operation returns 2 as reminder between 5%3 and then 2 get added with 9. So the result will be 11.
The result of the expression (23 * 2) % (int) 5.5 is
2
1
3
0
Answer: (int) is a casting operator and cast the 5.5 to integer as 5. The (23 *2) returns 46 and modulus in between 46 and 5 returns 1 as reminder, as outcome of the expression.
The result of 16>>2 is
4
8
2
5
Answer: The right sift is a division operation and 16 gets divided 2 times by 2. So the result will be 4.
The result of 5&&2 is
0
1
2
5
Answer: Logical && returns 1 when both the operand is true. In C/C++ any value other then 0 is considered as true. So, 5&&2 will returns 1 as true.
The value of c after the execution of the program will be
void main(){
int a, b, c;
a = 9;
b = 10;
c = (b<a || b >a);\\line 4
printf("\n c = %d",c);
}
c = 1
c = 0
c = -1
None of the above
Answer: line 4 represents multiple relationships between a, b and combined with logical OR. Logical OR operator returns true when one of his operands returns 1. b<a is a false relation and returns 0. The relation b>a returns a true value as 1. So the final relation outcome will be 1.