This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP Brainteaser (2) 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 "SCJP Brainteaser (2)" Watch "SCJP Brainteaser (2)" New topic
Author

SCJP Brainteaser (2)

Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Ok, new one:

What is the output when the following code is compiled and run?


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9396
    
    2

Hi Above,

"A NullPointerException gets printed out."

Change it to,

public class Main {


public static String getNumber(String label, Integer number) {
return "1: " + label + "=" + number.intValue();
}

public static String getNumber(String label, Number number) {
return "2: " + label + "=" + number.intValue();
}

public static void main(String[] args) {
try{
System.out.println(getNumber("Sum", null));
}catch(NullPointerException ss){
System.out.println("I get printed out from catch block");
}
}
}

Prints I get printed out from catch block.

When I change the method call as System.out.println(getNumber("Sum", 10));, the method with Integer argument is called as it is the most relevant one.
and prints out 1: Sum=10

Regards,
Jothi Shankar Kumar. S

[ October 30, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
[ October 30, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
joshua antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 117
I think we will get NullPointerException at runtime
Krishna Srinivasan
Ranch Hand

Joined: Jul 28, 2003
Posts: 1844

It throws Null pointer exception.


Krishna Srinivasan
Spring Tutorials, OCAJP Mock Questions, 400+ OCPJP Mock Questions
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
NullPointerException will be thrown.
From Valentin's question, one should also ask themself if one pass 123, 123.2, long, or etc. instead of "null" which method is going to invoke and why.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
And what about this?



all events occur in real time
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9396
    
    2

Hi Burkhard Hassel,

It will print out Integer: 1, provided that your class Main is public and also you change the Number n = 1 to Integer = 1. It complains at the line Number = 1 as required an int.

Regards,
Jothi Shankar Kumar. S
[ October 30, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
sai vargheese
Greenhorn

Joined: Sep 24, 2006
Posts: 2
Hi All,
A simple question.
Which method would get invoked???
one with Integer or Number,
Which method would be invoked.
I tried it this code, the method with Integer got invoked,
But i would like to know how this happens??

Somebody help me out.
Eddy Lee Sin Ti
Ranch Hand

Joined: Oct 06, 2005
Posts: 135
I think the code will works and print out "Number:1"

the line "Number n = 1;" is a tricky one and my understanding is that the value 1 will be boxed up into an instance of Integer and cast to the abstract class Number.


SCJP, SCWCD, SCJWS, IBM 700,IBM 701, IBM 704, IBM 705, CA Clarity Technical<br /> <br /><a href="http://eddyleesinti.blogspot.com" target="_blank" rel="nofollow">http://eddyleesinti.blogspot.com</a>
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9396
    
    2

Hi Above,

I think the code will works and print out "Number:1"

the line "Number n = 1;" is a tricky one and my understanding is that the value 1 will be boxed up into an instance of Integer and cast to the abstract class Number.


For me the declaration Number n = 1 says,
found : int
required: javaapplication19.Number
Number n = 1;
1 error
BUILD FAILED (total time: 0 seconds)

Why is that and how come that for you it prints Number : 1? Did you try it out? Please let me know. It's a good question, a real brainteaser.

Regards,
Jothi Shankar Kumar. S
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9396
    
    2

Hi Sai Varghese,

your quote,

A simple question.
Which method would get invoked???
one with Integer or Number,
Which method would be invoked.
I tried it this code, the method with Integer got invoked,
But i would like to know how this happens??


The method with the most significant argument will be invokde. Here Interger which is a subclass of Number is the most significant. So it gets invoked.

Regards,
Jothi Shankar Kumar. S
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

Jothi Shankar Kumar Sankararaj posted October 30, 2006 04:34 AM
I
t will print out Integer: 1, provided that your class Main is public and also you change the Number n = 1 to Integer = 1. It complains at the line Number = 1 as required an int.





That't true - for Java 1.4
But it wouldn't compile in Java 1.4 anyway because of the vararg in the main method.
I thought that it should be clear then, that it's Java5.

Classes don't have to be public to run their main methods!

Only if you like to start their main method from a different package.


Special service: the same for java 1.3 - runs 1.4 as well )-;





(and further down the thread)
The method with the most significant argument will be invokde. Here Interger which is a subclass of Number is the most significant. So it gets invoked.

Ha! Got you!


No. reference type of n is Number, so the number version of the overloaded method is called. Object type doesn't matter here. Object type only matters in overridden methods.

Bu
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9396
    
    2

Hi Bu,

I compiled the program using netBeans5.0 which works with jdk5 and gave me the error.

For me the declaration Number n = 1 says,
found : int
required: javaapplication19.Number
Number n = 1;
1 error
BUILD FAILED (total time: 0 seconds)

I did not get your statement as to how one can compile without a public class in a package? Can you please explain me.

Regards,
Jothi Shankar Kumar. S
anvesh charuvaka
Greenhorn

Joined: Sep 25, 2006
Posts: 11
Hi Ranchers

The above code gets compiled and prints Number:1

In the assignment in the main metod
Number n = 1;

the int literal is first Boxed to an Integer followed by a Reference Widening Cast which makes it possible to assign it to Number

The method with Number as argument is called because when calling a method implicit narrowcast does not Occur.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi Jothi,

compiled with NetBeans ... the first one (the one with the varargs) or the second one?
With Eclipse and also the original JDK15 it compiles nicely.

Don't need to be public:
One file, called DoesntMatter.java
two non-public classes
any package(doesn't matter here)
javac DoesntMatter.java
java Main

or if you manage to type
java €Number on the console ... different topic...

will produce Number:1

Maybe, (don't know) NetBeans does not allow multiple classes in one file?
Or has problems with the € symbol?
I'm guessing here

Yours,
Bu.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Alright guys ! It does throw a NullPointerException !

Now:
1) how about modifying the call to getNumber as follows: getNumber("Sum", new Integer(1));
2) how about modifying the call to getNumber as follows: getNumber("Sum", new Number(1));

What gets printed out in each case ? Try compiling and executing mentally before throwing it in the compiler!
Rancy Chadha
Ranch Hand

Joined: Jul 12, 2006
Posts: 135
Hi Valentine,

If we change add following lines

1) getNumber("Sum", new Integer(1));
It will compile and will give output 1: sum=1

2) getNumber("Sum", new Number(1));
Addition of this line will result in COMPILATION ERROR
The reason it happens is pretty obvious Number class is abstract class
and abstract class cannot be instantiated.

Thanks,
Rancy


Thanks,<br />-Rancy
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Good job guys !!

The key here was to know how methods get resolved when executing an overloaded method.

Relevant pointers to the JLS:
15.12.2 Compile-Time Step 2: Determine Method Signature
15.12.3 Compile-Time Step 3: Is the Chosen Method Appropriate?
15.12.4 Runtime Evaluation of Method Invocation

You might also want to have a look at an older article I wrote a couple years ago on the same subject: JLS 15.12 (Method Invocation Expressions) in Plain English

Thanks to those who participated. Stay tuned...

Previous brainteasers:
SCJP Brainteaser 1
sai vargheese
Greenhorn

Joined: Sep 24, 2006
Posts: 2
Thanks Jothi Shankar!!
Thanks to Valentin for starting this thread.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: SCJP Brainteaser (2)