• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

My interview questions

 
Manikandan Swaminathan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Some of the interview questions faced by me recently and the answers below.

1. Do we really need a finally() block?
[Ans]: Yes its better to have a finally block as we can close the connection objects and free other resources here.

2. I have some statements after try,catch,finally block.Will that be executed?
[Ans]: If there is any exception, the program control will go to the catch block & finally block & then the program execution stops.
If there is no exception, then the statements after the finally() block will be executed.

3. There is a class A.java & it has a method. Class B extends Class A & Class C extends Class B inturn. How to prevent the method in Class A from being accessed in Class C using C's instance? (Method in A is public ONLY)
[Ans]: I told that we can override the method in Class C with the same name & syntax as that of Class A so that the overridden method is called.. Dont know whether my answer is correct

4. In a singleton java class, how to make sure that more than one instance of the object is not getting created at any point of time?
[Ans]: Dont know (Is it possible through cloning ??)

Can you please review and let me know if my answers are correct.

Any suggestions are warmly welcome.

Thanks

 
arulk pillai
Author
Ranch Hand
Posts: 3387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
4. In a singleton java class, how to make sure that more than one instance of the object is not getting created at any point of time?


-- Firstly you need to ensure that your singleton class gets loaded by a single class loader.

-- How the instances are created? Eagerly instantiated or Lazily instantiated. If laziliy loaded, ensure that multiple threads cannot create more than a single instance. Google for double checked locking anti-pattern

-- Multiple singletons arising when Someone has subclassed your singleton. Ensure that the constructor is either private or protected to prevent instantiation from outside or extension. Where possible, mark your class as final.

-- it is possible for an instance of the class to be serialized once, then deserialized many times, resulting in many instance being created.

-- Reflection attacks are possible.


So, if you are using Java 5 or later then use the "enum".




for example --> http://whiteboxcomputing.com/java/enum_factory/


Also read, http://java.sun.com/developer/technicalArticles/Programming/singletons/



 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghuraman Guruswamy wrote:

2. I have some statements after try,catch,finally block.Will that be executed?
[Ans]: If there is any exception, the program control will go to the catch block & finally block & then the program execution stops.
If there is no exception, then the statements after the finally() block will be executed.



2. This is incorrect, if an Exception is thrown and it is the class (or subclass) of the Exception you have in your catch(..) then the catch block is executed, then the finally block and then after that the statements after the try-catch-finally will also be executed. If the exception is not caught by the catch block then the current execution will stop and an exception will be thrown which could be caught higher up.

Sean
 
arulk pillai
Author
Ranch Hand
Posts: 3387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only time the finally block is not reached is when

-- there is a System.exit() statement.
-- there is an endless loop

Other times, the finally block is always executed




The ouput is:

 
Manikandan Swaminathan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All,

Thanks a lot for all of your replies.

Would be great if you can review my third question also.

Cheers
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arulk pillai wrote:Only time the finally block is not reached is when

-- there is a System.exit() statement.
-- there is an endless loop
There are other possibilities, as in this post from 2006.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghuraman Guruswamy wrote:All,

Thanks a lot for all of your replies.

Would be great if you can review my third question also.

Cheers


My answer would be a reverse question to the interviewer. 'If you did not want the method to be inherited, why did you extend the class to begin with ?'

Either I do not understand the purpose of question 3, or the interviewer does not understand OO. Or was he / she trying to allude to a OperationNotSupportedException ?

[EDIT]

Replaced 'it' with class to add clarity
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2. I have some statements after try,catch,finally block.Will that be executed?

My quick answer which I'd guess they're looking for: It depends if the catch or finally block throws an exception.

That also answers the first question - i.e., yes, you need the finally block to ensure your clean up code (in the finally block) will be executed even if the catch block (intentionally or otherwise) throws an Exception.

Anyway, make sure you post back when you know if you got the job or not! (If you don't, I hope it's just an Exception!... )
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-- it is possible for an instance of the class to be serialized once, then deserialized many times, resulting in many instance being created.


The class has to implement Serializable interface for this to happen. If the constructor is protected you can subclass a singleton class and serialize it as the subclass can access the no arg constructor of the superclass. If the constructor is private no issues as you cannot subclass.
 
Joe Harry
Ranch Hand
Posts: 10112
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if we clone the singleton instance?? I guess it is a good practice to override clone and throw an exception if an attempt to clone a singleton instance is made.
 
jerry vn
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jothi Shankar Kumar wrote:What happens if we clone the singleton instance?? I guess it is a good practice to override clone and throw an exception if an attempt to clone a singleton instance is made.


Hi everybody,

I uploaded one file. But It can not display. I used internet explorer 8.0.

How can I repair? Or some problems happened to IE8?

Rgs
 
Sandeep Awasthi
Ranch Hand
Posts: 597
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check url, check server log, right click and check view source, use debugger ...........
Anyway this is job discussion forum. Ask question in respective forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic