Computer Applications
What will be the value of 'n' after the execution of the code given below?
int x=2, m=1, c=-1;
int n = x + c;
n = n - c + x;
System.out.println(n);
int x=2, m=1, c=-1;
int n = x + c;
n = n - c + x;
System.out.println(n);
Java Conditional Stmts
4 Likes
Answer
n will be 4 after execution of the code.
First n = x + c is executed ⇒ n = 2 + (-1) = 1.
Next, n = n - c + x is executed.
n = n - c + x
⇒ n = 1 - (-1) + 2
⇒ n = 4
So final value of n is 4.
Answered By
1 Like
Related Questions
Rewrite the following statement using if else:
int max=215, min=323; String str= (max>min) ? "Max is greater than Min" : "Min is Greater than Max";
Find the errors in the following code and rewrite the correct version:
char m="A"; Switch ("A"); { Case 'a'; System.out.println("A"); break; Case 'b'; System.out.println("B"); break; Default: System.out.println("Not a valid option"); }
What will be the output of the following code?
int x=2,y=5,a=0; a=x; x=y; y=a; System.out.println("x=" + x + " y=" + y);
Find the error, if any, in the following code. Write the correct statement.
int a=5, b=10; int x = (a>b)>true:false;