Chapter 4-Input
Chapter 4-Input
Chapter 4-Input
Operators in Java
Class 9 - APC Understanding Computer Applications with
BlueJ
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
Question 5
int m=8;
m*=8;
System.out.println("The output =" + m);
1. 8
2. 64 ✓
3. 16
4. 88
Question 6
double c;
int x, y, z;
x = 5; y = 10; z = 11;
c = x*y+z/2;
The value stored in c is:
1. 55.0 ✓
2. 55.5
3. 55
4. none
Explanation
c=x*y+z/2
⇒ c = 5 * 10 + 11 / 2
⇒ c = 5 * 10 + 5
⇒ c = 50 + 5
⇒ c = 55.0
Question 7
int m, p;
m = 5;
p = 0;
p = m-- + --m;
The output will be:
1. 11
2. 10
3. 8 ✓
4. 12
Explanation
p = m-- + --m;
⇒p=5+3
⇒p=8
Question 8
1. 13
2. 14
3. 15
4. -15 ✓
Explanation
p = ++a + --a
⇒p=8+7
⇒ p = 15
q -= p
⇒q=q-p
⇒ q = 0 - 15
⇒ q = -15
Question 1
p = a2 + bc
Answer
p=a*a+b*c
Question 2
m = a2 - b2 / (ab)
Answer
m = (a * a - b * b) / (a * b)
Question 3
s = ut + (1/2)at2
Answer
s = u * t + (1.0 / 2) * a * t * t
Question 4
f = uv / (u + v)
Answer
f = u * v / (u + v)
Question 5
(a + b)2 + b
Answer
(a + b) * (a + b) + b
Question 6
y = 2(lb + bh + lh)
Answer
y = 2 * (l * b + b * h + l * h)
Question 7
a2 + b2
Answer
a*a+b*b
Question 8
z = x3 + y3 - xy / 3
Answer
z=x*x*x+y*y*y-x*y/3
Question 1
What is an operator?
Question 2
Question 3
Question 4
Question 5
Differentiate between the following:
Arithmetic operators are used to Logical operators operate on boolean expressions to combine the
perform mathematical operations. results of these boolean expression into a single boolean value.
Binary operators work on two operands. Ternary operator work on three operands.
+, -, *, /, etc. are a few examples of Binary operators. The conditional operator ? : is a Ternary operator
Example: Example:
int a = 8, b = 13, c = 0; int a = 8, b = 13, c = 0;
if (a > 10 && b > 10) if (a > 10 || b > 10)
c = 10; c = 10;
else else
c = 5; c = 5;
Here, value of c will be 5 as one of the operands is Here, value of c will be 10 as at least one of the operan
false. true.
Example: Example:
int a = 99; int a = 99;
int b = ++a; int b = a++;
After the execution of these two statements, both After the execution of these two statements, a will have
a and b will have the value of 100. value of 100 and b will have the value of 99.
(e) System.out.print( ) and System.out.println( )
System.out.print( ) System.out.println( )
It prints data to the console but the cursor remains at the end It prints data to the console and places the c
of the data in the same line. in the next line.
Next printing takes place from the same line. Next printing takes place from next line.
Question 6
Question 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)?
(a) m -= n;
Answer
m -= n
⇒m=m-n
⇒m=5-2
⇒m=3
(b) n = m + m/n;
Answer
n = m + m/n
⇒n=5+5/2
⇒n=5+2
⇒n=7
Question 8
= ==
It is the assignment operator used for assigning a It is the equality operator used to check if a variable is eq
value to a variable. to another variable or literal.
E.g. int a = 10; assigns 10 to variable a. E.g. if (a == 10) checks if variable a is equal to 10 or not.
Question 9
int a=0,b=10,c=40;
a = --b + c++ +b;
System.out.println(" a = " + a);
Output
a = 58
Explanation
a = --b + c++ + b
⇒ a = 9 + 40 + 9
⇒ a = 58
Question 10
What will be the output of the following?
if x =5
(a) 5* ++x;
Answer
5 * ++x
⇒5*6
⇒ 30
(b) 5* x++;
Answer
5 * x++
⇒5*5
⇒ 25
Question 11
Answer
a - (b++) * (--c)
⇒2-3*8
⇒2-3*8
⇒ 2 - 24
⇒ -22
(b) a * (++b) % c;
Answer
a * (++b) % c
⇒ a * (++b) % c
⇒ 2 * (4) % 9
⇒8%9
⇒8
Question 12
If a = 5, b = 9, calculate the value of:
a += a++ - ++b + a;
Answer
a += a++ - ++b + a
⇒ a = a + (a++ - ++b + a)
⇒ a = 5 + (5 - 10 + 6)
⇒a=5+1
⇒a=6
Question 13
Output
a = 11 and b = 12
Explanation
The condition if(a>=10) is true so a++ increments a to 11. b remains the same.
Question 14
if(income<=100000)
tax = 0;
else
tax = (0.1*income);
Answer
Question 15
Answer
Question 1
Write a program to find and display the value of the given expressions:
(a) (x + 3) / 6 - (2x + 5) / 3; taking the value of x = 5
public class KboatExpression
{
public static void main(String args[]) {
int x = 5;
double value = ((x + 3) / 6.0) - ((2 * x + 5) / 3.0);
System.out.println("Result = " + value);
}
}
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.
public class KboatSalary
{
public static void main(String args[]) {
int salary = 25 * 350;
int fine = 5 * 30;
int netSalary = salary - fine;
System.out.println("Monthly Income = " + netSalary);
}
}
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.
public class KboatCompetitiveExam
{
public static void main(String args[]) {
int totalQuestions = 150;
int c1 = (int)(80 / 100.0 * totalQuestions);
int c2 = (int)(72 / 100.0 * totalQuestions);
System.out.println("Correct Answers of Candidate 1 = " + c1);
System.out.println("Correct Answers of Candidate 2 = " + c2);
}
}
Output
Question 4
Write a program to find and display the percentage difference, when:
(a) a number is updated from 80 to 90
public class KboatPercentIncrease
{
public static void main(String args[]) {
int orgNum = 80;
int newNum = 90;
int inc = newNum - orgNum;
double p = inc / (double)orgNum * 100;
System.out.println("Percentage Difference = " + p + "%");
}
}
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 KboatCelsius
{
public static void main(String args[]) {
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°]
public class KboatQuadRatio
{
public static void main(String args[]) {
int r1 = 3, r2 = 4, r3 = 5, r4 = 6;
double x = 360 / (double)(r1 + r2 + r3 + r4);
double a = r1 * x;
double b = r2 * x;
double c = r3 * x;
double d = r4 * x;
System.out.println("Angle A = " + a);
System.out.println("Angle B = " + b);
System.out.println("Angle C = " + c);
System.out.println("Angle D = " + d);
}
}