Operators
Operators
Question 1
1. ++n
2. n=n+4 ✓
3. n+1
4. none
Question 2
What will be the output of a & b in the following, if int a, b; a=10; b=a++;
1. 10,10
2. 10,11
3. 11,10 ✓
4. 11,11
Question 3
1. 1
2. -1
3. 0✓
4. none
Question 4
1. 5.0
2. 5
3. 0✓
4. none
Question 5
What is the result of the following in Java statement?
int m=8;
m*=8;
System.out.println("The output =" + m);
1. 8
2. 64 ✓
3. 16
4. 88
Explanation:
m*=8 m=m*8
m=8*8
m=64
Question 6
double c;
int x, y, z;
x = 5; y = 10; z = 11;
c = x*y+z/2;
1. 55.0 ✓
2. 55.5
3. 55
4. none
Explanation
⇒ c = 5 * 10 + 11 / 2
c=x*y+z/2
⇒ c = 5 * 10 + 5
⇒ c = 50 + 5
⇒ c = 55.0
Question 7
int m, p;
m = 5;
p = 0;
p = m-- + --m;
1. 11
2. 10
3. 8✓
4. 12
Explanation
⇒p=5+3
p = m-- + --m;
⇒p=8
Question 8
1. 13
2. 14
3. 15
4. -15 ✓
Explanation
⇒p=8+7
p = ++a + --a
⇒ p = 15
⇒q=q-p
q -= p
⇒ q = 0 - 15
⇒ q = -15
Write the Java expressions for the following:
p = a2 + bc p=a*a+b*c
m = a2 - b2 / (ab) m = (a * a - b * b) / (a * b)
s = ut + (1/2)at2 s = u * t + (1.0 / 2) * a * t * t
f = uv / (u + v) f = u * v / (u + v)
(a + b)2 + b (a + b) * (a + b) + b
y = 2(lb + bh + lh) y = 2 * (l * b + b * h + l *h )
a2 + b2 a*a+b*b
z = x3 + y3 - xy / 3 z=x*x*x+y*y*y-x*y/3
+, -, *, /, etc. are a few examples of &&, ||, ! are a few examples of Logical
Arithmetic operators. Operators
Binary operators work on two operands. Ternary operator work on three operands.
It evaluates to true only if both of its It evaluates to true if one or both of its
operands are true. operands are true.
System.out.print( ) System.out.println( )
Next printing takes place from the same line. Next printing takes place from next line.
7. If m=5 and n=2 then what will be the output of m and n after execution that will store in
(a) & (b)?
Answer Answer
⇒m=m-n ⇒n=5+5/2
m -= n n = m + m/n
⇒m=5-2 ⇒n=5+2
⇒m=3 ⇒n=7
= ==
It is the assignment operator used for It is the equality operator used to check if a
assigning a value to a variable. variable is equal to another variable or literal.
int a=0,b=10,c=40;
a = --b + c++ +b;
System.out.println(" a = " + a);
Output
a = 58
Explanation
⇒ a = 9 + 40 + 9
a = --b + c++ + b
⇒ a = 58
(a) 5* ++x;
Answer
⇒5*6
5 * ++x
⇒ 30
(b) 5* x++;
Answer
⇒5*5
5 * x++
⇒ 25
11. Evaluate the following expressions, if the values of the variables are:
a = 2, b = 3, and c = 9
Answer
⇒2-3*8
a - (b++) * (--c)
⇒2-3*8
⇒ 2 - 24
⇒ -22
(b) a * (++b) % c;
Answer
⇒ a * (++b) % c
a * (++b) % c
⇒ 2 * (4) % 9
⇒8%9
⇒8
Answer
⇒ a = a + (a++ - ++b + a)
a += a++ - ++b + a
⇒ a = 5 + (5 - 10 + 6)
⇒a=5+1
⇒a=6
Output
a = 11 and b = 12
Explanation
The condition if(a>=10) is true so a++ increments a to 11. b remains the same.
if(income<=100000)
tax = 0;
else
tax = (0.1*income);
Answer
if(p>5000)
d = p*5/100;
else
d = 2*p/100;
Answer
Question 1
Write a program to find and display the value of the given expressions:
Output
Output
Question 2
A person is paid ₹350 for each day he works and fined ₹30 for each day he remains absent.
Write a program to calculate and display his monthly income, if he is present for 25 days and
remains absent for 5 days.
Output
Question 3
In a competitive examination, there were 150 questions. One candidate got 80% correct and
the other candidate 72% correct. Write a program to calculate and display the correct answers
each candidate got.
Output
Question 4
Output
Output
Question 5
The normal temperature of human body is 98.6°F. Write a program to convert the
temperature into degree Celsius and display the output.
Hint: c / 5 = f - 32 / 9
public class temp
{
public static void main() {
double f = 98.6;
double c = 5 * (f - 32) / 9.0;
System.out.println("Temperature in Degree Celsius = "
+ c);
}
}
Output
Question 6
The angles of a quadrilateral are in the ratio 3:4:5:6. Write a program to find and display all
of its angles. [Hint: The sum of angles of a quadrilateral = 360°]
Output