Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Confusion about array instance

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Which two create an instance of an array? (Choose two)



Answer: A, D

How option D is creating an instance of Array.Please Explain.

Thanks,
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An array is an object. So a reference to an array can be assigned to a variable of type Object.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by yogi maheshnath:
Hi,

Which two create an instance of an array? (Choose two)



Answer: A, D

How option D is creating an instance of Array.Please Explain.

Thanks,




A. int[] ia = new int[15];
B. float fa = new float[20];
C. char[] ca = �Some String�;
D. Object oa = new float[20];
E. int ia[][] = { 4, 5, 6, }, { 1, 2, 3 };

I understand the following from the above choices

A. int[] ia = new int[15] -> This is correct

B. float fa = new float[20] -> This is incorrect, missing box brackets, the correct definition is
float[] fa = new float[20],

C. char[] ca = "Some String"

Please explain

D. Object oa = new float[20];

Please explain

E. int ia[][] = { 4, 5, 6, }, { 1, 2, 3 }; - > It should be declared like this ...
int[][] multiD = {
{ 1, 2, 3, 4 },
{ 5, 6, 7 }
};


Thanks
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char[] ca = "Some String"


"Some string" is a String and not an array of Characters.
It should be represented by

char[] ca={ 's','o','m','e','s','t','r','i','n','g','s'}.


And since array is an object, then you can assign it to the variable of type Object.
I dont know how to further explain this.
 
Avi Sridhar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Nabila Mohammad:
char[] ca = "Some String"


"Some string" is a String and not an array of Characters.
It should be represented by

char[] ca={ 's','o','m','e','s','t','r','i','n','g','s'}.


And since array is an object, then you can assign it to the variable of type Object.
I dont know how to further explain this.



Thanks for your eply. explanation is good enough.

Some things needs to be memorized here..
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome!
 
Do the next thing next. That’s a pretty good rule. Read the tiny ad, that’s a pretty good rule, too.
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic