www.webdeveloper.com
Results 1 to 5 of 5

Thread: exercise

  1. #1
    Join Date
    Sep 2005
    Posts
    8

    exercise

    i did not get this exercise...

    int [] p = new int [100];
    int [] s = p;

    Which of the following statements will change the last value of p to 75?

    a) p[99]=75;
    B) p[100]=75;
    C) s[99]=75;
    D)s[100]=75;
    E) two or more of the answers will change p to 75

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    array index start from 0 therefore always keep in mind that the when you set the array length it will start from 0 and the last index is length-1.
    So keeping that in mind last value in array will be at 99 index. Then there is another detail to remember that you are assingning the reference to array to another variable that is
    s=p
    which means a value changed in s is actually being changed in p not in s. keeping thi smind what can u see that which are ur answers?
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  3. #3
    Join Date
    Sep 2005
    Posts
    28
    Hey Indianj,

    If you got to change the value of the last value of p then,

    p[99]=75;

    Further,

    s[99]=75;

    Won't work since both are different and during declaration, only a reference of p[] would be passed to s[]and not p[] itself.

    Bye.

  4. #4
    Join Date
    Apr 2005
    Location
    Perth, Australia
    Posts
    340
    Java always uses its own type of pointers, when you create a new object and give it a name, in truth you are only making a pointer to that object by naming it, the object does not need a name. Arrays are indexed so an array of length 100 last element will be element 99

    a) p[99]=75; and C) s[99]=75;

    will both set the last value, therfore e) is the answer.

    ps. if this is for some kind of assignment or class work writing a program and finding out for your self would probably be a better method of investigation!

    Waylander.
    Last edited by Waylander; 09-14-2005 at 04:05 AM.

  5. #5
    Join Date
    Sep 2005
    Posts
    8
    thanks everyone..

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles