Computer Applications
Consider the program segment:
int p = 0;
for(p = 4; p > 0; p -= 2);
System.out.print(p);
System.out.println(p);
The above statement will display:
- 42
- 4200
- 0
0 - 00
Answer
00
Reason
1. Initialization: int p = 0;
- This line declares an integer variable
pand initializes it to 0.
2. For Loop: for(p = 4; p > 0; p -= 2);
p = 4→ loop starts withpassigned the value 4.- Condition:
p > 0is true (since 4 > 0). - The semicolon (
;) at the end means the loop body is empty and does nothing. - Update:
p -= 2decreasespby 2. - Next value of
p= 2 → still > 0, so the loop continues. - Decrease again:
p -= 2→p = 0→ now the conditionp > 0is false, loop stops.
3. After the Loop: p is now 0.
4. Output Statements:
System.out.print(p);prints 0 without moving to the next line.System.out.println(p);prints 0 and moves to the next line.
Related Questions
Which of the following pairs of methods will cause a compile-time error due to incorrect method overloading?
- void test(int a, int b) and void test(double a, double b)
- void test(int a, double b) and void test(double a, int b)
- void test(int a, double b) and void test(int a)
- void test(int a) and int test(int a)
Which of the following converts "25" to 25.0?
- Double.Parsedouble("25")
- Double.parse("25")
- Double.parseDouble("25")
- Double.parseDouble(25)
The output of the below statement is:
System.out.println("I said, \"It\'s wise to obey elders.\"");- I said, 'It is wise to obey elders.'
- I said, "It's wise to obey elders."
- I said, It's wise to elders.
- "'It's wise to obey elders.'"
What is the output of the statement given below?
"ANGER".compareTo("ANGEL")- 3
- -6
- 6
- 0