© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

C and C++ Interview Questions

17. What is mean by ternary operator?

Ans:
It has three operands.It is used to make decision.
Syntax->operand1?operand2:operand3
operand1->Conditional Expression.
operand2->True block.
operand3->False block.

Ex - 1: what will be output of the following program?

void main(){

float a=0.7;

if(a<0.7){

printf("C");

}

else{

printf("C++");

}

}

Ans: c

Explanation :
0.7f is float constant. Its binary value is written in 32 bit.
0.7 is double constant(default). Its binary value is written in 64 bit.
0.7L is long double constant. Its binary value is written in 80 bit.
binary value of 0.7=(0.1011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011)
now here a is a float variable while 0.7 is double constant .so a contain only 32 bit value
i.e
a=0.1011 0011 0011 0011 0011 0011 0011 0011
while
0.7=0.1011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011....
It is obvious a<0.7>

Ex - 2: What is the output?

#include<stdio.h>
void main()
{
int k=10;
k<<=1;
printf("%d\n",k);
}
a)10
b)0
c)20
d) compilation error
Ans: c

Ex - 3: What is the output?

#include<stdio.h>
void main()
{
int i=-10;
for(;i;printf("%d\n",i++));
}
a)error
b)prints -10 to -1
c)infinite loop
d)does not print anything
Ans
: b


SLogix Student Projects
bottom