Qust & ans inc
Upcoming SlideShare
Loading in...5
×
 

Qust & ans inc

on

  • 2,640 views

 

Statistics

Views

Total Views
2,640
Views on SlideShare
2,640
Embed Views
0

Actions

Likes
0
Downloads
117
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Qust & ans inc Qust & ans inc Document Transcript

  • “Questions & Answers In C”<br />(1)<br />What will be output of the following program?<br />#include<stdio.h> <br />int main(){<br />    float a=0.7;<br />    if(a<0.7){<br />         printf("C");<br />    }<br />    else{<br />         printf("C++");<br />         return 0;<br />    }<br />}<br />Explanation<br />Output: cExplanation: 0.7 is double constant (Default). Its binary value is written in 64 bit.<br />Binary value of 0.7 = (0.1011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011 )<br />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. <br />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<br />Hide<br />(2)<br />What will be output of the following program?<br />         <br />#include<stdio.h> <br />int main(){<br />    int i=5,j;<br />    j=++i+++i+++i;<br />    printf("%d %d",i,j);<br />    return 0;<br />}<br />Explanation<br />Output: 8 24Explanation:<br />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.<br />Compiler will treat this expression j = ++i+++i+++i; as<br />i = ++i + ++i + ++i; <br />Initial value of i = 5 due to three pre increment operator final value of i=8.<br />Now final value of i i.e. 8 will assigned to each variable as shown in the following figure:<br />So, j=8+8+8 <br />j=24 and<br />i=8<br />Hide<br />(3)<br />What will be output of the following program?<br />#include<stdio.h> <br />int main(){<br />    int i=1;<br />    i=2+2*i++;<br />    printf("%d",i);<br />    return 0;<br />}<br />Explanation<br />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, <br />i = 2 + 2 * 1 <br />i = 4 <br />Now i will be incremented by one so i = 4 + 1 = 5<br />Hide<br />(4)<br />What will be output of the following program?<br />#include<stdio.h> <br />int main(){<br />    int a=2,b=7,c=10;<br />    c=a==b;<br />    printf("%d",c);<br />    return 0;<br />}<br />Explanation<br />Output: 0Explanation: == is relational operator which returns only two values.<br />0: If a == b is false <br />1: If a == b is true <br />Since <br />a=2 <br />b=7 <br />So, a == b is false hence b=0<br />Hide<br />(5)<br />What will be output of the following program?<br />#include<stdio.h> <br />void main(){<br />    int x;<br />    x=10,20,30;<br />    printf("%d",x);<br />    return 0;<br />}<br />Explanation<br />Output: 10Explanation :<br />Precedence table:<br />OperatorPrecedenceAssociative =More than ,Right to left ,LeastLeft to right<br />Since assignment operator (=) has more precedence than comma operator .So = operator will be evaluated first than comma operator. In the following expression <br />x = 10, 20, 30 <br />First 10 will be assigned to x then comma operator will be evaluated.<br />Hide<br />(6)<br />What will be output of the following program?<br />#include<stdio.h> <br />int main(){<br />    int a=0,b=10;<br />    if(a=0){<br />         printf("true");<br />    }<br />    else{<br />         printf("false");<br />    }<br />    return 0;<br />}<br />Explanation<br />Output: falseExplanation:<br />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.<br />So, if(0) means condition is always false hence else part will execute.<br />Hide<br />(7)<br />What will be output of the following program?<br />#include<stdio.h> <br />int main(){<br />    int a;<br />    a=015 + 0x71 +5;<br />    printf("%d",a);<br />    return 0;<br />}<br />Explanation<br />Output: 131Explanation:015 is octal number its decimal equivalent is = 5 * 8 ^ 0 + 1 * 8 ^ 1 = 5 + 8 = 13 <br />0x71 is hexadecimal number (0x is symbol of hexadecimal) its decimal equivalent is = 1 * 16 ^ 0 + 7 * 16 ^ 1 = 1 + 112 = 113 <br />So, a = 13 + 113 + 5 = 131<br />Hide<br />(8)<br />What will be output of the following program?<br />#include<stdio.h> <br />int main(){<br />    printf("%d %d %d",sizeof(3.14),sizeof(3.14f),sizeof(3.14L));<br />    return 0;<br />}<br />Explanation<br />Output: 8 4 10<br />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.<br />Hide<br />(9)<br />What will be output of the following program?<br />#include<stdio.h> <br />int main(){<br />    int x=100,y=20,z=5;<br />    printf("%d %d %d");<br />    return 0;<br />}<br />Explanation<br />Output: 5 20 100<br />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.<br />Hide<br />(10)<br />What will be output of the following program?<br />#include<stdio.h>         <br />int main(){<br />    int a=2;<br />    a=a++ + ~++a;<br />    printf("%d",a);<br />    return 0;<br />}<br />Explanation<br />Output: -1Explanation: <br />Same theory as question (2) and (13).<br />Hide<br />(11)<br />What will be output of the following program?<br />#include<stdio.h> <br />int main(){<br />    int a;<br />    a=sizeof(!5.6);<br />    printf("%d",a);<br />    return 0;<br />}<br />Explanation<br />Output : 2Explanation: <br />! 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 <br />Since 0 is integer number and size of integer data type is two byte.<br />Hide<br />(12)<br />What will be output of the following program?<br />#include<stdio.h> <br />int main(){<br />    float a;<br />    (int)a= 45;<br />    printf("%d,a);<br />    return 0;<br />}<br />Explanation<br />Output: Compilation errorExplanation: <br />After performing any operation on operand it always return some constant value.<br />(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.<br />(int)a = 45; is equivalent to3456 = 45 ( Here 3456 in any garbage value of int(a)).<br />Hide<br />(13)<br />What will be output of the following program?<br />#include<stdio.h> <br />int main(){<br />     int i=5;<br />     int a=++i + ++i + ++i;<br />     printf("%d",a);<br />     return 0;<br />}<br />Explanation<br />Output: 21Explanation: <br />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:<br />(1) Declaration statement.(2) && or operator.(3) Comma (,) operator etc.<br />In the following expression: <br />int a=++i + ++i + ++i;<br />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. <br />So, a = 6 + 7 + 8;<br />http://cquestionbank.blogspot.com/2010/07/c-program-examples.html<br />Data type questions<br />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:<br />char :   1 byteint :    2 bytefloat :  4 bytedouble : 8 bytePlease adjust the answers according to size of data types in you compiler.1.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    printf("%dt",sizeof(6.5));<br />    printf("%dt",sizeof(90000));<br />    printf("%d",sizeof('A'));<br />}<br />Choose all that apply:<br />(A)4 2 1(B)8 2 1 (C)4 4 1(D)8 4 1(E)8 4 2<br />Explanation:<br />By default data type of numeric constants is:<br />6.5 :  double <br />90000: long int<br />‘A’: char <br />In C size of data type varies from compiler to compiler. <br />In TURBO C 3.0 (16 bit compilers) size of: <br />double is 8 byte<br />Long int is 4 byte<br />Character constant is 2 byte   (size of char data type is one byte)<br />In TURBO C 4.5 or Linux GCC compilers (32 bit compilers) size of:<br />double is 8 byte<br />long int is 8 byte<br />Character constant is 2 byte   <br />2.<br />Consider on following declaring of enum.<br />(i)        enum  cricket {Gambhir,Smith,Sehwag}c;<br />(ii)      enum  cricket {Gambhir,Smith,Sehwag};<br />(iii)    enum   {Gambhir,Smith=-5,Sehwag}c;<br />(iv)      enum  c {Gambhir,Smith,Sehwag};<br />Choose correct one:<br />(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<br />Explanation:<br />Syntax of enum data type is:<br />enum  [<tag_name>]{<br />    <enum_constanat_name> [=<integer_ value>],<br />    …<br />} [<var_name>,…]<br />Note: <br />[] : Represents optional .<br /><>: Represents any valid c identifier<br />3.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    signed x;<br />    unsigned y;<br />    x = 10 +- 10u + 10u +- 10;<br />    y = x;<br />    if(x==y)<br />         printf("%d %d",x,y);<br />    else if(x!=y)<br />         printf("%u  %u",x,y);<br />}<br />Choose all that apply:<br />(A)0 0(B)65536 -10 (C)0 65536 (D)65536 0(E)Compilation error<br />Explanation:<br />Consider on the expression:<br />x = 10 +- 10u + 10u +- 10;<br />10: It is signed integer constant.<br />10u: It is unsigned integer constant.<br />X: It is signed integer variable.<br />In any binary operation of dissimilar data type for example: a + b<br />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.<br />As we know operators enjoy higher precedence than binary operators. So our expression is:<br />x = 10 + (-10u) + 10u + (-10);<br />  = 10 + -10 + 10 + (-10); <br />  = 0<br />Note: Signed is higher data type than unsigned int.<br />So, Corresponding signed value of unsigned 10u is +10 4.<br />Which of the following is not modifier of data type in c?<br />(A)Extern(B)Interrupt(C)Huge(D)Register(E)All of these are modifiers of data type<br />Explanation:<br />To know more about these go to following link:<br />5.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    double num=5.2;<br />    int  var=5;<br />    printf("%dt",sizeof(!num));<br />    printf("%dt",sizeof(var=15/2));<br />    printf("%d",var);<br />}<br />Choose all that apply:<br />(A)4 2 7(B)4 4 5(C)2 2 5(D)2 4 7(E)8 2 7<br />Explanation:<br />sizeof(Expr)  operator always returns the an integer value which represents the size of the final value of the expression expr.<br />Consider on the following expression: <br />!num<br />=!5.2<br />=0<br />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.<br />Consider on the following expression: <br />var = 15/2<br />=> var = 7<br />=> 7<br />7 is int type integer constant.<br />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.<br />6.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    const int *p;<br />    int a=10;<br />    p=&a;<br />    printf("%d",*p);<br />}<br />Choose all that apply:<br />(A)0(B)10(C)Garbage value(D)Any memory address(E)Error: Cannot modify const object<br />Explanation:<br />In the following declaration <br />const int *p;<br />p can keep address of constant integer.<br />7.<br />Consider on following declaration:<br />(i)        short i=10;<br />(ii)      static i=10;<br />(iii)    unsigned i=10;<br />(iv)      const i=10;<br />Choose correct one:<br />(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 <br />Explanation:<br />Default data type of above all declaration is int.<br />8.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    int a= sizeof(signed) +sizeof(unsigned);<br />    int b=sizeof(const)+sizeof(volatile);<br />    printf("%d",a+++b);<br />}<br />Choose all that apply:<br />(A)10(B)9(C)8(D)Error: Cannot find size of modifiers(E)Error: Undefined operator +++<br />Explanation:<br />Default data type of signed, unsigned, const and volatile is int. In turbo c 3.0 size of int is two byte.<br />So, a = 4 and b =4<br />Now, a+++b<br />= a++ + b<br />= 4 + 4  //due to post increment operator.<br />=8<br />Note: In turbo c 4.5 and Linux gcc compiler size of int is 4 byte so your out will be 16<br />9.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    signed x,a;<br />    unsigned y,b;<br />    a=(signed)10u;<br />    b=(unsigned)-10;<br />    y = (signed)10u + (unsigned)-10;<br />    x = y;<br />    printf("%d  %ut",a,b);<br />    if(x==y)<br />         printf("%d %d",x,y);<br />    else if(x!=y)<br />         printf("%u  %u",x,y);<br />}<br />Choose all that apply:<br />(A)10 -10   0 0(B)10 -10   65516 -10 (C)10 -10   10 -10(D)10 65526      0 0(E)Compilation error<br />Explanation:<br />a=(signed)10u;<br />signed value of 10u is +10<br />so, a=10<br /> b=(unsigned)-10;<br />unsigned value of -10 is :<br />MAX_VALUE_OF_UNSIGNED_INT – 10 + 1<br />In turbo c 3.0 complier max value of unsigned int is 65535<br />So, b = 65526<br />y = (signed)10u + (unsigned)-10;<br />  = 10 + 65526 = 65536 = 0 (Since 65536 is beyond the range of unsigned int. zero is its corresponding cyclic vlaue)<br />X = y = 0<br />10.<br />Which of the following is integral data type?<br />(A)void(B)char(C)float(D)double(E)None of these<br />Explanation:<br />In c char is integral data type. It stores the ASCII value of any character constant.<br />11.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    volatile int a=11;<br />    printf("%d",a);<br />}<br />Choose all that apply:<br />(A)11(B)Garbage(C)-2(D)We cannot predict(E)Compilation error<br />Explanation:<br />We cannot predict the value of volatile variable because its value can be changed by any microprocessor interrupt.<br />12.<br />What is the range of signed int data type in that compiler in which size of int is two byte?<br />(A)-255 to 255(B)-32767 to 32767(C)-32768 to 32768(D)-32767 to 32768(E)-32768 to 32767<br />Explanation:<br />Note: Size of int is always equal to word length of micro preprocessor in which your compiler has based.<br />13.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />const enum Alpha{<br />      X,<br />      Y=5,<br />      Z<br />}p=10;<br />void main(){<br />    enum Alpha a,b;<br />    a= X;<br />    b= Z;<br />    printf("%d",a+b-p);  <br />}<br />Choose all that apply:<br />(A)-4(B)-5 (C)10(D)11(E)Error: Cannot modify constant object<br />Explanation:<br />Default value of enum constant X is zero and <br />Z = Y + 1 = 5 + 1 = 6<br />So, a + b – p<br />=0 + 6 -10 = -4<br />14.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    char a=250;<br />    int expr;<br />    expr= a+ !a + ~a + ++a;<br />    printf("%d",expr);<br />}<br />Choose all that apply:<br />(A)249(B)250(C)0(D)-6(E)Compilation error<br />Explanation:<br />char a = 250;<br />250 is beyond the range of signed char. Its corresponding cyclic value is: -6<br />So, a = -6<br />Consider on the expression:<br />expr= a+ !a + ~a + ++a;<br />Operator! , ~ and ++ have equal precedence. And it associative is right to left.<br />So, First ++ operator will perform the operation. So value a will -5<br />Now,<br />Expr = -5 + !-5 + ~-5 + -5<br />= -5 + !-5 + 4 - 5<br />= -5 + 0 + 4 -5<br />= -6<br />15.<br />Consider on order of modifiers in following declaration:<br />(i)char volatile register unsigned c;<br />(ii)volatile register unsigned char c;<br />(iii)register volatile unsigned char c;<br />(iv)unsigned char volatile register c;<br />(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<br />Explanation:<br />Order of modifier of variable in c has not any significant.<br />Choose correct one:<br />16.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    int a=-5;<br />    unsigned int b=-5u;<br />    if(a==b)<br />         printf("Avatar");<br />    else<br />         printf("Alien");<br />}<br />Choose all that apply:<br />(A)Avatar(B)Alien(C)Run time error(D)Error: Illegal assignment(E)Error: Don’t compare signed no. with unsigned no.<br />Explanation:<br />int a=-5;<br />Here variable a is by default signed int.<br />unsigned int b=-5u;<br />Constant -5u will convert into unsigned int. Its corresponding unsigned int value will be :<br />65536 – 5 + 1= 65532<br />So, b = 65532 <br />In any binary operation of dissimilar data type for example: a == b<br />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.<br />In c signed int is higher data type than unsigned int. So variable b will automatically type casted into signed int.<br />So corresponding signed value of 65532 is -5<br />Hence, a==b<br />17.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />extern enum cricket x;<br />void main(){<br />    printf("%d",x);  <br />}<br />const enum cricket{<br />    Taylor,<br />    Kallis=17,<br />    Chanderpaul<br />}x=Taylor|Kallis&Chanderpaul;<br />Choose all that apply:<br />(A)0(B)15(C)16(D)17(E)Compilation error<br />Explanation:<br />x=Taylor|Kallis&Chanderpaul<br />= 0 | 17 & 18<br />= 0 |(17 & 18) <br />//& operator enjoy higher precedence than |<br />=0 |16<br />=16<br />18.<br />Which of the following is not derived data type in c?<br />(A)Function(B)Pointer(C)Enumeration(D)Array(E)All are derived data type<br />Explanation:<br />Enum is primitive data type.<br />19.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />enum A{<br />    x,y=5,<br />    enum B{<br />         p=10,q<br />    }varp;<br />}varx;<br />void main(){<br />    printf("%d %d",x,varp.q);<br />}<br />Choose all that apply:<br />(A)0 11(B)5 10 (C)4 11(D)0 10(E)Compilation error<br />Explanation:<br />Nesting of enum constant is not possible in c.<br />20.<br />Consider on following declaration in c:<br />(i)short const register i=10;<br />(ii)static volatile const int i=10;<br />(iii)unsigned auto long register i=10;<br />(iv)signed extern float i=10.0;<br />Choose correct one:<br />(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 <br />Explanation:<br />Option (III) is in correct due to we cannot specify two storage class auto and register in the declaration of any variable.<br />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.<br />Looping questions<br />(1)<br />What will be output of following c code?<br />#include<stdio.h><br />extern int x;<br />int main(){<br />    do{<br />        do{<br />             printf("%o",x);<br />         }<br />         while(!-2);<br />    }<br />    while(0);<br />    return 0;<br />}<br />int x=8;<br />Explanation<br />Output: 10<br />Explanation:<br />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 =8<br />There 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. <br />Note: %o is used to print the number in octal format.<br />In inner do- while loop while condition is ! -2 = 0<br />In 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.<br />Hide<br />(2)<br />What will be output of following c code?<br />         <br />#include<stdio.h><br />int main(){<br />    int i=2,j=2;<br />    while(i+1?--i:j++)<br />         printf("%d",i);<br />    return 0;<br />}<br />Explanation<br />Output: 1<br />Explanation:<br />Consider the while loop condition: i + 1 ? -- i : ++j<br />In first iteration:<br />i + 1 = 3 (True)<br />So ternary operator will return -–i i.e. 1 <br />In c 1 means true so while condition is true. Hence printf statement will print 1 <br />In second iteration:<br />i+ 1 = 2 (True)<br />So ternary operator will return -–i i.e. 0 <br />In c zero means false so while condition is false. Hence program control will come out of the while loop.<br />Hide<br />(3)<br />What will be output of following c code?<br />#include<stdio.h><br />int main(){<br />    int x=011,i;<br />    for(i=0;i<x;i+=3){<br />         printf("Start ");<br />         continue;<br />         printf("End");<br />    }<br />    return 0;<br />}<br />Explanation<br />Output: Start Start Start <br />Explantion: <br />011 is octal number. Its equivalent decimal value is 9.<br />So, x = 9<br />First iteration:<br />i = 0<br />i < x i.e. 0 < 9  i.e. if loop condition is true.<br />Hence printf statement will print: Start<br />Due to continue keyword program control will come at the beginning of the for loop and value of variable i will be:<br />i += 3<br />i = i + 3 = 3<br />Second iteration:<br />i = 3<br />i < x i.e. 3 < 9 i.e. if loop condition is true.<br />Hence printf statement will print: Start<br />Due to continue keyword program control will come at the beginning of the for loop and value of variable i will be:<br />i += 3<br />i = i + 3 = 6<br />Third iteration:<br />i = 3<br />i < x i.e. 6 < 9 i.e. if loop condition is true.<br />Hence printf statement will print: Start<br />Due to continue keyword program control will come at the beginning of the for loop and value of variable i will be:<br />i += 3<br />i = i + 3 = 9<br />fourth iteration:<br />i = 6<br />i < x i.e. 9 < 9 i.e. if loop condition is false.<br />Hence program control will come out of the for loop.<br />Hide<br />(4)What will be output of following c code?<br />#include<stdio.h><br />int main(){<br />    int i,j;<br />    i=j=2,3;<br />    while(--i&&j++)<br />         printf("%d %d",i,j);<br />    return 0;<br />}<br />Explanation<br />Output: 13<br />Explanation:<br />Initial value of variable <br />i = 2<br />j = 2<br />Consider the while condition : --i && j++<br />In first iteration: <br />--i && j++<br />= 1 && 2 //In c any non-zero number represents true.<br />= 1 (True)<br />So while loop condition is true. Hence printf function will print value of i = 1 and j = 3 (Due to post increment operator)<br />In second iteration:<br />--i && j++<br />= 0 && 3  //In c zero represents false<br />= 0  //False<br />So while loop condition is false. Hence program control will come out of the for loop.<br />Hide<br />(5)What will be output of following c code?<br />#include<stdio.h><br />int main(){<br />    static int i;<br />    for(++i;++i;++i) {<br />         printf("%d ",i);<br />         if(i==4) break;<br />    }<br />    return 0;<br />}<br />Explanation<br />Output: 24<br />Explanation:<br />Default value of static int variable in c is zero. So, initial value of variable i = 0<br />First iteration:<br />For loop starts value: ++i i.e. i = 0 + 1 = 1 <br />For loop condition: ++i i.e. i = 1 + 1 = 2 i.e. loop condition is true. Hence printf statement will print 2<br />Loop incrimination: ++I i.e. i = 2 + 1 =3<br />Second iteration:<br />For loop condition: ++i i.e. i = 3 + 1 = 4 i.e. loop condition is true. Hence printf statement will print 4.<br />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.<br />Hide<br />(6)What will be output of following c code?<br />#include<stdio.h><br />int main(){<br />    int i=1;<br />    for(i=0;i=-1;i=1) {<br />         printf("%d ",i);<br />         if(i!=1) break;<br />    }<br />    return 0;<br />}<br />Explanation<br />Output: -1<br />Explanation:<br />Initial value of variable i is 1.<br />First iteration:<br />For loop initial value: i = 0<br />For 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 <br />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.<br />Hide<br />(7)What will be output of following c code?<br />#include<stdio.h><br />int main(){<br />    for(;;) {<br />         printf("%d ",10);<br />    }<br />    return 0;<br />}<br />Explanation<br />Output: Infinite loop<br />Explanation:<br />In for loop each part is optional.<br />Hide<br />(8)What will be output of following c code?<br />         <br />#include<stdio.h><br />int r();<br />int main(){<br />    for(r();r();r()) {<br />         printf("%d ",r());<br />    }<br />    return 0;<br />}<br />int r(){<br />    int static num=7;<br />    return num--;<br />}<br />Explanation<br />Output: 5 2<br />Explanation:<br />First iteration:<br />Loop initial value: r() = 7<br />Loop condition: r() = 6<br />Since condition is true so printf function will print r() i.e. 5<br />Loop incrimination: r() = 4<br />Second iteration:<br />Loop condition: r() = 3<br />Since condition is true so printf function will print r() i.e. 2<br />Loop incrimination: r() = 1<br />Third iteration:<br />Loop condition: r() = 0 <br />Since condition is false so program control will come out of the for loop.<br />Hide<br />(9)What will be output of following c code?<br />         <br />#include<stdio.h><br />#define p(a,b) a##b<br />#define call(x) #x<br />int main(){<br />    do{<br />         int i=15,j=3;<br />         printf("%d",p(i-+,+j));<br />    }<br />    while(*(call(625)+3));<br />    return 0;<br />}<br />Explanation<br />Output: 11<br />Explanation:<br />First iteration:<br />p(i-+,+j) <br />=i-++j   // a##b<br />=i - ++j<br />=15 – 4<br />= 11<br />While condition is : *(call(625)+ 3)<br />= *(“625” + 3) <br />Note: # preprocessor operator convert the operand into the string.<br />=*(It will return the memory address of character ‘0’)<br />= ‘0’<br />= 0  //ASCII value of character null character<br />Since loop condition is false so program control will come out of the for loop.<br />Hide<br />(10)<br />#include<stdio.h><br />int main(){<br />    int i;<br />    for(i=0;i<=5;i++);<br />    printf("%d",i)<br />    return 0;<br />}<br />Explanation<br />Output: 6<br />Explanation:<br />It possible for loop without any body.<br />Hide<br />(11)What will be output of following c code?<br />#include<stdio.h><br />int i=40;<br />extern int i;<br />int main(){<br />    do{<br />         printf("%d",i++);<br />    }<br />    while(5,4,3,2,1,0);<br />    return 0;<br />}<br />Explanation<br />Output: 40<br />Explanation: <br />Initial value of variable i is 40<br />First iteration:<br />printf function will print i++ i.e. 40<br />do - while condition is : (5,4,3,2,1,0)<br />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.<br />Hide<br />(12)What will be output of following c code?<br />#include<stdio.h><br />char _x_(int,...);<br />int main(){<br />    char (*p)(int,...)=&_x_;<br />    for(;(*p)(0,1,2,3,4); )<br />         printf("%d",!+2);<br />    return 0;<br />}<br />char _x_(int a,...){<br />    static i=-1;<br />    return i+++a;<br />}<br />Explanation<br />Output: 0<br />Explanation:<br />In c three continuous dot represents variable number of arguments. <br />p is the pointer to the function _x_<br />First iteration of for loop:<br />Initial value: Nothing // In c it is optional<br />Loop condition: (*p)(0,1,2,3,4)<br />= *(&_x_)(0,1,2,3,4)  // p = &_x_<br />= _x_(0,1,2,3,4)    //* and & always cancel to each other<br />= return i+++a<br />= return i+ ++a<br />= return -1 + 1<br />= 0<br />Since condition is false. But printf function will print 0. It is bug of c language.<br />Hide<br />(13)What will be output of following c code?<br />#include<stdio.h><br />int main(){<br />    int i;<br />    for(i=10;i<=15;i++){<br />         while(i){<br />             do{<br />                 printf("%d ",1);<br />                 if(i>>1)<br />                      continue;<br />             }while(0);<br />             break;<br />         }<br />    }<br />    return 0;<br />}<br />Explanation<br />Output: 1 1 1 1 1 1<br />For loop will execute six times. <br />Note: continue keyword in do-while loop bring the program its while condition (while(0)) which is always false.<br />Hide<br />(14)How many times this loop will execute?<br />#include<stdio.h><br />int main(){<br />    char c=125;<br />    do<br />         printf("%d ",c);<br />    while(c++);<br />    return 0;<br />}<br />Explanation<br />Output: Finite times<br />Explanation:<br />If we will increment the char variable c it will increment as:<br />126,127,-128,-127,126 . . . .   , 3, 2, 1, 0<br />When variable c = 0 then loop will terminate.    <br />Hide<br />(15)What will be output of following c code?<br />         <br />#include<stdio.h><br />int main(){<br />    int x=123;<br />    int i={<br />         printf("c" "++")<br />    };<br />    for(x=0;x<=i;x++){<br />         printf("%x ",x);<br />    }<br />    return 0;<br />}<br />Explanation<br />Output: c++0 1 2 3<br />Explanation:<br />First printf function will print: c++ and return 3 to variable i.<br />For loop will execute three time and printf function will print 0, 1, 2 respectively.<br />Switch case questions<br />1.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />     int check=2;<br />     switch(check){<br />        case 1: printf("D.W.Steyn");<br />        case 2: printf(" M.G.Johnson");<br />        case 3: printf(" Mohammad Asif");<br />        default: printf(" M.Muralidaran");<br />     } <br />}<br />Choose all that apply:<br />(A)M.G.Johnson(B)M.Muralidaran(C)M.G.Johnson Mohammad Asif M.Muralidaran(D)Compilation error(E)None of the above<br />Explanation:<br />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.<br />2.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />     int movie=1;<br />     switch(movie<<2+movie){<br />        default:printf("3 Idiots");<br />        case 4: printf(" Ghajini");<br />        case 5: printf(" Krrish");<br />        case 8: printf(" Race");<br />     }  <br />}<br />Choose all that apply:<br />(A)3 Idiots Ghajini Krrish Race(B)Race(C)Krrish(D)Ghajini Krrish Race(E)Compilation error<br />Explanation:<br />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.<br />3.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />#define L 10<br />void main(){<br />     auto money=10;<br />     switch(money,money*2){<br />        case L:  printf("Willian");<br />                  break;<br />        case L*2:printf("Warren");<br />                  break;<br />        case L*3:printf("Carlos");<br />                  break;<br />        default: printf("Lawrence");<br />        case L*4:printf("Inqvar");<br />                  break;<br />     }   <br />}<br />Choose all that apply:<br />(A)Willian(B)Warren(C)Lawrence Inqvar(D)Compilation error: Misplaced default(E)None of the above<br />Explanation:<br />In c comma is also operator which enjoy least precedence. So if<br />x = (a , b);<br />Then x = b<br />Note: Case expression can be macro constant.<br />4.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />     int const X=0;<br />     switch(5/4/3){<br />        case X:  printf("Clinton");<br />                  break;<br />        case X+1:printf("Gandhi");<br />                  break;<br />        case X+2:printf("Gates");<br />                  break;<br />        default: printf("Brown");<br />     }  <br />}<br />Choose all that apply:<br />(A)Clinton(B)Gandhi(C)Gates(D)Brown(E)Compilation error<br />Explanation:<br />Case expression cannot be constant variable.<br />5.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />enum actor{<br />    SeanPenn=5,<br />    AlPacino=-2,<br />    GaryOldman,<br />    EdNorton<br />};<br />void main(){<br />     enum actor a=0;<br />     switch(a){<br />        case SeanPenn:  printf("Kevin Spacey");<br />                         break;<br />        case AlPacino:  printf("Paul Giamatti");<br />                         break;<br />        case GaryOldman:printf("Donald Shuterland");<br />                         break;<br />        case EdNorton:  printf("Johnny Depp");<br />     }   <br />}<br />Choose all that apply:<br />(A)Kevin Spacey(B)Paul Giamatti(C)Donald Shuterland(D)Johnny Depp(E)Compilation error<br />Explanation:<br />Default value of enum constant <br />GaryOldman = -2 +1 = -1<br />And default value of enum constant <br />EdNorton = -1 + 1 = 0<br />Note: Case expression can be enum constant.<br />6.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />     switch(*(1+"AB" "CD"+1)){<br />        case 'A':printf("Pulp Fiction");<br />                  break;<br />        case 'B':printf("12 Angry Man");<br />                  break;<br />        case 'C':printf("Casabance");<br />                  break;<br />        case 'D':printf("Blood Diamond");<br />     }<br />     <br />}<br />Choose all that apply:<br />(A)Pulp Fiction(B)12 Angry Man(C)Casabance(D)Blood Diamond(E)Compilation error<br />Explanation:<br />Consider on the expression:<br />*(1+"AB" "CD"+1)<br />= *(2+"AB" "CD")<br />= *(2+"ABCD")<br />=*("CD")<br />='C'<br />Note: Case expression can be character constant.<br />7.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    char *str="ONE"<br />    str++;<br />    switch(str){<br />        case "ONE":printf("Brazil");<br />                break;<br />        case "NE": printf("Toy story");<br />                break;<br />        case "N":  printf("His Girl Friday");<br />                break;<br />        case "E":  printf("Casino Royale");<br />     }   <br />}<br />Choose all that apply:<br />(A)Brazil(B)Toy story(C)His Girl Friday(D)Casino Royale(E)Compilation error<br />Explanation:<br />Case expression cannot be string constant.<br />8.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    switch(5||2|1){<br />        case 3&2:printf("Anatomy of a Murder");<br />              break;<br />        case -~11:printf("Planet of Apes");<br />               break;<br />        case 6-3<<2:printf("The conversation");<br />               break;<br />    case 5>=5:printf("Shaun of the Dead");<br />     }  <br />}<br />Choose all that apply:<br />(A)Anatomy of a Murder(B)Planet of Apes(C)The conversation(D)Shaun of the Dead(E)Compilation error<br />Explanation:<br />Consider on the expression:<br />5||2|1<br />=5|| (2|1)  //Bitwise or has higher precedence<br />=5||3<br />=1<br />Now, value of each case expression:<br />3&2 = 2<br />-~11 = -(-12) =12<br />6-3<<2 = 3 <<2 = 12<br />5>=5 = 1<br />case -~11 and case 6-3<<2 have same constant expression i.e. case 12<br />In c duplicate case is not possible.<br />9.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    switch(6){<br />        case 6.0f:printf("Sangakkara");<br />               break;<br />        case 6.0: printf("Sehwag");<br />               break;<br />        case 6.0L:printf("Steyn");<br />               break;<br />        default:  printf("Smith");<br />    }  <br />}<br />Choose all that apply:<br />(A)Sangakkara(B)Sehwag(C)Steyn(D)Smith(E)Compilation error<br />Explanation:<br />Case expression must be integral constant expression. If it is not integer then it is automatically type casted into integer value.<br />so. (int)6.0f = 6<br />(int)6.0 = 6<br />(int)6.0L = 6<br />In c duplicate case is not possible.<br />10.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    switch(0X0){<br />        case NULL:printf("Thierry Henry");<br />             break;<br />        case '0':printf("Steven Gerrend");<br />             break;<br />        case 0: printf("Kaka");<br />             break;<br />        default: printf("Michael Ballack");<br />    }<br />     <br />}<br />Choose all that apply:<br />(A)Thierry Henry(B)Steven Gerrend(C)Kaka(D)Michael Ballack(E)Compilation error<br />Explanation:<br />Macro constant NULL has be defined as 0 in stdio.h<br />ASCII value of character constant '0' is 0<br />As we duplicate case is not possible in c.<br />11.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />     switch(5/2*6+3.0){<br />        case 3:printf("David Beckham");<br />             break;<br />        case 15:printf("Ronaldinho");<br />             break;<br />        case 0:printf("Lionel Messi");<br />             break;<br />        default:printf("Ronaldo");<br />     }   <br />}<br />Choose all that apply:<br />(A)David Beckham(B)Ronaldinho(C)Lionel Messi(D)Ronaldo(E)Compilation error<br />Explanation:<br />Consider on the expression:<br />5/2*6+3.0<br />=2*6+3.0<br />=12 + 3.0<br />=15.0<br />In c switch expression must return an integer value. It cannot be float, double or long double<br />12.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    unsigned char c=280;<br />    switch(c){<br />        printf("Startt");<br />        case 280:printf("David Beckhamt");<br />        case 24: printf("Ronaldinhot");<br />        default:  printf("Ronaldot");<br />        printf("End");<br />    }<br />     <br />}<br />Choose all that apply:<br />(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<br />Explanation:<br />280 is beyond the range of unsigned char. Its corresponding cyclic value is: 24<br />In c switch case statement program control always move from the case which satisfy the switch condition and end with either break keyword, terminating} or any null character which will come first.<br />13.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />#define TRUE 1<br />void main(){ <br />    switch(TRUE){<br />        printf("cquestionbank.blogspot.com");<br />     }   <br />}<br />Choose all that apply:<br />(A)cquestionbank.blogspot.com(B)It will print nothing(C)Runtime error(D)Compilation error(E)None of the above<br />Explanation:<br />In c it is possible a switch case statement without any case but it is meaning less.<br />14.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />     static int i;<br />     int j=1;<br />     int arr[5]={1,2,3,4};<br />     switch(arr[j]){<br />        case 1: i++;break;<br />        case 2: i+=2;j=3;continue;<br />        case 3: i%=2;j=4;continue;<br />        default: --i;<br />     }<br />     printf("%d",i);   <br />}<br />Choose all that apply:<br />(A)0(B)1(C)2(D)Compilation error(E)None of the above<br />Explanation:<br />We cannot use continue keyword in switch case. It is part loop.<br />15.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />     static int i;<br />     int j;<br />     for(j=0;j<=5;j+=2)<br />     switch(j){<br />        case 1: i++;break;<br />        case 2: i+=2;<br />        case 4: i%=2;j=-1;continue;<br />        default: --i;continue;<br />     }<br />     printf("%d",i);  <br />}<br />Choose all that apply:<br />(A)0(B)1(C)2(D)Compilation error(E)None of the above<br />Explanation:<br />In first iteration of for loop:<br />j = 0<br />So, control will come to default,<br />i = -1<br />Due to continue keyword program control will move to beginning of for loop<br />In second iteration of for loop:<br />j =2<br />So, control will come to case 2,<br />i+=2<br />i = i+2 = -1 +2 =1<br />Then come to case 4,<br />i%=2<br />i = i%2 = 1%2 = 1<br />j= -1<br />Due to continue keyword program control will move to beginning of for loop<br />In third iteration of for loop:<br />j = -1 +2 =1<br />So, control will come to case 1<br />i = 2<br />In the fourth iteration of for loop:<br />j = 1+ 2 =3<br />So, control will come to default,<br />so i = 1<br />In the fifth iteration of for loop:<br />j = 3 + 2 =5<br />So, control will come to default,<br />so i = 0<br />In the sixth iteration of for loop:<br />j = 5 + 2 =7<br />Since loop condition is false. So control will come out of the for loop.  16.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />     int x=3;<br />     while(1){<br />        switch(x){<br />             case 5/2: x+=x+++x;<br />             case 3%4: x+=x---x;continue;<br />             case 3>=3: x+=!!!x;break;<br />             case 5&&0:x+=~~~x;continue;<br />             default: x+=-x--;<br />        }<br />        break;<br />     }<br />     printf("%d",x);   <br />}<br />Choose all that apply:<br />(A)3(B)-1(C)5(D)Compilation error(E)None of the above<br />Explanation:<br />Think yourself.<br />17.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />     char *str="cquestionbank.blogspot.com";<br />     int a=2;<br />     switch('A'){<br />        case 97:<br />             switch(97){<br />                 default: str+=1;<br />             }<br />        case 65:<br />             switch(97){<br />                 case 'A':str+=2;<br />                     case 'a':str+=4;<br />         }<br />        default:<br />         for(;a;a--)<br />             str+=8;<br />     }<br />     printf("%s",str);  <br />}<br />Choose all that apply:<br />(A)cquestionbank.blogspot.com(B)blogspot.com(C)com(D)Compilation error(E)None of the above<br />Explanation:<br />ASCII value of the character constant 'A' is 65 and 'a' is 97<br />Nesting of switch case is possible in c.<br />18.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />     switch(2){<br />        case 1L:printf("No");<br />        case 2L:printf("%s","I");<br />             goto Love;<br />        case 3L:printf("Please");<br />        case 4L:Love:printf("Hi");<br />     }  <br />}<br />Choose all that apply:<br />(A)I(B)IPleaseHi(C)IHi(D)Compilation error(E)None of the above<br />Explanation:<br />It is possible to write label of goto statement in the case of switch case statement.<br />19.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />     int a=5;<br />     a=a>=4;<br />     switch(2){<br />        case 0:int a=8;<br />        case 1:int a=10;<br />        case 2:++a;<br />        case 3:printf("%d",a);<br />     } <br />}<br />Choose all that apply:<br />(A)8(B)11(C)10(D)Compilation error(E)None of the above<br />Explanation:<br />We can not declare any variable in any case of switch case statement.<br />20.<br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />     int a=3,b=2;<br />     a=a==b==0;<br />     switch(1){<br />        a=a+10;<br />     }<br />     sizeof(a++);<br />     printf("%d",a);  <br />}<br />Choose all that apply:<br />(A)10(B)11(C)12(D)1(E)Compilation error<br />Explanation:<br />Consider on the expression:<br />a=a==b==0;<br />a=(a==b)==0; //Since associate is right to left<br />a =(3==2)==0<br />a=0==0<br />a=1<br />switch case will not affect the value of variable a.<br />Also sizeof operator doesn't affect the value of the any variable<br />If else question<br />1. <br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    int a=5,b=10,c=1;<br />    if(a&&b>c){<br />         printf("cquestionbank");<br />    }<br />    else{<br />         break;<br />    }<br />}<br />Choose all that apply:<br />(A) cquestionbank(B) It will print nothing(C) Run time error(D)  Compilation error(E) None of the above<br /> D<br />Explanation:<br />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. <br />Hence when you will compile above code compiler will show an error message: Misplaced break.<br />2. <br />What will be output when you will execute following c code?<br />#define PRINT printf("Star Wars");printf(" Psycho");<br />#include<stdio.h><br />void main(){<br />    int x=1;<br />    if(x--)<br />         PRINT<br />    else<br />         printf("The Shawshank Redemption");<br />}<br />Choose all that apply:<br />(A) Stars Wars Psycho(B) The Shawshank Redemption(C) Warning: Condition is always true(D) Warning: Condition is always false(E) Compilation error<br /> E<br />Explanation:<br />PRINT is macro constant. Macro PRINT will be replaced by its defined statement just before the actual compilation starts.  Above code is converted as:<br />void main(){<br />    int x=1;<br />    if(x--)<br />         printf("Star Wars");<br />printf(" Psycho");<br />    else<br />         printf("The Shawshank Redemption");<br />}<br />    <br />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:<br />(i)<br />if(x--)<br />    printf("Star Wars");<br />It is if statement without any else. It is ok.<br />(ii)<br />printf(" Psycho");<br />It is a function call. It is also ok<br />(iii)<br />else<br />         printf("The Shawshank Redemption");<br />You cannot write else clause without any if clause. It is cause of compilation error. Hence compiler will show an error message: Misplaced else <br />3. <br />What will be output when you will execute following c code?<br />#define True 5==5<br />#include<stdio.h><br />void main(){<br />    if(.001-0.1f)<br />         printf("David Beckham");<br />    else if(True)<br />         printf("Ronaldinho");<br />    else<br />        printf("Cristiano Ronaldo");<br />}<br />Choose all that apply:<br />(A) David Beckham(B) Ronaldinho (C) Cristiano Ronaldo(D) Warning: Condition is always true(E) Warning: Unreachable code<br /> A.D.E<br />Explanation:<br />As we know in c zero represents false and any non-zero number represents true. So in the above code:<br />(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.<br />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<br />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:<br />Unreachable code<br />4. <br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    int a=100;<br />    if(a>10)<br />         printf("M.S. Dhoni");<br />    else if(a>20)<br />         printf("M.E.K Hussey");<br />    else if(a>30)<br />           printf("A.B. de villiers");<br />}<br />Choose all that apply:<br />(A) M.S. Dhoni(B) A.B. de villiers(C)   M.S DhoniM.E.K HusseyA.B. de Villiers(D) Compilation error: More than one conditions are true(E) None of the above<br /> A<br />Explanation:<br />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. <br />5. <br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    int x=-1,y=-1;<br />    if(++x=++y)<br />         printf("R.T. Ponting");<br />    else<br />         printf("C.H. Gayle");<br />}<br />Choose all that apply:<br />(A) R.T Ponting(B) C.H. Gayle(C) Warning: x and y are assigned avalue that is never used(D) Warning: Condition is always true(E) Compilation error<br /> C,E<br />Explanation:<br />Consider following statement:<br />++x=++y<br />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:<br />0=0<br />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<br />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  <br />6. <br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    if(sizeof(void))<br />         printf("M. Muralilidaran");<br />    else<br />         printf("Harbhajan Singh");<br />}<br />Choose all that apply:<br />(A) M. Muralilidaran(B) Harbhajan Singh(C) Warning: Condition is always false(D) Compilation error(E) None of the above<br /> D<br />Explanation:<br />It illegal to find size of void data type using sizeof operator. Because size of void data type is meaning less.<br />7. <br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    int m=5,n=10,q=20;<br />    if(q/n*m)<br />         printf("William Gates");<br />    else<br />         printf(" Warren Buffet");<br />         printf(" Carlos Slim Helu");<br />}<br />Choose all that apply:<br />(A) William Gates(B)  Warren Buffet Carlos Slim Helu(C) Run time error(D) Compilation error(E) None of the above<br /> E<br />Explanation:<br />Consider the following expression:<br />q / n * m<br />In this expression there are two operators. They are:<br />/: Division operator<br />*: Multiplication operator<br />Precedence and associate of each operator is as follow:<br />PrecedenceOperatorAssociate1/ , *Left to right<br />  <br />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.<br />= q / n * m<br />= 20 / 10 * 5<br />= 2 * 5<br />=10<br />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<br />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. <br />printf(" Carlos Slim Helu");<br />is not part of if-else statement. So at the end compiler will also print: Carlos Slim Helu   <br />So output of above code will be:<br />William Gates Carlos Slim Helu<br />8. <br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    if(!printf("Mukesh Ambani"))<br />    if(printf(" Lakashmi Mittal"));<br />}<br />Choose all that apply:<br />(A) Mukesh Ambani(B)  Lakashmi Mittal(C) It will print nothing(D) Mukesh Ambani Lakashmi Mittal(E) Compilation error: if statement without body<br /> A<br />Explanation:<br />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,<br />!printf("Mukesh Ambani")<br />= !13<br />= 0<br />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.<br />9. <br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    if("ABC") printf("Barack Obaman");<br />    if(-1)    printf("Hu Jintaon");<br />    if(.92L)  printf("Nicolas Sarkozyn");<br />    if(0)     printf("Ben Bernanken");<br />    if('W')   printf("Vladimir Putinn");<br />}<br />Choose all that apply:<br /> (A)It will print nothing(B) Barack ObamaHu JintaoNicolas SarkozyVladimir Putin(C)Barack ObamaHu JintaoNicolas SarkozyBen BernankeVladimir Putin(D)Hu JintaoNicolas SarkozyVladimir Putin (E)Compilation error<br /> B<br />Explanation:<br />“ABC”: It is string constant and it will always return a non-zero memory address.<br />0.92L: It is long double constant.<br />‘W’: It is character constant and its ASCII value is  <br />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.<br />10. <br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    if(0xA)<br />         if(052)<br />             if('xeb')<br />                 if('012')<br />                      printf("Tom hanks");<br />                 else;<br />             else;<br />         else;<br />    else;<br />}<br />Choose all that apply:<br />(A) Tom hanks(B) Compilation error: Misplaced else(C) Compilation error: If without any body(D) Compilation error: Undefined symbol(E) Warning: Condition is always true<br /> A,E<br />Explanation:<br />oxA: It is hexadecimal integer constant. <br />052: It octal integer constant.<br />‘xeb’: It is hexadecimal character constant.<br />‘012’: It is octal character constant.<br />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.<br />In c it is possible to write else clause without any body.<br />11. <br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    int a=10;<br />    if(printf("%d",a>=10)-10)<br />         for(;;)<br />             break;<br />    else;<br />}<br />Choose all that apply:<br />(A) It will print nothing(B) 0(C) 1(D) Compilation error: Misplaced else(E) Infinite loop<br /> C<br />Explanation:<br />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.<br />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 <br />= 1 - 10 <br />= -9<br />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.<br />12. <br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    int a=5,b=10;<br />    if(++a||++b)<br />         printf("%d  %d",a,b);<br />    else<br />         printf("John Terry");<br />}<br />Choose all that apply:<br />(A) 5 10(B) 6 11(C) 6 10(D) 5 11(E) John Terry<br /> c<br />Explanation:<br />Consider the following expression:<br />++a || ++b<br />In this expression || is Logical OR operator. Two important properties of this operator are:<br />Property 1: <br />(Expression1) || (Expression2) <br />|| operator returns 0 if and only if both expressions return a zero otherwise it || operator returns 1.<br />Property 2: <br />To optimize the execution time there is rule, Expression2 will only evaluate if and only if Expression1 return zero.<br />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.<br />13. <br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    static int i;<br />    for(;;)<br />    if(i+++"The Matrix")<br />          printf("Memento");<br />    else<br />         break;<br />}<br />Choose all that apply:<br />(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 +++<br />Explanation:<br />Think yourself<br />14. <br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    int x=1;<br />    if(x--)<br />         printf("The Godfather");<br />         --x;<br />    else<br />         printf("%d",x);<br />}<br />Choose all that apply:<br />(A) The Godfather(B) 1(C) 0(D) Compilation error(E) None of the above<br />Explanation:<br />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<br />15. <br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    if('0');<br />    else if(NULL)<br />         printf("cquestionbank");<br />    else;<br />}<br />Choose all that apply:<br />(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<br /> b&d<br /> <br />Explanation:<br />‘0’ is null character constant. Its ASCII value is zero. if(0) means false so program control will check it else if clause. <br />NULL is macro constant which has been defined in stdio.h which also returns zero.     <br />16. <br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    int a=5,b=10;<br />    clrscr();<br />    if(a<++a||b<++b)<br />         printf("%d  %d",a,b);<br />    else<br />         printf("John Terry");<br />}<br />Choose all that apply:<br />(A) 5 10(B) 6 11(C) 6 10(D) Compilation error(E) John Terry<br />Explanation:<br />Consider the following expression:<br />a<++a||b<++b<br />In the above expression || is logical OR operator. It divides any expression in the sub expressions. In this way we have two sub expressions:<br />(1)   a<++a<br />(2)   b<++b<br />In the expression: a< ++a<br />There are two operators. There precedence and associate are:<br />PrecedenceOperatorAssociate1++Right to left2< Left to right<br />From table it is clear first ++ operator will perform the operation then < operator.<br />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 <br />Initial value of variable a is 5. <br />Step 1: Increment the value of variable a in whole expression. Final value of a is 6.<br />Step 2: Now start assigning value to all a in the expression. After assigning 6 expression will be:<br />6 < 6<br />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. <br />17. <br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    int x=1,y=2;<br />    if(--x && --y)<br />         printf("x=%d  y=%d",x,y);<br />    else<br />         printf("%d %d",x,y);<br />}<br />Choose all that apply:<br />(A) 1 2(B) x=1 y=2(C) 0 2(D) x=0 y=1(E) 0 1<br />Explanation:<br />Consider the following expression:<br />--x && --y<br />In this expression && is Logical AND operator. Two important properties of this operator are:<br />Property 1: <br />(Expression1) && (Expression2) <br />&& operator returns 1 if and only if both expressions return a non-zero value other wise it && operator returns 0.<br />Property 2: <br />To optimize the execution time there is rule, Expression2 will only evaluate if and only if Expression1 return a non-zero value.<br />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.<br />18. <br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    signed int a=-1;<br />    unsigned int b=-1u;<br />    if(a==b)<br />         printf("The Lord of the Rings");<br />    else<br />         printf("American Beauty");<br />}<br />Choose all that apply:<br />(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<br />Explanation:<br />Read following tutorial:<br />Data type tutorial<br />19. <br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    char c=256;<br />    char *ptr="Leon";<br />    if(c==0)                                 <br />         while(!c)<br />             if(*ptr++)<br />                 printf("%+u",c);<br />             else<br />                 break;<br />}<br />Choose all that apply:<br />(A) +256+256+256+256(B) 0000(C) +0+0+0+0(D) It will print +256 at infinite times(E) Compilation error<br />Explanation:<br />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:<br />Since 256 is positive number move from zero in clock wise direction. You will get final value of c is zero.<br />if(c==0)<br />It is true since value of c is zero.<br />Negation operator i.e. ! is always return either zero or one according to following rule:<br />!0 = 1<br />!(Non-zero number) = 0<br /> So,<br />!c = !0 =1<br />As we know in c zero represents false and any non-zero number represents true. So<br />while(!c) i.e. while(1) is always true.<br />In the above program prt is character pointer. It is pointing to first character of string “Leon” according to following diagram:<br />In the above figure value in circle represents ASCII value of corresponding character.<br />Initially *ptr means ‘L’. So *ptr will return ASCII value of character constant ‘L’ i.e. 76<br />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. <br />20. <br />What will be output when you will execute following c code?<br />#include<stdio.h><br />void main(){<br />    int a=2;<br />    if(a--,--a,a)<br />         printf("The Dalai Lama");<br />    else<br />         printf("Jim Rogers");<br />}<br />Choose all that apply:<br />(A) The Dalai Lama(B) Jim Rogers(C) Run time error(D)Compilation error: Multiple parameters in if statement(E) None of the above<br />Explanation:<br />Consider the following expression:<br />a-- , --a , a<br />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.<br />After performing a-- : a will be 2<br />After performing --a : a will be 0<br />a=0<br />As we know in c zero represents false and any non-zero number represents true. Hence else part will execute.<br />