int() list (2, 5, 7, 8, 12, 13, 15, 18, 22, 25 Which of the folowing correctly finds the sum of the elements of lis (1) int sum = 0; for (int j = 0; j < 10; j++) sum = sum + list[j]; (i) int sum - 0; for (int j = 1; j <= 10; j++) sum = sum + list[j]; Only (i) b. Only (ii) c. Both (i) and (ii) d. None of these a.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter16: Searching, Sorting And Vector Type
Section: Chapter Questions
Problem 23SA
icon
Related questions
Question

JAVA

**Question 42:**

Consider the following declaration:

```plaintext
int[] list = {2, 5, 7, 8, 12, 13, 15, 18, 22, 25};
```

Which of the following correctly finds the sum of the elements of `list`?

(i)
```plaintext
int sum = 0;
for (int j = 0; j < 10; j++)
    sum = sum + list[j];
```

(ii)
```plaintext
int sum = 0;
for (int j = 1; j <= 10; j++)
    sum = sum + list[j];
```

Options:  
a. Only (i)  
b. Only (ii)  
c. Both (i) and (ii)  
d. None of these  

**Explanation:**

- **Option (i):** Loops through the list from index 0 to 9, correctly summing all the elements.
- **Option (ii):** Incorrectly starts from index 1 and attempts to access up to index 10, which is out of bounds.

Therefore, the correct answer is **a. Only (i)**.
Transcribed Image Text:**Question 42:** Consider the following declaration: ```plaintext int[] list = {2, 5, 7, 8, 12, 13, 15, 18, 22, 25}; ``` Which of the following correctly finds the sum of the elements of `list`? (i) ```plaintext int sum = 0; for (int j = 0; j < 10; j++) sum = sum + list[j]; ``` (ii) ```plaintext int sum = 0; for (int j = 1; j <= 10; j++) sum = sum + list[j]; ``` Options: a. Only (i) b. Only (ii) c. Both (i) and (ii) d. None of these **Explanation:** - **Option (i):** Loops through the list from index 0 to 9, correctly summing all the elements. - **Option (ii):** Incorrectly starts from index 1 and attempts to access up to index 10, which is out of bounds. Therefore, the correct answer is **a. Only (i)**.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Linked List Representation
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning