Loops and Strings
Worksheet
Name _________________ Date _________________
Evaluate the following code segments and determine what the output would be when the segment were executed. If
an error exists in the code segment, indicate with “error” and describe the reason for the error.
1. String word = "Wingardium Leviosa";
for(int x = 0; x<word.length(); x+=2)
System.out.print( word.substr
...[Show More]
Loops and Strings
Worksheet
Name _________________ Date _________________
Evaluate the following code segments and determine what the output would be when the segment were executed. If
an error exists in the code segment, indicate with “error” and describe the reason for the error.
1. String word = "Wingardium Leviosa";
for(int x = 0; x<word.length(); x+=2)
System.out.print( word.substring(x,x+1));
Answer: ______________________
2. String word = "Expelliarmus";
for(int x = 0; x<=word.length(); x--)
System.out.print( word.substring(x,x+1));
Answer: ______________________
3. String word = "Alohomora";
for(int x = word.length()- 1; x >= 0; x--)
System.out.print( word.substring(x,x+1));
Answer: ______________________
4. String word = "Expecto Patronum";
String build = "";
for(int x = 0; x < word.length(); x++)
{
String let = word.substring(x,x+1);
if (x % 2 == 1)
build = build + let;
}
System.out.println(build);
Answer: ______________________
5. String word = "Abracadabra";
String build = "";
for(int x = 0; x < word.length(); x++)
{
String let = word.substring
[Show Less]