Chapter 3 Progress Checks
1. Why are the following declarations all invalid?
int a, b, a;
float x, int;
float w, int x;
bigNumber double;
2. Assuming the following declarations are executed in sequence, why are the second and third declarations invalid?
int a, b;
int a;
float b;
3. Name six data types for numerical values.
4. Which of the following valid assignment statements (assuming the variables are properly declared)?
X= 12;
12= x;
y + y= x;
y= x + 12;
5. Evaluate the following expressions:
3 + 2 / 5 + -2 * 4
2 * (1 + -(3 / 4) / 2) * (2 - 6 % 3)
6. What is the data type of the result of the following expressions?
(3 + 5) / 7
(3 + 5) / (float) 7
(float) ((3 + 5) / 7)
7. Which of the following expressions is equivalent to
-b (c + 34)
--------
2a
a. -b * (c + 34) / 2 * a
b. -b * (c + 34) / (2 * a)
c. -b * c + 34 / (2 * a)
8. What's wrong with the following?
int age = 10;
outputBox.show(age);
9. What's wrong with the following?
int age = 20;
outputBox.show("Your age is age");
10. Will the following code work:
age = 20;
messageBox.show("Your age is " + age);
11. Draw the state-of-memory diagram for the following code:
Account latteAcct, espressoAcct;
latteAcct= new Account();
espressoAcct= new Account();
latteAcct= espressoAcct;
12. Suppose there are the following declartions:
int i= 3, j= 4, k= 5;
float x= 34.5f, y= 12.25f;
Determine the value for each of the following expressions or explain why it is not a valid expression.
+ 1.5) / (250.0 * (i / j))
x + 1.5 / 250.0 * i / j
-x * -y * (i + j) / k
Math.min(i, Math.min(j,k))
Math.exp(3, 2)
(int) y % x
Math.pow(3, 2)
i / 5 * y
13. Suppose we have th following declarations
int m, n, i= 3, j= 4, k= 5;
float v, w, x= 34.5f, y=12.25f;
Determine the value for each of the following expressions or explain why it is not a valid expression.
w= Math.pow(3, Math.pow(i, j));
v= x / i;
w= Math.ceil(y) % k;
n= (int) x / y * i / 2;
x= Math.sqrt(i*i - 4*j*k);
m= n + i * j;
n= k /(j * i) * x + y;
i= i + 1;
w= float(x + i);
x= x / i / y / j;
14. Suppose we have the following declarations:
int i, j;
float s, y;
double u, v;
Which of the following assignments are valid?
i= x;
x= u + y;
x= 23.4 + j * y;
v= (int) x;
y= j / i * x;
15. Write the Java expressions to computer the following:
The square root of B^2 + 4AC (A and C are distinct variables).
The square root of X + 4Y^3.
The cube root of the product of X and Y.
The area Pi*R^2 of a circle.
A sinC / sin A.
16. Determine the output of the following program without running it:
/*
Program TestOutputBox
*/
import javabook.*;
class TestOutputBox {
public static void main(String args[]) {
MainWindow mainWindow;
OutputBox outputBox;
mainWindow= new MainWindow("Program TestOutputBox");
outputBox= new OutputBox(mainWindow);
mainWindow.setVisible(true);
outputBox.setVisible(true);
outputBox.printLine("one");
outputBox.print("Two");
outputBox.skipLine(1);
outputBox.print("Three");
outputBox.printLine("Four");
outputBox.skipLine(1);
outputBox.print("Five");
outputBox.printLine("Six");
}
}
17. Determine the output of the following code:
int x, y;
x= 1;
y= 2;
messageBox.show("The output is " + x + y);
messageBox.show("The output is " + (x + y));