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

Which of the following could be used to replace /* missing code */ so that findClosest will work as intended?
Consider the following method, which is intended to return the element of a 2-dimensional array that is closest in value to a specified 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 could be used to replace /* missing code */ so that findClosest will work 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

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
APFIVE © 2020.
Email: [email protected]|Privacy Policy