• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

intersting

 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String[] a = null;
for(String s : a) // NullPointerException - implicit dereference of a
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is the new Tiger feature: enhanced for-loop.

Nick
 
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic