Java Programming - Operators and Assignments - Discussion
Discussion Forum : Operators and Assignments - Finding the output (Q.No. 6)
6.
What will be the output of the program?
class Test
{
public static void main(String [] args)
{
int x=20;
String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge";
System.out.println(sup);
}
}
Answer: Option
Explanation:
This is an example of a nested ternary operator. The second evaluation (x < 22) is true, so the "tiny" value is assigned to sup.
Discussion:
7 comments Page 1 of 1.
Nagaraju said:
1 decade ago
class Test
{
public static void main(String [] args)
{
int x=20;
String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge";
System.out.println(sup);
}
}
In this program the output is tiny, how it is possible in this program given
//int x=20;
String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge";
System.out.println(sup);
// it gives the second evaluation is true then output will be tiny, huge.
{
public static void main(String [] args)
{
int x=20;
String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge";
System.out.println(sup);
}
}
In this program the output is tiny, how it is possible in this program given
//int x=20;
String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge";
System.out.println(sup);
// it gives the second evaluation is true then output will be tiny, huge.
Pooja niranjane said:
1 decade ago
In above program the ternary operator perform operation right to left.
So first of all evaluates (x<22) ?"tiny":"huge";.
20 < 22 it return true.
And get the answer tiny.
Then statement like (x<15) ?"small":"tiny";.
20 <15 it return false.
And get the answer tiny.
So answer is tiny.
So first of all evaluates (x<22) ?"tiny":"huge";.
20 < 22 it return true.
And get the answer tiny.
Then statement like (x<15) ?"small":"tiny";.
20 <15 it return false.
And get the answer tiny.
So answer is tiny.
Pooja niranjane said:
1 decade ago
Nested ternary means nested if else,
Like :
if(x<22)
{
if(x<15)
{
printf("small");
}
printf("tiny");
}
else
{
printf("huge");
}
Like :
if(x<22)
{
if(x<15)
{
printf("small");
}
printf("tiny");
}
else
{
printf("huge");
}
Jacek said:
1 decade ago
Hi community.
I know only condition "if", where I can condition like in this line:
(x < 15)? "small" : (x < 22)? "tiny" : "huge".
I would check it in API but I have no idea where it looking for.
Thank you in advanced.
I know only condition "if", where I can condition like in this line:
(x < 15)? "small" : (x < 22)? "tiny" : "huge".
I would check it in API but I have no idea where it looking for.
Thank you in advanced.
Nasreen said:
1 decade ago
Any one please write a program to find whether the given input is alphabet or special character or numeral using conditional operator and if it is alphabet, convert into upper case.
Tshepo lucas malema said:
1 decade ago
Creat a JApplet that asks a user to enter a passwoed into a JTextField and then to press enter.
Karthi said:
1 decade ago
What is nested ternary operator.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers