filmov
tv
APCSA Practice: What Does This Java Method Output After Multiplying Array Elements? 💻

Показать описание
📢 What is the output of the following code?
java
Copy
private int[] a = {1, 3, -5, -2};
public void multAll(int amt) {
int i = 0;
a[i] = a[i] * amt;
i++;
}
}
Options:
A) {1, 3, -5, -2}
B) {3, 9, -15, -6}
C) {2, 6, -10, -4}
D) The code will never stop executing due to an infinite loop
📝 Solution Breakdown:
1️⃣ The array a is initialized as {1, 3, -5, -2}.
2️⃣ The method multAll(int amt) multiplies each element in a by amt (3 in this case).
3️⃣ The while loop iterates through the array, updating each element to its value multiplied by 3.
🔥 Final Answer: Option B — {3, 9, -15, -6}
🔑 Key Takeaway:
The method modifies the original array by multiplying all elements by the given amount using a while loop.
#Java #ArrayMultiplication #WhileLoop #JavaCoding #LearnJava #Programming #APCSA
java
Copy
private int[] a = {1, 3, -5, -2};
public void multAll(int amt) {
int i = 0;
a[i] = a[i] * amt;
i++;
}
}
Options:
A) {1, 3, -5, -2}
B) {3, 9, -15, -6}
C) {2, 6, -10, -4}
D) The code will never stop executing due to an infinite loop
📝 Solution Breakdown:
1️⃣ The array a is initialized as {1, 3, -5, -2}.
2️⃣ The method multAll(int amt) multiplies each element in a by amt (3 in this case).
3️⃣ The while loop iterates through the array, updating each element to its value multiplied by 3.
🔥 Final Answer: Option B — {3, 9, -15, -6}
🔑 Key Takeaway:
The method modifies the original array by multiplying all elements by the given amount using a while loop.
#Java #ArrayMultiplication #WhileLoop #JavaCoding #LearnJava #Programming #APCSA