- 7
If a=10 b= a++ + ++a what is b?
22 Answers
+ 18
Well, its 22. Try it yourself before down voting this answer. From first term of the expression
b=a++ + ++a;
a++ means 10 but it will increase it value if it is use again.
++a means increase value of a immediately. What is value of a. It is 10, no it will change it value by 1 if it use again. So from above line its value is 11 and than increase value of a immediately its value is 12.
So value of b = 22.
Use this below code
public class Program
{
public static void main(String[] args) {
int a=10,b;
b= a++ + ++a;
System.out.println(b);
}
}
+ 3
var++ gives the current value first then increments it by 1.
++var increments the value by 1 first then gives its value.
so in your question:
a = 10;
current value of a is 10.
a++ gives the current value first which is 10,
then makes its value 11.
so the current value of a is 11.
++a increments 11 by 1 first,
making a = 12,
then gives its current value which is 12.
so,
b = a++ + ++a
= 10 + 12
= 22
+ 1
10+12 = 22
+ 1
b=a++ + ++a;
equal
a=1+a;
b=a+a;
a=1+a;
soo b=22
+ 1
b = 22
+ 1
a++ : increment after assign value.
++a : assign value after increment.
+ 1
b=22 because it is incremented twice.
+ 1
(++a)+(a++)
+ 1
Here in this example ++a (prefix) having first priority over a++ (postfix) , so according to this rules first evaluates ++a so a = 11 now after assigning the evaluated values of a++ + ++a to b so 11 + 11 = 22 = b. then post increment operator will executes in last so the value of a again incremented, at the end of evaluation is a =12 , b = 22.
+ 1
What will be ++a + a++
0
22 is the answer for better practice use code playground
0
22
0
22 refer post and pre fix operators
0
22
0
according 2 my analysis in a single equ. same variable don't hv different values
from 1 st a++ = 10
but from 2nd ++a it become 11
know in equ is
b= a++ + ++a;
bt their is 2 values of a
so, it will use the lst update value of a which is 11
both the a become
a =11
if u want 2 check so chk for this qust
b= a + ++a;
its also give 22
0
Now try b=a+++a; //a is initially 10
0
++a++
0
14
0
Aditya kumar pandey correct
0
class OperatorExample {
public static void main(String args[]) {
int a = 10 ;
int b = 10 ;
System.out.println(a++ +++a); //10+12=22
System.out.println(b+++b++); // 10 + 11 = 21
}}
ans plz in details?