Lecture 5
Operators
and Expressions: Answers
Use
the code to find the answers to the activities.
Activity
A
Activity
B
Activity
C
Activity
D
Activity
E
Activity
A
Activity A |
-
Determine the values of x and y at the end
of the code fragment
int x
= 10, y = 5;
x +=
2;
y *=
x;
- Determine the values of p and q at the end of the code
fragment
int p
= 10, q = 16;
p
-= 2;
q
/= p;
Why not check your answers
by using code.
|
To
check your answers; try the following code:-
import
java.awt.*;
public class ActivityA {
public static void main (String[] args) {
int x = 10, y = 5;
int p = 10, q = 16;
x += 2;
y *= x;
p -= 2;
q /= p;
System.out.println ( "x is " +
x);
System.out.println ( "y is " +
y);
System.out.println ( "p is " + p);
System.out.println ( "q is " +
q);
}
}
Activity
B
Activity B |
-
Determine the values of x and y at the end
of the code fragment
int x
= 10, y = 5;
x = y
+ 2;
y = x
+ 4;
- Determine the values of p and q at the end of the code
fragment
int p
= 10, q = 5;
p
= 4 - q;
q
= 4 / p;
- Determine the values of rem at the end of the code
fragment
int
rem = 6 % 4;
Why not check your answers
by using code.
|
To
check your answers; try the following code:-
import
java.awt.*;
public class ActivityB {
public static void main (String[] args) {
int x = 10, y = 5;
x = y + 2;
y = x + 4;
int p = 10, q = 5;
p = 4 - q;
q = 4 / p;
int rem = 6 % 4;
System.out.println ( "x is " +
x);
System.out.println ( "y is " +
y);
System.out.println ( "p is " + p);
System.out.println ( "q is
" + q);
System.out.println ( "rem is " + rem);
}
}
Activity
C
Activity C |
-
Determine the values of ans and count at
the end of the code fragment
int
ans = 1, count = 2;
ans
+= count++;
- Determine the values of p and q at the end of the code
fragment
int p
= 10, q = 5;
p
= ++q + 3;
- Determine the values of x and y at the end of the code
fragment
int x
= 2, y = 5;
y
+= 3;
x *=
y--;
Why not check
your answers by using code. |
To
check your answers; try the following code:-
import
java.awt.*;
public class ActivityC {
public static void main (String[] args) {
int ans = 1, count = 2;
ans += count++;
int p = 10, q = 5;
p = ++q + 3;
int x = 2, y = 5;
y += 3;
x *= y--;
System.out.println (
"ans is " + ans);
System.out.println (
"count is " + count);
System.out.println (
"p is " + p);
System.out.println ( "q is
" + q);
System.out.println ( "x is " + x);
System.out.println ( "y is
" + y);
}
}
Activity
D
Activity D |
Determine the result of each of the
following expressions.
-
6 + 3 * 4 /
2
-
(6 + 3) * 4 /
2
-
-5 * 4 - 6 /
2
-
-5 * (4 - 6) /
2
Why not check your answers
by using code.
|
To
check your answers; try the following code:-
import
java.awt.*;
public class ActivityD {
public static void main (String[] args) {
System.out.println (
"6 + 3 * 4 /
2 = " + (6 + 3 * 4 /
2));
System.out.println (
"(6 + 3) * 4 /
2 = " + ((6 + 3) * 4 /
2));
System.out.println (
"-5 * 4 - 6 /
2 = " + (-5 * 4 - 6 /
2));
System.out.println ( "-5 * (4 - 6) /
2 = " + (-5 * (4 - 6) /
2));
}
}
Activity
E
Activity E |
Given the declaration
byte b2 = 65;
int i, j = 1000;
float f1, f2 = 5.2F;
char ch;
For each of the following, state the final
type and value of the operand on the left side of the =.
-
i = j + b1;
-
(int)f2;
-
f1 = (int) f2 +
10;
-
i = (int) f2 *
(byte)10.34;
-
ch = (char) (b1 +
3);
Why not check your answers
by using code. |
To
check your answers; try the following code:-
import java.awt.*;
public class ActivityE {
public static void main (String[] args) {
byte b1 = 65;
int i, j = 1000;
float f1, f2 = 5.2F;
char ch;
i = j + b1;
f1 = (int) f2 + 10;
System.out.println ( "i = " + i);
System.out.println ( "(int)f2 = " + (int)f2);
System.out.println ( "f1 = " + f1);
i = (int) f2 * (byte)10.34;
ch = (char) (b1 + 3);
System.out.println ( "i = " + i);
System.out.println ( "ch = " + ch);
}
}
That is folks!!
|