0% found this document useful (0 votes)
8 views3 pages

Untitled Document

The document explains the concept of operators in Java, detailing the three main types: Arithmetic, Relational, and Logical operators. It distinguishes between Java expressions and statements, providing examples for each type of operator and explaining the differences between unary and binary operators, as well as postfix and prefix increments/decrements. Additionally, it includes examples of rewriting conditional statements using if-else and ternary operators.

Uploaded by

rajatkb777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Untitled Document

The document explains the concept of operators in Java, detailing the three main types: Arithmetic, Relational, and Logical operators. It distinguishes between Java expressions and statements, providing examples for each type of operator and explaining the differences between unary and binary operators, as well as postfix and prefix increments/decrements. Additionally, it includes examples of rewriting conditional statements using if-else and ternary operators.

Uploaded by

rajatkb777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1. What is an operator? What are the three main types of operators? Name them.

Answer:
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. The three main types of operators in Java are:
Arithmetic Operators
Relational Operators
Logical Operators
2. How is Java expression different from statement?
Answer:
A Java expression is a combination of variables, operators, and method calls that evaluates
to a single value (e.g., a + b, x > y). A Java statement is a complete instruction that performs
an action and typically ends with a semicolon (e.g., int sum = a + b;,
System.out.println("Hello");). All expressions can be part of a statement, but not all
statements are expressions.
3. Explain the following with one example each.
(a) Arithmetic operator
Answer:
Arithmetic operators perform mathematical operations. Example: int sum = 5 + 3; (+ is an
arithmetic operator).
(b) Relational operator
Answer:
Relational operators compare two values and return a boolean result. Example: boolean
isEqual = (x == y); (== is a relational operator).
(c) Logical operator
Answer:
Logical operators combine or modify boolean expressions. Example: boolean result = (a > 0
&& b < 10); (&& is a logical operator).
(d) Ternary operator
Answer:
The ternary operator (or conditional operator) is a shorthand for an if-else statement.
Example: String message = (age >= 18) ? "Adult" : "Minor";
4. Distinguish between:
(a) Unary arithmetic operator and Binary arithmetic operator
Answer:
Unary arithmetic operators operate on a single operand (e.g., ++, --, -), while Binary
arithmetic operators operate on two operands (e.g., +, -, *, /).
(b) Postfix increment and Prefix increment
Answer:
Postfix increment (i++) increments the value of the variable after its current value is used in
the expression, while Prefix increment (++i) increments the value of the variable before its
value is used in the expression.
(c) Postfix decrement and Prefix decrement
Answer:
Postfix decrement (i--) decrements the value of the variable after its current value is used in
the expression, while Prefix decrement (--i) decrements the value of the variable before its
value is used in the expression.
8. Rewrite using if-else statements
a) String grade = (marks >= 90)?"A": (mark >= 80)? "B": "C";
Answer:
java
String grade;
if (marks >= 90) {
grade = "A";
} else if (marks >= 80) {
grade = "B";
} else {
grade = "C";
}
Use code with caution.

b) commission = (sale > 5000)? (sale*10)/100: 0;


Answer:
java
double commission;
if (sale > 5000) {
commission = (sale * 10) / 100.0; // Use 100.0 for double division
} else {
commission = 0;
}
Use code with caution.

**c) net = (salary > 10000)? salary - (8.33/100)salary: salary - (5/100)salary;


Answer:
java
double net;
if (salary > 10000) {
net = salary - (8.33 / 100.0) * salary;
} else {
net = salary - (5 / 100.0) * salary;
}
Use code with caution.

d) s = (a+b <= c || a+c <= b || b+c <= a) ? "Triangle is not possible": "Triangle is possible";
Answer:
java
String s;
if (a + b <= c || a + c <= b || b + c <= a) {
s = "Triangle is not possible";
} else {
s = "Triangle is possible";
}
Use code with caution.

e) c = (x >= 'A' && x<= 'Z') ? "Uppercase Letter": "Lowercase Letter";


Answer:
java
String c;
if (x >= 'A' && x <= 'Z') {
c = "Uppercase Letter";
} else {
c = "Lowercase Letter";
}
Use code with caution.

9. Rewrite using Ternary Operator


a) if (x%2==0) System.out.println("Even"); else System.out.println("Odd");
Answer:
java
System.out.println(x % 2 == 0 ? "Even" : "Odd");
Use code with caution.

b) if (bill>10000) discount = bill10.0/100; else discount = bill5.0/100;


Answer:
java
double discount = (bill > 10000) ? bill * 10.0 / 100 : bill * 5.0 / 100;
Use code with caution.

c) if(income< 10000) tax = 0; else tax = 12;


Answer:
java
int tax = (income < 10000) ? 0 : 12;
Use code with caution.

d) if(a>b) if(a>c) g=a; else g=c else if(b>c) g=b; else g=c
Answer:
java
int g = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
Use code with caution.

e) if(p>=4750) k=p5/100; else k=p10/100;


Answer:
java
double k = (p >= 4750) ? p * 5 / 100.0 : p * 10 / 100.0;
Use code with caution.

f) if(n1>n2) r = true; else r = false;


Answer:
java
boolean r = (n1 > n2) ? true : false;
// Alternatively, and more concisely:
// boolean r = n1 > n2;

You might also like