If
else or control flow objective type questions and answers with explanation in c
language for written test or interview
What will be output when you will execute following c code?
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 .
What will be output when you will execute following c code?
#define PRINT printf( "Star Wars" );printf( " Psycho" );
printf( "The Shawshank Redemption" );
PRINT is macro constant. Macro PRINT will be replaced by its defined statement just before the actual compilation starts. Above code is converted as:
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:
It is if statement without any else. It is ok.
It is a function call. It is also ok
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
What will be output when you will execute following c code?
printf( "Cristiano Ronaldo" );
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:
What will be output when you will execute following c code?
printf( "A.B. de villiers" );
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.
What will be output when you will execute following c code?
Consider following statement:
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:
In C language it is illegal to assign a constant value to another constant. Left side of = operator must be a container i.e. a variable. So compiler will show an error message: Lvalue required
In c if you assign any value to variable but you don’t perform any operator or perform operation only using unary operator on the variable the compiler will show a warning message: Variable is assigned a value that is never
What will be output when you will execute following c code?
printf( "M. Muralilidaran" );
printf( "Harbhajan Singh" );
It illegal to find size of void data type using sizeof operator. Because size of void data type is meaning less.
What will be output when you will execute following c code?
printf( " Warren Buffet" );
printf( " Carlos Slim Helu" );
Consider the following expression:
In this expression there are two operators. They are:
*: Multiplication operator
Precedence 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.
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
What will be output when you will execute following c code?
if (!printf( "Mukesh Ambani" ))
if (printf( " Lakashmi Mittal" ));
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,
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.
What will be output when you will execute following c code?
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" );
“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.
What will be output when you will execute following c code?
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.
What will be output when you will execute following c code?
if (printf( "%d" ,a>=10)-10)
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
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.
What will be output when you will execute following c code?
Consider the following expression:
In this expression || is Logical OR operator. Two important properties of this operator are:
(Expression1) || (Expression2)
|| operator returns 0 if and only if both expressions return a zero otherwise it || operator returns 1.
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.
What will be output when you will execute following c code?
What will be output when you will execute following c code?
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
What will be output when you will execute following c code?
‘\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.
What will be output when you will execute following c code?
Consider the following expression:
In the above expression || is logical OR operator. It divides any expression in the sub expressions. In this way we have two sub expressions:
In the expression: a< ++a
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:
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.
What will be output when you will execute following c code?
Consider the following expression:
In this expression && is Logical AND operator. Two important properties of this operator are:
(Expression1) && (Expression2)
&& operator returns 1 if and only if both expressions return a non-zero value other wise it && operator returns 0.
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.
What will be output when you will execute following c code?
printf( "The Lord of the Rings" );
printf( "American Beauty" );
What will be output when you will execute following c code?
In the above program c is signed (default) char variable. Range of signed char variable in Turbo c is from -128 to 127. But we are assigning 256 which is beyond the range of variable c. Hence variable c will store corresponding cyclic value according to following diagram:
Since 256 is positive number move from zero in clock wise direction. You will get final value of c is zero.
It is true since value of c is zero.
Negation operator i.e. ! is always return either zero or one according to following rule:
As we know in c zero represents false and any non-zero number represents true. So
while (!c) i.e. while (1) is always true.
In the above program prt is character pointer. It is pointing to first character of string “Leon” according to following diagram:
In the above figure value in circle represents ASCII value of corresponding character.
Initially *ptr means ‘L’. So *ptr will return ASCII value of character constant ‘L’ i.e. 76
if (*ptr++) is equivalent to : if(‘L’) is equivalent to: if(76) . It is true so in first iteration it will print +0. Due to ++ operation in second iteration ptr will point to character constant ‘e’ and so on. When ptr will point ‘\0’ i.e. null character .It will return its ASCII value i.e. 0. So if(0) is false. Hence else part will execute.
What will be output when you will execute following c code?
printf( "The Dalai Lama" );
Consider the following expression:
In c comma is behaves as separator as well as operator.In the above expression comma is behaving as operator.Comma operator enjoy lest precedence in precedence table andits associatively is left to right. So first of all left most comma operator will perform operation then right most comma will operator in the above expression.
After performing a-- : a will be 2
After performing --a : a will be 0
As we know in c zero represents false and any non-zero number represents true. Hence else part will execute.
27 comments:
very good collection...keep it up..bravo n thanxxxx :)
how to connect dos promt through c-language
good collection...
nice collection 2 increase d knowledge of bigner...
thank u.
thanks for these collection .
thnq.. its really helpful...
nice collection of question it helps to increase the knowledge
nice collection....it helped me a lot
plese explain that 13th question
19th example should be like this.. i mean format spacifier shoud be %d not %u ... If it is %u, we can't print + mark in visual c++
#include
void main(){
char c=256;
char *ptr="Leon";
if(c==0)
while(!c)
if(*ptr++)
printf("%+d",c);
else
break;
}
very nice collection and really very helpful..
#include
int main()
{
int a = 2 , b = 5 ;
char x = 1 , y = 10;
if (a,b,x,y)
{
printf(" TGF");
}
return 0;
}
What's the meaning of if(a,b,x,y) ?
I tried in TC.It gives following warnings:
a is assigned a value that is never used
b is assigned a value that is never used
x is assigned a value that is never used
-Ravi
ITS VERY USEFUL....
NICE TQ...
GAURAV GARG :
if you compile this in gcc then this code is not giving error
in gcc sizeof(void) = 1
#include
void main(){
if(sizeof(void))
printf("M. Muralilidaran");
else
printf("Harbhajan Singh");
}
please explain 13th question...........
this is very helpful
gebrehiwet
mekele ethiopia
this is very helpful. you have a good job. i wanna to appreciate for the owner of this site, this cqestions bank help to know the mystery of the c programming. even the beginner program learner , they can be easily understand.
by GEBREHIWET ABRHAM
MU=MEKELE UNIVERSITY
MEKELE INSTITUTE OF TECHNOLOGY(MIT)
DEPARTMENT BIOLOGICAL AND CHEMICAL ENGINEERING
very tricky questions.. Thanks a lot.. Keep it up
in gcc we can use sizeof(void)
sizeof(void ) iam getting 1.. in dev c++ alo getting 1
Really Nice Question... Thankx For Sharing.... More Question Please......
thanxs 4 sharing....really gud questions...
That all question is help to the clear the all concept .....THANKS
very good and thanks for help in improvement of our knowledge
#include
#include
void main()
{
int a,b,c;
char d;
clrscr();
printf("Enter any two numbers for calculation\n");
scanf("%d%d",&a,&b);
printf("Guidance: if you enter + result is addition and like that all\n");
printf("Enter operators '+' '-' '*' '/' '%' which one you required\n");
scanf("%s",&d);/* when i used %c it not take operator */
if(d=='+')
{
c=a+b;
printf("result = %d",c);
}
else if(d=='-')
{
c=a-b;
printf("result = %d",c);
}
else if(d=='*')
{
c=a*b;
printf("result = %d",c);
}
else if(d=='/')
{
c=a/b;
printf("result = %d",c);
}
else if(d=='%')
{
c=a%b;
printf("result = %d",c);
}
else
{
printf("invalid operator\n");
}
getch();
}
problem is :
it take two number but
when i used %c it goes last "else" option and print invalid operator
when i used %s it give answer but this answer is that number which is input first but it does not perform operation
please someone tell me where i'am wrong
please explain question no 13.plzzzzzzzzzzzzzzzzzzzzzzz
Post a Comment