Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

What will be output when you will execute following c code? #include<stdio.h> int main(){ double num=5.

2; int var=5; printf("%d\t",sizeof(!num)); printf("%d\t",sizeof(var=15/2)); printf("%d",var); return 0; } Choose all that apply:
(A) (B) (ans)(C) (D) (E)

4 2 7 4 4 5 2 2 5 2 4 7 8 2 7

E x p l a n a t i o n :
Turbo C++ 3.0: 2 2 5 Turbo C ++4.5: 2 2 5 Linux GCC: 4 4 5 Visual C++: 4 4 5 sizeof(Expr) operator always returns the an integer value which represents the size of the final value of the expression expr. Consider on the following expression: !num =!5.2 =0

0 is int type integer constant and it size is 2 by in TURBO C 3.0 compiler and 4 in the TURBO C 4.5 and Linux GCC compilers. Consider on the following expression: var = 15/2 => var = 7 => 7 7 is int type integer constant. Any expression which is evaluated inside the sizeof operator its scope always will be within the sizeof operator. So value of variable var will remain 5 in the printf statement

Consider on following declaration: (i) short i=10; (ii) static i=10; (iii) unsigned i=10; (iv) const i=10; Choose correct one:
(A)

Only (iv) is incorrect (B) Only (ii) and (iv) are incorrect (C) Only (ii),(iii) and (iv) are correct (D) Only (iii) is correct (ans)(E) All are correct declaration
Answ er

E x p l a n a t i o n :
Default data type of above all declaration is int.

What will be output when you will execute following c code? #include<stdio.h> int main(){ signed x,a; unsigned y,b; a=(signed)10u; b=(unsigned)-10; y = (signed)10u + (unsigned)-10; x = y; printf("%d %u\t",a,b); if(x==y) printf("%d %d",x,y); else if(x!=y) printf("%u %u",x,y); return 0; } Choose all that apply:
(A) 10

-10 0 0 (B) 10 -10 65516 -10 (C) 10 -10 10 -10 (D) 10 65526 0 0 (E) Compilation error
Answ er

E x p l a n a t i o n :
Turbo C++ 3.0: 10 65526 0 0 Turbo C ++4.5: 10 65526 0 0 Linux GCC: 10 4294967286 0 0

Visual C++: 10 4294967286 0 0 a=(signed)10u; signed value of 10u is +10

so, a=10 b=(unsigned)-10; unsigned value of -10 is : MAX_VALUE_OF_UNSIGNED_INT 10 + 1 In turbo c 3.0 complier max value of unsigned int is 65535 So, b = 65526 y = (signed)10u + (unsigned)-10; = 10 + 65526 = 65536 = 0 (Since 65536 is beyond the range of unsigned int. zero is its corresponding cyclic vlaue) X = y = 0

Which of the following is integral data type?


(A) void (B) char (C) float (E)

(D) double
Answ er

None of these

E x p l a n a t i o n :
In c char is integral data type. It stores the ASCII value of any character constant. What will be output when you will execute following c code? #include<stdio.h> int main(){ int a=-5; unsigned int b=-5u;

if(a==b) printf("Avatar"); else printf("Alien"); return 0;

Choose all that apply:


(A) Avatar (B) Alien (C) Run

time error (D) Error: Illegal assignment Error: Dont compare signed no. with unsigned (E) no.
Answ er

E x p l a n a t i o n :
Turbo C++ 3.0: Avatar Turbo C ++4.5: Avatar Linux GCC: Avatar Visual C++: Avatar int a=-5; Here variable a is by default signed int. unsigned int b=-5u; Constant -5u will convert into unsigned corresponding unsigned int value will be : 65536 5 + 1= 65532 So, b = 65532

int.

Its

In any binary operation of dissimilar data type for example: a == b Lower data type operand always automatically type casted into the operand of higher data type before performing the operation and result will be higher data type.

In c signed int is higher data type than unsigned int. So variable b will automatically type casted into signed int. So corresponding signed value of 65532 is -5 Hence, a==b

1 .
What will be output when you will execute following c code? #include<stdio.h> void main(){ int a=5,b=10,c=1; if(a&&b>c){ printf("cquestionbank"); } else{ break; } } Choose all that apply:
(A) (B) (C) (D) (E)

cquestionbank It will print nothing Run time error Compilation error None of the above

Answ er

E x p l a n a t i o n :

Keyword break is not syntactical part of if-else statement. So we cannot use break keyword in if-else statement. This keyword can be use in case of loop or switch case statement. Hence when you will compile above code compiler will show an error message: Misplaced break.

2 .
What will be output when you will execute following c code? #define PRINT printf("Star Wars");printf(" Psycho"); #include<stdio.h> void main(){ int x=1; if(x--) PRINT else printf("The Shawshank Redemption"); } Choose all that apply:
(A) (B) (C) (D) (E)

Stars Wars Psycho The Shawshank Redemption Warning: Condition is always true Warning: Condition is always false Compilation error

Answ er

E x p l a n a t i o n :

PRINT is macro constant. Macro PRINT will be replaced by its defined statement just before the actual compilation starts. Above code is converted as: void main(){ int x=1; if(x--) printf("Star Wars"); printf(" Psycho"); else printf("The Shawshank Redemption"); } If you are not using opening and closing curly bracket in if clause, then you can write only one statement in the if clause. So compiler will think: (i) if(x--) printf("Star Wars"); It is if statement without any else. It is ok. (ii) printf(" Psycho"); It is a function call. It is also ok (iii) else printf("The Shawshank Redemption"); You cannot write else clause without any if clause. It is cause of compilation error. Hence compiler will show an error message: Misplaced else

3 .
What will be output when you will execute following c code? #define True 5==5 #include<stdio.h> void main(){

if(.001-0.1f) printf("David Beckham"); else if(True) printf("Ronaldinho"); else printf("Cristiano Ronaldo");

Choose all that apply:


(A) (B) (C) (D) (E)

David Beckham Ronaldinho Cristiano Ronaldo Warning: Condition is always true Warning: Unreachable code

Answ er

E x p l a n a t i o n :
As we know in c zero represents false and any non-zero number represents true. So in the above code: (0.001 0.1f) is not zero so it represents true. So only if clause will execute and it will print: David Beckham on console. But it is bad programming practice to write constant as a condition in if clause. Hence compiler will show a warning message: Condition is always true Since condition is always true, so else clause will never execute. Program control cannot reach at else part. So compiler will show another warning message: Unreachable code

4 .

What will be output when you will execute following c code? #include<stdio.h> void main(){ int a=100; if(a>10) printf("M.S. Dhoni"); else if(a>20) printf("M.E.K Hussey"); else if(a>30) printf("A.B. de villiers"); } Choose all that apply:
(A) (B)

M.S. Dhoni A.B. de villiers M.S Dhoni M.E.K Hussey

(C)

A.B. de Villiers
(D) (E)

conditions are true None of the above

Compilation error: More than one

Answ er

E x p l a n a t i o n :
In case of if if else if else Statement if first if clause is true the compiler will never check rest of the if else clause and so on.

5 .
What will be output when you will execute following c code? #include<stdio.h> void main(){ int x=-1,y=-1; if(++x=++y) printf("R.T. Ponting"); else printf("C.H. Gayle"); } Choose all that apply:
(A) (B) (C) (D) (E)

R.T Ponting C.H. Gayle value that is never used Compilation error Warning: x and y are assigned a

Warning: Condition is always true

Answ er

E x p l a n a t i o n :
Consider following statement: ++x=++y As we know ++ is pre increment operator in the above statement. This operator increments the value of any integral variable by one and return that value. After performing pre increments above statement will be: 0=0

In C language it is illegal to assign a constant value to another constant. Left side of = operator must be a container i.e. a variable. So compiler will show an error message: Lvalue required
In c if you assign any value to variable but you dont perform any operator or perform operation only using unary operator on the variable the compiler will show a warning message: Variable is assigned a value that is never

6 .
What will be output when you will execute following c code? #include<stdio.h> void main(){ if(sizeof(void)) printf("M. Muralilidaran"); else printf("Harbhajan Singh"); } Choose all that apply:
(A) (B) (C) (D) (E)

M. Muralilidaran Harbhajan Singh Warning: Condition is always false Compilation error None of the above

Answ er

E x p l a n a t i o n :

It illegal to find size of void data type using sizeof operator. Because size of void data type is meaning less.

7 .
What will be output when you will execute following c code? #include<stdio.h> void main(){ int m=5,n=10,q=20; if(q/n*m) printf("William Gates"); else printf(" Warren Buffet"); printf(" Carlos Slim Helu"); } Choose all that apply:
(A) (B) (C) (D) (E)

William Gates Warren Buffet Carlos Slim Helu Run time error Compilation error None of the above

Answ er

E x p l a n a t i o n :
Consider the following expression: q / n * m In this expression there are two operators. They are: /: Division operator

*: Multiplication operator Precedence and associate of each operator is as follow: Precedence 1 Operator /,* Associate Left to right

Precedence of both operators is same. Hence associate will decide which operator will execute first. Since Associate is left to right. So / operator will execute then * operator will execute. = q / n * m = 20 / 10 * 5 = 2 * 5 =10 As we know in c zero represents false and any non-zero number represents true. Since 10 is non- zero number so if clause will execute and print: William Gates Since in else clause there is not any opening and closing curly bracket. So compiler will treat only one statement as a else part. Hence last statement i.e. printf(" Carlos Slim Helu"); is not part of if-else statement. So compiler will also print: Carlos Slim Helu So output of above code will be: William Gates Carlos Slim Helu at the end

8 .
What will be output when you will execute following c code? #include<stdio.h> void main(){ if(!printf("Mukesh Ambani")) if(printf(" Lakashmi Mittal"));

} Choose all that apply:


(A) (B) (C) (D) (E)

Mukesh Ambani Lakashmi Mittal It will print nothing Mukesh Ambani Lakashmi Mittal Compilation error: if statement without body

Answ er

E x p l a n a t i o n :
Return type of printf function is int. This function return a integral value which is equal to number of characters a printf function will print on console. First of all printf function will: Mukesh Ambani. Since it is printing 13 character so it will return 13. So, !printf("Mukesh Ambani") = !13 = 0 In c language zero represents false. So if(0) is false so next statement which inside the body of first if statement will not execute.

9 .
What will be output when you will execute following c code? #include<stdio.h> void main(){ if("ABC") printf("Barack Obama\n"); if(-1) printf("Hu Jintao\n");

if(.92L) if(0) if('W')

printf("Nicolas Sarkozy\n"); printf("Ben Bernanke\n"); printf("Vladimir Putin\n");

Choose all that apply:

(A) It will print nothing (B) Barack Obama

Hu Jintao Nicolas Sarkozy

Vladimir Putin (C) Barack Obama Hu Jintao Nicolas Sarkozy Ben Bernanke Vladimir Putin (D) Hu Jintao Nicolas Sarkozy Vladimir Putin (E)

Compilation error
Answ er

E x p l a n a t i o n :
ABC: It is string constant and it will always return a non-zero memory address. 0.92L: It is long double constant.

W: It is character constant and its ASCII value is As we know in c language zero represents false and any non-zero number represents true. In this program condition of first, second, third and fifth if statements are true.

10.
What will be output when you will execute following c code? #include<stdio.h> void main(){ if(0xA) if(052) if('\xeb') if('\012') printf("Tom hanks"); else; else; else; else; } Choose all that apply:
(A) (B) (C) (D) (E)

Tom hanks Compilation error: Misplaced else Compilation error: If without any body Compilation error: Undefined symbol Warning: Condition is always true

Answ er

E x p l a n a t i o n :

oxA: It 052: It \xeb: \012:

is hexadecimal integer constant. octal integer constant. It is hexadecimal character constant. It is octal character constant.

As we know in c zero represents false and any non-zero number represents true. All of the above constants return a non-zero value. So all if conditions in the above program are true. In c it is possible to write else clause without any body.

11.
What will be output when you will execute following c code? #include<stdio.h> void main(){ int a=10; if(printf("%d",a>=10)-10) for(;;) break; else; } Choose all that apply:
(A) (B) (C) (D) (E)

It will print nothing 0 1 Compilation error: Misplaced else Infinite loop

Answ er

E x p l a n a t i o n :
Return type of printf function is int. This function return a integral value which is equal to number of charcters printf function will print on console. Operator >= will return 1 if both operands are either equal or first operand is grater than second operand. So a>=10 will return 1 since a is equal to 10.Thus printf function will print 1. Since this function is printing only one character so it will also return 1. So, printf("%d",a>=10) - 10 = 1 - 10 = -9 Since -9 is non-zero number so if(-9) is true condition hence if clause will execute which contains an infinite loop but due to break keyword it will come out of loop.

12.

(Very Good).

What will be output when you will execute following c code? #include<stdio.h> void main(){ int a=5,b=10; if(++a||++b) printf("%d %d",a,b); else printf("John Terry"); } Choose all that apply:
(A) (B)

5 10 6 11

(C) (D) (E)

6 10 5 11 John Terry

Answ er

E x p l a n a t i o n :
Consider the following expression: ++a || ++b In this expression || is Logical OR operator. Two important properties of this operator are: Property 1: (Expression1) || (Expression2) || operator returns 0 if and only if both expressions return a zero otherwise it || operator returns 1. Property 2: To optimize the execution time there is rule, Expression2 will only evaluate if and only if Expression1 return zero. In this program initial value of a is 5. So ++a will be 6. Since ++a is returning a non-zero so ++b will not execute and if condition will be true and if clause will be executed.

13.
What will be output when you will execute following c code? #include<stdio.h> void main(){ static int i; for(;;)

if(i+++"The Matrix") printf("Memento"); else break;

Choose all that apply:


(A) (B) (C) (D) (E)

It will print Memento at one time It will print Memento at three times It will print Memento at ten times It will print Memento at infinite times Compilation error: Unknown operator +++

Answ er

E x p l a n a t i o n :
Think yourself

14.
What will be output when you will execute following c code? #include<stdio.h> void main(){ int x=1; if(x--) printf("The Godfather"); --x; else printf("%d",x); } Choose all that apply:

(A) (B) (C) (D) (E)

The Godfather 1 0 Compilation error None of the above

Answ er

E x p l a n a t i o n :
If you are not using { and } in if clause then you can write only one statement. Otherwise it will cause of compilation error: Misplace else

15.
What will be output when you will execute following c code? #include<stdio.h> void main(){ if('\0'); else if(NULL) printf("cquestionbank"); else; } Choose all that apply:
(A) (B) (C)

cquestionbank It will print nothing Warning: Condition is always true

(D) (E)

Warning: Unreachable code Compilation error: if statement without any body

Answ er

E x p l a n a t i o n :
\0 is null character constant. Its ASCII value is zero. if(0) means false so program control will check it else if clause. NULL is macro constant which has been defined in stdio.h which also returns zero.

16.
What will be output when you will execute following c code? #include<stdio.h> void main(){ int a=5,b=10; clrscr(); if(a<++a||b<++b) printf("%d %d",a,b); else printf("John Terry"); } Choose all that apply:
(A) (B) (C) (D) (E)

5 10 6 11 6 10 Compilation error John Terry

Answ er

E x p l a n a t i o n :
Consider the following expression: a<++a||b<++b In the above expression || is logical OR operator. It divides any expression in the sub expressions. In this way we have two sub expressions: (1) a<++a (2) b<++b In the expression: a< ++a There are two operators. There precedence and associate are: Precedence 1 2 Operator ++ < Associate Right to left Left to right

From table it is clear first ++ operator will perform the operation then < operator. One important property of pre-increment (++) operator is: In any expression first pre-increment increments the value of variable then it assigns same final value of the variable to all that variables. So in the expression: a < ++a Initial value of variable a is 5. Step 1: Increment the value of variable a in whole expression. Final value of a is 6. Step 2: Now start assigning value to all a in the expression. After assigning 6 expression will be: 6 < 6 Since condition is false .So second expression i.e. b<++b will be evaluated. Again 11 < 11 is false. So ||

will operator will return zero and else clause will execute.

17.
What will be output when you will execute following c code? #include<stdio.h> void main(){ int x=1,y=2; if(--x && --y) printf("x=%d y=%d",x,y); else printf("%d %d",x,y); } Choose all that apply:
(A) (B) (C) (D) (E)

1 2 x=1 y=2 0 2 x=0 y=1 0 1

Answ er

E x p l a n a t i o n :
Consider the following expression: --x && --y In this expression && is Logical AND operator. important properties of this operator are: Property 1: (Expression1) && (Expression2) Two

&& operator returns 1 if and only if both expressions return a non-zero value other wise it && operator returns 0. Property 2: To optimize the execution time there is rule, Expression2 will only evaluate if and only if Expression1 return a non-zero value. In this program initial value of x is 1. So x will be zero. Since -x is returning zero so -y will not execute and if condition will be false. Hence else part will be executed.

18.
What will be output when you will execute following c code? #include<stdio.h> void main(){ signed int a=-1; unsigned int b=-1u; if(a==b) printf("The Lord of the Rings"); else printf("American Beauty"); } Choose all that apply:
(A) (B) (C) (D)

The Lord of the Rings American Beauty


Compilation error: Cannot compare signed number with unsigned number

Compilation error: Undefined symbol -1u

(E)

Warning: Illegal operation

Answ er

E x p l a n a t i o n :
Read following tutorial: Data type tutorial

19.
What will be output when you will execute following c code? #include<stdio.h> void main(){ char c=256; char *ptr="Leon"; if(c==0) while(!c) if(*ptr++) printf("%+u",c); else break; } Choose all that apply:
(A) (B) (C) (D) (E)

+256+256+256+256 0000 +0+0+0+0 It will print +256 at infinite times Compilation error

Answ er

E x p l a n a t i o n :
In the above program c is signed (default) char variable. Range of signed char variable in Turbo c is from -128 to 127. But we are assigning 256 which is beyond the range of variable c. Hence variable c will store corresponding cyclic value according to following diagram:

Since 256 is positive number move from zero in clock wise direction. You will get final value of c is zero.

if(c==0) It is true since value of c is zero.

Negation operator i.e. ! is always return either zero or one according to following rule:
!0 = 1 !(Non-zero number) = 0 So, !c = !0 =1 As we know in c zero represents false and any nonzero number represents true. So while(!c) i.e. while(1) is always true. In the above program prt is character pointer. It is pointing to first character of string Leon according to following diagram:

In the above figure value in circle represents ASCII value of corresponding character. Initially *ptr means L. So *ptr will return ASCII value of character constant L i.e. 76 if(*ptr++) is equivalent to : if(L) is equivalent to: if(76) . It is true so in first iteration it will print +0. Due to ++ operation in second iteration ptr will point to character constant e and so on. When ptr will point \0 i.e. null character .It will return its ASCII value i.e. 0. So if(0) is false. Hence else part will execute.

20.
What will be output when you will execute following c code? #include<stdio.h> void main(){ int a=2; if(a--,--a,a) printf("The Dalai Lama"); else printf("Jim Rogers");

} Choose all that apply:


(A) (B) (C) (D) (E)

The Dalai Lama Jim Rogers Run time error Compilation error: Multiple parameters in if statement None of the above

Answ er

E x p l a n a t i o n :
Consider the following expression: a-- , --a , a In c comma is behaves as separator as well as operator.In the above expression comma is behaving as operator.Comma operator enjoy lest precedence in precedence table andits associatively is left to right. So first of all left most comma operator will perform operation then right most comma will operator in the above expression. After performing a-- : a will be 2 After performing --a : a will be 0 a=0 As we know in c zero represents false and any non-zero number represents true. Hence else part will execute.

You might also like