Computer Applications
Mention the output of this code snippet:
int lives = 5;
System.out.println(lives--);
System.out.println(lives);
Answer
Output
5
4
Explanation
- The variable
livesis initialized to 5. - The statement
System.out.println(lives--);first prints the current value oflives, which is 5, and then decreases the value oflivesby 1 becauselives--is the postfix decrement operator. - After this statement, the value of
livesbecomes 4. - The next statement
System.out.println(lives);prints the updated value oflives, which is 4.
Related Questions
Name the type of type casting in the given statements:
(a) double m = 'b';
(b) char ch = (char)68;Write Java statements for the following:
(a) To assign the cube root of -343 to a variable with the appropriate datatype.
(b) To assign the position of the last occurrence of @ in the String s with the appropriate datatype.Convert the following for loop segment to an exit-controlled loop.
for(k = 10;k >= -1;k--) System.out.println(k*2); System.out.println(k*4);Give the output of the following program segment:
int x[]={2,45,7,67,12,3}; int i, t=0; for(i=0, t=5;i<3;i++,t--) { System.out.println(x[i]+x[t]); }