The following segment of code contains at least one error - mark ALL of the errors that occur in this segment of code:int val = 0;while ( val < 10 ) { String msg = ""; int num = val; while ( num < 10 ) { if (num % 2 == 0 ) { msg = msg + num; num = num + 1; } }}System.out.println(msg);
Question
The following segment of code contains at least one error - mark ALL of the errors that occur in this segment of code:int val = 0;while ( val < 10 ) { String msg = ""; int num = val; while ( num < 10 ) { if (num % 2 == 0 ) { msg = msg + num; num = num + 1; } }}System.out.println(msg);
Solution
The errors in the code are:
-
The variable
msgis declared inside the outer while loop, so it is not accessible in the lineSystem.out.println(msg);which is outside the loop. To fix this, you should declaremsgbefore the outer while loop. -
The inner while loop
while ( num < 10 )will become an infinite loop whennumis an odd number because the incrementnum = num + 1;is inside the if conditionif (num % 2 == 0 ). Ifnumis odd, it will not be incremented and the conditionnum < 10will always be true. To fix this, you should move the increment outside the if condition.
Here is the corrected code:
int val = 0;
String msg = "";
while ( val < 10 ) {
int num = val;
while ( num < 10 ) {
if (num % 2 == 0 ) {
msg = msg + num;
}
num = num + 1;
}
val = val + 1;
}
System.out.println(msg);
Similar Questions
The following segment of code contains at least one error - mark ALL of the errors that occur in this segment of code:int val = 0;while ( val < 10 ) { String msg = ""; int num = val; while ( num < 10 ) { if (num % 2 == 0 ) { msg = msg + num; num = num + 1; } }}System.out.println(msg);Group of answer choicesThe variable msg is used outside of its scopeThe variable num is used outside of its scopeThe inner loop will loop foreverThe outer loop will loop foreverYou cannot add together a String and an int value
What is wrong with the following code segment if the numbers from 1 to 10 are meant to be output? int num = 1; while (num > 0){ num = num + 1; System.out.println(num); }a.)Nothing is printed in the while loop.b.)The variable should be initialized to 0.c.)The while expression should be evaluated with <.d.)The while loop is an infinite loop and will produce an error.
Select the correct answerWhat will be the output of the following Java program? class CT { public static void main(String args[]) { try { int a, sum; sum = 10; for (a = -1; a < 3; ++a) sum = (sum / a); } catch(ArithmeticException e) { System.out.print("0"); } System.out.print(sum); } }Options050Compilation ErrorRuntime Error
Given1. public class Program{2. public static void main(String[] args) {3. 4. for(int j = 0,k=5; j < k; k--) ;5. for(int j = 0; j++ < 3;) ;6. for(int i = 0; i < 5; i++, System.out.print(i + ".go ")) ;7. 8. }9. }What will be the result?*Compilation fails due to multiple errors.Compilation fails due to an error on line 6.0.go 1.go 2.go 3.go 4.go 5.goAn exception is thrown at runtime.1.go 2.go 3.go 4.go 5.go
Briefly describe the purpose of the following program. Identify the errors if any. Statethe line number and the error therein while writing your answer. (6 Marks)1. import java.util.*;2. public class MidExamPracticeQ2{3. public static void main(String[] a){4. int choice, num1, num2;5. Scanner keyboard = new Scanner(System.in);6. do7. {8. System.out.print("Enter a number: ");9. num1 = keyboard.nextDouble();10. System.out.print("Enter another number: ");11. num2 = keyboard.nextDouble();12. System.out.println("Their sum is " + num1 + num2);13. System.out.println("Do you want to do this again? ");14. System.out.print("1 = yes, 0 = no ");15. choice = keyboard.nextInt();16. } while (choice = 1)17. }18. }
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.