SAS Research Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
I believe this can be answered without knowing Java, but having an idea about what finally means (I don't know Java very well).
I bet Java provides some callbacks which are called during System.Exit. Setup those, and throw an exception in the callback.
i think this is called at the time of garbage colllection to last executes of the code.
Implement your own SecurityManager, override checkExit(int status) to raise SecurityException when status==0. Also make sure set all these up in a static block of Program class. That way we do not need to touch main() at all. Here is the code:
public class Program {
static {
System.setSecurityManager(new MySecurityManager());
}
public static void main(String[] args) {
try {
System.out.println("Hi");
System.exit(0);
}
finally {
System.out.println("Good Bye");
}
}
public static class MySecurityManager extends SecurityManager {
public MySecurityManager() {
}
public void checkExit(int status) {
super.checkExit(status);
if (status == 0)
throw new SecurityException();
}
}
}
using SecurityManager will work.. But the output will be
Hi
Good Bye
SecurityException is thrown
I dont want the exception to be thrown. I want the good Bye to be executed from finally without any exception.. This is wat interviewer told me!!
Having it executed from finally is not really an option, if you consider that some exception HAS TO arise in order for the finally block to be reached at all (well, you could maybe find some way to execute all that from within a catch block and suppress the exception, but I'm not sure that's what you want).
However, you can try this:
public void checkExit(int status) {
super.checkExit(status);
//hehehe
if (status == 0)
{
System.out.println ("Good Bye");
}
}
Class Program{
public static void main(String args[]) // change to public
{
Try{
System.out.println(“Hi”);
}
Finally{
System.out.println(“Good Bye”);
System.exit(0);
}}}
Condition is we cannot change main method.. I just wrote the condition inside "**" so that one can notice it
then the underlaying code works
public class Program {
static{
System.out.println("Hi");
System.out.println("Good Bye");
}
Public static void main(String[] args)//generates the exception
{
try{
System.out.println("Hi");
System.exit(0);
}
finally{
System.out.println("Good Bye");
}
}
}
Output of this will be:
Hi
GoodBye
Hi
Correct solution is :
public class Test1 {
static{
System.out.println("Hi");
System.out.println("Good Bye");
System.exit(0);
}
public static void main(String[] args)//generates the exception
{
try{
System.out.println("Hi");
System.exit(0);
}
finally{
System.out.println("Good Bye");
}
}
}
public class Test{
public static void main(String[] args)
{
try{
//IF YOU PUT SYSTEM.EXIT(0) IN THE TRY BLOCK THEN IT WILL TERMINATE THE CODE WITHOUT EXECUTING THE FINALLY BLOCK. SO MAKE SURE THAT IT SYSTEM.EXIT(0) CONTAINS IN THE FINALLY BLOCK. SO THAT IT COULD GIVE YOU THE EXACT RESULT AS PER THE REQUIREMENT.
System.out.println("Hi");
}finally{
System.out.println("Good Bye");
System.exit(0);
}
}
public class ExitTest
- Guy September 24, 2012{
static {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println("Good Bye");
}
});
}
public static void main(String args[]){
try
{
System.out.println("Hi");
System.exit(0);
}
finally
{
System.out.println("Good Bye");
}
}
}