Welcome Guest | Sign in | Register
Inner Classes - Discussion Page For Q.3015 | Java Programming Questions and Answers | Java Programming Free Online-Tests | LucentBlackBoard | LucentBlackBoard.com

Home > Java Programming > Inner Classes > Questions and Answers

Q:  What will be the output of the program?
public class HorseTest
{
public static void main (String [] args)
{
class Horse
{
public String name; /* Line 7 */
public Horse(String s)
{
name = s;
}
} /* class Horse ends */

Object obj = new Horse("Zippo"); /* Line 13 */
Horse h = (Horse) obj; /* Line 14 */
System.out.println(h.name);
}
} /* class HorseTest ends */
A. An exception occurs at runtime at line 10. B. It prints "Zippo".
C. Compilation fails because of an error on line 7. D. Compilation fails because of an error on line 13.

Answer and Explanation

Answer:It prints "Zippo".

Explanation:
The code in the HorseTest class is perfectly legal. Line 13 creates an instance of the method-local inner class Horse, using a reference variable declared as type Object. Line 14 casts the Horse object to a Horse reference variable, which allows line 15 to compile. If line 14 were removed, the HorseTest code would not compile, because class Object does not have a name variable.

Comment on this Question

Name:

Loading...




Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.