1.

Which is better in terms of performance for iterating an array?(a) for(int i=0; i=0; i–)(c) for(int i=100; i0; i++)I had been asked this question in a job interview.Origin of the question is Coding best practices topic in section Autoboxing & Miscellaneous of Java

Answer»

Correct option is (b) for(INT i=99; i>=0; i–)

The BEST I can EXPLAIN: reverse traversal of array take HALF number cycles as COMPARED to forward traversal. The other for loops will go in infinite loop.



Discussion

No Comment Found

Related InterviewSolutions