The document contains a worksheet with true/false questions about Java operators, expressions, and their outputs. It includes explanations for various Java programming concepts such as operators, expressions, and conditional statements. Additionally, it provides examples of Java code and expected outputs, along with explanations for the logic behind the code.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0 ratings0% found this document useful (0 votes)
75 views9 pages
Operators Worksheet 1 Solution
The document contains a worksheet with true/false questions about Java operators, expressions, and their outputs. It includes explanations for various Java programming concepts such as operators, expressions, and conditional statements. Additionally, it provides examples of Java code and expected outputs, along with explanations for the logic behind the code.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 9
‘Repeera-unta
ators in Java ;
Permtorsiniava WORKSHEET 1 (SoLUTION)
State whether the following statements are ‘True’ OF ‘False’
Question 1
The precedence of ‘Operators in Java follows BODMAS.
False
Question 2
The output of a+ will be 1, fint a
False
Question 3
The relational operators always result in terms of ‘True! or False
True
Question 4
Given: int m=5; m*=5 then the value stored in m results in 55
False
Question 5
The statement (a>b)&&(a>c) uses a logical operator.
True
Question 6
tint 3=27,
True
Question 7
The statement p+=5 means p=p*5.
False
Question 8
In the precedence of logical operators; NOT is followed by AND.
True
Write the Java expressions for the following
Question 1
z= 5x? + Qyxty
Answer
ZeBtx*x*x+2*y*xry
Question 2
m=a’+b?/(a+b)
Answer
m=(a*atb*b)/(a+b)
Question 3
s=ut+(1/2)at™
Answer
s=urt+(1.0/2)*a*t*t
Question 4
f=uv/(u+v)
Answer
feu*v/ (uty)
Question 5
d=v3x+x)/atb
inswer
a Math.sqrt(3 * x + x * x)/ (a + b)
Question 6
p=a’+b'+2ab
Answer
pratatb*b+2*a"b
Question 7
y=2(Ib + bh + Ih)
wer
seye(ieb+b*h+I*h)
Question 8
0; then c = a%b; results in 3,p=a/b'+b/a’
Answer
p=a/(b*b)+b/(a*a)
Question 9
zexty-y/2
Answer
ZEx*x*xtyty*ty-yl(z*z*2)
Question 10
q=1/v(a+b)+3/c
Answer
q=(1/Math.sqrt(a + b)) + 3/(c*c)
Predict the output
Question 1
int c= (<4)? 3°4:3+4;
Output
12
Explanation
As 3s less than 4 so condition of temary operator is true. Variable c is assigned the value of expression 1
which is 3 * 4 = 12.
Question 2
inta = 14, b=4;
boolean x = (a > b) ? true : false;
Output
true
Explanation
‘As 14 is greater than 4 so condition of ternary operator is true. Variable xis assigned the value of expression
1 which is true.
Question 3
int x = 90,
char ¢ = (x<=90)?Z""
Output
iz
Explanation
‘As value of x is 80 0 condition of temary operator is true, Ia aoieigaase/onedthe value ol. expression 1
which is Z.Son 4
ft a= 18; int = 12,
Scolean t= (a> 20 && b < 15)? true false;
Output
false
Explanation
The condition a > 20 is fal
ecconalle of ais 18. So the logical AND operator — && retums false. Variable tis
@Ssigned the value of expression 2 which is falge 6
Question 5
c= (val +550 <
1700)? 200: 400;
if (@) val = 1000
(b) val = 1500
Output
(a) 200
(b) 400
Explanation
When val = 1000, val + 550 = 1550. As
1850 is less than 1700 so condition of ternary operator is true.
Variable c is assigned the value of expression 1 which is 200.
When val = 1500, val + 550 = 2050.
AS 2050 is greater than 1700 so condition of ternary operator is false.
Variable c is assigned the value of expression 2 which is 400
Answer the following questions
Question 1
What is an operator? What are the three main types of operators? Name them
Answer
tion to be performed in Java programming
is a symbol or sign used to specify an operat ‘ -
Are hace main types of operators are Arithmetical, Logical and Relational,
Question 2
is Java expression different from statement?
How i
Answer
ion is a combination of
tof variables, constants Se ofr a variate. Gascon pietaaet oe a
An expression |S 2 802 hen an expression is assigne 5
sid operands.
operators
as a statement.
Question 3
mpl .
inthe following with one examPle a
eExplai(@) Arithmetic operator
Answer
.s on its operands. Operands of TE
jerators operate upon one operand.
{tors operate upon two operands.
Arithmetic operators are used to perform mathematical operation
sitio cberatare must be of numeric type. A few arithmetic OP
‘ey are called Unary Arithmetic operators. Other arithmetic opera! z
hey are called Binary Arithmetic operators. As an example consider the below statement:
int a= 10 + 29;
Here. the addition arithmetic operator, represented by the symbol * will add 10 and 20. So
Variable a will be 30.
(b) Relational operator
Answer
Gelational operators are used to determine the relationship between the operands. Relational
aberators compare their operands to check if the operands are equal to ( ==), not equal to ( !=), less
than (<), less than equal to (<= ), greater than (>), greater than equal to (>=) each other. The result
££ 2" operation involving relation operators is a boolean value — true or false.
Example:
int a= 8;
int b = 26;
boolean c = a k=544
=k=6
Question 6(b)
int y=10 then find int z = (+4y"(y+++8));
| Output
z = 176
Explanation
Z= (+4y*(y+++5))
325 (11*(1145))
325 (11* 16)
325176
Question 6(c)
Give the output of the following expression:
ats a++ + ++a+-—a + a-—; when a=7;
Output
a = 39
ExplanationExplanation
yis Hy tye ty
syty+(tyty-+-y)
sy=8+(9+9+7)
=y=8+25
sy=33
Question 7
follo\ Tisccment sme Ioleaet stead of the ternary operator.
Rewrite the following program segment using fatements in
(@) String grade = (marks>=90)?"A": (marks>=80)? "
Answer
String grade;
if (marks >= 90
grade = "A";
else if (marks >= 80)
grade = "B";
else
grade = "C
(b) commission = (sale > 5000) ? sale*10/100 : 0;
Answer
if (sale > 5@00)
commission = sale * 10 / 109;
else
commission = @;
(¢) net = (salary > 10000) ? salary - (8.33/100)*salary : salary - (5/100)*salary
Answer
if (salary > 1¢0¢0)
net = salary - (8.33/100) * salary;
else
net = salary - (5/100) * salary;
(@s=(a+b="A' 88 x<= 7) ? "Upper Case Letter": "Lower Case Lette
Answer
if (x >= "A" & x <= #29)
C = "Upper Case Letter";
else
c
‘ower Case Letter";ystion °
cenit the following Using ternary operator.
fa)
if (X% 2 == 0)
system. out. printin("Even") 5
else
System. out. print1n("Odd") 5
Answer
System.out.println(x % 2 == @ ? "Even"
(b)
if (bill > 10000)
discount=bill*10.0/100;
else
discount=bil1*5.0/100;
Answer
discount = bill > 10000 ? bill*10.0/18 : bi11*5. 9/199;
(co) 3
if(income < 19000)
tax = 0;
else
tax = 12;
Answer
tax = income < 10000 ? @ : 12;
(@)
if(a > b)
x
if (a > c)
B= a;
else
eB= c)
B= b;
else
(Soe
Answer
Gi=.a > b Qhalsicipean= ice
(e)
if (p >= 4750)
k=p* 5 / 109;
else
k = p * 10 / 109;
Answer
k = (p >= 4750) ? p * 5 / 1¢0 : p * 10 / 100;
9