Ask Question

Consider the partially-filled array named a. What does the following loop do? (cin is a Scanner object)

int[] a = {1, 3, 7, 0, 0, 0};

int size = 3, capacity = 6;

int value = cin. nextInt ();

while (size 0)

{

a[size] = value;

size++;

1. Reads one value and places it in the remaining three unused spaces in a.

2. Reads up to 3 values and places them in the array in the unused space.

3. Reads up to 3 values and inserts them in the array in the correct position.

4. Crashes at runtime because it tries to write beyond the array.

+3
Answers (1)
  1. 20 July, 16:59
    0
    Option (1)

    Explanation:

    value is read outside the loop so it is read only once. After that the loop has begun and in the loop condition it is checked if size < capacity and as size is initialized as 3 so it is true and value if entered more than 0 then loop starts and value is assigned to a[3] and size is incremented and it continues till size is 6 and condition becomes false. So loop executes 3 times but same value is assigned.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Consider the partially-filled array named a. What does the following loop do? (cin is a Scanner object) int[] a = {1, 3, 7, 0, 0, 0}; 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