qust & ans inc

101
“Questions & Answers In C” (1) What will be output of the following program? #include<stdio.h> int main(){ float a=0.7; if(a<0.7){ printf("C"); } else{ printf("C++"); return 0; } } Explanation Output: c Explanation: 0.7 is double constant (Default). Its binary value is written in 64 bit. Binary value of 0.7 = (0.1011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011 ) Now here variable a is a floating point variable while 0.7 is double constant. So variable a will contain only 32 bit value i.e.

Upload: nayakq

Post on 13-Jan-2015

3.126 views

Category:

Technology


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Qust & ans inc

“Questions & Answers In C”

(1)What will be output of the following program?

#include<stdio.h> int main(){    float a=0.7;    if(a<0.7){         printf("C");    }    else{         printf("C++");         return 0;    }}

Explanation

Output: cExplanation: 0.7 is double constant (Default). Its binary value is written in 64 bit.

Binary value of 0.7 = (0.1011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011 )

Now here variable a is a floating point variable while 0.7 is double constant. So variable a will contain only 32 bit value i.e.

a = 0.1011 0011 0011 0011 0011 0011 0011 0011 while0.7 = 0.1011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011....It is obvious a < 0.7

Page 2: Qust & ans inc

Hide

(2)What will be output of the following program?         #include<stdio.h> int main(){    int i=5,j;    j=++i+++i+++i;    printf("%d %d",i,j);    return 0;}

Explanation

Output: 8 24Explanation:

Rule :- ++ is pre increment operator so in any arithmetic expression it first increment the value of variable by one in whole expression then starts assigning the final value of variable in the expression.

Compiler will treat this expression j = ++i+++i+++i; asi = ++i + ++i + ++i;

Initial value of i = 5 due to three pre increment operator final value of i=8.Now final value of i i.e. 8 will assigned to each variable as shown in the following figure:

So, j=8+8+8

Page 3: Qust & ans inc

j=24 andi=8

Hide

(3)What will be output of the following program?

#include<stdio.h> int main(){    int i=1;    i=2+2*i++;    printf("%d",i);    return 0;}

Explanation

Output: 5Explanation:i++ i.e. when postfix increment operator is used any expression the it first assign the its value in the expression the it increments the value of variable by one. So, i = 2 + 2 * 1 i = 4 Now i will be incremented by one so i = 4 + 1 = 5

Hide

(4)What will be output of the following program?

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

Page 4: Qust & ans inc

    int a=2,b=7,c=10;    c=a==b;    printf("%d",c);    return 0;}

Explanation

Output: 0Explanation: == is relational operator which returns only two values.0: If a == b is false 1: If a == b is true Since a=2 b=7 So, a == b is false hence b=0

Hide

(5)What will be output of the following program?

#include<stdio.h> void main(){    int x;    x=10,20,30;    printf("%d",x);    return 0;}

Explanation

Output: 10Explanation :Precedence table:

Page 5: Qust & ans inc

Operator Precedence Associative = More than , Right to left , Least Left to right

Since assignment operator (=) has more precedence than comma operator .So = operator will be evaluated first than comma operator. In the following expression x = 10, 20, 30 First 10 will be assigned to x then comma operator will be evaluated.

Hide

(6)What will be output of the following program?

#include<stdio.h> int main(){    int a=0,b=10;    if(a=0){         printf("true");    }    else{         printf("false");    }    return 0;}

Explanation

Output: falseExplanation:As we know = is assignment operator not relation operator. So, a = 0 means zero will assigned to variable a. In c zero represent false and any non-zero number represents true.

Page 6: Qust & ans inc

So, if(0) means condition is always false hence else part will execute.

Hide

(7)What will be output of the following program?

#include<stdio.h> int main(){    int a;    a=015 + 0x71 +5;    printf("%d",a);    return 0;}

Explanation

Output: 131Explanation:015 is octal number its decimal equivalent is = 5 * 8 ^ 0 + 1 * 8 ^ 1 = 5 + 8 = 13 0x71 is hexadecimal number (0x is symbol of hexadecimal) its decimal equivalent is = 1 * 16 ^ 0 + 7 * 16 ^ 1 = 1 + 112 = 113 So, a = 13 + 113 + 5 = 131

Hide

(8)What will be output of the following program?

#include<stdio.h> int main(){    printf("%d %d %d",sizeof(3.14),sizeof(3.14f),sizeof(3.14L));

Page 7: Qust & ans inc

    return 0;}

Explanation

Output: 8 4 10

Explanation: 3.14f is floating point constant. Its size is 4 byte. 3.14 is double constant (default). Its size is 8 byte. 3.14L is long double constant. Its size is 10 byte. sizeof() operator always return the size of data type which is written inside the(). It is keyword.

Hide

(9)What will be output of the following program?

#include<stdio.h> int main(){    int x=100,y=20,z=5;    printf("%d %d %d");    return 0;}

Explanation

Output: 5 20 100

By default x, y, z are auto type data which are stored in stack in memory. Stack is LIFO data structure. So in stack first stores 100 then 20 then 5 and program counter will point top stack i.e. 5. Default value of %d in printf is data which is present in stack. So output is revere order of declaration. So output will be 5 20 100.

Page 8: Qust & ans inc

Hide

(10)What will be output of the following program?

#include<stdio.h>         int main(){    int a=2;    a=a++ + ~++a;    printf("%d",a);    return 0;}

Explanation

Output: -1Explanation: Same theory as question (2) and (13).

Hide

(11)What will be output of the following program?

#include<stdio.h> int main(){    int a;    a=sizeof(!5.6);    printf("%d",a);    return 0;}

Explanation

Page 9: Qust & ans inc

Output : 2Explanation: ! is negation operator it return either integer 0 or 1.! Any operand = 0 if operand is non zero.! Any operand = 1 if operand is zero.So, !5.6 = 0 Since 0 is integer number and size of integer data type is two byte.

Hide

(12)What will be output of the following program?

#include<stdio.h> int main(){    float a;    (int)a= 45;    printf("%d,a);    return 0;}

Explanation

Output: Compilation errorExplanation: After performing any operation on operand it always return some constant value.

(int) i.e. type casting operator is not exception for this. (int) a will return one constant value and we cannot assign any constant value to another constant value in c.

(int)a = 45; is equivalent to3456 = 45 ( Here 3456 in any garbage value of int(a)).

Page 10: Qust & ans inc

Hide

(13)What will be output of the following program?

#include<stdio.h> int main(){     int i=5;     int a=++i + ++i + ++i;     printf("%d",a);     return 0;}

Explanation

Output: 21Explanation: Rule : ++ (in ++i) is pre increment operator so in any arithmetic expression it first increment the value of variable by one in whole equation up to break point then start assigning the value of variable in the equation. There are many break point operators in. For example:

(1) Declaration statement.(2) && or operator.(3) Comma (,) operator etc.

In the following expression: int a=++i + ++i + ++i;

Here break point is due to declaration .It break after each increment i.e. (initial value of i=5) after first increment value 6 assign to variable i then in next increment will occur and so on. So, a = 6 + 7 + 8;

Page 11: Qust & ans inc

http://cquestionbank.blogspot.com/2010/07/c-program-examples.html

Data type questions

Note:  As you know size of data types is compiler dependent in c. Answer of all question is based upon that compilers whose word size is two byte. Just to know you, size of data type in 2 bytes compilers is as follow:

char :   1 byteint :    2 bytefloat :  4 bytedouble : 8 byte

Please adjust the answers according to size of data types in you compiler.

1.What will be output when you will execute following c code?

#include<stdio.h>void main(){    printf("%d\t",sizeof(6.5));    printf("%d\t",sizeof(90000));    printf("%d",sizeof('A'));}

Choose all that apply:(A) 4 2 1

(B) 8 2 1 

(C) 4 4 1

(D) 8 4 1

(E) 8 4 2Answ er

Page 12: Qust & ans inc

E x p l a n a t i o n :By default data type of numeric constants is:6.5 :  double 90000: long int‘A’: char In C size of data type varies from compiler to compiler. In TURBO C 3.0 (16 bit compilers) size of: double is 8 byteLong int is 4 byteCharacter constant is 2 byte   (size of char data type is one byte)In TURBO C 4.5 or Linux GCC compilers (32 bit compilers) size of:double is 8 bytelong int is 8 byteCharacter constant is 2 byte   

2.Consider on following declaring of enum.

(i)        enum  cricket {Gambhir,Smith,Sehwag}c;(ii)      enum  cricket {Gambhir,Smith,Sehwag};(iii)    enum   {Gambhir,Smith=-5,Sehwag}c;(iv)      enum  c {Gambhir,Smith,Sehwag};

Choose correct one:

(A) Only (i) is correct declaration(B) Only (i) and (ii) is correct declaration

(C) Only (i) and (iii) are correct declaration

(D) Only (i),(ii) and are correct declaration

(E) All four are correct declaration

E x p l a n a t i o n :Syntax of enum data type is:enum  [<tag_name>]{    <enum_constanat_name> [=<integer_ value>],    …} [<var_name>,…]

Answ er

Page 13: Qust & ans inc

Note: [] : Represents optional .<>: Represents any valid c identifier

3.What will be output when you will execute following c code?

#include<stdio.h>void main(){    signed x;    unsigned y;    x = 10 +- 10u + 10u +- 10;    y = x;    if(x==y)         printf("%d %d",x,y);    else if(x!=y)         printf("%u  %u",x,y);}

Choose all that apply:(A) 0 0

(B) 65536 -10 

(C) 0 65536 

(D) 65536 0

(E) Compilation error

E x p l a n a t i o n :Consider on the expression:x = 10 +- 10u + 10u +- 10;10: It is signed integer constant.10u: It is unsigned integer constant.X: It is signed integer variable.In any binary operation of dissimilar data type for example: a + bLower 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.

Answ er

Page 14: Qust & ans inc

As we know operators enjoy higher precedence than binary operators. So our expression is:x = 10 + (-10u) + 10u + (-10);  = 10 + -10 + 10 + (-10);   = 0Note: Signed is higher data type than unsigned int.So, Corresponding signed value of unsigned 10u is +10

4.Which of the following is not modifier of data type in c?

(A) Extern

(B) Interrupt

(C) Huge

(D) Register

(E) All of these are modifiers of data type

E x p l a n a t i o n :To know more about these go to following link:

5.What will be output when you will execute following c code?

#include<stdio.h>void main(){    double num=5.2;    int  var=5;    printf("%d\t",sizeof(!num));    printf("%d\t",sizeof(var=15/2));    printf("%d",var);}

Choose all that apply:(A) 4 2 7

(B) 4 4 5

(C) 2 2 5

(D) 2 4 7

Answ er

Page 15: Qust & ans inc

(E) 8 2 7

E x p l a n a t i o n :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=00 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=> 77 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.

6.What will be output when you will execute following c code?

#include<stdio.h>void main(){    const int *p;    int a=10;    p=&a;    printf("%d",*p);}

Choose all that apply:(A) 0

(B) 10

(C) Garbage value

(D) Any memory address

Answ er

Page 16: Qust & ans inc

(E) Error: Cannot modify const object

E x p l a n a t i o n :In the following declaration const int *p;p can keep address of constant integer.

7.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

(E) All are correct declaration 

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

8.What will be output when you will execute following c code?

#include<stdio.h>void main(){    int a= sizeof(signed) +sizeof(unsigned);    int b=sizeof(const)+sizeof(volatile);    printf("%d",a+++b);}

Choose all that apply:

Answ er

Answ er

Page 17: Qust & ans inc

(A) 10

(B) 9

(C) 8

(D) Error: Cannot find size of modifiers

(E) Error: Undefined operator +++

E x p l a n a t i o n :Default data type of signed, unsigned, const and volatile is int. In turbo c 3.0 size of int is two byte.So, a = 4 and b =4Now, a+++b= a++ + b= 4 + 4  //due to post increment operator.=8Note: In turbo c 4.5 and Linux gcc compiler size of int is 4 byte so your out will be 16

9.What will be output when you will execute following c code?

#include<stdio.h>void 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);}

Choose all that apply:(A) 10 -10   0 0

Answ er

Page 18: Qust & ans inc

(B) 10 -10   65516 -10 

(C) 10 -10   10 -10

(D) 10 65526      0 0

(E) Compilation error

E x p l a n a t i o n :a=(signed)10u;signed value of 10u is +10so, a=10 b=(unsigned)-10;unsigned value of -10 is :MAX_VALUE_OF_UNSIGNED_INT – 10 + 1In turbo c 3.0 complier max value of unsigned int is 65535So, b = 65526y = (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

10.Which of the following is integral data type?

(A) void

(B) char

(C) float

(D) double

(E) 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.

11.

Answ er

Answ er

Page 19: Qust & ans inc

What will be output when you will execute following c code?

#include<stdio.h>void main(){    volatile int a=11;    printf("%d",a);}

Choose all that apply:(A) 11

(B) Garbage

(C) -2

(D) We cannot predict

(E) Compilation error

E x p l a n a t i o n :We cannot predict the value of volatile variable because its value can be changed by any microprocessor interrupt.

12.What is the range of signed int data type in that compiler in which size of int is two byte?(A) -255 to 255

(B) -32767 to 32767

(C) -32768 to 32768

(D) -32767 to 32768

(E) -32768 to 32767

E x p l a n a t i o n :Note: Size of int is always equal to word length of micro preprocessor in which your compiler has based.

13.

Answ er

Answ er

Page 20: Qust & ans inc

What will be output when you will execute following c code?

#include<stdio.h>const enum Alpha{      X,      Y=5,      Z}p=10;void main(){    enum Alpha a,b;    a= X;    b= Z;    printf("%d",a+b-p);  }

Choose all that apply:(A) -4

(B) -5 

(C) 10

(D) 11

(E) Error: Cannot modify constant object

E x p l a n a t i o n :Default value of enum constant X is zero and Z = Y + 1 = 5 + 1 = 6So, a + b – p=0 + 6 -10 = -4

14.What will be output when you will execute following c code?

#include<stdio.h>void main(){    char a=250;    int expr;    expr= a+ !a + ~a + ++a;    printf("%d",expr);}

Answ er

Page 21: Qust & ans inc

Choose all that apply:(A) 249

(B) 250

(C) 0

(D) -6

(E) Compilation error

E x p l a n a t i o n :char a = 250;250 is beyond the range of signed char. Its corresponding cyclic value is: -6So, a = -6Consider on the expression:expr= a+ !a + ~a + ++a;Operator! , ~ and ++ have equal precedence. And it associative is right to left.So, First ++ operator will perform the operation. So value a will -5Now,Expr = -5 + !-5 + ~-5 + -5= -5 + !-5 + 4 - 5= -5 + 0 + 4 -5= -6

15.Consider on order of modifiers in following declaration:

(i)char volatile register unsigned c;(ii)volatile register unsigned char c;(iii)register volatile unsigned char c;(iv)unsigned char volatile register c;

Answ er

Page 22: Qust & ans inc

(A) Only (ii) is correct declaration

(B) Only (i) is correction declaration

(C) All are incorrect

(D) All are correct but they are different

(E) All are correct and same

E x p l a n a t i o n :Order of modifier of variable in c has not any significant.

Choose correct one:

16.What will be output when you will execute following c code?

#include<stdio.h>void main(){    int a=-5;    unsigned int b=-5u;    if(a==b)         printf("Avatar");    else         printf("Alien");}

Choose all that apply:(A) Avatar

(B) Alien

(C) Run time error

(D) Error: Illegal assignment

(E)Error: Don’t compare signed no. with unsigned no.

Answ er

Answ er

Page 23: Qust & ans inc

E x p l a n a t i o n :int a=-5;Here variable a is by default signed int.unsigned int b=-5u;Constant -5u will convert into unsigned int. Its corresponding unsigned int value will be :65536 – 5 + 1= 65532So, b = 65532

In any binary operation of dissimilar data type for example: a == bLower 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 -5Hence, a==b

17.What will be output when you will execute following c code?

#include<stdio.h>extern enum cricket x;void main(){    printf("%d",x);  }const enum cricket{    Taylor,    Kallis=17,    Chanderpaul}x=Taylor|Kallis&Chanderpaul;

Choose all that apply:(A) 0

(B) 15

(C) 16

(D) 17

Page 24: Qust & ans inc

(E) Compilation error

E x p l a n a t i o n :x=Taylor|Kallis&Chanderpaul= 0 | 17 & 18= 0 |(17 & 18) //& operator enjoy higher precedence than |=0 |16=16

18.Which of the following is not derived data type in c?

(A) Function

(B) Pointer

(C) Enumeration

(D) Array

(E) All are derived data type

E x p l a n a t i o n :Enum is primitive data type.

19.What will be output when you will execute following c code?

#include<stdio.h>enum A{    x,y=5,    enum B{         p=10,q    }varp;}varx;

void main(){    printf("%d %d",x,varp.q);}

Answ er

Answ er

Page 25: Qust & ans inc

Choose all that apply:(A) 0 11

(B) 5 10 

(C) 4 11

(D) 0 10(E) Compilation error

E x p l a n a t i o n :Nesting of enum constant is not possible in c.

20.Consider on following declaration in c:

(i)short const register i=10;(ii)static volatile const int i=10;(iii)unsigned auto long register i=10;(iv)signed extern float i=10.0;

Choose correct one:(A) Only (iv)is correct

(B) Only (ii) and (iv) is correct

(C) Only (i) and (ii) is correct

(D) Only (iii) correct

(E) All are correct declaration 

E x p l a n a t i o n :Option (III) is in correct due to we cannot specify two storage class auto and register in the declaration of any variable.Option (iv) is in correct due to we cannot use signed or unsigned modifiers with float data type. In c float data type by default signed and it cannot be unsigned.

Answ er

Answ er

Page 26: Qust & ans inc

Looping questions

(1)

What will be output of following c code?

#include<stdio.h>extern int x;int main(){    do{        do{             printf("%o",x);         }         while(!-2);    }    while(0);    return 0;}int x=8;

Explanation

Output: 10Explanation:Here variable x is extern type. So it will search the definition of variable x. which is present at the end of the code. So value of variable x =8There are two do-while loops in the above code.  AS we know do-while executes at least one time even that condition is false.  So program control will reach  at printf statement at it will print octal number 10 which is equal to decimal number 8. Note: %o is used to print the number in octal format.In inner do- while loop while condition is ! -2 = 0In C zero means false.  Hence program control will come out of the inner do-while loop.   In outer do-while loop while condition is 0. That is again false. So program control will also come out of the outer do-while loop.

Page 27: Qust & ans inc

Hide

(2)What will be output of following c code?         #include<stdio.h>int main(){    int i=2,j=2;    while(i+1?--i:j++)         printf("%d",i);    return 0;}

Explanation

Output: 1Explanation:Consider the while loop condition: i + 1 ? -- i : ++jIn first iteration:i + 1 = 3 (True)So ternary operator will return -–i i.e. 1 In c 1 means true so while condition is true. Hence printf statement will print 1 In second iteration:i+ 1 = 2 (True)So ternary operator will return -–i i.e. 0 In c zero means false so while condition is false. Hence program control will come out of the while loop.

Hide

(3)

Page 28: Qust & ans inc

What will be output of following c code?

#include<stdio.h>int main(){    int x=011,i;    for(i=0;i<x;i+=3){         printf("Start ");         continue;         printf("End");    }    return 0;}

Explanation

Output: Start Start Start Explantion: 011 is octal number. Its equivalent decimal value is 9.So, x = 9First iteration:i = 0i < x i.e. 0 < 9  i.e. if loop condition is true.Hence printf statement will print: StartDue to continue keyword program control will come at the beginning of the for loop and value of variable i will be:i += 3i = i + 3 = 3Second iteration:i = 3i < x i.e. 3 < 9 i.e. if loop condition is true.Hence printf statement will print: StartDue to continue keyword program control will come at the beginning of the for loop and value of variable i will be:i += 3i = i + 3 = 6Third iteration:i = 3

Page 29: Qust & ans inc

i < x i.e. 6 < 9 i.e. if loop condition is true.Hence printf statement will print: StartDue to continue keyword program control will come at the beginning of the for loop and value of variable i will be:i += 3i = i + 3 = 9fourth iteration:i = 6i < x i.e. 9 < 9 i.e. if loop condition is false.Hence program control will come out of the for loop.

Hide

(4)What will be output of following c code?

#include<stdio.h>

int main(){

    int i,j;

    i=j=2,3;

    while(--i&&j++)

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

    return 0;

Page 30: Qust & ans inc

}

Explanation

Output: 13Explanation:Initial value of variable i = 2j = 2Consider the while condition : --i && j++In first iteration: --i && j++= 1 && 2 //In c any non-zero number represents true.= 1 (True)So while loop condition is true. Hence printf function will print value of i = 1 and j = 3 (Due to post increment operator)In second iteration:--i && j++= 0 && 3  //In c zero represents false= 0  //FalseSo while loop condition is false. Hence program control will come out of the for loop.

Hide

(5)What will be output of following c code?

#include<stdio.h>

Page 31: Qust & ans inc

int main(){

    static int i;

    for(++i;++i;++i) {

         printf("%d ",i);

         if(i==4) break;

    }

    return 0;

}

Explanation

Output: 24Explanation:Default value of static int variable in c is zero. So, initial value of variable i = 0First iteration:For loop starts value: ++i i.e. i = 0 + 1 = 1 For loop condition: ++i i.e. i = 1 + 1 = 2 i.e. loop condition is true. Hence printf statement will print 2Loop incrimination: ++I i.e. i = 2 + 1 =3Second iteration:For loop condition: ++i i.e. i = 3 + 1 = 4 i.e. loop condition is true. Hence printf statement will print 4.

Page 32: Qust & ans inc

Since is equal to for so if condition is also true. But due to break keyword program control will come out of the for loop.

Hide

(6)What will be output of following c code?

#include<stdio.h>int main(){    int i=1;    for(i=0;i=-1;i=1) {         printf("%d ",i);         if(i!=1) break;    }    return 0;}

Explanation

Output: -1Explanation:Initial value of variable i is 1.First iteration:For loop initial value: i = 0For loop condition: i = -1 . Since -1 is non- zero number. So loop condition true. Hence printf function will print value of variable i i.e. -1 Since variable i is not equal to 1. So, if condition is true. Due to break keyword program control will come out of the for loop.

Hide

Page 33: Qust & ans inc

(7)What will be output of following c code?

#include<stdio.h>int main(){    for(;;) {         printf("%d ",10);    }    return 0;}

Explanation

Output: Infinite loopExplanation:In for loop each part is optional.

Hide

(8)What will be output of following c code?         #include<stdio.h>int r();int main(){    for(r();r();r()) {         printf("%d ",r());    }    return 0;}int r(){    int static num=7;    return num--;}

Page 34: Qust & ans inc

Explanation

Output: 5 2Explanation:First iteration:Loop initial value: r() = 7Loop condition: r() = 6Since condition is true so printf function will print r() i.e. 5Loop incrimination: r() = 4Second iteration:Loop condition: r() = 3Since condition is true so printf function will print r() i.e. 2Loop incrimination: r() = 1Third iteration:Loop condition: r() = 0 Since condition is false so program control will come out of the for loop.

Hide

(9)What will be output of following c code?         #include<stdio.h>#define p(a,b) a##b#define call(x) #xint main(){    do{         int i=15,j=3;         printf("%d",p(i-+,+j));    }    while(*(call(625)+3));    return 0;}

Page 35: Qust & ans inc

Explanation

Output: 11Explanation:First iteration:p(i-+,+j) =i-++j   // a##b=i - ++j=15 – 4= 11While condition is : *(call(625)+ 3)= *(“625” + 3) Note: # preprocessor operator convert the operand into the string.=*(It will return the memory address of character ‘\0’)= ‘\0’= 0  //ASCII value of character null characterSince loop condition is false so program control will come out of the for loop.

Hide

(10)

#include<stdio.h>int main(){    int i;    for(i=0;i<=5;i++);    printf("%d",i)    return 0;}

Explanation

Page 36: Qust & ans inc

Output: 6Explanation:It possible for loop without any body.

Hide

(11)What will be output of following c code?

#include<stdio.h>int i=40;extern int i;int main(){    do{         printf("%d",i++);    }    while(5,4,3,2,1,0);    return 0;}

Explanation

Output: 40Explanation: Initial value of variable i is 40First iteration:printf function will print i++ i.e. 40do - while condition is : (5,4,3,2,1,0)Here comma is behaving as operator and it will return 0. So while condition is false hence program control will come out of the for loop.

Hide

Page 37: Qust & ans inc

(12)What will be output of following c code?

#include<stdio.h>char _x_(int,...);int main(){    char (*p)(int,...)=&_x_;    for(;(*p)(0,1,2,3,4); )         printf("%d",!+2);    return 0;}char _x_(int a,...){    static i=-1;    return i+++a;}

Explanation

Output: 0Explanation:In c three continuous dot represents variable number of arguments. p is the pointer to the function _x_First iteration of for loop:Initial value: Nothing // In c it is optionalLoop condition: (*p)(0,1,2,3,4)= *(&_x_)(0,1,2,3,4)  // p = &_x_= _x_(0,1,2,3,4)    //* and & always cancel to each other= return i+++a= return i+ ++a= return -1 + 1= 0Since condition is false. But printf function will print 0. It is bug of c language.

Hide

(13)What will be output of following c code?

Page 38: Qust & ans inc

#include<stdio.h>int main(){    int i;    for(i=10;i<=15;i++){         while(i){             do{                 printf("%d ",1);                 if(i>>1)                      continue;             }while(0);             break;         }    }    return 0;}

Explanation

Output: 1 1 1 1 1 1For loop will execute six times. Note: continue keyword in do-while loop bring the program its while condition (while(0)) which is always false.

Hide

(14)How many times this loop will execute?

#include<stdio.h>int main(){    char c=125;    do         printf("%d ",c);    while(c++);    return 0;}

Page 39: Qust & ans inc

Explanation

Output: Finite timesExplanation:If we will increment the char variable c it will increment as:126,127,-128,-127,126 . . . .   , 3, 2, 1, 0When variable c = 0 then loop will terminate.    

Hide

(15)What will be output of following c code?         #include<stdio.h>int main(){    int x=123;    int i={         printf("c" "++")    };    for(x=0;x<=i;x++){         printf("%x ",x);    }    return 0;}

Explanation

Output: c++0 1 2 3Explanation:First printf function will print: c++ and return 3 to variable i.

Page 40: Qust & ans inc

For loop will execute three time and printf function will print 0, 1, 2 respectively.

Switch case questions1.What will be output when you will execute following c code?#include<stdio.h>void main(){     int check=2;     switch(check){        case 1: printf("D.W.Steyn");        case 2: printf(" M.G.Johnson");        case 3: printf(" Mohammad Asif");        default: printf(" M.Muralidaran");     } }

Choose all that apply:

(A) M.G.Johnson

(B) M.Muralidaran

(C) M.G.Johnson Mohammad Asif M.Muralidaran

(D) Compilation error

(E) None of the above

Explanation:

If we will not use break keyword in each case the program control will come in each case after the case witch satisfy the switch condition.

2.What will be output when you will execute following c code?#include<stdio.h>void main(){     int movie=1;     switch(movie<<2+movie){

Answ er

Page 41: Qust & ans inc

        default:printf("3 Idiots");        case 4: printf(" Ghajini");        case 5: printf(" Krrish");        case 8: printf(" Race");     }  }

Choose all that apply:(A) 3 Idiots Ghajini Krrish Race

(B) Race

(C) Krrish

(D) Ghajini Krrish Race

(E) Compilation error

Explanation:

We can write case statement in any order including the default case. That default case may be first case, last case or in between the any case in the switch case statement.

3.What will be output when you will execute following c code?#include<stdio.h>#define L 10void main(){     auto money=10;     switch(money,money*2){        case L:  printf("Willian");                  break;        case L*2:printf("Warren");                  break;        case L*3:printf("Carlos");                  break;        default: printf("Lawrence");        case L*4:printf("Inqvar");                  break;     }   }

Answ er

Page 42: Qust & ans inc

Choose all that apply:

(A) Willian

(B) Warren

(C) Lawrence Inqvar

(D) Compilation error: Misplaced default

(E) None of the above

Explanation:

In c comma is also operator which enjoy least precedence. So ifx = (a , b);Then x = bNote: Case expression can be macro constant.

4.What will be output when you will execute following c code?#include<stdio.h>void main(){     int const X=0;     switch(5/4/3){        case X:  printf("Clinton");                  break;        case X+1:printf("Gandhi");                  break;        case X+2:printf("Gates");                  break;        default: printf("Brown");     }  }

Choose all that apply:

(A) Clinton

(B) Gandhi

(C) Gates

(D) Brown

(E) Compilation error

Answ er

Page 43: Qust & ans inc

Explanation:

Case expression cannot be constant variable.

5.What will be output when you will execute following c code?#include<stdio.h>enum actor{    SeanPenn=5,    AlPacino=-2,    GaryOldman,    EdNorton};void main(){     enum actor a=0;     switch(a){        case SeanPenn:  printf("Kevin Spacey");                         break;        case AlPacino:  printf("Paul Giamatti");                         break;        case GaryOldman:printf("Donald Shuterland");                         break;        case EdNorton:  printf("Johnny Depp");     }   }

Choose all that apply:

(A) Kevin Spacey

(B) Paul Giamatti

(C) Donald Shuterland

(D) Johnny Depp

(E) Compilation error

Explanation:

Default value of enum constant GaryOldman = -2 +1 = -1

Answ er

Answ er

Page 44: Qust & ans inc

And default value of enum constant EdNorton = -1 + 1 = 0Note: Case expression can be enum constant.

6.What will be output when you will execute following c code?#include<stdio.h>void main(){     switch(*(1+"AB" "CD"+1)){        case 'A':printf("Pulp Fiction");                  break;        case 'B':printf("12 Angry Man");                  break;        case 'C':printf("Casabance");                  break;        case 'D':printf("Blood Diamond");     }     }

Choose all that apply:

(A) Pulp Fiction

(B) 12 Angry Man

(C) Casabance

(D) Blood Diamond

(E) Compilation error

Explanation:

Consider on the expression:*(1+"AB" "CD"+1)= *(2+"AB" "CD")= *(2+"ABCD")=*("CD")='C'

Note: Case expression can be character constant.

7.

Answ er

Page 45: Qust & ans inc

What will be output when you will execute following c code?#include<stdio.h>void main(){    char *str="ONE"    str++;    switch(str){        case "ONE":printf("Brazil");                break;        case "NE": printf("Toy story");                break;        case "N":  printf("His Girl Friday");                break;        case "E":  printf("Casino Royale");     }   }

Choose all that apply:

(A) Brazil

(B) Toy story

(C) His Girl Friday

(D) Casino Royale

(E) Compilation error

Explanation:

Case expression cannot be string constant.

8.What will be output when you will execute following c code?#include<stdio.h>void main(){    switch(5||2|1){        case 3&2:printf("Anatomy of a Murder");              break;        case -~11:printf("Planet of Apes");               break;        case 6-3<<2:printf("The conversation");               break;

Answ er

Page 46: Qust & ans inc

    case 5>=5:printf("Shaun of the Dead");     }  }

Choose all that apply:

(A) Anatomy of a Murder

(B) Planet of Apes

(C) The conversation

(D) Shaun of the Dead

(E) Compilation error

Explanation:

Consider on the expression:5||2|1=5|| (2|1)  //Bitwise or has higher precedence=5||3=1

Now, value of each case expression:3&2 = 2-~11 = -(-12) =126-3<<2 = 3 <<2 = 125>=5 = 1

case -~11 and case 6-3<<2 have same constant expression i.e. case 12In c duplicate case is not possible.

9.What will be output when you will execute following c code?#include<stdio.h>void main(){    switch(6){        case 6.0f:printf("Sangakkara");               break;        case 6.0: printf("Sehwag");               break;        case 6.0L:printf("Steyn");

Answ er

Page 47: Qust & ans inc

               break;        default:  printf("Smith");    }  }

Choose all that apply:

(A) Sangakkara

(B) Sehwag

(C) Steyn

(D) Smith

(E) Compilation error

Explanation:

Case expression must be integral constant expression. If it is not integer then it is automatically type casted into integer value.so. (int)6.0f = 6(int)6.0 = 6(int)6.0L = 6In c duplicate case is not possible.

10.What will be output when you will execute following c code?

#include<stdio.h>void main(){    switch(0X0){        case NULL:printf("Thierry Henry");             break;        case '\0':printf("Steven Gerrend");             break;        case 0: printf("Kaka");             break;        default: printf("Michael Ballack");    }     }

Answ er

Page 48: Qust & ans inc

Choose all that apply:

(A) Thierry Henry

(B) Steven Gerrend

(C) Kaka

(D) Michael Ballack

(E) Compilation error

Explanation:

Macro constant NULL has be defined as 0 in stdio.hASCII value of character constant '\0' is 0As we duplicate case is not possible in c.

11.What will be output when you will execute following c code?

#include<stdio.h>void main(){     switch(5/2*6+3.0){        case 3:printf("David Beckham");             break;        case 15:printf("Ronaldinho");             break;        case 0:printf("Lionel Messi");             break;        default:printf("Ronaldo");     }   }Choose all that apply:

(A) David Beckham

(B) Ronaldinho

(C) Lionel Messi

(D) Ronaldo

(E) Compilation error

Explanation:

Answ er

Answ er

Page 49: Qust & ans inc

Consider on the expression:5/2*6+3.0=2*6+3.0=12 + 3.0=15.0

In c switch expression must return an integer value. It cannot be float, double or long double

12.What will be output when you will execute following c code?

#include<stdio.h>void main(){    unsigned char c=280;    switch(c){        printf("Start\t");        case 280:printf("David Beckham\t");        case 24: printf("Ronaldinho\t");        default:  printf("Ronaldo\t");        printf("End");    }     }Choose all that apply:

(A) Start David Beckham Ronaldinho Ronaldo End

(B) Start David Beckham Ronaldinho Ronaldo

(C) Start Ronaldinho Ronaldo End

(D) Ronaldinho Ronaldo End

(E) Compilation error

Explanation:

280 is beyond the range of unsigned char. Its corresponding cyclic value is: 24In c switch case statement program control always move from the case which satisfy the switch condition

Answ er

Page 50: Qust & ans inc

and end with either break keyword, terminating} or any null character which will come first.

13.What will be output when you will execute following c code?

#include<stdio.h>#define TRUE 1void main(){     switch(TRUE){        printf("cquestionbank.blogspot.com");     }   }

Choose all that apply:

(A) cquestionbank.blogspot.com

(B) It will print nothing

(C) Runtime error

(D) Compilation error

(E) None of the above

Explanation:

In c it is possible a switch case statement without any case but it is meaning less.

14.What will be output when you will execute following c code?

#include<stdio.h>void main(){     static int i;     int j=1;     int arr[5]={1,2,3,4};     switch(arr[j]){        case 1: i++;break;        case 2: i+=2;j=3;continue;

Answ er

Page 51: Qust & ans inc

        case 3: i%=2;j=4;continue;        default: --i;     }     printf("%d",i);   }

Choose all that apply:

(A) 0

(B) 1

(C) 2

(D) Compilation error

(E) None of the above

Explanation:

We cannot use continue keyword in switch case. It is part loop.

15.What will be output when you will execute following c code?

#include<stdio.h>void main(){     static int i;     int j;     for(j=0;j<=5;j+=2)     switch(j){        case 1: i++;break;        case 2: i+=2;        case 4: i%=2;j=-1;continue;        default: --i;continue;     }     printf("%d",i);  }

Choose all that apply:

(A) 0

(B) 1

Answ er

Page 52: Qust & ans inc

(C) 2

(D) Compilation error

(E) None of the above

Explanation:

In first iteration of for loop:j = 0So, control will come to default,i = -1Due to continue keyword program control will move to beginning of for loopIn second iteration of for loop:j =2So, control will come to case 2,i+=2i = i+2 = -1 +2 =1Then come to case 4,i%=2i = i%2 = 1%2 = 1j= -1Due to continue keyword program control will move to beginning of for loopIn third iteration of for loop:j = -1 +2 =1So, control will come to case 1i = 2In the fourth iteration of for loop:j = 1+ 2 =3So, control will come to default,so i = 1In the fifth iteration of for loop:j = 3 + 2 =5So, control will come to default,so i = 0In the sixth iteration of for loop:j = 5 + 2 =7Since loop condition is false. So control will come out of the for loop. 

16.

Answ er

Page 53: Qust & ans inc

What will be output when you will execute following c code?

#include<stdio.h>void main(){     int x=3;     while(1){        switch(x){             case 5/2: x+=x+++x;             case 3%4: x+=x---x;continue;             case 3>=3: x+=!!!x;break;             case 5&&0:x+=~~~x;continue;             default: x+=-x--;        }        break;     }     printf("%d",x);   }

Choose all that apply:

(A) 3

(B) -1

(C) 5

(D) Compilation error

(E) None of the above

Explanation:

Think yourself.

17.What will be output when you will execute following c code?

#include<stdio.h>void main(){     char *str="cquestionbank.blogspot.com";     int a=2;     switch('A'){        case 97:

Answ er

Page 54: Qust & ans inc

             switch(97){                 default: str+=1;             }        case 65:             switch(97){                 case 'A':str+=2;                     case 'a':str+=4;         }        default:         for(;a;a--)             str+=8;     }     printf("%s",str);  }

Choose all that apply:

(A) cquestionbank.blogspot.com

(B) blogspot.com

(C) com

(D) Compilation error

(E) None of the above

Explanation:

ASCII value of the character constant 'A' is 65 and 'a' is 97Nesting of switch case is possible in c.

18.What will be output when you will execute following c code?

#include<stdio.h>void main(){     switch(2){        case 1L:printf("No");        case 2L:printf("%s","I");             goto Love;

Answ er

Page 55: Qust & ans inc

        case 3L:printf("Please");        case 4L:Love:printf("Hi");     }  }

Choose all that apply:

(A) I

(B) IPleaseHi

(C) IHi

(D) Compilation error

(E) None of the above

Explanation:

It is possible to write label of goto statement in the case of switch case statement.

19.What will be output when you will execute following c code?

#include<stdio.h>void main(){     int a=5;     a=a>=4;     switch(2){        case 0:int a=8;        case 1:int a=10;        case 2:++a;        case 3:printf("%d",a);     } }Choose all that apply:

(A) 8

(B) 11

(C) 10

(D) Compilation error

(E) None of the above

Answ er

Page 56: Qust & ans inc

Explanation:

We can not declare any variable in any case of switch case statement.

20.What will be output when you will execute following c code?

#include<stdio.h>void main(){     int a=3,b=2;     a=a==b==0;     switch(1){        a=a+10;     }     sizeof(a++);     printf("%d",a);  }

Choose all that apply:

(A) 10

(B) 11

(C) 12

(D) 1

(E) Compilation error

Explanation:

Consider on the expression:a=a==b==0;a=(a==b)==0; //Since associate is right to lefta =(3==2)==0a=0==0a=1

Answ er

Answ er

Page 57: Qust & ans inc

switch case will not affect the value of variable a.Also sizeof operator doesn't affect the value of the any variable

If else question

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)  cquestionbank

(B)  It will print nothing

(C)  Run time error

(D)   Compilation error

(E)  None of the above

D

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

Answ er

Page 58: Qust & ans inc

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)  Stars Wars Psycho

(B)  The Shawshank Redemption

(C)  Warning: Condition is always true

(D)  Warning: Condition is always false

(E)  Compilation error

E

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

Answ er

Page 59: Qust & ans inc

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");

Page 60: Qust & ans inc

    else if(True)         printf("Ronaldinho");    else        printf("Cristiano Ronaldo");}

Choose all that apply:

(A)  David Beckham

(B)  Ronaldinho 

(C)  Cristiano Ronaldo

(D)  Warning: Condition is always true

(E)  Warning: Unreachable code

A.D.E

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 .

Answ er

Page 61: Qust & ans inc

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)  M.S. Dhoni

(B)  A.B. de villiers

(C)

  

M.S DhoniM.E.K Hussey

A.B. de Villiers

(D)  Compilation error: More than one 

conditions are true

(E)  None of the above

A

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. 

Answ er

Page 62: Qust & ans inc

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)  R.T Ponting

(B)  C.H. Gayle

(C)  Warning: x and y are assigned a

value that is never used

(D)  Warning: Condition is always true

(E)  Compilation error

C,E

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

Consider following statement:++x=++yAs 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:

Answ er

Page 63: Qust & ans inc

0=0In 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 don’t perform any operator or perform operation only using unary operator on the variable the complier 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)  M. Muralilidaran

(B)  Harbhajan Singh

(C)  Warning: Condition is always false

(D)  Compilation error

(E)  None of the above

DAnsw er

Page 64: Qust & ans inc

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)  William Gates

(B)   Warren Buffet Carlos Slim Helu

(C)  Run time error

(D)  Compilation error

(E)  None of the above

E

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

Answ er

Page 65: Qust & ans inc

Consider the following expression:q / n * m

In this expression there are two operators. They are:/: Division operator*: Multiplication operatorPrecedence and associate of each operator is as follow:

Precedence Operator Associate

1 / , * 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 at the end compiler will also print: Carlos Slim Helu   So output of above code will be:William Gates Carlos Slim Helu

8 .

Page 66: Qust & ans inc

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)  Mukesh Ambani

(B)   Lakashmi Mittal

(C)  It will print nothing

(D)  Mukesh Ambani Lakashmi Mittal

(E)  Compilation error: if statement without body

A

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= 0In c language zero represents false. So if(0) is false so next statement which inside the body of first if statement will not execute.

Answ er

Page 67: Qust & ans inc

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)  printf("Nicolas Sarkozy\n");    if(0)     printf("Ben Bernanke\n");    if('W')   printf("Vladimir Putin\n");}

Choose all that apply:

 (A)

It will print nothing

(B) Barack ObamaHu JintaoNicolas Sarkozy

Vladimir Putin(C)Barack ObamaHu JintaoNicolas SarkozyBen Bernanke

Vladimir Putin(D)Hu JintaoNicolas Sarkozy

Vladimir Putin (E)

Compilation error

Page 68: Qust & ans inc

B

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)  Tom hanks

(B)  Compilation error: Misplaced else

Answ er

Page 69: Qust & ans inc

(C)  Compilation error: If without any body

(D)  Compilation error: Undefined symbol

(E)  Warning: Condition is always true

A,E

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

oxA: It is hexadecimal integer constant. 052: It octal integer constant.‘\xeb’: It is hexadecimal character constant.‘\012’: 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;}

Answ er

Page 70: Qust & ans inc

Choose all that apply:

(A)  It will print nothing

(B)  0

(C)  1

(D)  Compilation error: Misplaced else

(E)  Infinite loop

C

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.

Answ er

Page 71: Qust & ans inc

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)  5 10

(B)  6 11

(C)  6 10

(D)  5 11

(E)  John Terry

c

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:

Answ er

Page 72: Qust & ans inc

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)  It will print Memento at one time

(B)  It will print Memento at three times

(C)  It will print Memento at ten times

(D)  It will print Memento at infinite times

(E)  Compilation error: Unknown operator +++

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

Answ er

Page 73: Qust & ans inc

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)  The Godfather

(B)  1

(C)  0

(D)  Compilation error

(E)  None of the above

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

Answ er

Page 74: Qust & ans inc

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)  cquestionbank

(B)  It will print nothing

(C)  Warning: Condition is always true

(D)  Warning: Unreachable code

(E) Compilation error: if statement without any body

b&d

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.

Answ er

Page 75: Qust & ans inc

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)  5 10

(B)  6 11

(C)  6 10

(D)  Compilation error

(E)  John Terry

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

Answ er

Page 76: Qust & ans inc

There are two operators. There precedence and associate are:

Precedence Operator Associate

1 ++ Right to left

2 <  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 < 6Since 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);}

Page 77: Qust & ans inc

Choose all that apply:

(A)  1 2

(B)  x=1 y=2

(C)  0 2

(D)  x=0 y=1

(E)  0 1

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

Consider the following expression:--x && --y

In this expression && is Logical AND operator. Two important properties of this operator are:Property 1: (Expression1) && (Expression2) && 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.

Answ er

Page 78: Qust & ans inc

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)  The Lord of the Rings

(B)  American Beauty

(C) Compilation error: Cannot compare signed 

number with unsigned number

(D)  Compilation error: Undefined symbol -1u

(E)  Warning: Illegal operation

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

Read following tutorial:Data type tutorial

19.

Answ er

Page 79: Qust & ans inc

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)  +256+256+256+256

(B)  0000

(C)  +0+0+0+0

(D)  It will print +256 at infinite times

(E)  Compilation error

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:

Answ er

Page 80: Qust & ans inc

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.

Page 81: Qust & ans inc

Negation operator i.e. ! is always return either zero or one according to following rule:!0 = 1!(Non-zero number) = 0 So,!c = !0 =1As we know in c zero represents false and any non-zero number represents true. Sowhile(!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. 76if(*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.

Page 82: Qust & ans inc

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)  The Dalai Lama

(B)  Jim Rogers

(C)  Run time error

(D) Compilation error: Multiple parameters in 

if statement

(E)  None of the above

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

Consider the following expression:a-- , --a , aIn 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.

Answ er

Page 83: Qust & ans inc

After performing a-- : a will be 2After performing --a : a will be 0a=0

As we know in c zero represents false and any non-zero number represents true. Hence else part will execute.