You are on page 1of 3

Name: __________________________________

ACTIVITY: Study the following Java program.


import java.util.*;
public class ReversePrint
{
static Scanner console = new Scanner (System.in);
public static void main(String[ ] args)
{
int item0, item1, item2, item3, item4;
int sum;
System.out.println(“Enter five integers: “);
item0 = console.nextInt();
item1 = console.nextInt();
item2 = console.nextInt();
item3 = console.nextInt();
item4 = console.nextInt();
sum = item0 + item1 + item2 + item3 + item4;
System.out.println(“The sum of the numbers = “ + sum);
System.out.println(“The numbers in reverse order are: “);
System.out.println(item4 + “ “ + item3 + “ “ + item2 + “ “ + item1 + “ “ + item1 + “ “
+ item0;
}
}
Answer the following questions:
1. How many variables were used in the program?
2. What data types were used?
3. What was the program all about?
4. What is the output of the following code?

Name: _______________________
ACTIVITY: Study the following Java program.
import java.util.*;
public class ReversePrint
{
static Scanner console = new Scanner (System.in);
public static void main(String[ ] args)
{
int item0, item1, item2, item3, item4;
int sum;
System.out.println(“Enter five integers: “);
item0 = console.nextInt();
item1 = console.nextInt();
item2 = console.nextInt();
item3 = console.nextInt();
item4 = console.nextInt();
sum = item0 + item1 + item2 + item3 + item4;
System.out.println(“The sum of the numbers = “ + sum);
System.out.println(“The numbers in reverse order are: “);
System.out.println(item4 + “ “ + item3 + “ “ + item2 + “ “ + item1 + “ “ + item1 + “ “
+ item0;
}
}
Answer the following questions:
1. How many variables were used in the program?
2. What data types were used?
3. What was the program all about?
4. What is the output of the following code?
Name: _____________________________________

Directions: Write Java statements that do the following:


1. Declare an array alpha of 15 elements of type int.
2. Output the value of the tenth element of the array alpha.
3. Set the value of the fifth element of the array alpha to 35.
4. Set the value of the ninth element of the array alpha to the sum of the sixth and
thirteenth elements of the array alpha.
5. Set the value of the fourth element of the array alpha to three times the value of the
eighth element, minus 57.
6. Output alpha so that five elements per line are printed.

Name: _____________________________________

Directions: Write Java statements that do the following:


1. Declare an array alpha of 15 elements of type int.
2. Output the value of the tenth element of the array alpha.
3. Set the value of the fifth element of the array alpha to 35.
4. Set the value of the ninth element of the array alpha to the sum of the sixth and
thirteenth elements of the array alpha.
5. Set the value of the fourth element of the array alpha to three times the value of the
eighth element, minus 57.
6. Output alpha so that five elements per line are printed.
ASSESSMENT

Name: ______________________________________

I. Identification: Write your answer after each item.


A. Identify the following in this declaration:
double[ ] salary = new double[10];
1. The array name salary
2. The array size 10
3. The data type of each array component int
4. The range of values for the index of the array 0..9
5. The word used when an array is instantiated new
B. Determine whether the following array declarations are valid. Write Valid if it is valid and if it
is invalid, give the correct declaration.
1. int[75 ] list; correct
2. int size; incorrect int size=10
double[ ] list = new double[size];
3. int[ ] test = new int[-10]; incorrect [10]
4. double[ ] sales = new double[40.5]; incorrect [40]
5. grade int[] = new int[5]; incorrect int[ ] grade = new int[5];
II. Multiple Choice: Directions: Read each item carefully and write the letter of the correct answer
before the number.
A1. What are the legal indexes for the array ar, given the following declaration:
int[] ar = {2, 4, 6, 8 }
A. 0, 1, 2, 3 B.  1, 2, 3, 4 C. 2, 4, 6, 8 D. 0, 2, 4, 6
C2. What is the output of the following code fragment:
int[] ar = {2, 4, 6, 8 };
System.out.println( ar[0] + " " + ar[1] );
A.  2 6 B. 8 C.  2 4 D.  6 8
B3. For which of the following applications is an array NOT suitable:
A. Holding the scores on twelve midterms exams of a class
B. Holding the name, social security number, age, and income of one individual.
C. Holding the temperature readings taken every hour throughout a day.
D. Holding the total sales a store made in each of twelve months.
B4. Which of the following declares an array of int named img?
A. int img; B. int[ ] img; C. new int img[ ]; D. int img = int[ ];
A5. How do you access an array element?  
A. Variable = name[i]; B. Variable = i.name; C. Variable = name.get(i); D. Variable name;
III. What is the output of the following program?
import java.util.*;
public class exercise
{
static Scanner console = new Scanner(System.in);
public static void main(String[ ] args)
{
int count;
int[ ] alpha = new int[5];
alpha[0] =5;
for (count = 1; count < 5; count++)
{
alpha[count] = 5 * count + 10;
alpha[count – 1] = alpha[count] – 4;
}
System.out.print(“List elements: “);
for (count = 0; count < 5; count++)
System.out.print(alpha[count] + “ “);
System.out.println();
}
}
Answer: List elements: 11 16 21 26 30

You might also like