Related questions with answers
Identify and fix the errors in the following code: 1 public class Test { 2 public void main(String[] args) { 3 for (int i = 0; i < 10; i++); 4 sum += i; 5 6 if (i < j); 7 System.out.println(i) 8 else 9 System.out.println(j); 10 11 while (j < 10); 12 { 13 j ++; 14 } 15 16 do { 17 j ++; 18 } while (j < 10) 19 } 20 }
Solution
VerifiedErrors:
-
Line 2: the static identifier is missing
-
Line 3: the semicolon closes the body of the loop and prevents line 4 from belonging to the loop
-
Line 4: the sum variable is not initialized
-
Line 6: i and j variables are not initialized in the current scope
-
Line 6: the semicolon closes the body of the if and prevents line 7 from belonging to if block
-
Line 7: semicolon needs to be added after the println statement
-
Line 11: the semicolon closes the body of the while loop and prevents lines 12-14 from belonging to the loop
-
Line 18: semicolon needs to be added after the while condition
Create a free account to view solutions
Create a free account to view solutions
Recommended textbook solutions

Introduction to Java Programming, Brief
8th Edition•ISBN: 9780132130790 (1 more)Y. Daniel Liang
Fundamentals of Database Systems
7th Edition•ISBN: 9780133970777Ramez Elmasri, Shamkant B. Navathe
Introduction to Algorithms
3rd Edition•ISBN: 9780262033848Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen
Introduction to Algorithms
4th Edition•ISBN: 9780262046305Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. CormenMore related questions
- computer science
- computer science
1/4
- computer science
- computer science
1/7