JAVA: Question Set – 02

JAVA: Question Set – 02

What is meant by the ‘Synchronized’ keyword?

The Synchronized keyword is used to lock a single thread with shared data so that no other thread can access it. We can use the Synchronized keyword to synchronize a block or a method. A thread, when it is inside the synchronized block acquires the lock and then releases it while leaving the block.

Explain ‘Marshalling’ and ‘Unmarshalling’.

Marshalling is the process of writing Java Objects to XML files. Unmarshalling is the process of converting XML files to Java objects.

What is the difference between the ‘throws’ and ‘throw’ keywords in Java?

  • The ‘throw’ keyword is used to explicitly throw an exception from a method or a block of code. It is used to throw custom exceptions.
  • The ‘throws’ keyword is used in the signature of a method that indicates that the method with which it is used, might throw one of the exceptions mentioned. We can declare multiple exceptions using the ‘throws’ keyword.

What is the difference between ‘StringBuffer’ and ‘StringBuilder’?

  • StringBuffer is synchronized, which means it is thread-safe. So, two threads cannot call the methods of StringBuffer simultaneously. It is less efficient than StringBuilder.
  • StringBuilder, on the other hand, is non-synchronized, which means it is not thread-safe. Two threads can call the methods of StringBuilder simultaneously. It is more efficient than StringBuffer.

What is meant by Coupling in OOPs?

Coupling is one of the most popular terms in OOPs. Coupling is used to denote the degree of dependency of one class on another. There are two types of coupling- tight coupling and loose coupling. Tight coupling means the classes are dependent on each other. Loose coupled means the classes are independent of each other. Loose coupling is always preferred.

What is meant by Cohesion in OOPs?

Cohesion is also one of the popular terms in OOPs. It is used to tell how well a single class is defined. A class should be well-designed and should have a clear purpose. There are two types of cohesion- high cohesion and low cohesion. High cohesion is when a class is well-defined and it is preferred to have high cohesion. Low cohesion is when a class is not well-defined.

What are Sockets in Java?

A socket is an endpoint for communication between machines. A socket is an endpoint for two-way communication between two programs. A socket is bound to a port number.

What is the difference between Stack memory and heap memory?

  • Stack memory is used to store local variables and the order of method execution. In stack, memory is stored in the form of LIFO(Last in First Out). It is a physical memory space in RAM allocated at the run-time.
  • The heap memory is used to store objects and dynamic memory allocation and deallocation. Objects are stored in the heap memory and their reference is stored in the stack memory.