Consider the following Java code fragment: 1 public class While 2 { 3 public…
2014
Consider the following Java code fragment:
1 public class While
2 {
3 public void loop()
4 {
5 int x = 0;
6 while(1)
7 {
8 System.out.println("x plus one is" +(x+1));
9 }
10 }
11 }- A.
There is syntax error in line no. 1
- B.
There is syntax errors in line nos. 1 & 6
- C.
There is syntax error in line no. 8
- D.
There is syntax error in line no. 6
Attempted by 86 students.
Show answer & explanation
Correct answer: D
In Java, the condition within a while loop must be a boolean expression. Line 6 uses an integer literal '1' as the condition, which causes a compilation error because integers cannot be implicitly converted to booleans. The class name 'While' is valid as Java keywords are case-sensitive, and the string concatenation syntax on line 8 is correct.