What is the output of the programme below?public static void main(String[] args) { int x = 0; while (x < 10) { if (x & 2 == 0) { System.out.print(x + “ “); } x++; }}
Question
What is the output of the programme below?public static void main(String[] args) { int x = 0; while (x < 10) { if (x & 2 == 0) { System.out.print(x + “ “); } x++; }}
Solution
The program will not compile because the bitwise AND operator (&) is being used with an integer (x) and a boolean (2 == 0). In Java, the operands of the bitwise AND operator must be of integer or boolean type.
If you intended to print out all even numbers less than 10, you should use the modulus operator (%) to check if the remainder of x divided by 2 is 0. Here is the corrected code:
public static void main(String[] args) {
int x = 0;
while (x < 10) {
if (x % 2 == 0) {
System.out.print(x + " ");
}
x++;
}
}
This will output: 0 2 4 6 8
Similar Questions
What is the output of the programme below?public static void main(String[] args) { int x = 0; while (x < 10) { if (x & 2 == 0) { System.out.print(x + “ “); } x++; }}Group of answer choices1 3 5 7 92 4 6 8 100 2 4 6 80 1 2 3 4
What will be the output of the program?public class Sample{ public static void main(String [] args) { int i = 10; do while ( i < 10 ) System.out.print("The value of i is " + i); while ( i > 10 ) ; }}Select one:a.No output is produced.b.The value of i is 10 The value of i is 10c.Compilation errord.The value of i is 10
What will be the output of the following code?int i = 1;while(i<10){ if (i % 2 == 0) cout << i << “ ”; i++;}
What is the output of the following code snippet?int i = 0;while (i < 5) { if (i == 3) break; System.out.print(i + " "); i++;}
What is the output of the below program?public class Main{ public static void main(String[] args) { int a = 25; if(a > 5) System.out.print("Hi "); if(a < 20) System.out.print("Hello "); else System.out.print("Know Program "); }}
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.