Computer Applications
int a = 20, b=15;
if ( a > 10 )
{
a = a++;
b++;
}
System.out.println( a + "," + b);
What will be the values of a and b when executed?
Answer
20,16
Working
The value of a is 20 and b is 16. The condition (a > 10) is true, so the execution enters the if block. The statement a = a++; increments the value of a by 1 after the assignment. So a remains unchanged as we are assigning the original value of a (which is 20) back to a. The value of b is incremented by 1 so b becomes 16.
Related Questions
State whether the following statement is True or False :
The Math.min( ) can also find the minimum of three numbers.
Predict the output of the given snippet, when executed:
int x = 1,y = 1; if(n > 0) { x = x + 1; y = y + 1; } System.out.println(x + " , " + y);What will be the values of x and y, if the value of n is given as:
(i) 1
(ii) 0 ?Mohit wanted to design a program using scanner class. Due to some confusion, he could not complete the program and has left some places marked with (i), (ii), (iii) and (iv) to be filled with the keywords/expression, as given below:
(i) java.util; class Sample { public static void main(String args[]) { (ii) num; Scanner inp = (iii) Scanner(System.in); System.out.println("Enter a number:"); num=(iv); if(n % 2 == 0): System.out.println("Even number"); else: System.out.println("Odd number"); } }Help him to complete the program by answering the following questions:
(a) What keyword/expression will be filled in place of (i)?
(b) What keyword/expression will be filled in place of (ii)?
(c) What keyword/expression will be filled in place of (iii)?
(d) What keyword/expression will be filled in place of (iv)?
Explain Java function Math.pow( ) with an example.