Search Interview Questions | ![]() ![]() Click here and help us by providing the answer. ![]() Click Correct / Improve and please let us know. |
|
| ||||
Core Java - Interview Questions and Answers for 'For loop' - 11 question(s) found - Order By Newest | ||||
| ||||
Ans. 1. It shouldn't result in infinite loop. Please make sure that you have a condition that will terminate the loop and that condition should be reached. 2. Make sure to use the break statement if you aspire to only look for something. Not using break will unnecessarily execute it till the end of for loop in some cases. 3. Similarly use continue to execute the loop with next iteration and bypass the rest of the code block if required. 4. Try to avoid multiple nesting of for loops. If it''s required, Make sure to use break and continue properly so as to avoid some unnecessary processing. 5. Make sure to use try catch within the loop and not outside the for loop if you expect it to continue if one of the iteration fails. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. 1. for loop in java is used with a counter as following for(int counter=0;counter < 50;counter++){ System.out.println(list.get(counter)); } for iterating and printing the contents of a collection whereas foreach loop can be specified directly without the use of counter for(String str:list){ System.out.println(list.get(counter)); } 2. for Each loop syntax is more clean if we have to iterate over the elements of a collection and we need not keep track of the record count 3. For is preferred when we need loops without the usage of collections or Array of objects and entirely primitives are being used 4. for loop is preferred if we need to keep track of record count and have to perform some action of the basis of that. For example - If we have to print something after every 5 records, With for each loop in such case, we will have to keep a separate counter. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Break statement results in the termination of the statement to which it applies (switch, for, do, or while). A continue statement is used to end the current loop iteration and return control to the loop statement. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. No. It will result in infinite loop | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. I would avoid that. If we need to initialize member elements differently on the basis of some condition, I would prefer having overloaded constructors. I don't see a need to have a loop for initializing member elements unless the count of elements is huge and they all need to be initialized with common value. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Counter controlled repetitions are the loops that are controlled with the use of counters or the loops where the number of repetitions are known in advance. for loop in java is the counter controlled repetition. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. for, while and do while | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Infinite loop is a programming condition wherein the control goes into an infinite loop because the loop termination condition can never be met. For example - for(int x=1;x>0;x++){ } in this loop, with each increment the condition x > 0 will remain true till infinity. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. return is used within a method to return control out of the method. It may be followed by a value which is returned from the method to calling method immediately preceding the point of call. continue statement is used within a loop to start next iteration without executing the remaining statements in the loop. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Because String being an immutable object creates a new object upon each concatenation cycle. If there is any such need , we should use String Builder whose objects are mutable. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
![]() | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||