1. Find output.
void main()
{
int x=11,y=6,z;
z=x==5||y!=4;
printf("z=%d",z);
}
Ans: 1
Explanation: As = =and != have higher precedence then || and = operator. first x==5 and y!=4 will be evaluated. The 1st return 0 and the 2nd one will return 1. the expression get reduced to 0 || 1 which result in 1.
2. Find output.
void main()
{
int x=3,y,z;
z=y=x;
z*=y/+x;
printf("x=%d y=%d z=%d",x,y,z);
}
Ans. 3 1 3
Explanation: Both the z and y have the value 3. First y/=x (y=y/x) will be evaluated. Then z=z*y will be evaluated as both *= and /= operators are having same precedence and their associativity is form right to left. So the output will be 3 1 3
3. Find output.
void main()
{
int x,y,z;
z=++x || y&&++z;
printf(“x=%d y=%d z=%d”, x,y,z);
}
Ans: x=0 y=0 z=0
Explanation: The pre increment operator has higher precedence over the logical OR and logical AND operator. so, all the 3 variable will get incremented first and ther value will become 0. Then the "logical AND" and "logical OR" operator will get evaluated which results in 0 thatget stored in z. So, all the variable contains 0.
4.Find the output.
void main()
{
int y=10;
if (y++>9&&(y++!=10 || y++>10))
printf("%d",y);
else
printf("welcome to lakshya");
}
Ans: 12
Explanation: Inside the if statement, first y++>9 will be evaluated the result is true and value of y now become 11. then the condition y++!=10 will be evaluated which result in 1 and now the value of y will be 12 then the second part of the Or operator will not get evaluated as one operand is already true. The whole OR operation will return 1 and then the AND operation will be performed will become true and hence it will print the value of y which is 12.
5.Find the output.
void main()
{
int a=4,b=32;
if (a=0,b=3)
printf("happy friendship day");
else
printf("you are not my frinend");
}
Ans: Happy friendship Day
Explanation: Inside the if statement there are two statement which are separated by a ',' operator. As comma operator has the associativity form left to right the last expression will be taken into consideration in order to check whether the condition inside if is true or false. So, here b=3 will be evaluated which will return 1 and the condition inside if becomes true. So, the output will be happy friendship Day
6.What is the problem in the following code?
void main()
{
int x=10,y;
y=-x-;
printf("x=%d y=%d",x,y);
}
Ans: Ivalue required.
Explanation: The decremint operator has the associativity from right to left. First the post decremint operator will act on x which returns a Rvalue. A ivalue is required to store a rvalue. Here we are again trying to decremint the rvalue which flags an error.
7.Find the output.
void main()
{
int x=5,y=5,z=5;
z-=-x-y;
printf("x=%d y=%d z=%d",x,y,z);
}
Ans: 4 5 15
Explanation: the statement z-=-x-y will be evaluated as z-=-x- -y because if a compiler gets multiple operators at a time, then it will try to take the biggest possible combination of operator out of the given operators. So, the output will be 4 5 15 as first z-==x=y will be evaluated and after that x value will be decremented.
8.Find the output.
#define k j++; j++;
void main()
{
int j=10,i=10;
if (i<=10)
k;
(i>2)?i++:i-;
printf("%d %d",i,j);
}
Ans: 11 12
Explanation: The condition inside if statement is true and hence control will go to its next statement and there the statement k will be replaced by the two staments j++; and j++; so, the value of j now becomes 12. After that in case of the ternary operator, as the condition (i>2) is true the expression i++ will beevaluated. so, the value of i becomes 11. hence, the output is 11 12.
9.Find the output.
void main()
{
int x=3,z;
z=x-1;
printf("x=%d z=%d",x,z);
}
Ans: x=3 z=2 in linux but in Turbo C this is statement missing because
Explanation: Here compiler will try to take the biggest possible operator when it comes acros a multiple number of operators, here it will consider the two "_" operators as the post decrement operator and hence it will be a compile time error.
10.What is the problem in the following code?
void main()
{
int a=5,b=8,z;
z=x-1;
printf("x=%d",z);
}
Ans: Invalid indirection.
Explanation: In the statement, z*=a**b; As the copiler will try to take the biggest possible operator the compiler will take both the "*" operators as the indirection operator. Hence, it will show an error that invalid indirection operator.
11.Find the output.
void main()
{
int y;
y=sizeof (sizeof (sizeof(7.0)));
printf("%d",y);
}
Ans: 4
Explanation: The inner sizeof will be evaluated first, 7.0 is a double type constant which reseves 8 byte internally to get stored. So, the inner sizeof operator will return 8 which is an int and it takes 4 bytes in order to get stored. So the second sizeof operator will return 2 which will get stored in y.
12. What is the problem in the following code?
void main()
{
sum(3,6);
}
sum(int x, int y)
{
x>2&&y>5?return(x+y):return(x-y);
}
Ans: Expression syntax,
Explanation: return cannot be used inside the ternary operator, we can writ return x>2&&y>5?x+y:x-y to eliminate the error.
13.Find the output.
void main()
{
int c=0,d=5,e=19,a;
a=c>1?d>1 || e>1?100:200:300;
printf("a=%d",a);
}
Ans: 300
Explanation: As the associativity of the ternary operator is from right to left the expression will get evaluated from right to left. The '>' operator is having the highest precedence among all the operators present. so, first c>1,d>1 and e>1 are evaluated. the expression is now reduced as 0?1||1?100:200:300. Now the OR operator will get evaluated which will return 1. Now in the inner ternary operater condition is false the output will be 300.
14.Find the output.
void main()
{
int a.b=1.c=1;
int k=3*-~a+9*(3*c-(float)(a-b)/3);
printf("The value is %d",k);
}
Ans: The value is 33
Explanation:Here in the expression the operators are evaluated according to their associativity and their precedence. So the output is 33.
15.Find the output.
void main()
{
printf("%d",!(100==100)+1);
}
Ans: 1
Explanation: First of all the statement inside the parentheses will get evaluated i.e100=100 which results in 1. The, the logical negation operator will be evaluated and hence !| will become 0. After that 0+1 will be evaluated and 1 will get printed.
16.Find the output.
main()
{
int i.j;
i=10;
j=sizeof (i++);
printf(1+"%d",i++);
}
Ans: d
explanation : The size of operater will only calcucate the size of i and then i++ will not be evaluated. printf () takes an address as its argument and as we are giving 1+"%d", the address of string d will be passed and hence only d get printed.
17.Find the output.
void main()
{
printf("%d %d",sizeof (NULL),
sizeof ('\0'));
}
Ans: In gcc null is undeclared but 2 2 in Turbo C.
Explanation: NULL is a macro defined in stdio.h having value as 0. As o is an integer sizeof will give the size as 2 bytes. '\0' retuns the ASCII value, i.e, 0 which is an integer and sizeof will give the size as 2 byte.
18.Find the output.
void main()
{
printf("%d",10?0?5:11:12);
}
Ans: 11
Explanation: The associativity of the ternary operator is from right to left, first 0?5:11 will be evaluated to 11. the expression now reduced to 10?11:12 which results in 11.
19.Find the output.
void main(void)
{
int var1,var2,var3,minmax;
var1=5;
var2=5;
var3=6;
minmax=(var1>car2++)?(var1>var3++)
?var1+:var3++:(var2>var3)?var2++:++var3;
printf("%d\n",minmax);
}
Ans: 7
Explanation: The associativity of the ternary operator is from right to left, first (var2>var3)?var2++:++var3 willl be evaluated and as condition is false, the result is ++var3 so var3=7. After that (var1>var3++)?var1++:var3++will be evaluated. here, the result is var3++ and the value od var3 now becomes 8. After that (var1>var2++)?8:7 will be evaluated and the result 7get stored in minmax.
20.Find the output if you entered brainq as input.
void main()
{
int i=4,j=7;
char s[30];
j=j&&scanf("%s",0) || i++&&
printf("Cquest");
printf("%d %d",i,j);
}
Ans:41
Explanation: First j&&scanf ("%s",0) gets evaluated. After entering brainq, scanf will return the number of characters scanned which is 6.7&&6 returns 1. As the left part of OR operator is true the right part will not get evaluated and the output will be 4 1.
21.Find the output.
void main()
{
int x=1;
printf("%d",+1-2*3/4!=++x);
}
Ans: 1
Explanation: First ++x will be evaluated and x will become 2. Then 2*3=6 will be evaluated and then 6/4=1 will be evaluated (both *and /have same priority and their associativity is from left to right and hence first multiplication and then decision will be performed) then 1-1=0 will be evaluated and at last 0!=2 will be evaluated which results in 1.
22.Find the output.
void main()
{
int i=-6,j=-7,k=10,l=21,m;
m=i++&&j++&&k++ || l++&&m++;
printf("%d %d %d %d %d", i,j,k,l,m);
}
Ans: -5 -6 11 21 1.
Explanation: First i++&&j++ will be evaluated whose result will be 1and hence i will become -5 and j will become -6. Then, 1&&k++will be evaluated whose result will be 1 and hence the value of k now becomes 11. As, the left operand of OR is already 1 (true), the right part will not be evaluated. so, the output will be -5 -6 11 21 1.
23.Find the output.
void main()
{
int i=10;
i=!i>=14;
printf("i=%d",i);
}
Ans: i=0
Explanation: First !it will be evaluated which will be 0. Then, 0>=14 will be evaluated and result is 0. So the output will be i=0.
24.Find the output.
void main()
{
int x=0,y=2,z,a;
if (x++(+!!2 ||++y&&2))
z=2;
a=2;
printf(%d %d,z,x);
}
Ans: 2 1
Explanation: First !!2 will be evaluated which results in 1(!(!2)) as !2 is 0 and !0 is 1). As, the left part of the OR becomes 1, the right part will not be executed. After that x=x+1 will be evaluated. So, the output will be 2 1.
25.Find the output.
void main()
{
int i=0;
while(+(+i-)!=0)
i-=i++;
printf("%d",i);
}
Ans: -1
Explanation: first i-- will be executed and then (i--)!=0 will be executed (i.e 0!=0). Condition becomes false. After that i will get decremented and the output is -1.
26.Find the output.
int add (int a,int b)
{
return printf("%*s%%s",a,"",b,"");
}
void main()
{
int a=6,b=4;
printf("%d",add(a,b));
}
Ans: 10
Explanation:Here we are passing 6 and 4 to the function add (). As * is present in between % and s the printf() will print the string "" after 6 space and the string "" after another 4 space and then the printf() will return 10 as it has printed 10 as it has printed 10 characters. So, the output will be 10.
27.Find the output.
void main()
{
int arr2D[3][3];
printf("%d",((arr2D==*arr2D)&&(*arr2D==arr2D[0])));
}
Ans:1
Explanation: Let the base address of arr2D[3][3] is 100. *arr2D is the base address of the 1-D array which is also 100. so, both arr 2d and *arr2D will point to the same ocation. arr2D[0] will also point to the base address of the first 1-D array of that array which is also 100. so all the three are equal and both the conditions of the AND operator are true and hence 1 get printed.
28.Find the output.
void main()
{
int i=10,j=20;
j=i,j?(i,j)?i:j:j;
printf("%d %d",i,j);
}
Ans: 10 10
Explanation: The associativity of the ternary operator is from right to left. First (i,j)?i:j will be executed. As the condition is true it returns i. Now the expression reduced to i,j?i:j. Here also the condition is true so it returns 10.So finally both variable i and j contains 10.
29.Find the output.
void main()
{
int i=5,j=10;
i=i&=i&=j&&10+2;
printf("%d %d",i,j);
}
Ans: 1 10
Explanation: First 10+2 will be evaluated.After that j&&12 will be evaluated and the result will be 1. As &=operator has the assocearivity from right to left. hence i=i&1 will be 1 again. after that the final value of i will be 1.
30.What is the problem in the following code?
void main()
{
int a=5,b=6,c;
c++=b%(b-a);
printf("%d",c);
}
Ans: Lvalue require.
Explanation: The expression c++ always return rvalue. We are trying to store an Lvalue object to an Rvalue. So the compiler flags an error.
31.Find the output.
void main()
{
while(1)
{
if(!printf("%d"+1,
printf("%d",2555)))
break;
else
printf("ok");
break;
}
}
Ans: 2555dok
Explanation: The inner printf will get executed first which will print 2555 and returns 4 as number of character printed is 4. The outer printf will get 4 as an argument but there is no format specifier to print the outer printed and it returns 1.then !1 will be evaluated which results in 0. As the condition of if becomes false the else part gets executed.so the output is 2555dok.
32.What is the problem in the following code?
void main()
{
int i;
printf("%d",sizeof (int));
}
Ans:Expression syntax
Explanation: Here, we can write sizeof (int) and also siezof i, but we can not write sizeof int. if a data type is used in sizeof it should be used within parenthesis.
33.Find the output.
#define size(arr,type)\ sizeof (arr+1)/sizeof(type)
void main()
{
int arr[10],i;
i=size(arr,int);
printf("5d",i);
}
Ans: 1
Explanation: If we will write eizeof (arr), then it will give 10*2=20 (if we will give the array name then the size of operator eill give the size of the array ) but, arr+1 is simplu and address and its size will be 2 bytes.
34.What is the problem in the following code?
void main()
{
int i=3;
for(;i++=0;);
printf("%d",i);
}
Ans: Lvalue required
Explanation:The statement i++=0 will show Lvalue required as i++ is a Rvalue and we are assigning a Lvalue to a Rvalue.
35.What is the problem in the following code?
void main()
{
int a=5,b=1,c;
c=a?a=1:b=2;
printf("%d %d %d ",a,b,c);
}
Ans:Lvalue required.
Explanation: Assignment operator can not be in the second part (b=2) in a ternary operator.
36.What is the problem in the following code?
void main()
{
int i=10,j=2;
int *ip=&i,*jp=&j;
int k=*ip/*jp;
printf("%d",k);
}
Ans: Unexpected end of file.
Explanation: In the statemint in k=*ip/*jp; the compiler will take "/*" as the starting of a comment line. So, it will show an error as end of the comment line ("*/") is not provided.
37.Which one is the most perfect of the following condition?
a==0
0==a
Ans: 0==a
Explanation: If anyone writes a==0 it may happen that by mistake he may write a=0 in place of a==0. The compiler will not show any error but logically the result will be unexpected. But while writing 0==a if by mistake he used single =in place of double = = The compiler will flag an error
38.Find the output.
void main()
{
int x=7;
int a;
a=6+2||-x;
printf("%d",x);
}
Ans: 7
Explanation: First 6+2=8 will be executed which is true so the second part of OR will not be executed. So, value of x will not get change and the output will be 7.
39.Give the fastest way to multiply any number by 5.
Ans: x=(x<<2)+x;
Explanation: x<<2 means the number is shifted one bit left, i.e., it is multiplied by 4. Then we are adding x which result in multiplication of that number with 5. This is the fastest way to multiply any number with 5.
40.Write the missing statement so that the output will be -1.
void main()
{
int i=0;
while (+(+i-)!=0)
/*add here*/
printf("%d",i);
}
Ans: Any statement will work here.
Explanation: We can here give a null statement like ';' As the condition in the while loop is false the control will not get into the while loop so any statement will work here.
41.Write the precedence of the operator used in the following expression.
x=!5+14/4>>1*7<<1&2
Ans: !,/,*,+,>>,<<,&
42.How to multiply any two number without using '*' operator?
Ans:
void main()
{
int a,b,i,j,count=0;
printf("Enter two number:");
scanf("%d%d",&a,&b);
for(i=1;i<=a;i++)
for(j=1;j<=b;j++)
count++;
printf("%d",count);
}
43.Give the precedence and associativity of all operators used in the following expression.
int(*(*p)())[4]
Ans: int(*(*p)())[4]
4 21 3 5
Explanation: Here p is a pointer to function which is returning address of an array
44.Find the greatest among three number without using relational operator.
Ans:
#include<stdio.h>
void main()
{
int x,y,z;
printf("Enter three integers: ");
scanf("%d %d %d ",&x, &y, &z);
if ((x-y)>>(sizeof (int)*8=1))
if ((y-z)>>(sizeof (int)*8-1))
printf("*d is greatest",z);
else
printf("%d is greatest",y);
else if ((x-z)>>(sizeof(int)*8-1))
printf("%d is greatest",z);
else
printf("%d is greatest",x);
printf("\n");
}
45. Write one line statement to swa between two numbers.
Ans: a^=b^=a^=b;
46.Convert the following statement into conditional operator (Ternary).
void main()
{
int x=5;
int a;
if(x>2)
a=50;
}
Ans: x>2?a=50:1;
Explanation: After colon we have to give any dummy operator like 1. Here as it has no role to play.
47.Find the output (given the input value is 5 6).
void main()
{
int n[]={1,2,3,4};
int *p=n;
*p=scanf("%d",p++);
*p=printf("%d %d %d %d", *++p,*++p,*++p,*++p);
}
Ans: 4 3 1 5
Explanation: pointer p is pointing to the array n. Through scanf we are taking input in p, i.e., the starting address of array n. so n[0] becomes 5. The address gets increminted and now p is pointing to n [1] and the return value of scanf, i.e., 1 will be stored in n[1]. Now in the printf statement the execution of the expression starts from right to left, so first *--p will be evaluated which results in 5, then *++p result in 1, then *++p results in3, and lastly *++p results in 4. The values get printed from left to right so the output will be 4 3 1 5.
49.Find the output.
void main()
}
int a=1,b=2,c=3,d=4;
printf("%d",!a?b?!c:!d:a);
}
Ans: 1
Explanation:As in the condition part of the while loop condition is false so the NULL statement after while loop not get executed and as there is post decrement operator so value of y is now changed to -1. Then the expression i-=i++; gets evaluated which changed the value of i to 0. After the statement the post increment operator will increment the value of i to 1.
50.Check whether the given number is even of odd without using modulus operator?
Ans:
#include<stdio.h>
void main()
{
int x;
printf("Enter an integer :");
scanf("%d",&x);
(x&1)?printf("%d is odd",x):printf("%dis even",x);
printf("\n");
}
OR
void main()
{
int x,y;
printf("enter the number:");
y=x;
while(x!=0)
{
x=x-2;
if (x==-1)
{
printf("%d is odd",y);
break;
}
}
if (x==0)
printf("%d is even",y);
}
void main()
{
int x=11,y=6,z;
z=x==5||y!=4;
printf("z=%d",z);
}
Ans: 1
Explanation: As = =and != have higher precedence then || and = operator. first x==5 and y!=4 will be evaluated. The 1st return 0 and the 2nd one will return 1. the expression get reduced to 0 || 1 which result in 1.
2. Find output.
void main()
{
int x=3,y,z;
z=y=x;
z*=y/+x;
printf("x=%d y=%d z=%d",x,y,z);
}
Ans. 3 1 3
Explanation: Both the z and y have the value 3. First y/=x (y=y/x) will be evaluated. Then z=z*y will be evaluated as both *= and /= operators are having same precedence and their associativity is form right to left. So the output will be 3 1 3
3. Find output.
void main()
{
int x,y,z;
z=++x || y&&++z;
printf(“x=%d y=%d z=%d”, x,y,z);
}
Ans: x=0 y=0 z=0
Explanation: The pre increment operator has higher precedence over the logical OR and logical AND operator. so, all the 3 variable will get incremented first and ther value will become 0. Then the "logical AND" and "logical OR" operator will get evaluated which results in 0 thatget stored in z. So, all the variable contains 0.
4.Find the output.
void main()
{
int y=10;
if (y++>9&&(y++!=10 || y++>10))
printf("%d",y);
else
printf("welcome to lakshya");
}
Ans: 12
Explanation: Inside the if statement, first y++>9 will be evaluated the result is true and value of y now become 11. then the condition y++!=10 will be evaluated which result in 1 and now the value of y will be 12 then the second part of the Or operator will not get evaluated as one operand is already true. The whole OR operation will return 1 and then the AND operation will be performed will become true and hence it will print the value of y which is 12.
5.Find the output.
void main()
{
int a=4,b=32;
if (a=0,b=3)
printf("happy friendship day");
else
printf("you are not my frinend");
}
Ans: Happy friendship Day
Explanation: Inside the if statement there are two statement which are separated by a ',' operator. As comma operator has the associativity form left to right the last expression will be taken into consideration in order to check whether the condition inside if is true or false. So, here b=3 will be evaluated which will return 1 and the condition inside if becomes true. So, the output will be happy friendship Day
6.What is the problem in the following code?
void main()
{
int x=10,y;
y=-x-;
printf("x=%d y=%d",x,y);
}
Ans: Ivalue required.
Explanation: The decremint operator has the associativity from right to left. First the post decremint operator will act on x which returns a Rvalue. A ivalue is required to store a rvalue. Here we are again trying to decremint the rvalue which flags an error.
7.Find the output.
void main()
{
int x=5,y=5,z=5;
z-=-x-y;
printf("x=%d y=%d z=%d",x,y,z);
}
Ans: 4 5 15
Explanation: the statement z-=-x-y will be evaluated as z-=-x- -y because if a compiler gets multiple operators at a time, then it will try to take the biggest possible combination of operator out of the given operators. So, the output will be 4 5 15 as first z-==x=y will be evaluated and after that x value will be decremented.
8.Find the output.
#define k j++; j++;
void main()
{
int j=10,i=10;
if (i<=10)
k;
(i>2)?i++:i-;
printf("%d %d",i,j);
}
Ans: 11 12
Explanation: The condition inside if statement is true and hence control will go to its next statement and there the statement k will be replaced by the two staments j++; and j++; so, the value of j now becomes 12. After that in case of the ternary operator, as the condition (i>2) is true the expression i++ will beevaluated. so, the value of i becomes 11. hence, the output is 11 12.
9.Find the output.
void main()
{
int x=3,z;
z=x-1;
printf("x=%d z=%d",x,z);
}
Ans: x=3 z=2 in linux but in Turbo C this is statement missing because
Explanation: Here compiler will try to take the biggest possible operator when it comes acros a multiple number of operators, here it will consider the two "_" operators as the post decrement operator and hence it will be a compile time error.
10.What is the problem in the following code?
void main()
{
int a=5,b=8,z;
z=x-1;
printf("x=%d",z);
}
Ans: Invalid indirection.
Explanation: In the statement, z*=a**b; As the copiler will try to take the biggest possible operator the compiler will take both the "*" operators as the indirection operator. Hence, it will show an error that invalid indirection operator.
11.Find the output.
void main()
{
int y;
y=sizeof (sizeof (sizeof(7.0)));
printf("%d",y);
}
Ans: 4
Explanation: The inner sizeof will be evaluated first, 7.0 is a double type constant which reseves 8 byte internally to get stored. So, the inner sizeof operator will return 8 which is an int and it takes 4 bytes in order to get stored. So the second sizeof operator will return 2 which will get stored in y.
12. What is the problem in the following code?
void main()
{
sum(3,6);
}
sum(int x, int y)
{
x>2&&y>5?return(x+y):return(x-y);
}
Ans: Expression syntax,
Explanation: return cannot be used inside the ternary operator, we can writ return x>2&&y>5?x+y:x-y to eliminate the error.
13.Find the output.
void main()
{
int c=0,d=5,e=19,a;
a=c>1?d>1 || e>1?100:200:300;
printf("a=%d",a);
}
Ans: 300
Explanation: As the associativity of the ternary operator is from right to left the expression will get evaluated from right to left. The '>' operator is having the highest precedence among all the operators present. so, first c>1,d>1 and e>1 are evaluated. the expression is now reduced as 0?1||1?100:200:300. Now the OR operator will get evaluated which will return 1. Now in the inner ternary operater condition is false the output will be 300.
14.Find the output.
void main()
{
int a.b=1.c=1;
int k=3*-~a+9*(3*c-(float)(a-b)/3);
printf("The value is %d",k);
}
Ans: The value is 33
Explanation:Here in the expression the operators are evaluated according to their associativity and their precedence. So the output is 33.
15.Find the output.
void main()
{
printf("%d",!(100==100)+1);
}
Ans: 1
Explanation: First of all the statement inside the parentheses will get evaluated i.e100=100 which results in 1. The, the logical negation operator will be evaluated and hence !| will become 0. After that 0+1 will be evaluated and 1 will get printed.
16.Find the output.
main()
{
int i.j;
i=10;
j=sizeof (i++);
printf(1+"%d",i++);
}
Ans: d
explanation : The size of operater will only calcucate the size of i and then i++ will not be evaluated. printf () takes an address as its argument and as we are giving 1+"%d", the address of string d will be passed and hence only d get printed.
17.Find the output.
void main()
{
printf("%d %d",sizeof (NULL),
sizeof ('\0'));
}
Ans: In gcc null is undeclared but 2 2 in Turbo C.
Explanation: NULL is a macro defined in stdio.h having value as 0. As o is an integer sizeof will give the size as 2 bytes. '\0' retuns the ASCII value, i.e, 0 which is an integer and sizeof will give the size as 2 byte.
18.Find the output.
void main()
{
printf("%d",10?0?5:11:12);
}
Ans: 11
Explanation: The associativity of the ternary operator is from right to left, first 0?5:11 will be evaluated to 11. the expression now reduced to 10?11:12 which results in 11.
19.Find the output.
void main(void)
{
int var1,var2,var3,minmax;
var1=5;
var2=5;
var3=6;
minmax=(var1>car2++)?(var1>var3++)
?var1+:var3++:(var2>var3)?var2++:++var3;
printf("%d\n",minmax);
}
Ans: 7
Explanation: The associativity of the ternary operator is from right to left, first (var2>var3)?var2++:++var3 willl be evaluated and as condition is false, the result is ++var3 so var3=7. After that (var1>var3++)?var1++:var3++will be evaluated. here, the result is var3++ and the value od var3 now becomes 8. After that (var1>var2++)?8:7 will be evaluated and the result 7get stored in minmax.
20.Find the output if you entered brainq as input.
void main()
{
int i=4,j=7;
char s[30];
j=j&&scanf("%s",0) || i++&&
printf("Cquest");
printf("%d %d",i,j);
}
Ans:41
Explanation: First j&&scanf ("%s",0) gets evaluated. After entering brainq, scanf will return the number of characters scanned which is 6.7&&6 returns 1. As the left part of OR operator is true the right part will not get evaluated and the output will be 4 1.
21.Find the output.
void main()
{
int x=1;
printf("%d",+1-2*3/4!=++x);
}
Ans: 1
Explanation: First ++x will be evaluated and x will become 2. Then 2*3=6 will be evaluated and then 6/4=1 will be evaluated (both *and /have same priority and their associativity is from left to right and hence first multiplication and then decision will be performed) then 1-1=0 will be evaluated and at last 0!=2 will be evaluated which results in 1.
22.Find the output.
void main()
{
int i=-6,j=-7,k=10,l=21,m;
m=i++&&j++&&k++ || l++&&m++;
printf("%d %d %d %d %d", i,j,k,l,m);
}
Ans: -5 -6 11 21 1.
Explanation: First i++&&j++ will be evaluated whose result will be 1and hence i will become -5 and j will become -6. Then, 1&&k++will be evaluated whose result will be 1 and hence the value of k now becomes 11. As, the left operand of OR is already 1 (true), the right part will not be evaluated. so, the output will be -5 -6 11 21 1.
23.Find the output.
void main()
{
int i=10;
i=!i>=14;
printf("i=%d",i);
}
Ans: i=0
Explanation: First !it will be evaluated which will be 0. Then, 0>=14 will be evaluated and result is 0. So the output will be i=0.
24.Find the output.
void main()
{
int x=0,y=2,z,a;
if (x++(+!!2 ||++y&&2))
z=2;
a=2;
printf(%d %d,z,x);
}
Ans: 2 1
Explanation: First !!2 will be evaluated which results in 1(!(!2)) as !2 is 0 and !0 is 1). As, the left part of the OR becomes 1, the right part will not be executed. After that x=x+1 will be evaluated. So, the output will be 2 1.
25.Find the output.
void main()
{
int i=0;
while(+(+i-)!=0)
i-=i++;
printf("%d",i);
}
Ans: -1
Explanation: first i-- will be executed and then (i--)!=0 will be executed (i.e 0!=0). Condition becomes false. After that i will get decremented and the output is -1.
26.Find the output.
int add (int a,int b)
{
return printf("%*s%%s",a,"",b,"");
}
void main()
{
int a=6,b=4;
printf("%d",add(a,b));
}
Ans: 10
Explanation:Here we are passing 6 and 4 to the function add (). As * is present in between % and s the printf() will print the string "" after 6 space and the string "" after another 4 space and then the printf() will return 10 as it has printed 10 as it has printed 10 characters. So, the output will be 10.
27.Find the output.
void main()
{
int arr2D[3][3];
printf("%d",((arr2D==*arr2D)&&(*arr2D==arr2D[0])));
}
Ans:1
Explanation: Let the base address of arr2D[3][3] is 100. *arr2D is the base address of the 1-D array which is also 100. so, both arr 2d and *arr2D will point to the same ocation. arr2D[0] will also point to the base address of the first 1-D array of that array which is also 100. so all the three are equal and both the conditions of the AND operator are true and hence 1 get printed.
28.Find the output.
void main()
{
int i=10,j=20;
j=i,j?(i,j)?i:j:j;
printf("%d %d",i,j);
}
Ans: 10 10
Explanation: The associativity of the ternary operator is from right to left. First (i,j)?i:j will be executed. As the condition is true it returns i. Now the expression reduced to i,j?i:j. Here also the condition is true so it returns 10.So finally both variable i and j contains 10.
29.Find the output.
void main()
{
int i=5,j=10;
i=i&=i&=j&&10+2;
printf("%d %d",i,j);
}
Ans: 1 10
Explanation: First 10+2 will be evaluated.After that j&&12 will be evaluated and the result will be 1. As &=operator has the assocearivity from right to left. hence i=i&1 will be 1 again. after that the final value of i will be 1.
30.What is the problem in the following code?
void main()
{
int a=5,b=6,c;
c++=b%(b-a);
printf("%d",c);
}
Ans: Lvalue require.
Explanation: The expression c++ always return rvalue. We are trying to store an Lvalue object to an Rvalue. So the compiler flags an error.
31.Find the output.
void main()
{
while(1)
{
if(!printf("%d"+1,
printf("%d",2555)))
break;
else
printf("ok");
break;
}
}
Ans: 2555dok
Explanation: The inner printf will get executed first which will print 2555 and returns 4 as number of character printed is 4. The outer printf will get 4 as an argument but there is no format specifier to print the outer printed and it returns 1.then !1 will be evaluated which results in 0. As the condition of if becomes false the else part gets executed.so the output is 2555dok.
32.What is the problem in the following code?
void main()
{
int i;
printf("%d",sizeof (int));
}
Ans:Expression syntax
Explanation: Here, we can write sizeof (int) and also siezof i, but we can not write sizeof int. if a data type is used in sizeof it should be used within parenthesis.
33.Find the output.
#define size(arr,type)\ sizeof (arr+1)/sizeof(type)
void main()
{
int arr[10],i;
i=size(arr,int);
printf("5d",i);
}
Ans: 1
Explanation: If we will write eizeof (arr), then it will give 10*2=20 (if we will give the array name then the size of operator eill give the size of the array ) but, arr+1 is simplu and address and its size will be 2 bytes.
34.What is the problem in the following code?
void main()
{
int i=3;
for(;i++=0;);
printf("%d",i);
}
Ans: Lvalue required
Explanation:The statement i++=0 will show Lvalue required as i++ is a Rvalue and we are assigning a Lvalue to a Rvalue.
35.What is the problem in the following code?
void main()
{
int a=5,b=1,c;
c=a?a=1:b=2;
printf("%d %d %d ",a,b,c);
}
Ans:Lvalue required.
Explanation: Assignment operator can not be in the second part (b=2) in a ternary operator.
36.What is the problem in the following code?
void main()
{
int i=10,j=2;
int *ip=&i,*jp=&j;
int k=*ip/*jp;
printf("%d",k);
}
Ans: Unexpected end of file.
Explanation: In the statemint in k=*ip/*jp; the compiler will take "/*" as the starting of a comment line. So, it will show an error as end of the comment line ("*/") is not provided.
37.Which one is the most perfect of the following condition?
a==0
0==a
Ans: 0==a
Explanation: If anyone writes a==0 it may happen that by mistake he may write a=0 in place of a==0. The compiler will not show any error but logically the result will be unexpected. But while writing 0==a if by mistake he used single =in place of double = = The compiler will flag an error
38.Find the output.
void main()
{
int x=7;
int a;
a=6+2||-x;
printf("%d",x);
}
Ans: 7
Explanation: First 6+2=8 will be executed which is true so the second part of OR will not be executed. So, value of x will not get change and the output will be 7.
39.Give the fastest way to multiply any number by 5.
Ans: x=(x<<2)+x;
Explanation: x<<2 means the number is shifted one bit left, i.e., it is multiplied by 4. Then we are adding x which result in multiplication of that number with 5. This is the fastest way to multiply any number with 5.
40.Write the missing statement so that the output will be -1.
void main()
{
int i=0;
while (+(+i-)!=0)
/*add here*/
printf("%d",i);
}
Ans: Any statement will work here.
Explanation: We can here give a null statement like ';' As the condition in the while loop is false the control will not get into the while loop so any statement will work here.
41.Write the precedence of the operator used in the following expression.
x=!5+14/4>>1*7<<1&2
Ans: !,/,*,+,>>,<<,&
42.How to multiply any two number without using '*' operator?
Ans:
void main()
{
int a,b,i,j,count=0;
printf("Enter two number:");
scanf("%d%d",&a,&b);
for(i=1;i<=a;i++)
for(j=1;j<=b;j++)
count++;
printf("%d",count);
}
43.Give the precedence and associativity of all operators used in the following expression.
int(*(*p)())[4]
Ans: int(*(*p)())[4]
4 21 3 5
Explanation: Here p is a pointer to function which is returning address of an array
44.Find the greatest among three number without using relational operator.
Ans:
#include<stdio.h>
void main()
{
int x,y,z;
printf("Enter three integers: ");
scanf("%d %d %d ",&x, &y, &z);
if ((x-y)>>(sizeof (int)*8=1))
if ((y-z)>>(sizeof (int)*8-1))
printf("*d is greatest",z);
else
printf("%d is greatest",y);
else if ((x-z)>>(sizeof(int)*8-1))
printf("%d is greatest",z);
else
printf("%d is greatest",x);
printf("\n");
}
45. Write one line statement to swa between two numbers.
Ans: a^=b^=a^=b;
46.Convert the following statement into conditional operator (Ternary).
void main()
{
int x=5;
int a;
if(x>2)
a=50;
}
Ans: x>2?a=50:1;
Explanation: After colon we have to give any dummy operator like 1. Here as it has no role to play.
47.Find the output (given the input value is 5 6).
void main()
{
int n[]={1,2,3,4};
int *p=n;
*p=scanf("%d",p++);
*p=printf("%d %d %d %d", *++p,*++p,*++p,*++p);
}
Ans: 4 3 1 5
Explanation: pointer p is pointing to the array n. Through scanf we are taking input in p, i.e., the starting address of array n. so n[0] becomes 5. The address gets increminted and now p is pointing to n [1] and the return value of scanf, i.e., 1 will be stored in n[1]. Now in the printf statement the execution of the expression starts from right to left, so first *--p will be evaluated which results in 5, then *++p result in 1, then *++p results in3, and lastly *++p results in 4. The values get printed from left to right so the output will be 4 3 1 5.
49.Find the output.
void main()
}
int a=1,b=2,c=3,d=4;
printf("%d",!a?b?!c:!d:a);
}
Ans: 1
Explanation:As in the condition part of the while loop condition is false so the NULL statement after while loop not get executed and as there is post decrement operator so value of y is now changed to -1. Then the expression i-=i++; gets evaluated which changed the value of i to 0. After the statement the post increment operator will increment the value of i to 1.
50.Check whether the given number is even of odd without using modulus operator?
Ans:
#include<stdio.h>
void main()
{
int x;
printf("Enter an integer :");
scanf("%d",&x);
(x&1)?printf("%d is odd",x):printf("%dis even",x);
printf("\n");
}
OR
void main()
{
int x,y;
printf("enter the number:");
y=x;
while(x!=0)
{
x=x-2;
if (x==-1)
{
printf("%d is odd",y);
break;
}
}
if (x==0)
printf("%d is even",y);
}
No comments:
Post a Comment