You are on page 1of 5

Array Exercises – Program Tracing & Coding

NAME: Chiara Marie C. Canque

1. What is the output of the following code?


public class MainProgram {

public static void main (String [] args) {

String [] strArr = new String[10];

for (int i = strArr.length - 1; i >= 0; i--)

strArr [i] = "b" + (i - 1);

System.out.println("Value : " + strArr[5]);

ANSWERS

Output: Value : b4

Program Tracing/Iteration Steps Executed. Show the step-by-step execution based on the elements or
components of the loop. Add more rows/columns, if necessary, for the given table below.

Iterate Value of i >= 0 Action i-- strArr[i]


variable i
1st 9 True 9 is decreased to 8 i=8 b8
2nd 8 True 8 is decreased to 7 i=7 b7
3rd 7 True 7 is decreased to 6 i=6 b6
4th 6 True 6 is decreased to 5 i=5 b5
5th 5 True 5 is decreased to 4 i=4 b4
6th 4 True 4 is decreased to 3 i=3 b3
7th 3 True 3 is decreased to 2 i=2 b2
8th 2 True 2 is decreased to 1 i=1 b1
9th 1 True 1 is decreased to 0 i=0 b0
10th 0 True 0 is decreased to -1 i = -1 b-1
11th -1 False Loop terminated Loop Loop
terminated terminated
Output:

System.out.println("Value : " + strArr[5]); //highlighted on the table


Array Exercises – Program Tracing & Coding

NAME: Chiara Marie C. Canque

2. What is the output of the following program?


public class MainProgram {
public static void main (String [] args)
{
double [] dblArr = {3.5, 6.8, 2.3, 9.1, 1.0};
for (int i = 0; i < dblArr.length; i++)
{
dblArr[i] = dblArr[i] / 2;
}

for (int i = dblArr.length - 1; i >= 0; i--)


{
System.out.println("Value : " + dblArr[i]);
}
}
}

ANSWERS

Output: Value : 0.5


Value : 4.55
Value : 1.15
Value : 3.4
Value: 1.75

Program Tracing/Iteration Steps Executed. Show the step-by-step execution of the first loop in the
program, based on the elements or components of the loop. Add more rows/columns, if necessary, for the
given table below.

Iterate Value of variable i<dblArr.length Action i++ dblArr[i]


i
1st 0 True 3.5 / 2 = 1.75 i=1 1.75
2nd 1 True 6.8 / 2 = 3.4 i=2 3.4
3rd 2 True 2.3 / 2 = 1.15 i=3 1.15
4th 3 True 9.1 / 2 = 4.55 i=4 4.55
5th 4 True 1.0 / 2 = 0.5 i=5 0.5
6th 5 False Loop terminated Loop Loop
terminated terminated

Output:
for (int i = dblArr.length - 1; i >= 0; i--)
{
System.out.println("Value : " + dblArr[i]); //highlighted on the table but the variable i
} starts with 4 then decrement
Array Exercises – Program Tracing & Coding

NAME: Chiara Marie C. Canque

3. What is the output of the following program?


public class MainProgram {
public static void main(String [] args ) {
int [] intArr = {1, 2, 3, 4, 5};
double [] dblArr = {.5, 1, 1.5, 2, 2.5};
for (int i = 0; i < intArr. length; i++)
{
dblArr [i] = dblArr [i] * intArr [i];
}

for (int i = 0; i < intArr.length; i++)


{
System.out.println(intArr[i] + ": " + dblArr[i]);
}
}
}

ANSWERS

Output: 1: 0.5
2: 2.0
3: 4.5
4: 8.0
5: 12.5

Program Tracing/Iteration Steps Executed. Show the step-by-step execution of the first loop in the
program, based on the elements or components of the loop. Add more rows/columns, if necessary, for the
given table below.

Iterate Value of i<intArr.length Action i++ intArr[i] dblArr[i]


variable i
1st 0 True .5 * 1 = 0.5 i=1 1 0.5
2nd 1 True 1 * 2 = 2.0 i=2 2 2.0
3rd 2 True 1.5 * 3 = 4.5 i=3 3 4.5
4th 3 True 2 * 4 = 8.0 i=4 4 8.0
5th 4 True 2.5 * 5 = i=5 5 12.5
12.5
6th 5 False Loop Loop Loop Loop
terminated terminated terminated terminated

Output:
for (int i = 0; i < intArr.length; i++)
{
System.out.println(intArr[i] + ": " + dblArr[i]); //highlighted on the table
}
Array Exercises – Program Tracing & Coding

NAME: Chiara Marie C. Canque

4. What is the output of the following program?


public class MainProgram {
public static void main(String [] args) {
int [] intArr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (int i = 0; i < intArr.length; i += 3)
{
System.out.println("Value : " + intArr [i]);
}
}
}

ANSWERS

Output: Value : 1
Value : 4
Value : 7
Value : 10

Program Tracing/Iteration Steps Executed. Show the step-by-step execution based on the elements or
components of the loop. Add more rows/columns, if necessary, for the given table below.

Iterate Value of variable i i<intArr.length Action i+=3 intArr[i]


1st 0 True Print value of i=3 1
intArr[i]
2nd 3 True Print value of i=6 4
intArr[i]
3rd 6 True Print value of i=9 7
intArr[i]
4th 9 True Print value of i = 12 10
intArr[i]
5th 12 False Loop Loop Loop
terminated terminated terminated

Output:

for (int i = 0; i < intArr.length; i += 3)


{
System.out.println("Value : " + intArr [i]); //highlighted on the table
}
Array Exercises – Program Tracing & Coding

NAME: Chiara Marie C. Canque

5. Create a for-loop structure that will display each index and the element stored (of the corresponding
index) in the array named strArr. In the display, separate the index and the corresponding
element value by a colon (:). Each entry should be displayed on a separate line. Do not hardcode
the length of the array in any way.

ANSWER:
public class MainProgram {
public static void main(String [] args) {
int [] strArr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (int i = 0; i < strArr.length; i ++)
{
System.out.println(i + " : " + strArr [i]);
}
}
}

You might also like