Check The Programming Section
Which of the following combinations of the variables x,y,z makes the variable ‘a’ get the value 4 In the following expression? a=(x>y)?((x>z)?x:z):( (y>z)?y:z)
x=3;y=4;z=2
x=6;y=5;z=3
x=5;y=4;y=5
x=6;y=3;z=5
Answer:- In the available options, only y has the value as 4 and the value of a can become 4 when the above expression returns the value of y. The exp1 x>y evaluates to false, so exp3 gets evaluted and if we consider the first option as the value of x, y and z the return value of exp3 will be 4. Because y (=4) > z (=2) is true and the value of y is assigned to a.
What will be the output of the following code statements? integer a = 50, b = 25, c = 5 print a * b / c + c
120
125
255
250
Answer:- The expression will be evaluted as (((a * b) / c) + c). This expression have only arithmetic operators and according to the operator precedence the operators * and > ahs equal precedence. So operator associativity will be followed and execution is started for left to right. At first a * b gives 1250 and 1250/c considered as integer divission ard returns 250. At last the value of c 5 is added to the 250 and return 255 as the outcome of the expression.
What will be the output of the following code statements? integer a = 10, b = 35, c = 5 print a * b / c - c ?
65
60
Error
70
Answer:- (a). Depend upon the preference of operator.
integer a = 10, b = 35, c = 5. Comment about the output of the two statements?
print a * b + c / d
print c / d + a * b ?
Differ due to left - to- right precedence
Differ by 10
Differ by 20
Same
Answer:- Same.
integer a = 60, b = 35, c = -30. What will be the output of the following two statements:
print ( a > 45 OR b > 50 AND c > 10 )
print ( ( a > 45 OR b > 50 ) AND c > 10 )
0 and 1
0 and 0
1 and 1
1 and 0
Answer:- 1 and 0
What will be the output of the following pseudo-code statements:
integer a = 984, b=10.
//float is a data-type to store real numbers.
float c.
c = a / b print c?
984
98.4
98
Error
Answer:- 98
What will be the output of the following pseudo-code statements:
integer a = 984
//float is a data-type to store rational numbers.
float b= 10, c.
c = a / b
print c.
984
Error
98.4
98
Answer:- 98.4
What will be the output of the code?
void main(){
int i=0, j=1, k=2, m;
m = i++ || j++ || k++;
printf("%d %d %d %d", m, i, j, k);
}
1 1 2 3
1 1 2 2
0 1 2 2
0 1 2 3
Answer:- 1 1 2 2
What will be the output of the code?
void main(){
int i=10;
i = !i>14;
printf("i=%d", i);
}
10
14
0
1
Answer:- 0
What will be the output of the following program?
void main(){
int a, b, c, d;
a = 3;
b = 5;
c = a, b;
d = (a, b);
printf("c=%d d=%d", c, d);
}
c = 3 and d = 3
c = 3 and d = 5
c = 5 and d = 3
c = 5 and d = 5
Answer:- (b) The comma operator evaluates both of its operands and produce the value of the second. It also has lower precedence then assignment . Hence, c(a,b) is equivalent to c = a and d(a,b) is equivalent to d = b.
What will be the output of this program on an implementation where int occupies 2 bytes?
#include <stdio.h>
void main(){
int i = 3;
int j;
j = sizeof(++i + ++i);
printf("i=%d j=%d", i, j);
}
i=4 j=2
i=3 j=2
i=5 j=2
The behaviour is undefined
Answer:- (b). Evaluating ++i + ++i would produce undefined behavior, but the operand of sizeof is not evaluated, so remains 3 throughout the program. The type of the expression (int) is reduced at compile time, and the size of this type (2) is assigned to j.
What will be the output?
void main(){
int a=10, b=20;
char x=1, y=0;
if(a,b,x,y){
printf("EXAM");
}
}
XAM is printed
Exam is printed
Compile error
Nothing is printed
Answer:- Nothing is printed
What number will z in the sample code given below?
int z, x=5, y= -10, a=4, b=2;
z = x++ - --y*b/a;
5
6
9
10
Answer:- 10. x++(Postfix operator) i.e x will become 5, y--(Prefix operator) i.e y will become -11, * and / have same priority so they will be executed according to their associativity i.e left to right. So, *(Multiplication) will execute first and then /(division). Then do (Subtraction). So the complete expression would be = (-11)*2/4 = 5 - (-22)/4 = 5 - (-5) = 5 + 5 = 10.
14. What is the output of the following statements?
int i = 0;
printf("%d %d", i, i++);
0 1
1 0
0 0
1 1
Answer:- 1 0. Since the evaluation is from right to left. So when the print statement execute value of i = 0. Since its execute from right to left when i++ will be execute first and print value 0 (since its post increment ) and after printing 0 value of i become 1. So it its prints for 1 for next i.