Knowee
Questions
Features
Study Tools

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);

🧐 Not the exact question you are looking for?Go ask a question

Solution

The errors in the code are:

  1. The variable msg is declared inside the outer while loop, so it is not accessible in the line System.out.println(msg); which is outside the loop. To fix this, you should declare msg before the outer while loop.

  2. The inner while loop while ( num < 10 ) will become an infinite loop when num is an odd number because the increment num = num + 1; is inside the if condition if (num % 2 == 0 ). If num is odd, it will not be incremented and the condition num < 10 will 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);

This problem has been solved

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. }

1/3

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.