Welcome Guest | Sign in | Register

Home > C Programming > Operators > Questions and Answers

01. What will the output of following C code?
main()
{
int i=5;
printf("%d%d%d%d%d%d",i++,i--,++i,--i,i);
}
A. 45545 B. 45540
C. 44545 D. 55545

Answer and Explanation

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.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. What will the output of following C code?
main()
{
int i=5,j=6,z;
printf("%d",i+++j);
}
A. 11 B. 12
C. 10 D. error

Answer and Explanation

Answer: 11

Explanation:
the expression i+++j is treated as (i++ + j)

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. What will the output of following C code?
main()
{
char *p;
int *q;
long *r;
p=q=r=0;
p++;
q++;
r++;
printf("%p...%p...%p",p,q,r);
}
A. 0004...0002...0001 B. 0001...0002...0004
C. 0002...0004...0008 D. 0001...0002...0006

Answer and Explanation

Answer: 0001...0002...0004

Explanation:
++ operator when applied to pointers increments address according to their corresponding data-types.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. What will the output of following C code?
main()
{
int i =0;j=0;
if(i && j++)
printf("%d..%d",i++,j);
printf("%d..%d,i,j);
}
A. 1..1 B. 1..0
C. 0..1 D. 0..0

Answer and Explanation

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.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. What will the output of following C code?
void main()
{
int i=i++,j=j++,k=k++;
printf(“%d%d%d”,i,j,k);
}
A. Error B. Garbage values.
C. Compiler error D. No output

Answer and Explanation

Answer: Garbage values.

Explanation:
An identifier is available to use in program code from the point of its declaration.
So expressions such as i = i++ are valid statements. The i, j and k are automatic variables and so they contain some garbage value. Garbage in is garbage out (GIGO).

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06. What will the output of following C code?
void main()
{
static int i=i++, j=j++, k=k++;
printf(“i = %d j = %d k = %d”, i, j, k);
}
A. i = 2 j = 1 k = 1 B. i = 1 j = 2 k = 0
C. i = 0 j = 0 k = 0 D. i = 1 j = 1 k = 1

Answer and Explanation

Answer: i = 1 j = 1 k = 1

Explanation:
Since static variables are initialized to zero by default.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
07. What will the output of following C code?
main()
{
unsigned int i=65000;
while(i++!=0);
printf("%d",i);
}
A. 0 B. 2
C. 1 D. 65000

Answer and Explanation

Answer: 1

Explanation:
Note the semicolon after the while statement. When the value of i becomes 0 it comes out of while loop. Due to post-increment on i the value of i while printing is 1.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
08. What will the output of following C code?
main()
{
int i=0;
while(+(+i--)!=0)
i-=i++;
printf("%d",i);
}
A. 0 B. -1
C. -2 D. 1

Answer and Explanation

Answer: -1

Explanation:
Unary + is the only dummy operator in C. So it has no effect on the expression and now the while loop is, while(i--!=0) which is false and so breaks out of while loop. The value –1 is printed due to the post-decrement operator.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
09. What will the output of following C code?
main()
{
int i=5;
printf("%d",++i++);
}
A. Compiler error B. 5
C. 7 D. 6

Answer and Explanation

Answer: Compiler error

Explanation:
Compiler error: Lvalue required in function main

++i yields an rvalue. For postfix ++ to operate an lvalue is required.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
10. What will the output of following C code?
main()
{
int i=5;
printf(“%d”,i=++i ==6);
}
A. 0 B. 5
C. 6 D. 1

Answer and Explanation

Answer: 1

Explanation:
The expression can be treated as i = (++i==6), because == is of higher precedence than = operator. In the inner expression, ++i is equal to 6 yielding true(1). Hence the result.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved 2012-2015 SoftLucent.