• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

arrays

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this ?is from http://www.parikosh.com/
Q#10. True or false
public class Arrays1 {
public static void main(String[] args) {
int[] a1 = { 1, 2, 3, 4, 5 };
int a2[];
float f1[] = new float[5];
a2 = a1;
for(int i = 0; i < a2.length; i++)
f1[i] = (float)a2[i]++;
for(int i = 0; i < f1.length; i++)
System.out.println(
"a1[" + i + "] = " + f1[i]);
}
}
a. Program will give compiler error, because incompatable array type
b. output will be 0.0 0.0 0.0 0.0 0.0
c. output will be null null null null null
d. program will give complier error, because possible use of array before its
initialization.
e. will give 1.0 2.0 3.0 4.0 5.0
the answer given is e.
how does a2=a1 compile both being arrays?thanks in advance.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a2 is, after the assignment, an alias to the same object pointed by a1.
Being arrays of the same primitive type keeps the compiler happy.
 
Do not set lab on fire. Or this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic