You are on page 1of 2

TM105

Exercises on Meeting 3

Q1. Given the following declarations: int i; double d;

What are the values of i and d after executing each of the following Java statements?

1. i = 4 + 6 / 2;
2. i = (4+6) / 2;
3. i = 8 + 4 / 4 – 2;
4. i = 16 / 8 / 2;
5. i = 12 / 2 * 3;
6. i = 6 % 3;
7. d = 10 / 4 + 1;
8. d = 1 / 4 + 3 / 4;
9. d = 10.0 / 4 + 1;
10. d = 1.0 / 4 + 3.0 / 4;
11. d = 10 / 4 + 1.5;
12. d = (double) 5 / 2;
13. i = 4 % 6;
14. i = 1 + 7 % 2;
15. i = 10 % 2 % 3;
16. d = (6 + 2 * (3 / 2)) / 10;
17. i = (int) 8.6;

Q2: Write Java programs to do the following:

a) Prompt the user to enter an integer and display on the screen a message if this integer
is divisible by both 2 and 3 or not.

b) Prompt the user to enter 2 positive integers and displays on the screen a message if the
first integer is a multiple of the second one or not.

c) Prompt the user to enter a word and displays on the screen a message if this word starts
with the letter 'A'/'a' (lower case or uppercase).

1
d) Print the academic status of AOU students as follows:
- "Regular" if the GPA ≥ 2
- "Warned" if the GPA < 2 and number of warnings received < 5
- "Dismissed" if the GPA < 2 and number of warnings received = 5

The program should start by reading the GPA of a student as a real number. If the GPA
is 2 or more, the program should not read the number of warnings. Otherwise, the
program should read the number of warnings and print the academic status as indicated
above.

e) Modify the above program so that it checked the validity on the GPA. If the user enters
a GPA that is less than 0 or more that 4, the program should print error message.
Otherwise, print the academic status of the student as before.

Q3. What is the exact output of the following piece of code?

int w = 7, z = 10;
if (w <= 7 && z != 10) System.out.println("ONE");
if (w <= 7 || z != 10) System.out.println("TWO");
if (w <= 7 ^ z == 10) System.out.println("THREE");
if (!(w <= 7 && z != 10)) System.out.println("FOUR");
if (w == 8); System.out.println("FIVE");
if (z == 10)
System.out.println("YES");
else;
System.out.println("OK");
if (w != 7)
System.out.println("WWW");
if (z % 5 == 0)
System.out.println("ZZZ");
else
System.out.println("AAA");

You might also like