Java Reference
In-Depth Information
7.3
What is the output 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);
7.4
Indicate true or false for the following statements:
Every element in an array has the same type.
The array size is fixed after an array reference variable is declared.
The array size is fixed after it is created.
The elements in an array must be a primitive data type.
7.5
Which of the following statements are valid?
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 ();
7.6
How do you access elements in an array?
7.7
What is the array index type? What is the lowest index? What is the representation of
the third element in an array named a ?
7.8
Write statements to do the following:
a. Create an array to hold 10 double values.
b. Assign the value 5.5 to the last element in the array.
c. Display the sum of the first two elements.
d. Write a loop that computes the sum of all elements in the array.
e. Write a loop that finds the minimum element in the array.
f. Randomly generate an index and display the element of this index in the array.
g. Use an array initializer to create another array with the initial values 3.5 , 5.5 ,
4.52 , and 5.6 .
7.9
What happens when your program attempts to access an array element with an inva-
lid index?
7.10
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 = 0 ; i < r.length(); i++);
6 r(i) = Math.random * 100 ;
7 }
8 }
7.11
What is the output of the following code?
1 public class Test {
2
public static void main(String[] args) {
3
int list[] = { 1 , 2 , 3 , 4 , 5 , 6 };
 
Search WWH ::




Custom Search