The document contains Java code solutions for a practice exam, including methods to count multiples of a number, count numbers above average, check if a list of strings is in ascending order, and find the minimum value in a list. It features a main method that tests these functionalities using various sample data. Each method is defined with clear logic to achieve its respective task.
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 ratings0% found this document useful (0 votes)
6 views4 pages
Solutions For The Practice Exam
The document contains Java code solutions for a practice exam, including methods to count multiples of a number, count numbers above average, check if a list of strings is in ascending order, and find the minimum value in a list. It features a main method that tests these functionalities using various sample data. Each method is defined with clear logic to achieve its respective task.
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/ 4
// Solutions for the Practice Exam
import java.util.ArrayList;
public class PracticeExam {
// Solution for Question 1
public static int countMultiplesOf(ArrayList<Integer> values, int n) { int count = 0; for (int value : values) { if (value % n == 0) { count++; } } return count; }
ArrayList<Integer> minValues = new ArrayList<>(); minValues.add(25); minValues.add(12); minValues.add(18); minValues.add(5); minValues.add(30);
int minimum = min(minValues);
System.out.println("Minimum value: " + minimum); } // Solution for Question 3a public static int countAboveAverage(ArrayList<Integer> values) { double sum = 0; for (int value : values) { sum += value; } double average = sum / values.size();
int count = 0; for (int value : values) { if (value > average) { count++; } } return count; }
// Solution for Question 4
public static boolean isAscending(ArrayList<String> words) { for (int i = 0; i < words.size() - 1; i++) { if (words.get(i).compareTo(words.get(i + 1)) > 0) { return false; } } return true; }
// Solution for Question 5
public static int min(ArrayList<Integer> values) { int minValue = values.get(0); for (int value : values) { if (value < minValue) { minValue = value; } } return minValue; } }