+ All Categories
Home > Documents > Chapter2 REVIEW QUESTIONS

Chapter2 REVIEW QUESTIONS

Date post: 26-Dec-2015
Category:
Upload: adityascribd
View: 343 times
Download: 16 times
Share this document with a friend
Description:
About Alghoritm and Java programming.
19
REVIEW QUESTIONS Sections 2.2–2.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?
Transcript
Page 1: Chapter2 REVIEW QUESTIONS

REVIEW QUESTIONSSections 2.2–2.72.1 Which of the following identifiers are valid? Which are Java keywords?

applet, Applet, a++, ––a, 4#R, $4, #44, appsclass, public, int, x, y, radius

Valid identifiers Invalid identifiersApplet , 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 andMILES_PER_KILOMETER, and assign the result to kilometers.■ Step 4: Display kilometers to the console.What is kilometers after Step 4?

Page 2: Chapter2 REVIEW QUESTIONS

2.3 What are the benefits of using constants? Declare an int constant SIZE with value 20.

1. You don’t 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 code3. A descriptive name for a constant makes the program easy to read.

Sections 2.8–2.102.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 Resulta = 46 / 9; 5a = 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.0d += 1.5 * 3 + (++a); 7.5d -= 1.5 * 3 + a++; -4.5

Must be declared and initialized in the same statement

Constants named in uppercase

Page 3: Chapter2 REVIEW QUESTIONS

2.5 Show the result of the following remainders.

% output takes the sign of the numeratorRemainder Result 56 % 6 278 % -4 2-34 % 5 -4-34 % -5 -45 % 1 01 % 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

Page 4: Chapter2 REVIEW QUESTIONS

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 wishedthe 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 –27 (-128) to 27–1 (127) 8-bit signed

short –215 (-32768) to 215–1 (32767) 16-bit signed

int –231 (-2147483648) to 231–1 (2147483647) 32-bit signed

long –263 to 263–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

Page 5: Chapter2 REVIEW QUESTIONS

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.

Page 6: Chapter2 REVIEW QUESTIONS

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 67 System.out.println("j is " + j + " and8 k is " + k);9 }10 }

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

Section 2.112.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?

Page 7: Chapter2 REVIEW QUESTIONS

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);

Page 8: Chapter2 REVIEW QUESTIONS

Section 2.132.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

Page 9: Chapter2 REVIEW QUESTIONS

Extra work to verify results

Page 10: Chapter2 REVIEW QUESTIONS

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

Page 11: Chapter2 REVIEW QUESTIONS

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;

Page 12: Chapter2 REVIEW QUESTIONS

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

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

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

Page 13: Chapter2 REVIEW QUESTIONS

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);}}

Page 14: Chapter2 REVIEW QUESTIONS

Section 2.152.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);

Page 15: Chapter2 REVIEW QUESTIONS

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

Page 16: Chapter2 REVIEW QUESTIONS

Sections 2.16–2.172.27 What are the naming conventions for class names, method names, constants, andvariables? 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 methodpublic 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.

Page 17: Chapter2 REVIEW QUESTIONS

Section 2.182.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?


Recommended