More java programming questions

What is virtual memory:

In computing, virtual memory is a memory management technique that is implemented using both hardware and software. It maps memory addresses used by a program, called virtual addresses, into physical addresses in computer memory.

The operating system manages virtual address spaces and the assignment of real memory to virtual memory. Address translation hardware in the CPU, often referred to as a memory management unit or MMU, automatically translates virtual addresses to physical addresses. Software within the operating system may extend these capabilities to provide a virtual address space that can exceed the capacity of real memory and thus reference more memory than is physically present in the computer.

What is the heap and stack

1) Main difference between heap and stack is that stack memory is used to store local variables and function call, while heap memory is used to store objects in Java.

What is runnable object

A Runnable is basically a type of class (Runnable is an Interface) that can be put into a thread, describing what the thread is supposed to do.

The Runnable Interface requires of the class to implement the method run() like so:

public class MyRunnableTask implements Runnable {
public void run() {
// do stuff here
And then use it like this:

Thread t = new Thread(new MyRunnableTask());

Advanced: Anonymous Type

Note that you do not need to define a class as usual, you can do all of that inline:

Thread t = new Thread(new Runnable() {
public void run() {
// stuff here

Notice that another way to create a class that extends Thread class in Java. This is less flexible than using the Runnable interface, but easier to code

Class MyThread extends Thread {
public void run() {
//code here

To run the new Thread, just call
(new MyThread()).start();

What is final method.

You can declare some or all of a class’s methods final. You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses. The Object class does this—a number of its methods are final.

What is java final object

You are always allowed to initialize a final variable. The compiler makes sure that you can do it only once.

Note that calling methods on an object stored in a final variable has nothing to do with the semantics of final. In other words: final is only about the reference itself, and not about the contents of the referenced object.

Final class means the class can not be extended. However, it can extend another non-final class.

What is the difference between interface and abstract class

1. Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.
2. Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
3. Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..
4. Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.
5. An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
A Java class can implement multiple interfaces but it can extend only one abstract class.
Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.
In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.

What is the range of short and int primitive type
The range of int is from 0 to 2^32 – 1 or -2^31 to 2^31 – 1 for signed integer.

Ones complement -> reverse all the bits to represent the negative value, which result to +0 and -0. i.e. +1 => 0001 -1 => 1110

Twos compelement -> reverse all the bits and plus one to represent the negative value. i.e. +1 => 0001 -1=> 1111

What is the difference between process and thread

1) Both process and Thread are independent path of execution but one process can have multiple Threads.

2) Every process has its own memory space, executable code and a unique process identifier (PID) while every thread has its own stack in Java but it uses process main memory and share it with other threads.

3) Threads are also refereed as task or light weight process (LWP) in operating system

4) Threads from same process can communicate with each other by using Programming language construct like wait and notify in Java and much simpler than inter process communication.

5) Another difference between Process and Thread in Java is that it’s How Thread and process are created. It’s easy to create Thread as compared to Process which requires duplication of parent process.

6) All Threads which is part of same process share system resource like file descriptors , Heap Memory and other resource but each Thread has its own Exception handler and own stack in Java.

There were some of the fundamental difference between Process and Thread in Java. Whenever you talk about Process vs Thread, just keep in mind that one process can spawn multiple Thread and share same memory in Java. Each thread has its own stack.

What is the difference between a HashMap and a HashTable.

The 2 are very similar except 4 differences.
1. HashMap is not thread safe and HashTable is, which also means in a single thread environment the HashMap is preferred since it is faster.
2. HashMap accepts null as key and value and HashTable does not
3. HashMap iterator is fail fast (if you modify the content of the HashMap using methods other than the iterator’s own remove() method, a ConcurrentModificationException will be thrown) The enumerator of HashTable will not and may throw an unexpected exception.
4. HashMap has a subclass LinkedHashMap, which keeps an order of the elements. So it is easier if we would like to swap the HashMap to LinkedHashMap when we would like to implement order of the element.

How to implement a immutable class in Java
What is an immutable object: The attribute of the object can only be set once, at the time when it is initialized, and can not be changed later
1. first remove all the setter method from the class.
2. set all member variables private and final so that they can not be modified or point to other objects.
3. Make all method final to make sure they can not be overridden. A simpler way is to make the class final, a more sophisticated way is to make the constructor private and produce new instance form a factory pattern.
4. If a member variable is an object, make sure its reference is not exposed to the outside (returned by a public function) so it can not be modified.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s