Anda di halaman 1dari 17

REVIEW QUESTIONS

Sections 2.22.7
2.1 Which of the following identifiers are valid? Which are Java keywords?

applet, Applet, a++, a, 4#R, $4, #44, apps
class, public, int, x, y, radius

Valid identifiers Invalid identifiers
Applet , applet ,$4, x , y , radius, apps

a++ , --a,4#R , #44, class, public, int ,

Java keywords: class, public, int

Example of error in NetBeans when you use invalid identifiers:



2.2 Translate the following algorithm into Java code:
Step 1: Declare a double variable named miles with initial value 100;
Step 2: Declare a double constant named MILES_PER_KILOMETER with
value1.609;
Step 3: Declare a double variable named kilometers, multiply miles and
MILES_PER_KILOMETER, and assign the result to kilometers.
Step 4: Display kilometers to the console.
What is kilometers after Step 4?


2.3 What are the benefits of using constants? Declare an int constant SIZE with value
20.
1. You dont have to repeatedly type the same value.
2. If you have to change the constant value, you need to change it only in a single location in the source
code
3. A descriptive name for a constant makes the program easy to read.




Sections 2.82.10
2.4 Assume that int a = 1 and double d = 1.0, and that each expression is
independent.
What are the results of the following expressions?
Expression Result
a = 46 / 9; 5
a = 46 % 9 + 4 * 4 - 2; 15//% in the same level of *
a = 45 + 43 % 5 * (23 * 3 % 2); 48
a %= 3 / a + 3; 1//%assignment at the end
d = 4 + d * d + 4; 9.0
d += 1.5 * 3 + (++a); 7.5
d -= 1.5 * 3 + a++; -4.5

Must be declared and initialized in the same
statement
Constants named in uppercase

2.5 Show the result of the following remainders.
% output takes the sign of the numerator
Remainder Result
56 % 6 2
78 % -4 2
-34 % 5 -4
-34 % -5 -4
5 % 1 0
1 % 5 1

2.6 If today is Tuesday, what will be the day in 100 days?

Day1: Monday, Day2: Tuesday, Day3: Wednesday,
Day4: Thursday, Day5:Firday, Day6:Saturday, Day7: Sunday

(2+100)%7
2: today Tuesday is the second day
100: after 100
7: a week has 7 days
The result is 4 Thursday









2.7 Find the largest and smallest byte, short, int, long, float, and double.
Which of these data types requires the least amount of memory?






























2.8 What is the result of 25 / 4? 6

How would you rewrite the expression if you wished
the result to be a floating-point number? 25.0 / 4 OR 25.0 /4.0 OR 25/4.0


2.9 Are the following statements correct? If so, show the output.
System.out.println("25 / 4 is " + 25 / 4);
System.out.println("25 / 4.0 is " + 25 / 4.0);
System.out.println("3 * 2 / 4 is " + 3 * 2 / 4);
System.out.println("3.0 * 2 / 4 is " + 3.0 * 2 / 4);


Name Range Storage Size

byte 2
7
(-128) to 2
7
1 (127) 8-bit signed

short 2
15
(-32768) to 2
15
1 (32767) 16-bit signed

int 2
31
(-2147483648) to 2
31
1 (2147483647) 32-bit signed

long 2
63
to 2
63
1 64-bit signed
(i.e., -9223372036854775808
to 9223372036854775807)
float Negative range: 32-bit IEEE 754
-3.4028235E+38 to -1.4E-45
Positive range:
1.4E-45 to 3.4028235E+38

double Negative range: 64-bit IEEE 754
-1.7976931348623157E+308 to
-4.9E-324
Positive range:
4.9E-324 to 1.7976931348623157E+308




2.10 How would you write the following arithmetic expression in Java?


4/(3*(r+34))- 9*(a+b*c)+(3+d*(2+a)/(a+b*d));

2.11 Suppose m and r are integers. Write a Java expression for mr2 to obtain a
floatingpoint result.

m*r*2.0

2.12 Which of these statements are true?
(a) Any expression can be used as a statement.
(b) The expression x++ can be used as a statement.
(c) The statement x = x + 5 is also an expression.
(d) The statement x = y = x = 0 is illegal.


2.13 Which of the following are correct literals for floating-point numbers?
12.3, 12.3e+2, 23.4e-2, 334.4, 20, 39F, 40D

2.14 Identify and fix the errors in the following code:
1 public class Test {
2 public void main(string[] args) {
3 int i;
4 int k = 100.0;//K=100;
5 int j = i + 1; // i has to be initialized
6
7 System.out.println("j is " + j + " and
8 k is " + k);
9 }
10 }

2.15 How do you obtain the current minute using the System.currentTimeMillis()
method?



Section 2.11
2.16 Can different types of numeric values be used together in a computation?
Yes

2.17 What does an explicit conversion from a double to an int do with the fractional
part of the double value?

Does casting change the variable being cast?























2.18 Show the following output.
float f = 12.5F;
int i = (int)f;
System.out.println("f is " + f);
System.out.println("i is " + i);
























Section 2.13
2.19 Use print statements to find out the ASCII code for '1', 'A', 'B', 'a', 'b'.




Use print statements to find out the character for the decimal code 40, 59, 79, 85, 90.


Wrong result because it has to be:
int x='1';
System.out.println(x);
The output is 49 not 1





Extra work to verify results

























Use print statements to find out the character for the hexadecimal code
40, 5A, 71, 72, 7A.



2.20 Which of the following are correct literals for characters?
'1', '\u345dE', '\u3fFa', '\b', \t


2.21 How do you display characters \ and "?


2.22 Evaluate the following:
int i = '1';
int j = '1' + '2';
int k = 'a';
char c = 90;


2.23 Can the following conversions involving casting be allowed? If so, find the
converted result.
char c = 'A';
i = (int)c;




float f = 1000.34f;
int i = (int)f;



double d = 1000.34;
int i = (int)d;



int i = 97;
char c = (char)i;



2.24 Show the output of the following program:
public class Test {
public static void main(String[] args) {
char x = 'a';
char y = 'c';
System.out.println(++x);
System.out.println(y++);
System.out.println(x - y);
}
}





Section 2.15
2.25 Show the output of the following statements (write a program to verify your
result):
System.out.println("1" + 1);
System.out.println('1' + 1);
System.out.println("1" + 1 + 1);
System.out.println("1" + (1 + 1));
System.out.println('1' + 1 + 1);






2.26 Evaluate the following expressions (write a program to verify your result):
1 + "Welcome " + 1 + 1
1 + "Welcome " + (1 + 1)
1 + "Welcome " + ('\u0001' + 1)
1 + "Welcome " + 'a' + 1


Sections 2.162.17
2.27 What are the naming conventions for class names, method names, constants, and
variables? Which of the following items can be a constant, a method, a variable, or a
class according to the Java naming conventions?

MAX_VALUE, Test, read, readInt

2.28 Reformat the following program according to the programming style and
documentation guidelines. Use the next-line brace style.
public class Test
{
// Main method
public static void main(String[] args) {
/** Print a line */
System.out.println("2 % 3 = "+2%3);
}
}

2.29 Describe syntax errors, runtime errors, and logic errors.





Section 2.18
2.30 Why do you have to import JOptionPane but not the Math class?
2.31 How do you prompt the user to enter an input using a dialog box?
2.32 How do you convert a string to an integer? How do you convert a string to a
double?

Anda mungkin juga menyukai