0% found this document useful (0 votes)
85 views

Exercises - 1 - Arrays - Exercises

This document introduces programming exercises on arrays in Java. It contains two exercises: 1) The first exercise has the student create an array that can store 20 integer values. It then has them assign a value to the 8th element, output the 8th element, and use a for loop to populate the array with random integers between 1-20. 2) The second exercise has the student create an array that stores 5 string values ("Cool", "Awesome", etc.). It then prompts the user for a word and checks if it exists in the array, printing a match message or not found message.

Uploaded by

Kavya Pandya
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)
85 views

Exercises - 1 - Arrays - Exercises

This document introduces programming exercises on arrays in Java. It contains two exercises: 1) The first exercise has the student create an array that can store 20 integer values. It then has them assign a value to the 8th element, output the 8th element, and use a for loop to populate the array with random integers between 1-20. 2) The second exercise has the student create an array that stores 5 string values ("Cool", "Awesome", etc.). It then prompts the user for a word and checks if it exists in the array, printing a match message or not found message.

Uploaded by

Kavya Pandya
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

INTRODUCTION TO ARRAYS:

PROGRAMMING EXERCISES

WRITTEN RESPONSE EXERCISE:


You can make these answers as comments in a new application program (Class) in Java

1. What is the size of each array?

a) int[] val = new int[10];

b) int n = 20;
int[] val = new int[n/5];

c) int[] val = new int[10];

d) double i = 5;
int[] num = new int[val.length *2];

2. What is the output of the following if int[] line = { 3, 5, 0, 9};

a) line[3];
b) line[0]
c) line [1+2];
d) line [line[2]];
e) line [line [3] – line[1]];
f) line[4];

PROGRAMMING EXERCISES:
1. (a) Create a new Java application called FirstArrays.

(b) Add the following line of code:

int[] numbers = new int[20];

(c) Write a comment above the line indicating how many values and what type of data is stored in
the array.

(d) Write a statement that will assign a value of 8 to the 8 th element in the array.

(e) Write a statement that will output the eighth element.

Arrays Exercises Page 1 of 2


(f) Add the following lines:

for (int counter = 0; counter < numbers.length; counter++)


{
numbers[counter] = rnd.nextInt(20) + 1;
}

(g) Write a comment above the for statement to explain what the loop does.

(h) Create a similar loop that displays all the elements of the array, along with their value as follows:

Save the project as First Arrays in your UNIT 3 folder.

2. Create a project called Word Array that stores the following words in an array of size five (5):

• Cool
• Awesome
• Programming
• Java
• Class

Then ask the user for a word and check if that word exists in the array. If it does, print “Match was
Found!”, otherwise tell the user “The word is not in the array”. You will need to use the
equalsIgnoreCase() method.

Save the project as Word Array in your UNIT 3 folder.

Arrays Exercises Page 2 of 2

You might also like