• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

object refrence variables

 
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following program, which statements are true?

A. Compile time error at line 3.
B. The program will throw a j ava.lang.ClassCastException at the line labelled 2 when
run.
C. The program will throw a j ava.lang.ClassCastException at the line labelled 3 when run.
D. The program will compile and run if the (B[ ] ) cast in the line 2 and the whole line 3 is removed.
E. The cast at line 2 is needed.

Correct Options are : C E
but according to me if C is correct then why not B. As a1 and a both refer to the same array object.
 
Sheriff
Posts: 27235
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:As a1 and a both refer to the same array object.



But they don't. Not at the line with the comment //2 at the end, anyway.
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:But they don't. Not at the line with the comment //2 at the end, anyway.


correct me if I am wrong
in starting both a and a1 were pointing to the same new A[10] but after //1 a and b will refer to new B[20] and a1 will refer to new A[10].
so no error or exception is at //2
 
Paul Clapham
Sheriff
Posts: 27235
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that looks right to me. (And it makes the answers in your original post come out correct, too.)

By the way could you please tell us where that code came from? Read our FAQ entry QuoteYourSources (<-- click that link) for information about why we want to know that.
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:By the way could you please tell us where that code came from? Read our FAQ entry QuoteYourSources (<-- click that link) for information about why we want to know that.


I have a book -> webpage

test1- question number 52
 
Ranch Hand
Posts: 5397
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me try to explain :

a= new A[10]; a1= a;
b= new B[20];
a= b; // 1
b= (B[]) a; // 2
b= (B[]) a1; // 3


Following are references to memory location :

a = 1000 -> A[10]
a1=1000 -> because of a1 = a
b = 2000 -> B[20]

because of line //1
a = 2000

NOTE, a1 is still pointing to 1000

//2 require casting,

//3 will throw run time casting exception because a1 points to A[]

HTH
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic