2.20. Programming Exercises

 
[Page 60 ( continued )]

Review Questions

Sections 2.22.6

2.1 Which of the following identifiers are valid?

applet , Applet , a++ , a , 4#R , $4 , #44 , apps

2.2 Which of the following are Java keywords?

class , public , int , x , y , radius

2.3 Translate the following pseudocode into Java code:
  • Step 1: Declare a double variable named miles with initial value 100 ;

  • Step 2: Declare a double constant named MILE_TO_KILOMETER with value 1.609 ;

  • Step 3: Declare a double variable named kilometer , multiply miles and MILE_TO_KILOMETER and assign the result to kilometer ;

  • Step 4: Display kilometer to the console.

What is kilometer after Step 4?

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

Section 2.7 Numeric Data Types and Operations

2.5 Assume that int a = 1 and double d = 1.0 , and that each expression is independent. What are the results of the following expressions?
 a =   46   /   9   ; a =   46   %   9   +   4   *   4   -   2   ; a =   45   +   43   %   5   * (   23   *   3   %   2   ); 

[Page 61]
 a %=   3   / a +   3   ; d =   4   + d * d +   4   ; d +=   1.5   *   3   + (++a); d -=   1.5   *   3   + a++; 

2.6 Show the result of the following remainders.
   56   %   6     78   %   -4     -34   %   5     -34   %   -5     5   %   1     1   %   5   

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 ? How would you rewrite the expression if you wished the result to be a floating-point number?
2.9 Are the following statements correct? If so, show the output.
 System.out.println(   "the output for 25 / 4 is "   +   25 / 4   ); System.out.println(   "the output for 25 / 4.0 is "   +   25 / 4.0   ); 

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


2.11 Which of these statements are true?
  1. Any expression can be used as a statement.

  2. The expression x++ can be used as a statement.

  3. The statement x = x + 5 is also an expression.

  4. The statement x = y = x = 0 is illegal.

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

2.13 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   ; 5   int   j = i +   1   ; 6 7 System.out.println(   "j is "   + j +   " and   8   k is "   + k); 9 } 10 } 

Section 2.8 Numeric Type Conversions

2.14 Can different types of numeric values be used together in a computation?
2.15 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.16 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 62]

Section 2.9 Character Data Type and Operations

2.17 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 . Use print statements to find out the character for the hexadecimal code 40 , 5A , 71 , 72 , 7A .
2.18 Which of the following are correct literals for characters ?

'1' , '\u345dE' , '\u3fFa' , '\b' , \t

2.19 How do you display characters \ and " ?
2.20 Evaluate the following:
   int   i =   '1'   ;   int   j =   '1'   +   '2'   ;   int   k =   'a'   ;   char   c =   90   ; 

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

Section 2.10 The String Type

2.22 Show the output of the following statements:
 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.23 Evaluate the following expressions:
   1   +   "Welcome "   +   1   +   1     1   +   "Welcome "   + (   1   +   1   )   1   +   "Welcome "   + (   '\u0001'   +   1   )   1   +   "Welcome "   +   'a'   +   1   

Sections 2.112.12

2.24 How do you convert a decimal string into a double value? How do you convert an integer string into an int value?
2.25 How do you obtain the current minute using the System.currentTimeMillis() method?

Sections 2.142.15

2.26 How do you denote a comment line and a comment paragraph?
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   


[Page 63]
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.
 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net