Sie sind auf Seite 1von 46

Questions on datatypes

1. What will be the output of the C program?

#include<stdio.h>

int main()

charnum = '\010';

printf("%d", num);

return 0;

OUTPUT

A. 010 B. 08 C. 10 D. 8

Explanation

The value of a variable num is '\010'. Which means the character with value 10 in octal, which is 8 in
decimal.

2.What will be the output of the C program?

#include<stdio.h>

int main()

void *ptr;

intnum = 10;

ptr = &num;

printf("%d", ptr);

return 0;

OUTPUT

A. 10 B. Compilation error C. 0 D. Address

Explanation
Though, void is not a valid data type for declaring variables. It is a valid for declaring pointer variable
*ptr thus printf displays the address of a normal variable num

3.What will be the output of the C program?

#include<stdio.h>

int main()

void num=10;

printf("%v", num);

return 0;

OUTPUT

A. Compilation error B. 10 C. Garbage value D. 0

Explanation

Void is not a valid data type for declaring variables.

4. What will be the output of the C program?

#include<stdio.h>

int main()

printf("%d\t",sizeof(2.5));

printf("%d\t",sizeof(2));

printf("%d",sizeof('A'));

return 0;

OUTPUT

A. 8 4 2 B. 8 4 1 C. 4 4 1 D. 2.5 2 A

Explanation

C compiler by default will assign any undeclared float data type as double. Thus 8 4 1 is outputted.

5.What will be the output of the C program?


#include<stdio.h>

int main(){

signed a;

unsigned b;

a = 6u + -16 + 16u + -6;

b = a + 1;

if(a == b)

printf("%d %d",a,b);

else

printf("%u %u",a, b);

return 0;

OUTPUT

A. Compilation error B. 0 0 C. 0 1 D. address address

Explanation

Clearly, a != b and it execute the else part, where we ask compiler to display the value of a and b.

6. Which one of the following is incorrect?

OUTPUT

A. enum fruits = { apple, banana }f; B. enum fruits{ apple, banana }f ;

C. enumfruits{ apple, banana }; D. enum f{ apple, banana };

Explanation

Syntax error occur in option A.

7.What will be the output of the C program?

#include<stdio.h>

int main(){

float me = 5.25;

double you = 5.25;


if(me == you)

printf("I love U");

else

printf("I hate U");

return 0;

OUTPUT

A. Compilation error B. I love U C. Runtime error D. I hate U

Explanation

For floating point numbers (float, double, long double) the values cannot be predicted exactly.
Depending on the number of bytes, the precession with of the value represented varies. Float takes
4 bytes and long double takes 10 bytes. So float stores 0.9 with less precision than long double.

8.What will be the output of the C program?

#include<stdio.h>

int main()

externintnum;

num = 5;

printf("%d", num);

return 0;

OUTPUT

A. Compilation Error B. Linker error C. Runtime error D. 5

Explanation

extern is a C keyword which specifies to the compiler that the memory for num is allocated in some
other program and that address will be give to our current(this) program at the time of execution or
linking. Here extern intnum; is declared within the scope of the program.

9. What will be the output of the C program ?

#include<stdio.h>
int main(){

char *ptr;

printf("%d %d", sizeof(*ptr), sizeof(ptr));

return 0;

OUTPUT

A. 2 4 B. 4 4 C. 1 4 D. 1 2

Explanation

The sizeof() operator returns the number of bytes taken by its operand ptr.Here *ptr is the character
pointer which holds the memory byte of 1 and ptr is allocated with 4 bytes to hold the address of
the character pointer variable *ptr

10.Which of the following data type is right C programming?

#include<stdio.h>

int main(){

intnum = - -2;

printf("num = %d", num);

return 0;

OUTPUT

A. Runtime error B. Compilation error C. -2 D. 2

Explanation

minus and minus are get multiplied, thus the result is 2.

11.What will be the output of the C program?

#include<stdio.h>

int main()

float a = 5.0;

printf ("Result is = %d ", (24 / 5) * a);


return 0;

OUTPUT

A. 20.000000 B. 20 C. Runtime error D. 0

Explanation

Output must be 20.000000. As we use %d as a format specifier, it displays 0

12.What will be the output of the C program?

#include<stdio.h>

int main()

10;

printf("%d", 10);

OUTPUT

A. Compilation Error B. 10 C. Runtime error D. No output

Explanation

10; is an executable statement. So it doesn't end up with compilation error.

13.What will be the output of the C program ?

#include<stdio.h>

int main()

charnum = 127;

num = num + 1;

printf("%d", num);

return 0;

OUTPUT
A. garbage value B. Compilation error C. range out of bond D. -128

Explanation

Though a char is of ranging from -128 to 127. Whatever a integer number we are adding, it will loop
through out its range. Thus after 127 the number is -128.

14.What will be the output of the C program?

#include<stdio.h>

int main()

int size = sizeof(volatile) + sizeof(const);

printf("%d",++size);

return 0;

OUTPUT

A. Compilation error B. 4 C. Runtime error D. 9

Explanation

Volatile and const are C Keywords and their individual size is 4 bytes

15.What will be the output of the C program?

#include<stdio.h>

int main()

staticintnum = 6;

printf("%d ",num--);

if(num)

main();

return 0;

OUTPUT

A. Compilation Error B. 6 5 4 3 2 1 C. 5 4 3 2 1 D. No output


Explanation

Simply, function is looping again and again till num becomes 0 to exit the if condition.

16.What will be the output of the C program ?

#include<stdio.h>

int main()

if (sizeof(char) > -12)

printf("yes");

else

printf("No");

return 0;

OUTPUT

A. Compilation error B. Yes C. No D. Runtime error

Explanation

By default, all integer literals such as

-12

-56

23

are assigned as unsigned integer by C compiler. So if negative integers are given it will loop through
its range 0 to 65,535.i.e) -12 is equal to 65524.

17.What will be the output of the C program?

#include<stdio.h>

int main()

printf(" %%% ");

return 0;

}
OUTPUT: A

A. % B. No output C. %% D. %%%

Explanation

Alternative % are treated as format specifier and it won't display.

18.What will be the output of the C program?

#include<stdio.h>

int main()

float x = 3.14;

double y = 3.14;

printf("%f %ff",x, y);

return 0;

OUTPUT

A. Runtime error B. Compilation error C. 3.140000 3.140000 D. 3.140000 3.140000f

Explanation

In C language, float, double are all real data types.

The format specifier of float is %f.

The format specifier of double can also represent by %f

19.What will be the output of the C program?

#include<stdio.h>

int main()

staticintnum = 3;

if(--num)

main();
printf("%d ",num);

return 0;

OUTPUT

A. 2 1 B. 0 0 C. 1 1 D. 1 0

Explanation

A static variable num is shared among all calls of a function finally ended up with a output 0

Questions on Variables

1.What will be the output of the C program?

#include<stdio.h>

int main()

int 1_one = 25;

printf("%d",1_one);

return 0;

A) 25 B) 0 C)RUNTIME ERROR D)COMPILETIME ERROR

Explanation

Invalid variable name. Variable name must begins with either alphabet or underscore( _ ).

2. What will be the output of the C program?

#include<stdio.h>

int main(){

int default = 5, a = 3;

if(a > 2)

printf("%d",default);

return 0;
}

OUTPUT

A) COMPILETIME ERROR B)RUNTIME ERROR C)5 D)3

Explanation

default is a C reserved keyword and hence it cannot be used.

3.What will be the output of the C program?

#include<stdio.h>

int main()

int class;

int public = 5;

int private = 10;

int protected = 15;

class = public + private + protected;

printf("%d",class);

return 0;

OUTPUT:

A. garbage value B. Compilation error C. Runtime error D. 30

EXPLANATION

C programming don't have any access specifier. Thus it is possible to use class, Public, Private and
Protected as a variable name.

4. What will be the output of the C program?

#include<stdio.h>

int main()

int _int = 5;

printf("%d",_int);
return 0;

OUTPUT:

A. Runtime error B. 5 C. Garbage value D. Compilation error

Explanation

Though int is a keyword, it is prefixed with underscore, this makes it possible to use _int as a variable

5.What will be the output of the C program?

#include<stdio.h>

int main(){

int _ = 5;

int __=5;

int ___ = _ + __;

printf("%d",___);

return 0;

OUTPUT

A. Runtime error B. Compilation error C. 10 D. Some Garbage value

Explanation

Using underscore ( _ ) alone as a variable name is too possible in C programming.

6. What will be the output of the C program?

#include<stdio.h>

staticnum=5;

externintnum;

int main()

printf("%d",num);
return 0;

OUTPUT

A. 0 B. Compilation error C. Runtime error D. 5

Explanation

Two of the global variable can have same variable name.

7. What will be the output of the C program?

#include<stdio.h>

staticnum=5;

externintnum=10;

int main()

printf("%d",num);

return 0;

OUPTUT

A. Compilation error B. 5 C. 0 D. 10

Explanation

Though global variables can have same name, but we can initialize only one of them.

8. What will be the output of the C program?

#include<stdio.h>

int main()

int min-value = 2;

int max-value = 10;

intavg = max-value + min-value / 2;


printf("%d",avg);

return 0;

OUTPUT

A. 6 B. 7 C. Runtime error D. Compilation error

Explanation

Special characters are not allowed in declaring a variable name. In this case the special character is
min-value.

9. What will be the output of the C program?

#include<stdio.h>

int main(){

structnum

intnum;

};

structnum key={25};

printf("%d",key.num);

return 0;

OUTPUT

A. 0 B. 25 C. Garbage value D. Compilation error

Explanation

Two variables(num) can have same name in different scope.

10. What will be the output of the C program?

#include<stdio.h>
int main()

printf("%d",EOF);

return 0;

OUTPUT

A. Garbage value B. Compilation error C. -1 D. 0

EXPLANATION

In C, EOF - End of File which is a macro and its header file is stdio.h. In C, EOF is equal to -1 by
default. We can use EOF in some programs to check whether file pointer reaches the end of the file.

11.What will be the output of the C program?

#include<stdio.h>

int main()

intscanf = 13;

printf("%d",scanf);

return 0;

OUTPUT

A. 0 B. 13 C. Runtime error D. Compilation error

Explanation

scanf as a variable name is possible because we have not used it as function declaration(
intscanf()=13 ).

12. What will be the output of the C program?

#include<stdio.h>

int main()
{

intprintf = 13;

int c = 7 + printf;

printf("%d",c);

return 0;

OUTPUT

A. Compilation error B. 20 C. Runtime error D. garbage value

Explanation

What's wrong with printf here and what's right with previous question scanf. Here we explain

printf can be declared as a variable, when to outputted the value stored in the printf variable, we
use printf() inbuilt function.

when we use printf() it will search for printf() function in our program, here we declared printf as a
variable and not as a function , thus it leads to compiletime error.

13.What will be the output of the C program?

#include<stdio.h>

int main()

charprintf[25] = "printf";

puts(printf);

return 0;

OUTPUT

A. Garbage value B. Compilation error C. Runtime error D. printf

Explanation

As we explained in previous program, we used puts() instead of printf() in-built function. Thus there
is no problem with output.
14. What will be the output of the C program?

#include<stdio.h>

int main(){

int EOF = 0;

printf("%d",EOF);

return 0;

OUTPUT:

A. 0 B. Compilation error C. Garbage value D. -1

Explanation

In C, EOF is a predefined macro constant. We do not have any permission even to declare EOF as a
variable name. Then for sure we cannot declare or increment any values to EOF- End Of File

15. What will be the output of the C program?

#include<stdio.h>

int main()

int xyz = 20;

int xyz = 40;

printf("%d",xyz);

return 0;

OUTPUT

A. garbage value B. Compilation error C. 40 D. 20

Explanation
int xyz = 20; is a global variable, and int xyz = 40 is a local variable, Here we are calling xyz outside
the scope of xyz = 40. Thus it outputted 20.

16. What will be the output of the C program?

#include<stdio.h>

int main()

int xyz = --20;

printf("%d",xyz);

return 0;

OUTPUT

A. Runtime error B. Compilation error C. 19 D. 20

Explanation

decrement or increment operator cannot be initialize to any values.

17. What will be the output of the C program?

#include<stdio.h>

int main(){

chararr[] = "Cat";

*arr = 'B';

printf("%s", arr);

return 0;

OUTPUT

A. Cat B. Compilation error C. Bat D. Some Garbage value

Explanation

*arr = 'B' tells to compiler to replace the first letter of arr[] to 'B'. Thus Cat changes to Bat.

18.What will be the output of the C program?

#include<stdio.h>
int main()

char *ptr = "Cat";

*ptr = 'B';

printf("%s", ptr);

return 0;

OUTPUT

A. Cat B. Bat C. B D. Runtime error

Explanation

*ptr='B', trying to change the character at base address to 'C' of a constant string, which is
impossible and it leads to runtime error.

19. What will be the output of the C program?

#include<stdio.h>

int main()

char arr1[50];

char arr2[50] = "aptitude in c";

arr1 = arr2;

printf("%s", arr1);

return 0;

OUTPUT

A. Compiletime error B. Runtime error C. garbage string D. aptitude in c

Explanation

arr1 refers to base address and is constant. Hence lvalue required compile time error.

20.What will be the output of the C program?

#include<stdio.h>
int main()

char arr1[] = "aptitude in c";

char arr2[] = "aptitude in c";

if(arr1 == arr2)

printf("cheers");

return 0;

OUTPUT

A. Runtime error B. Compilation error C. cheers D. No output

Explanation

Here we are comparing both base addresses and they never going to be same, hence No output.

21. What will be the output of the C program?

#include<stdio.h>

int main()

char *ptr, **ptr1;

printf("%u", sizeof(p));

printf("%u", sizeof(q));

return 0;

OUTPUT

A. Runtime error B. 4 4 C. 2 2 D. Compilation error

Explanation

The size of pointer variable of any data type(char, int, double, float) will be 4 and are same.

22. What will be the output of the C program?


#include<stdio.h>

#define num 10

int main()

#define num 50

printf("%d",num);

return 0;

OUTPUT

A. 10 B. Compilation error C. 50 D. Runtime error

Explanation

The pre processor directives in c programming can be redefined anywhere in the program. So the
most recently assigned value will be outputted.

QUESTIONS ON OPERATORS

https://www.2braces.com/c-questions/operators-questions-c-1

1. What will be the output of the C program?

#include<stdio.h>

int main()

intnum = 8;

printf ("%d %d", num<< 1, num>> 1);

return 0;

OUTPUT

A. 7 9 B. 4 16 C. 9 7 D. 16 4

Explanation

Shift operator shift the value stored in a variable num. One to the right(<<) and other to the left(>>).

2. What will be the output of the C program?


#include<stdio.h>

int main()

int i = 5;

int a = ++i + ++i;

printf("%d",a);

return 0;

A. 14 B. 13 C. 12 D. 11

Explanation

How 14 comes instead of 13. Let's explain it

actuallty a = 6 + 7;, but not because second (++) operators result will be overwritten to the first
operand.

Note that even if you have n number of operands with increment operator(++). the above discussed
effect will take place only between first and second operand. Please check next quiz to clear it 100%.

3.What will be the output of the C program?

#include<stdio.h>

int main()

int i = 5;

int a = ++i + ++i + ++i;

printf("%d",a);

return 0;

A. 24 B. 23 C. 21 D. 22
Explanation

Now we go straight way to the program.

a = ++i + ++i + ++i;

a = 6 + ++i + ++i

a = 6 + 7 + ++i

a = 7 + 7 + ++i

a=7+7+8

a=7+7+8

Finally a = 22. got it?

4.What will be the output of the C program?

#include<stdio.h>

int main()

int i = 5;

int a = ++i + ++i + ++i + ++i;

printf("%d",a);

return 0;

A. 2 1 B.31 C. 1 1 D. 1 0
Explanation

Now we go straight way to the program.

a = ++i + ++i + ++i + ++i;

a = 6 + ++i + ++i + ++i;

a = 6 + 7 + ++i + ++i

a = 7 + 7 + ++i + ++i

a = 7 + 7 + 8 + ++i

a=7+7+8+9

Finally a = 31. got it?

5.What will be the output of the C program?

#include<stdio.h>

int main(){

int i = 16;

i =! i> 15;

printf("i = %d",i);

return 0;

A. 16 B. 1 C. 0 D. Compilation error

Explanation
Note !i which means for any value of variable i to 0.

Now, we have the condition i = 0 > 14;. Thus i = 0 ( condition false ) will is outputted.

6.What will be the output of the C program?

#include<stdio.h>

int main()

int i = 5;

int a = --i + --i;

printf("%d",a);

return 0;

A. 8 B. 5 C. 7 D. 6

Explanation

Same principle as we do for ++ operator.

7.What will be the output of the C program?

#include<stdio.h>

int main()

int i = 5;

int a = --i + --i + --i;

printf("%d",a);

return 0;

A. 8 B. 9 C. 7 D. 6
Explanation

a = --i + --i + --i;

a = 4 + --i + --i

a = 4 + 3 + --i

a = 3 + 3 + --i

a=3+3+2

Finally a = 8. got it?

8.What will be the output of the C program?

#include<stdio.h>

int main()

int i = 5;

int a = --i - --i - --i - --i;

printf("%d",a);

return 0;

A. -2 B. -3 C. -1 D. -4

Explanation

Now we go straight way to the program.

a = --i - --i - --i - --i;


a = 4 - --i - --i - --i

a = 4 - 3 - --i - --i

a = 3 - 3 - --i - --i

a = 3 - 3 - 2 - --i

a=3-3-2-1

Finally a = -3. got it?

9.What will be the output of the C program?

#include<stdio.h>

int main()

int a = 2, b = 2, c = 0, d = 2, m;

m = a++ && b++ &&c++ || d++;

printf("%d %d %d %d %d",a, b, c, d, m);

return 0;

A. Compilation error B. 3 3 1 3 1 C. 3 3 1 3 0 D. some garbage value

Explanation

As operator enjoys priority && have more priority than ||.

m = a++ && b++ &&c++ || d++;


m = 1 &&c++ || d++;

m = 1 || d++;

m = 1 || 0;

m = 1;

10.What will be the output of the C program?

#include<stdio.h>

int main()

int i = 5;

int a = --i + ++i - i-- + --i;

printf("%d",a);

return 0;

A. 7

B. 9

C. 8✔

D. 10

x
WoW you are Great

Option: C

Explanation

a = --i + ++i - i-- + --i

a = 4 + ++i - i-- + --i

a = 4 + 5 - i-- + --i

a = 5 + 5 - 5 + --i

a=5+5-5+3

Finally a = 8. got it?

11.What will be the output of the C program?

#include<stdio.h>

int main()

int a = 5;

a = 1, 2, 3;

printf("%d", a);

return 0;

A. 3 B. 5 C. compilation error D. 1
Explanation

Priority for the values assigned to any variable is given from left to right.

12. What will be the output of the C program?

#include<stdio.h>

int main()

int a;

a = (1, 2, 3);

printf("%d", a);

return 0;

A. 8 B. 3 C. 7 D. 6

Explanation

Priority for the values inside a brackets () assigned to any variable is given from right to left.

13. What will be the output of the C program?

#include<stdio.h>

int main()

int x[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

printf("%d",sizeof(x));

return 0;

}
A. 2 B. 4 C. 20 D. 40

Explanation

Now we go straight way to the program.

Here x is an array variable which holds 10 integer values. Thus sizeofint is 4, then 4 * 10 = 40.

14. What will be the output of the C program?

#include<stdio.h>

int main()

unsignedintnum = -4;

printf("%d", ~num);

return 0;

A. Compilation error B. 3 C. 4 D. some garbage value

Explanation

~ is a One's Complement bitwise operator.

the function of ~ is a One's Complement is to inverse all value only after converting a decimal value
to binary 0's and 1's

Here intnum = -4;

which is num = -100 (in binary 0's and 1's)


~num = +011 (in binary 0's and 1's)

~num = 3

15. What will be the output of the C program?

#include<stdio.h>

int main()

int x = 2;

(x& 1) ? printf("true") : printf("false");

return 0;

A. Compilation error B. true C. false D. Runtime error

Explanation

As & is a unary operator we have to assume all decimal values to binary(0's and 1's)

int x = 2 (in decimal)

x = 00000010( in decimal 0's and 1's)

Now we go for condition (00000010 & 00000001)

Clearly, condition false as it leads to 0 when multiplied.

16.What will be the output of the C program?


#include<stdio.h>

int main()

int a = 4, b = 2;

printf("a^b = %d", a^b);

return 0;

A. 12 B. 10 C. 8 D. 6

Explanation

^ represent XOR otherwise called as exclusive OR.

Bitwise EX OR operator returns 1 if and only if either of the operands is 1

Bitwise EX OR operator returns 0 for similar inputs

Here a = 4 which is a = 0100

and b = 2 which is b = 0010

0100 * 0010 = 0110

Thus output is 0110 i.e) 6

17. What will be the output of the C program?

#include<stdio.h>
int main()

int a = 4, b = 2;

printf("a|b = %d\n", a|b);

return 0;

A. 6 B. 7 C. 5 D. 4

Explanation

Bitwise OR operator returns 1 either or both of the operands are 1 .

Here a = 4 which is a = 0100

and b = 2 which is b = 0010

0100 * 0010 = 0110

Thus output is 0110 i.e) 6

18. What will be the output of the C program?

#include<stdio.h>

int main()

int a = 7, b = 4, c = 2;

printf("a|b&c = %d\n", a|b&c);

return 0;

}
A. 3 B. 8 C. 6 D. 7

Explanation

As operator enjoys priority, & has higher priority over |.

a = 7 which is 0111

b = 4 which is 0100

c = 2 which is 0010

Now a|b&c which is 0111|0100& 0010.

0111|0000.

0111 which is 7 and then outputted.

19. What will be the output of the C program?

#include<stdio.h>

int main()

int s[3] = {'\0', '\0'};

int x[3] = {'\0', '\0'};

int c = s[1] + x[0];

printf("%d",c);

return 0;
}

A. Compilation error B. 0 C. No Output D. some garbage value

Explanation

all value are set to \0 NULL.

20. What will be the output of the C program?

#include<stdio.h>

int main()

int a = NULL - true;

printf("%d",a);

return 0;

A. Compilation error B. 1 C. -1 D. Runtime error

Explanation

Simply, NULL = 0; and true = 1;

0 - 1 = -1

21.What will be the output of the C program?

#include<stdio.h>

int main()

printf("%d",3 * 2--);
}

A. 3 B. 9 C. 6 D. Compilation error

Explanation

2-- stands meaningless

22. What will be the output of the C program?

#include<stdio.h>

int main()

int a = 1, b = 3, c;

c = b << a;

b = c * (b * (++a)--);

a = a >> b;

printf("%d",b);

return 0;

A. 36 B. Compilation error C. 30 D. 24

Explanation

c = 3 >> 1;

c = 0011 << 1;

c = 0110;

c=6

b = c * (b * (++a)--);

b = 6 * (3 * (2)--);
b = 6 * 6( post -- is neglected in this case)

b = 36 which is outputted.

23. What will be the output of the C program?

#include<stdio.h>

int main()

int i = 10;

i++;

i * i;

printf("%d\n",i);

return 0;

A. 121 B. 100 C. 10 D. 11

Explanation

i++ alone store the result in variable i.

24. What will be the output of the C program?

#include<stdio.h>

int main()

struct bb

char name[] = "SMARTINDIA.in";


};

struct bb *s = malloc(sizeof(struct bb));

printf("%s",s -> name);

return 0;

A. SMARTINDIA.in B. Compilation error C. Runtime error D. garbage value

Explanation

Initialization for structure members should not be done inside the structure declaration itself.

25. What will be the output of the C program?

#include<stdio.h>

#define x =

int main()

int a;

a x 5;

printf("%d",a);

return 0;

A. Compilation error B. Runtime error C. 5 D. program incomplete

Explanation

= is defined in proprocessor with the name of x. Thus no problem with a normal compilation flow.

Decision making statements


1. What will be the output of the C program?

#include<stdio.h>

int x = 0;

int main(){

if(x == x)

printf("hai this is if");

else

printf("hai this is else");

return 0;

A. hai this is if B. hai this is else C. prints nothing D. Compile Time Error

Explanation

This program will print the statement inside the if block because if statement just compare two
values(0 == 0, condition is true) and returns 1(true). So the statement inside if block is executed.

2. What will be the output of the C program?

#include<stdio.h>

#define FALSE -1

#define NULL 0

#define TRUE 1

int main(){

if(NULL)

printf("NULL");

else if(FALSE)
printf("TRUE");

else

printf("FALSE");

return 0;

A. FALSE B. NULL C. TRUE D. Compilation Error

Explanation

The if block will be executed if the condition returns a non-zero value. In the above program,
if(NULL) is appears like if(0), here the condition is false. Then else if(FALSE) is appears like else if(-1),
here it has a non-zero value so the else if block is executed.

3. What will be the output of the C program?

#include<stdio.h>

int main(){

int i = 0, j = 0;

if(i++ == j++)

printf("%d %d", i--, j--);

else

printf("%d %d", i, j);

return 0;

A. 0 0 B. 0 1 C. 1 0 D. 1 1

Explanation

In the above program, the if(i++ == j++) is appears like if(0 == 0), condition is true. So the if block gets
executed.

4. What will be the output of the C program?


#include<stdio.h>

int main(){

int i = 0, j = 1, k = 0;

if(++k, j, i++)

printf("%d %d %d", i, j, k);

return 0;

A. Prints Nothing B. 1 1 0 C. 0 1 0 D. Compilation Error

Explanation

In the above program, the if(++k, j, i++) will appears like if(1, 1, 0) consider this as (expression1,
expression2, expression3), in this context, first expression1 is evaluated, expression2 is evaluated,
then expression3 is evaluated, and the value of expression3 is returned for the whole expression. As
a result the condition is false. So it will prints nothing.

5. What will be the output of the C program?

#include<stdio.h>

int main(){

int i;

if(true)

printf("This will work");

else

printf("This will not work");

return 0;

A. This will work B. This will not work C. Compilation Error D. Runtime Error

Explanation
Error: Undefined symbol 'true'.

The above C program will prints error message because C programming does not support the
keyword 'true' and 'false'.

6. What will be the output of the C program?

#include<stdio.h>

int main()

charstr[8] = "if block";

if(str == "if block")

printf("if block executed");

else

printf("else block executed");

return 0;

A. if block executed B. else block executed C. Compilation Error D. None of the above

Explanation

7. What will be the output of the C program?

#include<stdio.h>

int main()

charstr[] = "\0";

if(printf("%s",str))

printf("inside if block");

else
printf("inside else block");

return 0;

A. inside else block B. inside if block C. Compilation Error D. Runtime Error

Explanation

In the above C program, the printf statement or expression inside if block prints nothing. So the
expression returns 0 and else block gets executed.

8. What will be the output of the C program?

#include<stdio.h>

int main()

if(printf("0"))

printf("inside if block");

else

printf("inside else block");

return 0;

A. inside if block B. inside else block C. 0inside else block D. 0inside if block

Explanation

The printfstatement inside if block prints 0, so the expression returns 1. Finally the if block gets
executed.

9. What will be the output of the C program?


#include<stdio.h>

#define NULL 0

int main()

if(printf("0") == NULL)

printf("inside if block");

else

printf("inside else block");

return 0;

A. 0inside if block B. 0inside else block C. inside else block D. inside if block

Explanation

The if statement compares two expressions and returns a result. We can consider the if(printf("0")
== NULL) as if(expression1 == expression2). The expression1(printf("0") prints 0) returns 1 and
expression2 is 0. So the condition is false and else block will be executed.

10. What will be the output of the C program?

#include<stdio.h>

int main(){

int i = 5, j = 4;

if(!printf(""))

printf("%d %d", i, j);

else

printf("%d %d", i++, ++j);

return 0;

A. 6 5 B. 5 5 C. 5 4 D. 6 4
Explanation

The if(!printf("")) is in a format if(!expression). The expression(printf("") returns 0 because it prints


nothing) and the condition becomes true because of ! operator. So the if block gets executed.

Das könnte Ihnen auch gefallen