• Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Have any question?

(+91) 98222 16647
info@simplycoding.in
RegisterLogin
Simply Coding
  • Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Java

Java ternary operator examples and problems to solve

  • Categories Java, Java Operators, Operators
Java ternary operator problems
  1. Rewrite the following using ternary operator :

if(x%2 == 0)
c = ‘E’;
else
c = ‘O’;    

Ans:    

c = (x % 2 == 0) ? ‘E’ : ‘O’;

  1. Rewrite the following program segment using the if-else statements instead of the ternary operator.

String grade = (mark >= 90) ? “A” : (mark >= 80) ? “B” : “C”;

Ans.

String grade;
if(marks >= 90) {
grade = “A”;
} else if( marks >= 80 ) {
grade = “B”;
} else {
grade = “C”;
}

  1. Rewrite the following statements using ternary operator.

if(ch> “C”)
value = 200;
else
value =100;

Ans.

value = (ch> “C”) ? 200 : 100 ;

  1. What is output of the above program code?

class conditional{
public static void main(String args[ ]){
int i=20;
int j= 55;
int z= 0;
z= i < j ? i : j;
System.out.printin(“The final value assigned to z = ” + z);
}
}

Ans.

The final value assigned to z= 20

  1. Evaluate the value of n if the value of p=5 and q=19:

int n = (q-p)>(p-q)?(q-p):(p-q);

Ans.

n=14

Explanation:

p=5, q=16

n= (16-5) > (5-16)? (16-5) : (5-16)

= 11 > (-11) ? (14) : (-14)        [(expression) ? expressionTrue : expressinFalse;]

n= 14                               [as 11 is greater than (-11) so given expression is true.]

  1. What are the values of x and y when the following statements are executed?

int a = 63, b = 36;
boolean x = (a < b ) ? true : false;
int y= (a > b ) ? a : b;

Ans.

x=false    y=63

  1. Rewrite the following using ternary operator:

if (bill >10000 )
discount = bill * 10.0/100;
else
discount = bill * 5.0/100;

Ans.

discount=(bill >10000 ) ? bill * 10.0/100: bill * 5.0/100;

  1. Rewrite the following program segment using the if ..else statement.

comm = (sale>15000)?sale*5/100:0;

Ans.

if (sale>15000)
comm=sale*5/100
else
comm=0;

  1. Rewrite the following statement using suitable ‘if()’ statement.

int ans = res > 365 ? 180/3:90/ 3;

Ans:

int ans;
if( res > 365)
ans= 180/3;
else
ans= 90/ 3;

  1. Rewrite the following statement using suitable ternary operator.

if(number > 0.0)
System.out.println(“positive”);
else
System.out.println(“not positive”);

Ans.

System.out.println( (number > 0.0) ? “positive” : “not positive”);

  1. What will be the output of the following code?

char x = ‘E’ ; int m;
m=(x==’e’) ? ‘E’ : ‘e’;
System.out.println(“m=”+m);

Ans.

m= 101

  • Share:
author avatar
Simply Coding

Previous post

Java if else & switch Practice Questions
June 10, 2020

Next post

7 Tips to make online classes more effective!
June 11, 2020

You may also like

Java Operator
Java Operators
16 June, 2021
Java Class Librares
Java Class Libraries and Packages
16 June, 2021
Crack Java Math Problems
Forming Math Expressions
13 June, 2020

Leave A Reply Cancel reply

You must be logged in to post a comment.

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String
Simply Coding Computer Courses for School                Privacy Policy     Terms of Use     Contact Us

© 2021 Simply Coding

Login with your site account

Lost your password?

Not a member yet? Register now

Register a new account

Are you a member? Login now