• 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: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following variable declarations.

The following options make statements about the results of various operations executed before any change has affected these variables. Which of these statements is true?




A The logical test x == null will return true.

B The value retrieved by addressing y[ 0 ] is 0.

C Addressing the element s1[ 1 ] produces a null value.

D Addressing the element s2[ 1 ] produces an empty String.


Your recorded answer B,C
Answer given:A,B


I felt B,C
All the arrays are intialized by default. So elements of object arrays will be assigned to null.
But here its given array object itself is assigned to null .Is it specific to static arrays or string arrays??

Please explain!!

Cheers
Smitha
[ August 24, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A The logical test x == null will return true.
B The value retrieved by addressing y[ 0 ] is 0.
C Addressing the element s1[ 1 ] produces a null value.

A is true
x is declared as static integer array
static fileds get initialized during class load...
so x is assigned to null since arrays are treated as objects in java
B is true
arrays elements get default values when initialized
C is False
since S[1]="" and not null since it is String object
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A is correct because x is an instance variable pointing to an array. Since arrays are objects and all instance variables are initialized to their default values, x will contain null, the default value for objects. Note, the array hasn't actually be instantiated yet.

B is correct because all elements of a newly initialized array are set to their default values. Since B is pointing to an array of integers and the initial value for integers is 0, all 20 values in the array referenced by y are 0.

C is incorrect because the array has not been initialized. Only the instance variable has been created. The variable s1 points to null just like the int array x does. Trying to access s1[1] will throw an exception.

D is incorrect. Remember the rule from answer B: all elements of a newly initialized array are set to their default values. Since Strings are objects, all the elements of s2 are initialized to null, NOT to empty Strings.

Hope that helps!
[ August 24, 2005: Message edited by: Ryan Kade ]
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>>Addressing the element s1[ 1 ] produces a null value.

s1 is declared as a String of Arrays, but the declaration haven't
yet provided how much space to be allocated, i.e., it is not declared
something like
String[] s1 = new String[10];


>>Addressing the element s2[ 1 ] produces an empty String.
This doesn't produce empty String, but outputs null, as element
of s2 is a String and String gets a default null reference value.
 
vijaya bacina
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Option C is wrong
and it neither outputs empty string nor null
it raises NullPointer Exception
 
reply
    Bookmark Topic Watch Topic
  • New Topic