Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following instance variable and method. private String[] nameList;

ID: 3643369 • Letter: C

Question

Consider the following instance variable and method.

private String[] nameList;

public int getIt(String name)
{
int val = 0;

for(String fromList: nameList)
{
if(fromList.equals(name))
val++;
}
return val;
}

Assume that nameList has been appropriately initialized. Which of the following best describes what, if anything, is returned by the call getIt("Dennis")?

A.) The index of the first occurrence of "Dennis" in nameList.
B.) The index of the last occurrence of "Dennis" in nameList.
C.) The number of times "Dennis" occurs in nameList
D.) The number of items not equal to "Dennis" in nameList.
E.) Nothing because the code is incorrect and will not compile.

Explanation / Answer

C.) The number of times "Dennis" occurs in nameList each time dennis appears val is added 1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote