Interview Questions and Answers for 'Loop' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Interview Questions and Answers for 'Loop' - 13 question(s) found - Order By Newest

Very frequently asked. Among first few questions in almost all interviews. Among Top 5 frequently asked questions. Frequently asked in Indian service companies (HCL,TCS,Infosys,Capgemini etc based on multiple feedback ) and Epam Systems
  Q1. Difference between == and .equals() ?Core Java
Ans. "equals" is the method of object class which is supposed to be overridden to check object equality, whereas "==" operator evaluate to see if the object handlers on the left and right are pointing to the same object in memory.

x.equals(y) means the references x and y are holding objects that are equal. x==y means that the references x and y have same object.

Sample code:

String x = new String("str");
String y = new String("str");

System.out.println(x == y); // prints false
System.out.println(x.equals(y)); // prints true

  Sample Code for equals

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   string comparison   string   object class   ==    equals   object equality  operator   == vs equals   equals vs ==     Asked in 294 Companies      basic        frequent

Try 6 Question(s) Test


 Q2. What are the considerations to be made in case of loops in Java ?Core Java
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.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   loops   continue   break   for loop  control statements  loop statement.while loop  control statements  loop statement   architecture


 Q3. What is the difference between for and foreach loop in java ? Which one should be used in which cases ?

Core Java
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.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     loop  for loop  control statements  loop statement  foreach loop  for vs foreach loop


 Q4. What is the advantage of using arrays over variables ?
Ans. Arrays provide a structure wherein multiple values can be accessed using single reference and index. This helps in iterating over the values using loops.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   arrays   loops   variables   basic interview question


 Q5. What is the difference between a break statement and a continue statement?Core Java
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.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   break   continue   loop   for loop  control statements  loop statement   while loop  control statements  loop statement   break   continue   difference between   basic interview question      basic        frequent


 Q6. Will a for loop like following throw a compilation error ? for(;;);
Ans. No. It will result in infinite loop

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     loop  for loop  java


 Q7. Is it a good design practice to have programming constructs like if and loops in constructor ?Design
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.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     design constructor  for loop


 Q8. What are the counter controlled repetitions in java ? Core Java
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.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     for loop  control statements  loop statement


 Q9. What are the different types of loops in Java ?Core Java
Ans. for, while and do while

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     loops  different type of loops  for loop  control statements  loop statement      basic


 Q10. What is an infinite loop ?Core Java
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.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     loop  infinite loop  for loop  control statements  loop statement      basic


 Q11. What is the difference between return and continue statement ?Core Java
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.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     return statement  continue statement  return vs continue  for loop  control statements  loop statement


 Q12. Why shouldn't we use string concatenation extensively or in a loop ?Core Java
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.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     string  string immutable  immutability  for loop  control statements  loop statement     Asked in 1 Companies


 Q13. Find unique values between two arrays using single for loop ?Core Java
 This question was recently asked at 'Net connect'.This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     for loop  control statements  loop statement     Asked in 1 Companies



Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: