Which of the following will compile successfully?*for(int j = 0,int k=5; j < k; k--) ;for(;;System.out.print(“a”)) ;for();for(int k = 10; k--; k>0 ) ;None of above.
Question
Which of the following will compile successfully?*for(int j = 0,int k=5; j < k; k--) ;for(;;System.out.print(“a”)) ;for();for(int k = 10; k--; k>0 ) ;None of above.
Solution
The correct answer is "None of above."
Here's why:
-
for(int j = 0,int k=5; j < k; k--) ;This will not compile because you cannot declare multiple variables of the same type in the initialization part of the for loop using a comma. You should declare them separately. -
for(;;System.out.print(“a”)) ;This will not compile because the print statement is in the wrong place. It should be inside the body of the for loop, not in the condition part. -
for();This will not compile because the for loop requires at least two semicolons to separate the initialization, condition, and increment/decrement parts. -
for(int k = 10; k--; k>0 ) ;This will not compile because the decrement operation (k--) is in the wrong place. It should be in the increment/decrement part, not in the condition part.
So, none of the given options will compile successfully.
Similar Questions
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
Which of the following for loop declaration is not valid?for ( int i = 20; i >= 2; - -i )for ( int i = 7; i <= 77; i += 7 )for ( int i = 99; i >= 0; i / 9 )for ( int i = 2; i <= 20; i = 2* i )
What is the final output of the following code?int a = 5;int b = 10;int c = 3;for(int x =0; x< 5; x++){ a += 2; b -= 1; c *= 2;}cout<<"A is "<<a<<" and C is "<<c<<endl
.What will be the output of the program ?public class Test { public static void main(String [] args) { signed int x = 10; for (int y=0; y<5; y++, x--) System.out.print(x + ", "); }}10, 9, 8, 7, 6,9, 8, 7, 6, 5,Compilation fails.An exception is thrown at runtime.
What is output of below program?int main(){int i,j,count;count=0;for(i=0; i<5; i++);{for(j=0;j<5;j++);{ count++;}}printf("%d",count);return 0;} (A) 55(B) 54(C) 0(D) 1
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.