Exercises 5
a) Java operators are evaluated from left to right.
b) The following are all valid variable names: _under_bar_, m928134, t5, j7, her_sales$,
his_$account_total, a, b$, c, z and z2.
c) A valid Java arithmetic expression with no parentheses is evaluated from left to right.
d) The following are all invalid variable names: 3g, 87, 67h2, h22 and 2h.
2.10 Assuming that x = 2 and y = 3, what does each of the following statements display?
a) System.out.printf(“x = %d%n”, x);
b) System.out.printf(“Value of %d + %d is %d%n”, x, x, (x + x));
c) System.out.printf(“x =”);
d) System.out.printf(“%d = %d%n”, (x + y), (y + x));
2.11 Which of the following Java statements contain variables whose values are modified?
a) int p = i + j + k + 7;
b) System.out.println(“variables whose values are modified”);
c) System.out.println(“a = 5”);
d) int value = input.nextInt();
2.12 Given that y = ax3 + 7, which of the following are correct Java statements for this equation?
a) int y = a * x * x * x + 7;
b) int y = a * x * x * (x + 7);
c) int y = (a * x) * x * (x + 7);
d) int y = (a * x) * x * x + 7;
e) int y = a * (x * x * x) + 7;
f) int y = a * x * (x * x + 7);
2.13 State the order of evaluation of the operators in each of the following Java statements, and
show the value of x after each statement is performed:
a) int x = 7 + 3 * 6 / 2 – 1;
b) int x = 2 % 2 + 2 * 2 – 2 / 2;
c) int x = (3 * 9 * (3 + (9 * 3 / (3))));
2.19 What does the following code print?
System.out.printf(“*%n**%n***%n****%n*****%n”);
© 2018 Pearson Education, Inc., 330 Hudson Street, NY NY 10013. All rights reserved.