• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

explain the answer  RSS feed

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

This question is taken Valentin's Mock Exam . can any one explain the answer .Is this under Java cert objective?


public class Question24
{
public static void main(String[] args)
{
Question24 q24 = null;
int i = q24.throwDice(); //line 1
int j = getDice().throwDice(); //line 2
System.out.println("Result: "+i+","+j);
}
private static int throwDice(){
return 1+(int)(Math.random()*6);
}
private static Question24 getDice(){
return null;
}
}
A. Compilation error.
B. The code compiles but throws a NullPointerException at line 1 when run.
C. The code compiles but throws a NullPointerException at line 2 when run.
D. The program will always print Result: x,y, where x and y are two numbers between 1 and 6.
E. The code compiles fine.
 
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Question24
{
public static void main(String[] args)
{
Question24 q24 = null;
int i = q24.throwDice(); //line 1
int j = getDice().throwDice(); //line 2
System.out.println("Result: "+i+","+j);
}
private static int throwDice(){
return 1+(int)(Math.random()*6);
}
private static Question24 getDice(){
return null;
}
}


I think answer 2 is correct .
q24 is reference that is pointing to null ( has no object ) & when we call anything from null reference it throws NullPointerException .
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The answer D & E is correct and the program complies and runs perferctly.

The reason for not getting a NullPointerException is the method throwDice() method is static.

Because static method invocations are done based on reference type and doesn't cares about value of reference variable (even it could be null).

If you make throwDice() method as non-static try to call getDice().throwDice() method you would get null pointer exception.

check out this

JLS

for more explanation

- Raja.
 
ganesh subbiah
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

Thanx for the answer i know it is d & e ..But i need someone who can
simplify this concept of Target Reference and Static Methods ...with an easy explanaion
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!