Unit 4A - Arrays Code Practice
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java2 class periods10 exercises28 graded tests
Files
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-ArraysPractice1folder and editTest1.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-ArraysPractice2folder and editTest1.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-ArraysPractice3folder and editTest1.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-ArraysPractice4folder and editTest1.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-ArraysPractice5folder and editTest1.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-ArraysPractice6folder and editTest1.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-ArraysPractice7folder and editTest1.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-ArraysPractice8folder and editTest1.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-ArraysPractice9folder and editTest1.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-ArraysPractice10folder and editTest1.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
- http://codingbat.com/prob/p167011
- http://codingbat.com/prob/p191991
- http://codingbat.com/prob/p146256
- http://codingbat.com/prob/p199519
- http://codingbat.com/prob/p109537
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
- Import the lesson into your class from the Lessons page, or with New assignment → Use a lesson. It arrives as a draft with its tests already set up.
- Publish it.
- The original page is on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/ArrayPractice.html
- CS Awesome's own teacher materials: the CS Awesome teacher site
In class
- Students read
README.md. - Students do exercises 01–10, each in its own folder, pressing Run to try their code.
- 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
| Folder | File | Grading | Tests |
|---|---|---|---|
01-ArraysPractice1 | Test1.java | Autograded | 4 |
02-ArraysPractice2 | Test1.java | Autograded | 4 |
03-ArraysPractice3 | Test1.java | Checks code text only | 2 |
04-ArraysPractice4 | Test1.java | Autograded | 2 |
05-ArraysPractice5 | Test1.java | Autograded | 2 |
06-ArraysPractice6 | Test1.java | Autograded | 2 |
07-ArraysPractice7 | Test1.java | Autograded | 2 |
08-ArraysPractice8 | Test1.java | Autograded | 4 |
09-ArraysPractice9 | Test1.java | Autograded | 3 |
10-ArraysPractice10 | Test1.java | Autograded | 3 |
Teachers can download a reference solution after signing in.