C Technical Questions
1
1.) main()
{
int var=--3;
printf(“var=%d”,var);
}
Output:
Error Lvalue required
Explanation:
Post fix or pre fix operator cant be used on constant values.
2 2.) main()
{
enum{var1=10,var2=20,var3=30};
printf(“%d”,++var1);
}
Output:
Error: Lvalue required
Explanation:
Enumeration constants cannot be modified, so cannot apply operators.
3) main()
{
int var=5;
printf(“var=%d”,var=++var==6);
}
Output:
var=1
Explanation:
The resolved
expression is var=(++var==6), because == is higher precedence than assignment(=)
operator. In the inner expression, ++var is evaluated first which yields
TRUE(1).
4) main()
{
char arr[]=”\0”;
if(printf(“%s\n”,arr))
printf(“Nothing”);
else
printf(“Something”);
}
Output:
Nothing
Explanation:
Printf will return an integer,
depending on the number of characters that are printed.
5) main()
{
int var=5;
printf(“%d”,var++*var++);
}
Output:
30
Explanation:
This may be Compiler dependent. Here output is resolved as follows: 6*5=30 and
the value becomes 7.
6.) main()
{
char a[4]="HELLO";
printf("%s",a);
}
Answer:
Compiler error: Too many initializers
Explanation:
The array a is of size 4 but the string
constant requires 6 bytes to get stored.
7.) main()
{
char a[4]="HELL";
printf("%s",a);
}
Answer:
HELL
Explanation:
The character array has the memory just enough
to hold the string “HELL” and doesnt have enough space to store the terminating
null character. So it prints the HELL correctly and continues to print garbage
values till it accidentally comes across a NULL character.
8.) main()
{
int c=- -2;
printf("c=%d",c);
}
Answer:
c=2;
Explanation:
Here unary minus (or negation) operator is
used twice. Same maths rules applies, ie. minus * minus= plus.
Note:// Space is necessary between both – else
will be treated as decrement operator.
9.) void main()
{
int i;
char a[]="\0";
if(printf("%s\n",a))
printf("Ok here \n");
else
printf("Forget it\n");
}
Answer:
Ok here
Explanation:
Printf will return how many characters does it
print. Hence printing a null character returns 1 which makes the if statement
true, thus "Ok here" is printed.
10.) What is the output of the program given below
main()
{
signed
char i=0;
for(;i>=0;i++) ;
printf("%d\n",i);
}
Answer
-128
Explanation
Notice the semicolon at the end of the for
loop. THe initial value of the i is set to 0. The inner loop executes to
increment the value from 0 to 127 (the positive range of char) and then it
rotates to the negative value of -128. The condition in the for loop fails and
so comes out of the for loop. It prints the current value of i that is -128.
11.) main()
{
char i=0;
for(;i>=0;i++) ;
printf("%d\n",i);
}
Answer:
Behavior is implementation dependent.
Explanation:
The detail if the char is signed/unsigned by
default is implementation dependent. If the implementation treats the char to
be signed by default the program will print –128 and terminate. On the other
hand if it considers char to be unsigned by default, it goes to infinite loop.
12.) main()
{
int i = 3;
for (;i++=0;) printf(“%d”,i);
}
Answer:
Compiler Error: Lvalue required.
Explanation:
As we know that increment operators return
rvalues and hence it cannot appear on the left hand side of an assignment
operation.
13.) main()
{
int i=5,j=10;
i=i&=j&&10;
printf("%d %d",i,j);
}
Answer:
1 10
Explanation:
The expression can be written as
i=(i&=(j&&10)); The inner expression (j&&10) evaluates to 1
because j==10. i is 5. i = 5&1 is 1. Hence the result.
14.) main()
{
int
i=-1,j=-1,k=0,l=2,m;
m=i++&&j++&&k++||l++;
printf("%d
%d %d %d %d",i,j,k,l,m);
}
Answer:
0 0 1 3 1
Explanation :
Logical operations always give a result of 1
or 0 . And also the logical AND (&&) operator has higher priority over
the logical OR (||) operator. So the expression ‘i++ && j++
&& k++’ is executed first. The result of this expression is
0 (-1 && -1 && 0 = 0). Now the expression is
0 || 2 which evaluates to 1 (because OR operator always gives 1 except for ‘0
|| 0’ combination- for which it gives 0). So the value of m is 1. The values of
other variables are also incremented by 1.
15.) main()
{
int i=5;
printf("%d%d%d%d%d%d",i++,i--,++i,--i,i);
}
Answer:
45545
Explanation:
The arguments in a function call are pushed
into the stack from left to right. The evaluation is by popping out from the
stack. and the evaluation is from right to left, hence the result.
16.) main()
{
int i=5,j=6,z;
printf("%d",i+++j);
}
Answer:
11
Explanation:
the expression i+++j is treated as (i++ +
j)
17.) main()
{
int i =0;j=0;
if(i && j++)
printf("%d..%d",i++,j);
printf("%d..%d,i,j);
}
Answer:
0..0
Explanation:
The value of i is 0. Since this information is
enough to determine the truth value of the boolean expression. So the statement
following the if statement is not executed. The values of i and j remain
unchanged and get printed.
18.) void main()
{
unsigned
giveit=-1;
int gotit;
printf("%u
",++giveit);
printf("%u
\n",gotit=--giveit);
}
Answer:
0 65535
Explanation:
Variables will take values in a cyclic manner
within the the range of the data type. Unsigned will take value from 0-65535
and signed from -32768 - +32767, hence the equivalent of -1 in unsigned is
65635 which again increased will become zero.
19.) main()
{
unsigned int i=10;
while(i-->=0)
printf("%u ",i);
}
Answer:
10 9 8 7 6 5 4 3 2 1 0 65535 65534…..
Explanation:
Since i is an unsigned integer it can never
become negative. So the expression i-- >=0 will always be true,
leading to an infinite
loop.
20.) main()
{
int
i=4,j=7;
j = j || i++ && printf("YOU CAN");
printf("%d
%d", i, j);
}
Answer:
4 1
Explanation:
The boolean expression
needs to be evaluated only till the truth value of the expression is not known.
j is not equal to zero itself means that the expression’s truth value is 1.
Because it is followed by ||, so the remaining expression is not evaluated and
so the value of i remains the same.Similarly when && operator is
involved in an expression, when any of the operands become false, the whole expression’s
truth value becomes false and hence the remaining expression will not be
evaluated.
No comments:
Post a Comment