Java Reference
In-Depth Information
14.
What is an array index out-of-bound? Does Java check for array indices
within bound?
15.
Suppose that
scores
is an array of 10 components of type
double
, and
scores = {2.5, 3.9, 4.8, 6.2, 6.2, 7.4, 7.9, 8.5, 8.5, 9.9}
The following is supposed to ensure that the elements of
scores
are in
nondecreasing order. However, there are errors in the code. Find and
correct the errors.
for
(
int
i = 1; i <= 10; i++)
if
(scores[i] >= scores[i + 1])
System.out.println(i + " and " + (i + 1)
+ " elements of scores are out of order.");
16.
Write Java statements to define and initialize the following arrays.
a.
Array
heights
of 10 components of type
double
. Initialize this
array to the following values:
5.2
,
6.3
,
5.8
,
4.9
,
5.2
,
5.7
,
6.7
,
7.1
,
5.10
,
6.0
.
b.
Array
weights
of 7 components of type
int
. Initialize this array to the
following values:
120
,
125
,
137
,
140
,
150
,
180
,
210
.
c.
Array
specialSymbols
of
type
char
. Initialize this array to the
following values:
'$'
,
'#'
,
'%'
,
'@'
,
'&'
,
'! '
,
'^'
.
d.
Array
seasons
of 4 components of type
String
. Initialize this array to
the following values:
"fall"
,
"winter"
,
"spring"
,
"summer"
.
17.
Determine whether the following array declarations are valid.
9
a.
int
[] a
¼
{0, 4, 3, 2, 7};
b.
int
[10] b
¼
{0, 7, 3, 12};
c.
int
[] c
¼
{12, 13, , 14, 16, , 8};
d.
double
[] lengths
¼
{12.7, 13.9, 18.75, 20.78};
18.
Suppose that you have the following declaration:
int
[] list = {8, 9, 15, 12, 80};
What is stored in each components of
list
?
19.
What is the output of the following code?
int
[] list ={6, 8, 2, 14, 13};
for
(
int
i = 0; i < 4; i++)
list
[i] = list[i] - list[i + 1];
for
(
int
i = 0; i < 5; i++)
System.out.println(i + " " + list[i]);
Search WWH ::
Custom Search