6.14. Review Questions

 
[Page 202]

Review Questions

Section 6.2 Array Basics

6.1 How do you declare and create an array?
6.2 How do you access elements of an array?
6.3 Is memory allocated when an array is declared? When is the memory allocated for an array? What is the printout of the following code?
   int   x =   30   ;   int   [] numbers =   new int   [x]; x =   60   ; System.out.println(   "x is "   + x); System.out.println(   "The size of numbers is "   + numbers.length); 

6.4 Indicate true or false for the following statements:
  • Every element in an array has the same type.

  • The array size is fixed after it is declared.

  • The array size is fixed after it is created.

  • The elements in an array must be of primitive data type.

6.5 Which of the following statements are valid array declarations?
   int   i =   new int   (   30   );   double   d[] =   new double   [   30   ];   char   [] r =   new char   (   1..30   );   int   i[] = (   3   ,   4   ,   3   ,   2   );   float   f[] = {   2.3   ,   4.5   ,   6.6   };   char   [] c =   new char   (); 

6.6 What is the array index type? What is the lowest index?
6.7 What is the representation of the third element in an array named a ?
6.8 What happens when your program attempts to access an array element with an invalid index?
6.9 Identify and fix the errors in the following code:
 1   public class   Test {  2   public static void   main(String[] args) {  3   double   [   100   ] r;  4  5   for   (   int   i =     ; i < r.length(); i++);  6       r(i) = Math.random *   100   ;  7   }  8 } 

Section 6.3 Copying Arrays

6.10 Use the arraycopy() method to copy the following array to a target array t :
   int   [] source = {   3   ,   4   ,   5   }; 

6.11 Once an array is created, its size cannot be changed. Does the following code resize the array?
   int   [] myList; myList = new   int   [   10   ];  // Some time later you want to assign a new array to myList  myList = new   int   [   20   ]; 


[Page 203]

Sections 6.4 “6.5

6.12 When an array is passed to a method, a new array is created and passed to the method. Is this true?
6.13 Show the output of the following two programs:
6.14 Where are the arrays stored during execution? Show the contents of the stack and heap during and after executing createArray , displayArray , countLetters , displayCounts in Listing 6.4,

Section 6.6 Variable-Length Argument Lists

6.15 What is wrong in the following method declaration?
   public static void   print(String... strings,   double   ... numbers)   public static void   print(   double   ... numbers, String name)   public static double   ... print(   double   d1,   double   d2) 

6.16 Can you invoke the printMax method in Listing 6.5 using the following statements?
 printMax(   1   ,   2   ,   2   ,   1   ,   4   ); printMax(   new double   []{   1   ,   2   ,   3   }); printMax(   new int   []{   1   ,   2   ,   3   }); 

Sections 6.7 “6.8

6.17 Use Figure 6.11 as an example to show how to apply the binary search approach to a search for key 10 and key 12 in list { 2 , 4 , 7 , 10 , 11 , 45 , 50 , 59 , 60 , 66 , 69 , 70 , 79 }.
6.18 Use Figure 6.12 as an example to show how to apply the selection sort approach to sort { 3.4 , 5 , 3 , 3.5 , 2.2 , 1.9 , 2 }.
6.19 Use Figure 6.13 as an example to show how to apply the insertion sort approach to sort { 3.4 , 5 , 3 , 3.5 , 2.2 , 1.9 , 2 }.
6.20 How do you modify the selectionSort method in Listing 6.8 to sort numbers in decreasing order?
6.21 How do you modify the insertionSort method in Listing 6.9 to sort numbers in decreasing order?

[Page 204]

Section 6.9 The Arrays Class

6.22 What types of array can be sorted using the java.util.Arrays.sort method? Does this sort method create a new array?
6.23 To apply java.util.Arrays.binarySearch(array, key) , should the array be sorted in increasing order, in decreasing order, or either?
6.24 Show the contents of the array after the execution of each line.
   int   [] list = {   2   ,   4   ,   7   ,   10   }; java.util.Arrays.fill(list,   7   ); java.util.Arrays.fill(list,   1   ,   3   ,   8   ); System.out.print(java.util.Arrays.equals(list, list)); 

Section 6.10 Two-Dimensional Arrays

6.25 Declare and create a 4 x 5 int matrix.
6.26 Can the rows in a two-dimensional array have different lengths?
6.27 What is the output of the following code?
   int   [][] array =   new int   [   5   ][   6   ];   int   [] x = {   1   ,   2   }; array[     ] = x; System.out.println(   "array[0][1] is "   + array[     ][   1   ]); 

6.28 Which of the following statements are valid array declarations?
   int   [][] r =   new int   [   2   ];   int   [] x =   new int   [];   int   [][] y =   new int   [   3   ][]; 

 


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