Assume that the method Integer promptInteger(String prompt) gives the user a pro
ID: 3643325 • Letter: A
Question
Assume that the method Integer promptInteger(String prompt) gives the user a prompt message to enter an integer at the keyboard and then returns the Integer entered by the user.Consider the following method getVals which is intended to read positive integers entered by the user and add them to an ArrayList<Integer> until the value 0, not to be included in the list, is entered. The method getVals does not work as intended.
public ArrayList<Integer> getNames()
{
ArrayList<Integer> list = new ArrayList<Integer>();
Integer val = new Integer(1);
Integer zero = new Integer(0);
while(!val.equals(zero))
{
val = promptInteger("Enter next integer: ");
list.add(val);
}
return list;
}
Which of the following best describes the error in this method?
A.) The first entry in the list returned will always be 1.
B.) The last entry in the list returned will always be 0.
C.) The variable list is not correctly declared and initialized.
D.) The variable val is not correctly declared and initialized.
E.) The loop may never exit because the loop condition is incorrect.
Explanation / Answer
C.) The variable list is not correctly declared and initialized.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.