Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger


Sunday 31 August 2008

Java Interview Questions and Answers 31

Does Java pass by Value or reference?
Its uses Reference while manipulating objects but pass by value when sending method arguments. Those who feel why I added this simple question in this section while claiming to be maintaining only strong and interesting questions, go ahead and answer following questions.
a)What is the out put of:

import java.util.*;

class TestCallByRefWithObject
{
ArrayList list = new ArrayList(5);


public void remove(int index){
list.remove(index);
}

public void add(Object obj){
list.add(obj);
}

public void display(){
System.out.println(list);
}

public static void main(String[] args)
{
TestCallByRefWithObject test = new TestCallByRefWithObject();

test.add("1");
test.add("2");
test.add("3");
test.add("4");
test.add("5");

test.remove(4);
test.display();
}
}

b) And now what is the output of:


import java.util.*;

class TestCallByRefWithInt
{
int i = 5;


public void decrement(int i){
i--;
}

public void increment(int i){
i++;
}

public void display(){
System.out.println("\nValue of i is : " +i);
}

public static void main(String[] args)
{
TestCallByRefWithInt test = new TestCallByRefWithInt();

test.increment(test.i);

test.display();
}
}

Why Thread is faster compare to process?
A thread is never faster than a process. If you run a thread(say there's a process which has spawned only one thread) in one JVM and a process in another and that both of them require same resources then both of them would take same time to execute. But, when a program/Application is thread based(remember here there will be multiple threads running for a single process) then definetly a thread based appliation/program is faster than a process based application. This is because, when ever a process requires or waits for a resource CPU takes it out of the critical section and allocates the mutex to another process.
Before deallocating the ealier one, it stores the context(till what state did it execute that process) in registers. Now if this deallocated process has to come back and execute as it has got the resource for which it was waiting, then it can't go into critical section directly. CPU asks that process to follow scheduling algorithm. So this process has to wait again for its turn. While in the case of thread based application, the application is still with CPU only that thread which requires some resource goes out, but its co threads(of same process/apllication) are still in the critical section. Hence it directly comes back to the CPU and does not wait outside. Hence an application which is thread based is faster than an application which is process based.
Be sure that its not the competion between thread and process, its between an application which is thread based or process based.

When and How is an object considered as Garbage by a GC?
An object is considered garbage when it can no longer be reached from any pointer in the running program. The most straightforward garbage collection algorithms simply iterate over every reachable object. Any objects left over are then considered garbage.

No comments:

Post a Comment