Ask Question

What does the following code do? Assume list is an array of int values, temp is some previously initialized int value, and c is an int initialized to 0. for (int j = 0; j < list. length; j++) if (list[j] < temp) c++;

a. It finds the smallest value and stores it in temp

b. It finds the largest value and stores it in temp

c. It counts the number of elements equal to the smallest value in the list

d. It counts the number of elements in the list that are less than temp

+4
Answers (2)
  1. 18 March, 20:08
    0
    d) It counts the number of elements in list that are less than temp

    Explanation:

    Lets have a look at the code fragment.

    There is a loop variable j which starts from then moves through the array length.

    This loop will continue to execute till it reaches the end of the list.

    Also the value of j will be incremented by 1 at the end of every iteration.

    The variable c will also be incremented by 1 every time the if condition evaluates to true.

    Lets say that temp = 5 c=0 and list has the following elements: 1 2 3 4 5 6

    In the first iteration the value of j=0 which is less than array length as array length is 6.

    So the control enters the loop body, if statement is checks if the jth index of array is less than value of temp?

    Here list[j]=list[0] = 1

    As the value of j is 0 and the element at 0 th index of array is 1

    So the condition is true because 1 is less than the value of temp i. e. 5

    After this the statement c+ + is executed which will increment value of c by 1 so c=1.

    So this loop will iterate till the end of the list is reached.

    At every iteration it will be checked if the element in the list is less than the value of temp and c will keep counting the number of times this condition evaluates to true.

    So it is clear that this code counts the number of elements in the list that are less than temp.
  2. 18 March, 20:11
    0
    Option D is correct.

    Explanation:

    Option D is correct because when the condition if (list[j] < temp) is tested it only gets true when element in list[] array at jth position is less than the value in temp and after that it increments the value of c by this statement: c+ + and so c is incremented from 0 to as much times as much elements in list[] are lesser than temp.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “What does the following code do? Assume list is an array of int values, temp is some previously initialized int value, and c is an int ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers