File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Arrays" Watch "Arrays" New topic
Author

Arrays

Smitha Ballikar
Ranch Hand

Joined: Aug 02, 2005
Posts: 99
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

Joined: Aug 23, 2005
Posts: 155
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

Joined: Aug 16, 2005
Posts: 69
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

Joined: Jul 05, 2005
Posts: 60
>>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

Joined: Aug 23, 2005
Posts: 155
hi Option C is wrong
and it neither outputs empty string nor null
it raises NullPointer Exception
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays
 
Similar Threads
Sun Cirtification
New Mock Exam Questions
Please explain
Inheritence
Generic