Computer Science, asked by NISHANTUPADHYAY6598, 1 year ago

what is the output of this program? class output { public static void main(string args[]) { int a = 1; int b = 2; int c; int d; c = ++b; d = a++; c++; b++; ++a; system.out.println(a + " " + b + " " + c); } } 3 2 4 3 2 3 2 3 4 3 4 4

Answers

Answered by anuj
4
HEYA friend ,

so ur program is quite like this 


class output
  {

     public static void main()   
  {

         int a = 1;     
         int b = 2;   
         int c;     
         int d;        
         c = ++b;   
         d = a++;    
         c++;    
         b++;       
        ++a;      
        System.out.println(a + " " + b + " " + c);

        } 

    } 




The output is   3 4 4
HOW?
its like this


INITIAL VALUES 
a=1
b=2
c=0

FINAL VALUES
c=3
[after Pre-increment VALUE inCrease by 1 AT INSTANT]
b's value also increases  by one since its ++b ]
WE see c is post c++; incremented before printing statement ....there the value increases by 1 but will only change at the time of execution i.e at the time of printing. hence the value of  c will be 3+1=4

NOTE
1>if u assign  the value of b++ or ++b to another variable ....then the value stored is [b+1] IN BOTH THE CASES.
 2>if before any statement like post or pre is there then while in the printing the value of the variable increases by 1.[in both  pre and post].



so the value assigned to c is also 3 and b is also 3
before printing we see the value of b++;[post increment]
there the value of b is increased by 1 but changes only at the time of execution.hence in the printing statement the value goes to be 4.


a's initial vlaue is 1 and first it is assigned to d =a++;
both value increases by 1 
d=a=2

then before printing statement the value is like ++a hence the output will be 3




HOPE IT HELPS!!!!!!!!!!!!!!





Similar questions