25.7k views
2 votes
Which input value causes the loop body to execute a 2nd time, thus outputting "In loop" again? { String s = "Go"; while ((!s.equals("q"))&& (!s.equals("")) System.out.println("In loop"); 5 - scnr.nextO;

a) "Quit"
b) "q" only
c) "Q only
d) Either "q" or "Q"

1 Answer

4 votes

Answer:

a) "Quit"

c) "Q only

Step-by-step explanation:

Given

String s = "Go";

while ((!s.equals("q"))&& (!s.equals(""))) {

System.out.println("In loop");

s = scnr.next();

}

Required

What input causes another execution

Analyzing the while condition

while ((!s.equals("q"))&& (!s.equals("")))

This can be split into:

!s.equals("q")) && (!s.equals(""))

Meaning

When s is not equal to "q" and when s is not an empty string

In other words,

the loop will be executed when user input is not "q" and user input is not empty.

So, from the list of given options: The loop both will be executed when:

a) Input is "Quit"

c) Input is Q only

Input of q will terminate the loop, hence b and d are incorrect

User Govind Singh
by
6.6k points