0% found this document useful (0 votes)
13 views2 pages

03tracing Arrays Practice DaysOfWeek

The document discusses arrays in Java code. It provides an example program that declares and prints the days of the week. It asks questions about the example code and its output. It also provides exercises to modify and trace code involving arrays.

Uploaded by

xxfloraxa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

03tracing Arrays Practice DaysOfWeek

The document discusses arrays in Java code. It provides an example program that declares and prints the days of the week. It asks questions about the example code and its output. It also provides exercises to modify and trace code involving arrays.

Uploaded by

xxfloraxa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Name:__________

Date: __________
Days of the Week – Basic Arrays
1. Look at the following program.

public class PrintDaysOfWeek


{
public static void main (String args[])
{
String DaysOfWeek[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday"};

System.out.println ("Printing one element at a time:");


System.out.println (DaysOfWeek [0]);
System.out.println (DaysOfWeek [1]);
System.out.println (DaysOfWeek [2]);
System.out.println (DaysOfWeek [3]);
System.out.println (DaysOfWeek [4]);
System.out.println (DaysOfWeek [5]);
System.out.println (DaysOfWeek [6]);

System.out.println ("\n\nPrinting all elements:");


for (int i = 0 ; i < DaysOfWeek.length ; i++)
{
System.out.println (DaysOfWeek [i]);
}
System.out.println ("\nArray Length: " + DaysOfWeek.length);
}
}

Answer these questions about the code:

a. What is the array type?

b. What is the array name?

c. What is the array length?

d. What is DaysOfWeek.length?

e. What is in DaysOfWeek [4]?

f. Why does DaysOfWeek[9] cause an


error?

g. Circle the two different ways to print the elements of an array.


Label one with "first way" and the second with "second way".
Which way is easier if the array contains hundreds or more elements? (The first or the second)
Why is it easier?
2. Consider this array.
String DaysOfWeek[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday"};

(a) Change each Boolean expression so that it is true.

i. DaysOfWeek[1].equals(“Sunday”)
ii. DaysOfWeek[7].equals(“Saturday”)
iii. DaysOfWeek.length==6
iv. DaysOfWeek[0]== “Sunday”
v. WeekDays[3].equals(“Tuesday”)

(b) Correct each declaration and construction so that the code runs.

i. String DaysOfWeek = {"Sunday", "Monday", "Tuesday", "Wednesday",


"Thursday", "Friday", "Saturday"};
ii. String DaysOfWeek[] = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"}
iii. String DaysOfWeek[] = {‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’,
‘Thursday’, ‘Friday’, ‘Saturday’};
iv. String DaysOfWeek[] = ("Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday");
v. String DaysOfWeek{} = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};

3. Trace what is happening in the following array, using the tables provided.

int s = 2;
int a [] = {-1, -3, 5, 31, 0, 7};
[0] [1] [2] [3] [4] [5]

a [4] =2;
[0] [1] [2] [3] [4] [5]

a [5] = 88;
[0] [1] [2] [3] [4] [5]

a [s] --;
[0] [1] [2] [3] [4] [5]

a [0] = a [3];
[0] [1] [2] [3] [4] [5]

a[3] = s + 2;
[0] [1] [2] [3] [4] [5]

a[1] ++;
[0] [1] [2] [3] [4] [5]

You might also like