3.12. Programming Exercises

 
[Page 89 ( continued )]

Review Questions

Section 3.2 boolean Data Type and Operations

3.1 List six comparison operators.
3.2 Assume that x is 1 , show the result of the following Boolean expressions:
 (   true   ) && (   3   >   4   ) !(x >     ) && (x >     ) (x >     )  (x <     ) (x !=     )  (x ==     ) (x >=     )  (x <     ) (x !=   1   ) == !(x ==   1   ) 

3.3 Write a Boolean expression that evaluates to true if a number stored in variable num is between 1 and 100 .

[Page 90]
3.4 Write a Boolean expression that evaluates to true if a number stored in variable num is between 1 and 100 or the number is negative.
3.5 Assume that x and y are int type. Which of the following are correct Java expressions?
 x > y >     x = y && y x /= y x or y x and y (x !=     )  (x =     ) 

3.6 Can the following conversions involving casting be allowed? If so, find the converted result.
   boolean   b =   true   ; i = (int)b;   int   i =   1   ; boolean b = (   boolean   )i; 

3.7 Suppose that x is 1. What is x after the evaluation of the following expression?
 (x >   1   ) & (x++ >   1   ) 

3.8 Suppose that x is 1. What is x after the evaluation of the following expression?
 (x >   1   ) && (x++ >   1   ) 

3.9 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(++y);     System.out.println(y++);     System.out.println(x > y);     System.out.println(x - y);   } } 

Section 3.3 if Statements

3.10 Suppose x = 3 and y = 2 , show the output, if any, of the following code. What is the output if x = 3 and y = 4 ? What is the output if x = 2 and y = 2 ? Draw a flowchart of the following code:
   if   (x >   2   ) {   if   (y >   2   ) {   int   z = x + y;     System.out.println(   "z is "   + z);   } }   else   System.out.println(   "x is "   + x); 

3.11 Which of the following statements are equivalent? Which ones are correctly indented?
[Page 91]
3.12 Suppose x = 2 and y = 3 , show the output, if any, of the following code. What is the output if x = 3 and y = 2 ? What is the output if x = 3 and y = 3 ? (Hint: please indent the statement correctly first.)
   if   (x >   2   )   if   (y >   2   ) {   int   z = x + y;     System.out.println(   "z is "   + z);   }   else   System.out.println(   "x is "   + x); 

3.13 Are the following two statements equivalent?

3.14 Which of the following is a possible output from invoking Math.random() ?
   323.4   ,   0.5   ,   34   ,   1.0   ,   0.0   ,   0.234   

3.15 How do you generate a random integer i such that less-than or equal to i < 20?

How do you generate a random integer i such that 10 less-than or equal to i < 20?

How do you generate a random integer i such that 10 less-than or equal to i less-than or equal to 50?

Section 3.4 switch Statements

3.16 What data types are required for a switch variable? If the keyword break is not used after a case is processed , what is the next statement to be executed? Can you convert a switch statement to an equivalent if statement, or vice versa? What are the advantages of using a switch statement?
3.17 What is y after the following switch statement is executed?
 x =   3   ; y =   3   ;   switch   (x +   3   ) {   case   6   :  y =   1   ;   default   : y +=   1   ;  } 


[Page 92]
3.18 Use a switch statement to rewrite the following if statement and draw the flowchart for the switch statement:
   if   (a ==   1   )   x +=   5   ;   else if   (a ==   2   )   x +=   10   ;   else if   (a ==   3   )   x +=   16   ;   else if   (a ==   4   )   x +=   34   ; 

Section 3.5 Conditional Expressions

3.19 Rewrite the following if statement using the conditional operator:
   if   (count %   10   ==     )   System.out.print(count +   "\n"   );   else   System.out.print(count +   " "   ); 

Section 3.6 Formatting Console Output and Strings

3.20 What are the specifiers for outputting a boolean value, a character, a decimal integer, a floating-point number, and a string?
3.21 What is wrong in the following statements?
 (a) System.out.printf(   "%5d %d"   ,   1   ,   2   ,   3   ); (b) System.out.printf(   "%5d %f"   ,   1   ); (c) System.out.printf(   "%5d %f"   ,   1   ,   2   ); 

3.22 Show the output of the following statements.
 (a) System.out.printf(   "amount is %f %e\n"   ,   32.32   ,   32.32   ); (b) System.out.printf(   "amount is %5.4f %5.4e\n"   ,   32.32   ,   32.32   ); (c) System.out.printf(   "%6b\n"   , (   1   >   2   )); (d) System.out.printf(   "%6s\n"   ,   "Java"   ); (e) System.out.printf(   "%-6b%s\n"   , (   1   >   2   ),   "Java"   ); (f) System.out.printf(   "%6b%-s\n"   , (   1   >   2   ),   "Java"   ); 

3.23 How do you create a formatted string?

Sections 3.7 “3.8

3.24 List the precedence order of the Boolean operators. Evaluate the following expressions:
   true     true   &&   false     true     true   &&   false     true     true   &   false   

3.25 Show and explain the output of the following code:
  1.  int i =     ; System.out.println(i + i + i++); System.out.println(i + ++i); 
  2.    int   i =     ; i = i + (i =   1   ); System.out.println(i); 

    [Page 93]
  3.    int   i =     ; i = (i =   1   ) + i; System.out.println(i); 
3.26 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 = (a =   3   ) + a; a = a + (a =   3   ); a += a + (a =   3   ); a =   5   +   5   *   2   % a; a =   4   +   1   +   4   *   5   % (++a +   1   ); d +=   1.5   *   3   + (++d); d -=   1.5   *   3   + d++; 

 


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