Email address:
Password:
SCJP/OCPJP 1.6 certification
Practice Exams and Training
SCJP Online Training » Discussions

Can you please explain me the answer line by line

  boolean b1=true;
  Boolean b2=new Boolean("TrUe");
  while(b1){
    System.out.print(b2);
    for(int i=0; i<5; System.out.print(i))
      i++;
    if(!b2){
      b1=true;
    }else{
      b1=new Boolean("false");
    }
  }

So at the start we know both b1 and b2 are true.


Since b1 is true, it passes the while condition.


At line-4, it prints the value of b2, which is true.


Next to that, there is a for loop. Note that it iterates through 0 to 4, but since the printing happens AFTER the increment, it prints from 1 to 4. That makes the output true1234.


Line-7 checks whether b2 is false. Since b2 is actually true, the else part runs. Inside the else part, line-10 assigns false to b1.


At the end of the loop, it tries to repeat the loop, but since b1 is false now, the while condition fails, and therefore stops the while loop from running. That makes the final output to be true1234

ExamLab © 2008 - 2024