• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt about how many objects are created

 
Ranch Hand
Posts: 84
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


how many objects are created in this program?

1: Dozens[] da=new Dozens[3]; (array is an object)
2 and 3:da[0]=new Dozens(); da[0] refers to an object(included as array dz (object)instance variable)
4 and 5: Dozens d=new Dozens(); d refers to an object(included as array dz(object)instance variable)

so the answer is 5:

but i think main(String[] args) here String[] array is also an Object

so the answer is 6:

this is correct or not?



 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saravanan ragunathan wrote:

but i think main(String[] args) here String[] array is also an Object

so the answer is 6:

this is correct or not?



your question is good. yes, when you run a program[which has main method] with out command line parameter, jvm creates an empty string array object and passes to the main.

to your question: technically it should be 6.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all.
I think only 5 objects will be created.
for String[] args of main method --- how new object will be created because we are not using any new keyword there.

Please clear my doubt

Thanks
 
Ranch Hand
Posts: 68
MyEclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends,

we can see that there is the zero length string array object which is refered by args reference(if do not pass anything) through this program...

public class Practice3 {
public static void main(String[] args){
System.out.println(args.length);
System.out.println(args);

}
}
and its output is:

0
[Ljava.lang.String;@18d107f

so,we can say that there are 6 objects...

but you asked a different question that, how many objects are created in this program?

i think its answer is 5 because zero length string object is not created in this program.
 
Greenhorn
Posts: 25
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, my answer would be 6 too.

One for the array,
Three for the three objects in the array,
One for he object in line 10,
One for the String[] args.

Indeed the String[] args object is created because you can issue a args.length without getting a nullPinterException even if no parameters are passed.

Very nice question
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joel Mata wrote:Very nice question


Don't like it, myself. The answer comes down to whether you consider the args array to have been created "in this program", or created by the JVM and passed into the program. Which means you can perfectly understand what's going on and still get the answer wrong if you guess the wrong interpretation. Well written questions don't have that sort of ambiguity.
 
Joel Mata
Greenhorn
Posts: 25
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:

Joel Mata wrote:Very nice question


Don't like it, myself. The answer comes down to whether you consider the args array to have been created "in this program", or created by the JVM and passed into the program. Which means you can perfectly understand what's going on and still get the answer wrong if you guess the wrong interpretation. Well written questions don't have that sort of ambiguity.



i thunk i learned something, "or created by the JVM and passed into the program" so you say that the JVM can create objects and pass then to the program... it's true that the "new" operator is not used but the array is a object so it must have been created, can you clarify it?

I meant it was a good questions because it made me thing, and having this discussion about it.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
saravanan please QuoteYourSources when you post a mock exam question. So tell us where you got this question from...
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saravanan ragunathan wrote:

how many objects are created in this program?

1: Dozens[] da=new Dozens[3]; (array is an object)
2 and 3:da[0]=new Dozens(); da[0] refers to an object(included as array dz (object)instance variable)
4 and 5: Dozens d=new Dozens(); d refers to an object(included as array dz(object)instance variable)

so the answer is 5:

but i think main(String[] args) here String[] array is also an Object

so the answer is 6:

this is correct or not?





Hey Pals I think I am getting this wrong...The question says: How Many Objects are in "THIS PROGRAM"??.....Now my doubt is as follows: Where does "THIS PROGRAM" start from?, Line 1 or line 4?? if "THIS PROGRAM" starts from line 1, then I think the " int[] dz = { 1, 2, 3 }; " object created in line 2 was NOT included in your balance. Can somebody please help to clearify this doubt??. Because the exams can come up with such a "trickish" question.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic