Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

UNIT SAINS KOMPUTER

KOLEJ MATRIKULASI MELAKA

Group Kitkat Lollipop Marshmallow Nougat Oreo


Name
Class F1T2 F3T2 K1T2 Date

1.
(a) Trace the output.

int a = 12, b = 5;
// addition operator
System.out.println("a + b = " + (a + b));

// subtraction operator
System.out.println("a - b = " + (a - b));

// multiplication operator
System.out.println("a * b = " + (a * b));

// division operator
System.out.println("a / b = " + (a / b));

// modulus operator
System.out.println("a % b = " + (a % b));

a + b = 17
a-b=7
a * b = 60
a/b=2
a%b=2

(b) Given int a = 7, b = 11;

Identify whether the following Java statement is true or false.

(i) (a == b); false

(ii) (a != b); true

(iii) (a > b); false

(iv) (a < b); true

(v) (a >= b); false

(vi) (a <= b); true

1|
UNIT SAINS KOMPUTER
KOLEJ MATRIKULASI MELAKA

PSPM SC025 COLLECTIONS

SESSION 2018/2019

Learning Outcome (CLO1)


(d) Identify various operators (arithmetic, relational and logical)

1. Based on the given Java expressions, determine the operator type.

Java Expression Operator Type

(x < y) Relational

(x && y) Logical

x == y Relational

2|
UNIT SAINS KOMPUTER
KOLEJ MATRIKULASI MELAKA

LECTURE QUESTIONS
3.0: JAVA LANGUAGE
3.2 : DATA TYPE, OPERATOR AND EXPRESSIONS

Learning Outcome (CLO1)


(f) Determine the operator precedence and associativity of operators.

UPS FORMAT QUESTIONS

1.
(a) Examine the following Java segment code and give the output.

int i = 8, j = 5, k;
double z;

(i) k = 3 * ((i / 5) + (4 * (j - 3))% (i + j – 2));

k = 3 * ((8/5) + (4 * (5-3)) % (8 + 5 - 2))

k = 3 * ((8/5) + 8 % 11))

= 27

(ii) z = 2 * i / 5 + 4 * j – 3 % i + j – 2;

z=2*8/5+4*5–3%8+5–2

z = 16/5 +20 - 3 + 5-2

= 23.2

(b) Identify the output of the following segment code.

(i) int result = 4 + 5 * 6 + 2;


System.out.println (result);
36

(ii) int a = 5 + 7 % 2;
System.out.println (a);
6

3|
UNIT SAINS KOMPUTER
KOLEJ MATRIKULASI MELAKA

2.
(a) Assuming x is 5, y is 6, and z is 8, indicate whether each of the following relational
expressions is true or false:

Expressions Comparison value True/False

(i) x == 5 5==5 true

(ii) 7 <= (x + 2) 7<=(5+2) true

(iii) z > 4 8>4 true

(iv) (2 + x) != y (2+5)!=6 false

(v) z != 4 8!=4 true

(vi) x >= 0 5>=0 true

(vii) x <= (y * 2) 5<=(6*2) true

(b) Find the output for the Java statements below.


(i)

int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;

e = (a + b) * c / d;
System.out.println (“Value of (a + b) * c / d is : “ + e;

e = ((a + b) * c) / d;
System.out.println (“Value of ((a + b) * c) / d is : “ + e;

e = (a + b) * (c / d);
System.out.println (“Value of (a + b) * (c / d) is : “ + e;

e = a + (b * c) / d;
System.out.println (“Value of a + (b * c) / d is : “ + e;

Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is :90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is : 50

4|
UNIT SAINS KOMPUTER
KOLEJ MATRIKULASI MELAKA

(ii) What is the output for the following Java program segment?

int a = 5, b = 10;
int c;
c = a * 2 + b;
System.out.println (“c = “ + c );

c = 20

(iii) What is the output for the following Java program segment?

int a = 5, b = 10;
int c;
c = a * 2 + b/2;
System.out.println (“c = “ + c );

c = 15

(iv) Find the output for the following program segment.

int a = 5, b = 10;
int c;
c = ((a*2)+(b/2));
System.out.println (“c = “ + c );

C= 15

(v) What is the output for the following Java program segment?

int a = 7 * 3 + 24 / 3 - 5;
int b = (7 * 3) + (24 / 3) - 5;
int z;
z = a + b;
System.out.println (“value of a = “ + a );
System.out.println (“value of b = “ + b );
System.out.println (“value of z = “ + z );

value of a= 24
value of b=24
value of z=48

5|
UNIT SAINS KOMPUTER
KOLEJ MATRIKULASI MELAKA

PSPM SC025 COLLECTIONS

SESSION 2018/2019
Learning Outcome (CLO1)
(f) Determine the operator precedence and associativity of operators.

1. Given the following Java codes, determine the value of c and y.


int a=10, b=5;
double c=2, x=4.2, y=33, z=2.0;
c = a + b – x / c;
y = 56 % 5 / 2 + a;

c = 12.9

y = 10.0

SESSION 2019/2020
Learning Outcome (CLO1)
(f) Determine the operator precedence and associativity of operators.

1. Give the value of following Java expressions.

(i) true || false && 7 < 8 || !( 4==5 ) ;

true

(ii) (6 % 4) % 5 ;

12

[END OF QUESTIONS]

6|

You might also like