C Questions and Answers Part-8

1. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int y = 0;
if (1 |(y = 1))
printf("y is %d\n", y);
else
printf("%d\n", y);
}
a) y is 1
b) 1
c) run time error
d) undefined

Answer: a

2. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int y = 1;
if (y & (y = 2))
printf("true %d\n", y);
else
printf("false %d\n", y);
}
a) true 2
b) false 2
c) either true 2 or false 2
d) true 1

Answer: a

3. What will be the output of the following C code?
#include <stdio.h>
void main()
{
int x = 0;
if (x = 0)
printf("Its zero\n");
else
printf("Its not zero\n");
}
a) Its not zero
b) Its zero
c) Run time error
d) None

Answer: a

4. What will be the output of the following C code?
#include <stdio.h>
void main()
{
int k = 8;
int x = 0 == 1 && k++;
printf("%d%d\n", x, k);
}
a) 0 9
b) 0 8
c) 1 8
d) 1 9

Answer: b

5. What will be the output of the following C code?
#include <stdio.h>
void main()
{
char a = 'a';
int x = (a % 10)++;
printf("%d\n", x);
}
a) 6
b) Junk value
c) Compile time error
d) 7

Answer: c

6. What will be the output of the following C code snippet?
#include <stdio.h>
void main()
{
1 < 2 ? return 1: return 2;
}
a) returns 1
b) returns 2
c) Varies
d) Compile time error

Answer: d

7. What will be the output of the following C code snippet?
#include <stdio.h>
void main()
{
unsigned int x = -5;
printf("%d", x);
}
a) Run time error
b) Aries
c) -5
d) 5

Answer: c

8. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int x = 2, y = 1;
x *= x + y;
printf("%d\n", x);
return 0;
}
a) 5
b) 6
c) Undefined behaviour
d) Compile time error

Answer: b

9. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int x = 2, y = 2;
x /= x / y;
printf("%d\n", x);
return 0;
}
a) 2
b) 1
c) 0.5
d) Undefined behaviour

Answer: a

10. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int x = 1, y = 0;
x &&= y;
printf("%d\n", x);
}
a) Compile time error
b) 1
c) 0
d) Undefined behaviour

Answer: a