0% found this document useful (0 votes)
10 views3 pages

Practice Questions On Unit 4 Lab00 and 01

The document contains practice questions and an answer key related to array manipulation in programming. It includes tasks such as declaring arrays, filling them with values, printing elements, and performing calculations like finding the smallest number and counting negative values. The answer key provides code snippets and explanations for each question to aid in understanding array operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Practice Questions On Unit 4 Lab00 and 01

The document contains practice questions and an answer key related to array manipulation in programming. It includes tasks such as declaring arrays, filling them with values, printing elements, and performing calculations like finding the smallest number and counting negative values. The answer key provides code snippets and explanations for each question to aid in understanding array operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Practice Questions on Unit 4, Lab00 and 01

Okay, here are two documents based on the source material you provided: a set of
questions with modified numerical values and a corresponding answer key.
Document 1: Questions
1. Draw a picture of what this declaration accomplishes: int[] grades = new
int[8];
2. Write the code to fill the array above with 120’s.
3. Draw a picture of what this declaration accomplishes: char[] symbols = new
char[6];
4. Write the code to fill each cell in the array above with a 'B'.
5. Write the code to print each element of the symbols array in the console I/O
window.
6. Given an array of size NUMITEMS containing doubles, write the code to
multiply each element by 5.0.
7. Given an integer array of size N which is empty, write the code to insert N
number of random integers, between 1000 and 2000, inclusive.
8. public static final int NUMITEMS = 50;
double[] values = new double[NUMITEMS];
Draw a picture of the array. Include indexes.
9. Write the code to visit each cell in the array above and fill it with random
doubles between
-25 and 25, inclusive.

10.Write the code to traverse the array and find the smallest number in the
array above.

11.Write the code that searches the array and counts the number of negative
numbers.

12.Write the code to calculate the sum of all the numbers in the array above.

13.Use the result from the previous question to calculate the average of the
numbers in the array.

14.Traverse the array above, calculating the sum of the negative numbers and
the sum of the positive numbers.
Document 2: Answer Key
1. The declaration int[] grades = new int; creates an array named grades that
can hold 8 integer values. Initially, all elements are set to 0. The picture
should represent an array with 8 boxes, each representing an element, with
the index numbers 0 through 7.
2. for(int index = 0; index < 8; index++)
grades[index] = 120;
This code iterates through each index of the grades array and assigns the
value 120 to each element.
3. The declaration char[] symbols = new char[6]; creates an array named
symbols that can hold 6 character values. The picture should represent an
array with 6 boxes representing the elements, with index numbers 0 through
5.
4. for(int index = 0; index < symbols.length; index++)
symbols[index] = 'B';
This code iterates through each index of the symbols array and assigns the
character 'B' to each element.
5. for(int pos = 0; pos < 6; pos++)
System.out.print(symbols[pos] + "\t"); //B B B B B B
This code iterates through the symbols array and prints each element
followed by a tab.
6. for(int i = 0; i < NUMITEMS; i++)
someArray[i] = someArray[i] * 5.0;
This code multiplies each element in the someArray array by 5.0.
7. for(int index = 0; index < N; index++)
arrayOfIntegers[index] = (int)(Math.random() * 1001 + 1000);
This code fills the arrayOfIntegers array with N random integers between
1000 and 2000 inclusive. Math.random() generates a double between 0.0 and
1.0. Multiplying by 1001 yields a number between 0.0 and 1001.0. Adding
1000 shifts the range to between 1000.0 and 2001.0. Finally, casting to (int)
truncates the decimal part, resulting in an integer between 1000 and 2000.
8. The array values should be pictured with 50 elements, indexed from 0 to 49,
each initialized to 0.0.
9. for(int i = 0; i < values.length; i++)
values[i] = Math.random() * 51 + -25;
This code assigns each element a random double between -25 and 25
inclusive.

10. double smallest = values;


for (int i=1; i< values.length; i++)
if(values[i] < smallest)
smallest = values[i];
This code finds the smallest number in the values array.
11.int count = 0;
for(int i = 0; i < values.length; i++)
if(values[i] < 0)
count++;
This code counts the number of negative numbers in the values array.
12.double sum = 0.0;
for (int i = 0; i < values.length; i++)
sum += values[i];
This code calculates the sum of all the numbers in the values array.
13.double average = sum / values.length;
This code calculates the average of the numbers in the values array.
14.double sumNeg = 0;
double sumPos = 0.0;
for (int i = 0; i < values.length; i++)
if(values[i] > 0)
sumPos += values[i];
else
sumNeg = sumNeg + values[i];
This code calculates the sum of the negative numbers and the sum of the
positive numbers in the values array.

You might also like