SAS Research Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
3
of 3 vote

public class ExitTest
{
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");
}
}
}

- Guy September 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This works perfect!

- tarasan September 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

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.

- C# Guy. August 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think this is called at the time of garbage colllection to last executes of the code.

- om kumar August 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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();
        }
    }
}

- Anonymous August 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can register callbacks that run when System.exit() is called, but these run in a separate thread. I'm not sure that there's any way to use these callbacks to generate an exception within this thread, which would cause control to leave System.exit() and go into the finally block.

- Anonymous August 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The condition here is we cannot change main method. hence the above solution won't work

- kinshu August 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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!!

- kinshu August 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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");
            }
}

- eugene.yarovoi September 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
}}}

- balu August 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Condition is we cannot change main method.. I just wrote the condition inside "**" so that one can notice it

- kinshu August 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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");
}

}

}

- balu September 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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");
}

}

}

- Sushant September 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

here is simplest answer :

public class Program {

static{
System.out.println("Hi");
System.out.println("Good Bye");
System.exit(0);
}

public static void main(String[] args) {
try{
System.out.println("Hi");
System.exit(0);
}
finally{
System.out.println("Good Bye");
}

}

}

- shree August 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't understand what does the interviewer gains by asking such questions, that "we've crap code but you aren't allowed to fix it so let's write crappier code around it to have a work around until the whole system crumbles to the ground"

- Anonymous September 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Class Program
{
void method()
{
system.out.println("hi");
system.out.println("good bye");
}
public static void main(String args[])
{
Program o1=new Program()
o1.method();
}
}

- Anonymous September 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Class Program
{
void method()
{
system.out.println("hi");
system.out.println("good bye");
}
public static void main(String args[])
{
Program o1=new Program()
o1.method();
}
}

- Anonymous September 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
}
}

- deb September 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Just replace System.exit(0) by return statement as shown below:

class FinallyTest {
	public static void main(String args[]) {
		try {
			System.out.println("Hi");
			//System.exit(0);
			return;
		}

		finally {
			System.out.println("Good Bye");
		}
	}
}

Output:
Hi 
Good Bye

- Veeru August 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

interviewer is an idiot

- Anonymous September 05, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More