This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Practice Tests - Is it a infinite loop or not?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm studying Java for ocajp 8 exam.
I answered a question and I'm quite sure that is infinite loop (or missing info). I've asked my question to author of book (maybe I should have asked on forum first).
Anyway i took a quick and polite response to my message;
" It's not in error. Please post in the OCA forum why you think it is wrong (or don't understand the answer) and I or someone else will reply with more detail. "
So could you please tell me what is the output of the below code?
As I wrote, I answered;
"This is infinitie loop".
The right answer is "aa".
How can  the correct answer be "aa", I really do not get that :/
Thanks for opinions & answers.
 

What is the fallowing code code output?

 0- package xxxx;
 1-   public class Abcd{
 2-     public static void main(String[] args){
 3-         for(int i= args.length; i>=0; i++)
 4-           System.out.println("args");
 5-   }
 6- }

 
 
Ranch Hand
Posts: 207
3
Oracle MySQL Database Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome Emre to the JavaRanch.

Well according to your question, you haven't mentioned that what you have passed as Command Line Argument, whatever you passed as Command line argument that is stored inside the String[] args parameter of the main method.

So, here I am assuming that you haven't passed anything, then your answer is Infinite Loop, since I>=0(for all i from 1 to infinite) this condition is remains true forever and hence it is a infinite loop.
 
author & internet detective
Posts: 39392
761
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. Yes, I prefer questions be in the forum rather than over PM. It allows others to answer and also helps future people with the same question.

In your PM, you listed this as being chapter 4 question 14. That question is unrelated. Chapter 5 question 15 looks like this code, so I'm going to assume you made a typo and meant that one.

The question say the call is
java peregrine.TimeLoop September 3 1940

This means args is an array of length 3 since there are the arguments passed to the program. So Option D is correct because there is an infinite loop. Which is the answer in the book.

Where do you see something about "aa"?
 
Saloon Keeper
Posts: 3407
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:(...)
The question say the call is
java peregrine.TimeLoop September 3 1940

This means args is an array of length 3 since there are the arguments passed to the program. So Option D is correct because there is an infinite loop. Which is the answer in the book.
(...)


I don't understand. There is no infinite loop in the code as listed by OP. As soon as i overflows the loop will stop, having printed "args" many times.
 
Jeanne Boyarsky
author & internet detective
Posts: 39392
761
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um. Philosophically yes. In practice, a loop that would not end and relies on running out of memory is an infinite loop.

Ah. Variable overflow rather than memory overflow. You are correct. However, in the simplified world of the OCA, readers wouldn't know that.
 
Emrik SjöKa
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I might be making a wrong, to posting the question but there is the only way to express my question correctly.
Here is the question.
confused-by-the-q.png
[Thumbnail for confused-by-the-q.png]
 
Jeanne Boyarsky
author & internet detective
Posts: 39392
761
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah. I was looking at the paper book. As you can see from our errata, the publisher posted the wrong code in chapter 5 question 14. It's right in the print book. The correct code is in this post.

And I've reminded the publisher to fix it online again.
 
Marshal
Posts: 65019
246
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Emrik SjöKa wrote:Hi, I'm studying Java for ocajp 8 exam.. . .

I shall now challenge you. Is that an infinite loop or not?
 
Emrik SjöKa
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Emrik SjöKa wrote:Hi, I'm studying Java for ocajp 8 exam.. . .

I shall now challenge you. Is that an infinite loop or not?



Thanks for your challenge

This is an infinite loop, because that loop in the main method and that does not return anything. As you know java application start with main method. This method is static and a static method can't be called by any instance (it may be called but static methods belongs the its own class) and main is a non daemon threat (used by jvm as garbage c.). When command line argument pass into String arrray non - daemon begins to execute in this situation there is no way to exit bc deamon threat keep going to run.


 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!