Using this code snippet, what common English word will cause the program to prin
ID: 3770069 • Letter: U
Question
Using this code snippet, what common English word will cause the program to print out "eroo". Scanner keyboard = new Scanner (System, in); String input = keyboard.next () ; if (input .char At (0) == 's' && input. length () == 6) {if (input.contains ("ch") && (! input.contains("a")| |! input.contains("e")| | ! input.contains("u"))&& input. contains ("w") ){ if (input, substring (2, 4) .equals ("it") && ! input, substring (1, 2) .equals("n")){ System, out. print ("eroo");} } }Explanation / Answer
The answer is "switch". This word satisfies all conditions of code snippet as below:
1. First if condition: First 'if' checks for a string starting with character 's' and of length 6. "switch" starts with "s" and is of length 6, hence it satisfies first 'if' condition.
2. Second nested if condition: Once first condition is satisfied, second 'if' condition (nested in first 'if' condition) is checked for containing character sequence "ch", for not containing any of vowels 'a' 'e' and 'u', and for containing "w" in the string. "switch" satisfies this condition also.
3. Third nested if condition: After second nested condition is tested and satisfied, third 'if' condition (nested inside second 'if' condition) is tested for containing character sequence "it" in substring from index 2 to index 4 and not containing "n" in substring from index 1 to index 2. Substring of "switch" from index 2 to index 4 is "itc" which contains "it". While substring from index 1 to 2 is "wi" that doesn't contain "n". Hence "switch" satisfies third condition too.
Therefore "switch" is the word that satisfies all conditions and will cause the printing of "erroo".
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.