aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes intersting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "intersting" Watch "intersting" New topic
Author

intersting

Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
i didn't get what this meant by

Make the following line of code throw a java.lang.NullPointerException.


long x = y;

Answer


Long y = null;


q 2)

01 public class ConstantsCharsStrings
02 {
03 private ConstantsCharsStrings()
04 {
05
06 }
07
08 public static void main(String[] args)
09 {
10 char c1 = 'x';
11 final char c2 = 'y';
12 int i1 = 7;
13 final int i2 = 8;
14 final int i3 = i1 >>> 3;
15 final int i4 = i2 >>> 4;
16
17 System.out.println("xxx" == (c1 + "xx"));
18 System.out.println("yxx" == (c2 + "xx"));
19 System.out.println("7xx" == (i1 + "xx"));
20 System.out.println("8xx" == (i2 + "xx"));
21 System.out.println("7xx" == (i3 + "xx"));
22 System.out.println("8xx" == (i4 + "xx"));
23 }
24 }


What is the output and why?

a) false false false false false false
b) true true true true true true
c) false true false true false true
d) false true false false true false
e) false true false true false false
f) compile-time error ________
g) runtime exception ________
h) something else ________
[ April 19, 2005: Message edited by: Parameswaran Thangavel ]
Kalyani Marathe
Ranch Hand

Joined: Apr 06, 2005
Posts: 72
Hi,

For the first ques. if you make y= null then any .operator will result in exception so that could be the answer.

I don't know if it is true or not. But I guessed the ans for 2nd to be "e".
There won't be runtime or compile error as char can be added to String.
I know last two comparisons are false and I assumed the final constant
should evaluate to true on ==. So I chose "e"

Thanks.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Long y = null;
long x = y;

This will throw a NullPointerException.
This is one of two new ways of implicitly dereferencing a reference in J2SE 1.5.


Tony Morris
Java Q&A (FAQ, Trivia)
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
I ran the first question (don't know why the original poster didn't run it) and e is the correct answer.

As for reasoning. The final variables can be resolved at compile time so they are treated as constants.
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi tony
u mentioned this is one of two new ways of implicitly dereferencing a reference in J2SE 1.5

can i know the other way of implicityl dereferenciung a reference
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
String[] a = null;
for(String s : a) // NullPointerException - implicit dereference of a
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi tony

i didn't cameacross this type of syntax for(String s : a)in k&B book
is this included in the scjp1.4
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
It is the new Tiger feature: enhanced for-loop.

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: intersting
 
Similar Threads
String == returns true?
OO basics doubt
Constant - Char & String
Small doubt in Boxing & == & equals()
== , equals() method problem