• 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

Arrays

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A {}
class B extends A {}

public class C
{
public static void main(String[] args)
{
A[] arrA;
B[] arrB;

arrA = new A[10];
arrB = new B[20];

arrA = arrB; // (1)
arrB = (B[]) arrA; // (2)

arrA = new A[10];
arrB = (B[]) arrA; // (3)
}
}

Select one right answer.


1 The code will fail to compile owing to the line labeled (1)
2 The code will throw a java.lang.ClassCastException at the line labelel (2), when run
3 The code will throw a java.lang.ClassCastException at the line labelel (3), when run
4 The code will compile and run without problems, even if the (B[]) cast in the lines labeled (2) & (3) were removed
5 The code will compile and run without problems, but would not do so if the (B[]) cast in the lines (2) & (3) were removed
 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
kindly explain??
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whats the answer ?
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3..
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey dude you cant simply ask us to explain every question....first you should try to understand it...then ask where you got stuck..no hard feelings ok
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok..i got the answer..
The line labeled (1) will be allowed during compilation, since assignment is done from a subclass reference to a superclass reference. The line labeled (2) convinces the compiler that arrA will refer to an object that can bereferenced by arrB, and this will work when run, since arrA will refer to an object of type B[]. The line labeled (3) also convinces the compiler that arrA will refer to an object that can be referenced by arrB.This will not work when run, since arrA will refer to an object of type A[].
reply
    Bookmark Topic Watch Topic
  • New Topic