Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays

 
Smitha Ballikar
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • 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 ]
 
vijaya bacina
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • 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
 
Ryan Kade
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • 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 ]
 
raghu babu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
hi Option C is wrong
and it neither outputs empty string nor null
it raises NullPointer Exception
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic