0% 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.

Uploaded by

dinaabdulrasoul
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)
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.

Uploaded by

dinaabdulrasoul
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/ 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;
}

// Solution for Question 2


public static void main(String[] args) {
// Testing countMultiplesOf method
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(4);
numbers.add(9);
numbers.add(15);
numbers.add(20);
numbers.add(25);
numbers.add(30);

int result = countMultiplesOf(numbers, 5);


System.out.println("Count of multiples of 5: " + result);
// Testing countAboveAverage method (Question 3)
ArrayList<Integer> testValues = new ArrayList<>();
testValues.add(5);
testValues.add(10);
testValues.add(15);
testValues.add(20);

int countAboveAvg = countAboveAverage(testValues);


System.out.println("Count of numbers above average: " + countAboveAvg);

// Testing isAscending method (Question 4)


ArrayList<String> words = new ArrayList<>();
words.add("apple");
words.add("banana");
words.add("cherry");

boolean isAscendingResult = isAscending(words);


System.out.println("Is ascending: " + isAscendingResult);

// Testing min method (Question 5)


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;
}
}

You might also like