107k views
4 votes
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 < capacity && value > 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.

User FuegoFro
by
5.5k points

1 Answer

2 votes

Answer:

Option (1)

Step-by-step 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.

User Kim Gentes
by
5.0k points