| preferred AP College board partner for AP classes
AP Computer Science A/Unit 8: 2D Array
Start Practice TestPractice Test
About Exam
medium Solved by 6 students
Finding Closest Value in a 2D Array
< Prev
Next >

Consider the following findClosest method, which is intended to return the element in a 2D array mat that is closest in value to a given number val.

/** @return the element of 2-dimensional array mat whose value is closest to val */
public double findClosest(double[][] mat, double val)
{
    double answer = mat[0][0];
    double minDiff = Math.abs(answer - val);
    for (double[] row : mat)
    {
        for (double num : row)
        {
            if ( /* missing code */ )
            {
                answer = num;
                minDiff = Math.abs(num - val);
            }
        }
    }
    return answer;
}

Which of the following expressions can replace /* missing code */ so that the method works as intended?

A

val - row[num] < minDiff

B

Math.abs(num - val) < minDiff

C

Math.abs(row[num] - val) < minDiff

D

val - num < 0.0

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Time
Score
#1kaisuki11 0m 00s 100
#2angelajxi1101 0m 00s -10
#3psak1202 0m 00s -20
#4y.seong202711 8m 07s -387
Items per page:
10
1 – 4 of 4
No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy