Good C Questions
-
Author
premanand020288 -
Category
Documents
-
view
224 -
download
0
Embed Size (px)
Transcript of Good C Questions
-
8/13/2019 Good C Questions
1/31
What will be output when you will execute following ccode?
#includeint main(){
double num=5.2;int var=5;printf("%d\t",sizeof(!num));printf("%d\t",sizeof(var=15/2));printf("%d",var);return 0;
}
Choose all that apply:
(A) 4 2 7
(B) 4 4 5
(ans)(C) 2 2 5
(D) 2 4 7
(E) 8 2 7
E x p l a n a t i o n :Turbo C++ 3.0: 2 2 5Turbo C ++4.5: 2 2 5Linux GCC: 4 4 5Visual C++: 4 4 5
sizeof(Expr) operator always returns the an integervalue which represents the size of the final value of
the expression expr.Consider on the following expression:!num=!5.2=0
-
8/13/2019 Good C Questions
2/31
0 is int type integer constant and it size is 2 by inTURBO C 3.0 compiler and 4 in the TURBO C 4.5 andLinux 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 sizeofoperator its scope always will be within the sizeofoperator. So value of variable var will remain 5 in theprintf statement
Consider on following declaration:
(i) short i=10;
(ii) static i=10;
(iii) unsigned i=10;
(iv) const i=10;
Choose correct one:
(A) Only (iv) is incorrect
(B) Only (ii) and (iv) are incorrect
(C) Only (ii),(iii) and (iv) are correct
(D) Only (iii) is correct
(ans)(E) All are correct declaration
Answ er
E x p l a n a t i o n :Default data type of above all declaration is int.
-
8/13/2019 Good C Questions
3/31
What will be output when you will execute following ccode?
#includeint main(){
signed x,a;unsigned y,b;a=(signed)10u;b=(unsigned)-10;y = (signed)10u + (unsigned)-10;x = y;printf("%d %u\t",a,b);if(x==y)
printf("%d %d",x,y);
else if(x!=y)printf("%u %u",x,y);return 0;
}
Choose all that apply:
(A) 10 -10 0 0
(B) 10 -10 65516 -10
(C) 10 -10 10 -10
(D) 10 65526 0 0
(E) Compilation error
Answ er
E x p l a n a t i o n :
Turbo C++ 3.0: 10 65526 0 0Turbo C ++4.5: 10 65526 0 0
Linux GCC: 10 4294967286 0 0
Visual C++: 10 4294967286 0 0
a=(signed)10u;signed value of 10u is +10
-
8/13/2019 Good C Questions
4/31
so, a=10b=(unsigned)-10;unsigned value of -10 is :MAX_VALUE_OF_UNSIGNED_INT 10 + 1In turbo c 3.0 complier max value of unsigned int is65535So, b = 65526y = (signed)10u + (unsigned)-10;
= 10 + 65526 = 65536 = 0 (Since 65536 is beyond therange of unsigned int. zero is its corresponding cyclicvlaue)X = y = 0
Which of the following is integral data type?
(A) void
(B) char
(C) float
(D) double
(E) None of theseAnsw er
E x p l a n a t i o n :
In c char is integral data type. It stores the ASCIIvalue of any character constant.
What will be output when you will execute following ccode?
#includeint main(){
int a=-5;unsigned int b=-5u;
-
8/13/2019 Good C Questions
5/31
if(a==b)printf("Avatar");
elseprintf("Alien");
return 0;}
Choose all that apply:
(A) Avatar
(B) Alien
(C) Run time error
(D) Error: Illegal assignment
(E) Error: Dont compare signed no. with unsignedno.
Answ er
E x p l a n a t i o n :
Turbo C++ 3.0: AvatarTurbo C ++4.5: Avatar
Linux GCC: AvatarVisual C++: Avatar
int a=-5;Here variable a is by default signed int.unsigned int b=-5u;Constant -5u will convert into unsigned int. Itscorresponding unsigned int value will be :65536 5 + 1= 65532So, b = 65532
In any binary operation of dissimilar data type forexample: a == bLower data type operand always automatically typecasted into the operand of higher data type beforeperforming the operation and result will be higher datatype.
-
8/13/2019 Good C Questions
6/31
In c signed int is higher data type than unsigned int.So variable b will automatically type casted intosigned int.So corresponding signed value of 65532 is -5
Hence, a==b
1 .
What will be output when you will execute following ccode?
#includevoid 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
Answ er
E x p l a n a t i o n :
-
8/13/2019 Good C Questions
7/31
Keyword break is not syntactical part of if-elsestatement. So we cannot use break keyword in if-elsestatement. This keyword can be use in case of loop orswitch case statement.
Hence when you will compile above code compiler willshow an error message: Misplaced break.
2 .
What will be output when you will execute following ccode?
#define PRINT printf("Star Wars");printf(" Psycho");
#includevoid main(){
int x=1;if(x--)
PRINTelse
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
Answ er
E x p l a n a t i o n :
-
8/13/2019 Good C Questions
8/31
PRINT is macro constant. Macro PRINT will be replacedby its defined statement just before the actualcompilation 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 inthe 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. Itis cause of compilation error. Hence compiler will showan error message: Misplaced else
3 .
What will be output when you will execute following ccode?
#define True 5==5#includevoid main(){
-
8/13/2019 Good C Questions
9/31
if(.001-0.1f)printf("David Beckham");
else if(True)printf("Ronaldinho");
elseprintf("Cristiano Ronaldo");
}
Choose all that apply:
(A) David Beckham
(B) Ronaldinho
(C)Cristiano Ronaldo
(D) Warning: Condition is always true
(E) Warning: Unreachable code
Answ er
E x p l a n a t i o n :
As we know in c zero represents false and any non-zeronumber represents true. So in the above code:
(0.001 0.1f) is not zero so it represents true. Soonly if clause will execute and it will print: DavidBeckham on console.But it is bad programming practice to write constant asa condition in if clause. Hence compiler will show awarning message: Condition is always true
Since condition is always true, so else clause willnever execute. Program control cannot reach at elsepart. So compiler will show another warning message:Unreachable code
4 .
-
8/13/2019 Good C Questions
10/31
What will be output when you will execute following ccode?
#include
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
Answ er
E x p l a n a t i o n :In case of if if else if else Statement if firstif clause is true the compiler will never check rest ofthe if else clause and so on.
-
8/13/2019 Good C Questions
11/31
5 .
What will be output when you will execute following ccode?
#includevoid 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
Answ er
E x p l a n a t i o n :
Consider following statement:
++x=++yAs we know ++ is pre increment operator in the abovestatement. This operator increments the value of anyintegral variable by one and return that value. Afterperforming pre increments above statement will be:
0=0
-
8/13/2019 Good C Questions
12/31
In C language it is illegal to assign a constant valueto another constant. Left side of = operator must be acontainer i.e. a variable. So compiler will show anerror message: Lvalue required
In c if you assign any value to variable but you dontperform any operator or perform operation only usingunary operator on the variable the compiler will show awarning message: Variable is assigned a value that isnever
6 .
What will be output when you will execute following c
code?
#includevoid main(){
if(sizeof(void))printf("M. Muralilidaran");
elseprintf("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
Answ er
E x p l a n a t i o n :
-
8/13/2019 Good C Questions
13/31
It illegal to find size of void data type using sizeofoperator. Because size of void data type is meaningless.
7 .What will be output when you will execute following ccode?
#includevoid main(){
int m=5,n=10,q=20;if(q/n*m)
printf("William Gates");
elseprintf(" 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 x p l a n a t i o n :Consider the following expression:q / n * m
In this expression there are two operators. They are:/: Division operator
Answ er
-
8/13/2019 Good C Questions
14/31
*: Multiplication operatorPrecedence and associate of each operator is as follow:
Precedence Operator Associate
1 / , * Left to right
Precedence of both operators is same. Hence associatewill decide which operator will execute first. SinceAssociate is left to right. So / operator will executethen * operator will execute.= q / n * m= 20 / 10 * 5
= 2 * 5=10
As we know in c zero represents false and any non-zeronumber represents true. Since 10 is non- zero number soif clause will execute and print: William Gates
Since in else clause there is not any opening andclosing curly bracket. So compiler will treat only onestatement as a else part. Hence last statement i.e.
printf(" Carlos Slim Helu");
is not part of if-else statement. So at the endcompiler will also print: Carlos Slim HeluSo output of above code will be:William Gates Carlos Slim Helu
8 .
What will be output when you will execute following ccode?
#includevoid main(){
if(!printf("Mukesh Ambani"))if(printf(" Lakashmi Mittal"));
-
8/13/2019 Good C Questions
15/31
}
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
Answ er
E x p l a n a t i o n :
Return type of printf function is int. This functionreturn a integral value which is equal to number ofcharacters a printf function will print on console.First of all printf function will: Mukesh Ambani. Sinceit is printing 13 character so it will return 13. So,
!printf("Mukesh Ambani")= !13= 0In c language zero represents false. So if(0) is falseso next statement which inside the body of first ifstatement will not execute.
9 .
What will be output when you will execute following ccode?
#includevoid main(){
if("ABC") printf("Barack Obama\n");if(-1) printf("Hu Jintao\n");
-
8/13/2019 Good C Questions
16/31
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 Obama
Hu JintaoNicolas Sarkozy
Vladimir Putin
(C)Barack ObamaHu JintaoNicolas SarkozyBen Bernanke
Vladimir Putin
(D)Hu JintaoNicolas Sarkozy
Vladimir Putin
(E)
Compilation error
Answ er
E x p l a n a t i o n :
ABC: It is string constant and it will always returna non-zero memory address.0.92L: It is long double constant.
-
8/13/2019 Good C Questions
17/31
W: It is character constant and its ASCII value is
As we know in c language zero represents false and anynon-zero number represents true. In this programcondition of first, second, third and fifth if
statements are true.
10.
What will be output when you will execute following ccode?
#includevoid 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
(C) Compilation error: If without any body
(D) Compilation error: Undefined symbol
(E) Warning: Condition is always true
Answ er
E x p l a n a t i o n :
-
8/13/2019 Good C Questions
18/31
-
8/13/2019 Good C Questions
19/31
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 ofcharcters printf function will print on console.
Operator >= will return 1 if both operands are eitherequal or first operand is grater than second operand.So a>=10 will return 1 since a is equal to 10.Thusprintf function will print 1. Since this function isprinting 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 conditionhence if clause will execute which contains an infiniteloop but due to break keyword it will come out of loop.
12.(Very Good).
What will be output when you will execute following c
code?
#includevoid 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
-
8/13/2019 Good C Questions
20/31
-
8/13/2019 Good C Questions
21/31
if(i+++"The Matrix")printf("Memento");
elsebreak;
}
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 +++
Answ er
E x p l a n a t i o n :
Think yourself
14.What will be output when you will execute following ccode?
#includevoid main(){
int x=1;if(x--)
printf("The Godfather");--x;
elseprintf("%d",x);
}
Choose all that apply:
-
8/13/2019 Good C Questions
22/31
(A) The Godfather
(B) 1
(C) 0
(D) Compilation error
(E) None of the above
Answ er
E x p l a n a t i o n :
If you are not using { and } in if clause then you canwrite only one statement. Otherwise it will cause ofcompilation error: Misplace else
15.
What will be output when you will execute following ccode?
#includevoid 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
-
8/13/2019 Good C Questions
23/31
(D) Warning: Unreachable code
(E) Compilation error: if statement without any body
Answ er
E x p l a n a t i o n :
\0 is null character constant. Its ASCII value iszero. if(0) means false so program control will checkit else if clause.NULL is macro constant which has been defined instdio.h which also returns zero.
16.
What will be output when you will execute following ccode?
#includevoid main(){
int a=5,b=10;clrscr();
if(a
-
8/13/2019 Good C Questions
24/31
Answ er
E x p l a n a t i o n :
Consider the following expression:
a
-
8/13/2019 Good C Questions
25/31
will operator will return zero and else clause willexecute.
17.
What will be output when you will execute following ccode?
#includevoid main(){
int x=1,y=2;if(--x && --y)
printf("x=%d y=%d",x,y);else
printf("%d %d",x,y);}
Choose all that apply:
(A) 1 2
(B) x=1 y=2
(C) 0 2
(D) x=0 y=1
(E) 0 1
Answ er
E x p l a n a t i o n :
Consider the following expression:--x && --y
In this expression && is Logical AND operator. Twoimportant properties of this operator are:Property 1:(Expression1) && (Expression2)
-
8/13/2019 Good C Questions
26/31
-
8/13/2019 Good C Questions
27/31
(E) Warning: Illegal operation
Answ er
E x p l a n a t i o n :
Read following tutorial:Data type tutorial
19.
What will be output when you will execute following c
code?#includevoid main(){
char c=256;char *ptr="Leon";if(c==0)
while(!c)if(*ptr++)
printf("%+u",c);
elsebreak;}
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
Answ er
-
8/13/2019 Good C Questions
28/31
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 isfrom -128 to 127. But we are assigning 256 which isbeyond the range of variable c. Hence variable c willstore corresponding cyclic value according to followingdiagram:
-
8/13/2019 Good C Questions
29/31
Since 256 is positive number move from zero in clockwise direction. You will get final value of c iszero.
if(c==0)
It is true since value of c is zero.
Negation operator i.e. ! is always return either zeroor one according to following rule:
!0 = 1!(Non-zero number) = 0So,!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 ispointing to first character of string Leonaccording to following diagram:
-
8/13/2019 Good C Questions
30/31
In the above figure value in circle represents ASCIIvalue of corresponding character.Initially *ptr means L. So *ptr will return ASCIIvalue 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 ptrwill point \0 i.e. null character .It will return itsASCII value i.e. 0. So if(0) is false. Hence else partwill execute.
20.
What will be output when you will execute following ccode?
#includevoid main(){
int a=2;if(a--,--a,a)
printf("The Dalai Lama");else
printf("Jim Rogers");
-
8/13/2019 Good C Questions
31/31
}
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
Answ er
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 asoperator.In the above expression comma is behaving as
operator.Comma operator enjoy lest precedence inprecedence table andits associatively is left to right.So first of all left most comma operator will performoperation then right most comma will operator in theabove expression.
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-zeronumber represents true. Hence else part will execute.