Question:

Users must be above 10 years to open a self-operated bank account. Write this logic using a ternary operator and store the result (the eligibility message) in a String variable named \texttt{idStatus and print it.}

Show Hint

The ternary operator is a short form of if-else. It is useful when you need to assign one of two values based on a condition.
Hide Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

Step 1: Understand the condition.
The user is eligible only when age is greater than \(10\). So the condition is:
\[ \texttt{age>10} \] Step 2: Use the ternary operator.
A ternary operator in Java has the form:
\[ \texttt{condition ? value1 : value2} \] If the condition is true, the first message is stored; otherwise, the second message is stored.
Step 3: Write the required Java code.
The required code is:
\texttt{String idStatus = (age>10) ? "Eligible" : "Not Eligible";}
\texttt{System.out.println(idStatus);}
Step 4: Explain the result.
If the age is more than \(10\), the program prints \texttt{"Eligible"}. Otherwise, it prints \texttt{"Not Eligible"}.
Was this answer helpful?
0
0

Top Questions on Control Statements

View More Questions

Questions Asked in ICSE Class X board exam

View More Questions