Unit 4A - Arrays Code Practice

From CS Awesome 2CC BY-NC-SA 4.0Original page

Java2 class periods10 exercises28 graded tests

Files

README.mdDownload

Unit 4A - Arrays Code Practice

Each coding exercise has its own folder. Open its .java file and press Run to try it. Run Tests checks every graded exercise in this lesson at once.

Practice Problems

Exercise 1

Fix the code so that it prints every other value in the array arr1 starting with the value at index 0.

Open the 01-ArraysPractice1 folder and edit Test1.java. Press Run to try it; Run Tests checks it (4 tests).

Exercise 2

Fix the following to print the values in the array a1 starting with the value at the last index and then backwards to the value at the first index.

Open the 02-ArraysPractice2 folder and edit Test1.java. Press Run to try it; Run Tests checks it (4 tests).

Exercise 3

Rewrite the code so that it prints all the values in an array arr1 using a for-each loop instead of a for loop.

Open the 03-ArraysPractice3 folder and edit Test1.java. Press Run to try it; Run Tests checks it (2 tests). Its tests check your code, not what it prints: “for each loop”, “print statement variable name”.

Exercise 4

Finish the code so that it prints out all of the odd values in the array a1. Hint: use % to check for odd values.

Open the 04-ArraysPractice4 folder and edit Test1.java. Press Run to try it; Run Tests checks it (2 tests).

Exercise 5

Finish the following method getSum to return the sum of all values in the passed array.

Open the 05-ArraysPractice5 folder and edit Test1.java. Press Run to try it; Run Tests checks it (2 tests).

Exercise 6

Finish the following method to return the sum of all of the non-negative values in the passed array.

Open the 06-ArraysPractice6 folder and edit Test1.java. Press Run to try it; Run Tests checks it (2 tests).

Exercise 7

Finish the code to print the strings at the odd indices in the array.

Open the 07-ArraysPractice7 folder and edit Test1.java. Press Run to try it; Run Tests checks it (2 tests).

Exercise 8

Finish the method getSumChars to return the total number of characters in the array of strings strArr.

Open the 08-ArraysPractice8 folder and edit Test1.java. Press Run to try it; Run Tests checks it (4 tests).

Exercise 9

Finish the method findMin so that it finds and returns the minimum value in the array.

Open the 09-ArraysPractice9 folder and edit Test1.java. Press Run to try it; Run Tests checks it (3 tests).

Exercise 10

Finish the method getAverage to calculate and return the average of all of the values in the array.

Open the 10-ArraysPractice10 folder and edit Test1.java. Press Run to try it; Run Tests checks it (3 tests).

More Practice

For practice with simple array manipulation and conditionals, but no loops see http://codingbat.com/java/Array-1. For more practice with loops and arrays go to http://codingbat.com/java/Array-2.

Here are problems without loops

Here are problems with loops

For teachers

Unit 4A - Arrays Code Practice

Students read CSAwesome2's “Unit 4A - Arrays Code Practice” in CS Room and work through its 10 Java coding exercises.

  • Time: 2 class periods
  • Tests: 28 in 10 graded exercises, set up for you when you import the lesson

Goals

  • Practice for Unit 4: Data Collections.

Before class

In class

  1. Students read README.md.
  2. Students do exercises 01–10, each in its own folder, pressing Run to try their code.
  3. Run Tests checks every graded exercise at once; scores appear in the Gradebook. Grading uses your copy of the tests, so a student who edits theirs does not change the grade.

Exercises

FolderFileGradingTests
01-ArraysPractice1Test1.javaAutograded4
02-ArraysPractice2Test1.javaAutograded4
03-ArraysPractice3Test1.javaChecks code text only2
04-ArraysPractice4Test1.javaAutograded2
05-ArraysPractice5Test1.javaAutograded2
06-ArraysPractice6Test1.javaAutograded2
07-ArraysPractice7Test1.javaAutograded2
08-ArraysPractice8Test1.javaAutograded4
09-ArraysPractice9Test1.javaAutograded3
10-ArraysPractice10Test1.javaAutograded3

Teachers can download a reference solution after signing in.