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

Method calling the same method itself

Hi all,

I am confused with the correct answer - it is D here (CADBE) what is not true, because E is in the "else" branch of the method.

The correct answer should be CADB.

Please pay attention to it

No, it's not. When it comes to the line main(new String[]{"D"}); it calls the main method for a second cycle as you have already spotted. After that second cycle ends by printing "B", the first cycle needs to print the remaining statement, which prints "E" at the end.


Think of it as just as a method calling another:


void methodA() {
   System.out.println("A");
}

void methodB() {
   methodA();
   System.out.println("B");
}


When you call methodB(), it invokes methodA() which prints "A". After the execution of methodA() is completed, the methodB() has to print the remaining statements, which prints "B".


The code in this question uses the same flow, except instead of two methods, it's the same method.


Does that make it clear?


ExamLab © 2008 - 2024