Credit Suisse Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
2
of 4 vote

Yes. It will terminate the whole program. Other threads will also stop.

- Brandy May 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why ? can u explain ?

- Rajesh May 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you are wrong. It won't terminate whole program.

- Atul September 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if its synchronized lock is automatically removed and other thread continues to execute. if there is Lock mechanism we need the remove the lock explicitly in finally block. since there is no exception handling done this thread will have the lock and the program hangs. correct me if am wrong

- Sirius February 17, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

No. Other threads will continue to run without impact.

You can test it with following program. It does not matter if its an Checked - Unchecked Exception / Error

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ThreadLocalExample {
    static int i=0;
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                ThreadLocal<String> threadLocal = new ThreadLocal<String>();
                ThreadLocal<Long> threadLocalInt = new ThreadLocal<Long>();
                threadLocal.set(Thread.currentThread().getName());
                threadLocalInt.set(Thread.currentThread().getId());
                while(true){
                    try {
                        TimeUnit.MILLISECONDS.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("I am Thread "+Thread.currentThread().getName()+ " with variable "+threadLocal.get()+" Integer "+threadLocalInt.get());
                }
            }
        };
        Runnable runnableException = new Runnable() {
            @Override
            public void run() {
                try {
                    TimeUnit.MILLISECONDS.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                throw new Error("I am getting killed "+Thread.currentThread().getName());
            }
        };
        ExecutorService service = Executors.newFixedThreadPool(5);
        service.execute(runnableException);
        service.execute(runnable);
        service.execute(runnable);
        service.execute(runnable);
        service.execute(runnable);
        service.shutdown();
    }
}

- vishal.avad May 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

No. As thread is running in process address space, it will terminate you process.

Here is the linux code where thread 2 wont reach at the second printf.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

static void *thread_fun1 (void *arg)
{
	char *msg = (char *) arg;
	char *p = NULL;
   printf (" Thread Func : Message = \"%s\"\n",msg);
	*p='a';
}
static void *thread_fun (void *arg)
{
	char *msg = (char *) arg;
	int i=0;
   printf (" Thread Func : Message = \"%s\"\n",msg);
   while ( i++ < 10000) sleep (1000);
   printf (" Thread Func : Message = \"%s\"\n",msg);
}

int main ()
{
	pthread_t thread1, thread2;
	const char *msg1=" Thread 1";
	const char *msg2=" Thread 2";
	int iret1, iret2;

	/* concrete independent threads each of which will execute function */
	iret1 = pthread_create (&thread1, NULL, thread_fun, (void *) msg1);
	if (iret1) {
		fprintf (stderr, "Error : pthread_create () return code %d\n", iret1);
		exit (EXIT_FAILURE);
	}
	
	/* concrete independent threads each of which will execute function */
	iret1 = pthread_create (&thread2, NULL, thread_fun1, (void *) msg2);
	if (iret1) {
		fprintf (stderr, "Error : pthread_create () return code %d\n", iret1);
		exit (EXIT_FAILURE);
	}
	pthread_join (thread1, NULL);
	pthread_join (thread2, NULL);
        printf (" At the End\n");
}

- Sach June 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't think that will terminate all other threads...if we think this way...the program( i.e. the main) as the parent thread and all other threads as child threads.....then termination of one child shouldn't affect the other.
Please correct me if that's wrong logic

- sushmita abhyankar July 31, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well, C++ has a predefined function called terminate which will be called when there is no matching handler for the exception. The terminate function will then call another function named abort. The abort function will then stop program execution.

- Anonymous September 22, 2017 | 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