Java
TOPIC
07 – ARITHMETIC EXPRESSIONS
AP EXTRA
AP LESSON WORK
QUESTION
1 – FOCUS ON ++ OPERATOR
What will be outputted by each of the
following programs?
You are strongly recommended to paste these programs into Eclipse and test
them to see if you are correct. But be
sure to right down your answers first to see how well you understand.
a)
int a = 9;
System.out.println(a++);
System.out.println(a);
|
b)
int a = 5;
int b = 2 + a--;
System.out.println(b + "
" + a);
|
c)
int a = 1;
int b = a++;
int c = b++;
int d = c++;
int e = d++;
System.out.println(a + b + c + d + e);
|
d)
double a = 100;
a--;
System.out.println(a/100);
|
e)
double num
= 3;
double den = num-- + 1;
double ans = num / den++;
System.out.println(ans++);
|
QUESTION 2 – FOCUS ON += OPERATOR
What will be outputted by each of the
following programs?
You are strongly recommended to paste these programs into Eclipse and test
them to see if you are correct. But be
sure to right down your answers first to see how well you understand.
a)
int coco = 10;
coco += 5;
System.out.println(coco);
|
b)
int coco = 10;
coco *= 2;
coco -= 1;
System.out.println(coco);
|
c)
int zerg = 10;
zerg /= 2;
zerg %= 2;
System.out.println(zerg);
|
d)
int num1 = 5;
int num2 = 3;
num1 += num2;
System.out.println(num1);
|
e)
int num1 = 5;
int num2 = 3;
int num3 = 2;
num1 *= num2 + num3;
System.out.println(num1);
|
QUESTION 3
What will be outputted by each of the
following programs? Note that these
programs contain statements that are not good form as they are over the top difficult
to follow.
You are strongly recommended to paste these programs into Eclipse and test
them to see if you are correct. But be
sure to right down your answers first to see how well you understand.
a)
int a = 2;
int b = 3;
int c = a++ + b--;
int d = a + b + c;
System.out.println(d);
|
b)
int a = 2;
int b = 3;
a += b++;
b *= b++;
System.out.println(b);
|
c)
int x = 0;
int y = 1;
x += y--;
y += x++;
System.out.println(x + y);
|
d)
int x = 1;
int y = 1;
int z = 1;
z++;
y += z;
z *= x++;
x = x-1;
System.out.println("" + x + y + z);
|
e)
int x = 1;
int y = 2;
int z = 3;
z /= y;
x += y-z;
System.out.println(x);
|
|