Monday, October 31, 2016

java-page2

6. Define class?

Ans:
A class is a blue print from which individual objects are created. A class can contain fields and methods to describe the behaviour of an object.

7. What is JVM?

Ans:
Java Virtual Machine (JVM) :
It is an interpreter for bytecode. JVM is a platform independent and converts Java byte code into machine language and executes it. Program calls compiler to translate the program code into executable code that computer can understand and execute. The executable code depends upon the computer operating system that we use to execute our program. It is machine dependent.

8. What is multithreading and what is the class in which these methods are defined?

Ans:
Multithreading is the mechanism which allows us to do many things simultaneously. A multithreaded enable us to executed two or more program at same time which are totally independent of each other. Each part of such a program is called a thread. Each thread defines a separate path of execution inside the program. To communicated between two threads we use methods like wait (), notify () and notifyAll() and these methods are in Object class.wait().

9. What is the difference between Process and Thread?

Ans:
The major difference between threads and processes is:

1) Threads share the address space of the process that created it whereas processes have their own address.
2) Threads can directly access to the data segment of its process whereas processes have their own copy of the data segment of the parent process.
3) Threads communicate directly with other threads of its process whereas processes use inter-process communication to communicate with child processes.
4) Threads have almost no overhead whereas processes have considerable overhead.
5) New threads are easily created whereas new processes require duplication of the parent process.
6) Threads can exercise considerable control over threads of the same process; processes can only exercise control over child processes.
7) Changes to the main thread affect the behavior of the other threads of the process whereas changes to the parent process do not affect child processes.

10. How do you declare a class as private?

Ans:
We can declare a private class as an inner class.

For example :

class MyPrivateClass
{
   private static class MyKey
   {
       String key = "91234";
   }
   public static void main(String[] args)
   {
       System.out.println(new MyKey().key);//prints 91234
   }
}




11. What do you mean by Constructor?

Ans:
Constructor gets invoked when a new object is created. Every class has a constructor. If we do not explicitly write a constructor for a class the java compiler builds a default constructor for that class.

12. What is data encapsulation and what’s its significance?

Ans:
Encapsulation is a concept in Object Oriented Programming for combining properties and methods in a single unit.
Encapsulation helps programmers to follow a modular approach for software development as each object has its own set of methods and variables and serves its functions independent of other objects. Encapsulation also serves data hiding purpose.

13. What are Loops in Java? What are three types of loops?

Ans:
Looping is used in programming to execute a statement or a block of statement repeatedly. There are three types of loops in Java:

1) For Loops
For loops are used in java to execute statements repeatedly for a given number of times. For loops are used when number of times to execute the statements is known to programmer.

2) While Loops
While loop is used when certain statements need to be executed repeatedly until a condition is fulfilled. In while loops, condition is checked first before execution of statements.

3) Do While Loops
Do While Loop is same as While loop with only difference that condition is checked after execution of block of statements. Hence in case of do while loop, statements are executed at least once.

14.  How garbage collection is done in Java?

Ans:
In java, when an object is not referenced any more, garbage collection takes place and the object is destroyed automatically. For automatic garbage collection java calls either System.gc() method or Runtime.gc() method.

15. What’s difference between Stack and Queue?

Ans:

Stack and Queue both are used as placeholder for a collection of data. The primary difference between a stack and a queue is that stack is based on Last in First out (LIFO) principle while a queue is based on FIFO (First In First Out) principle.



<Back   I   Next>


EmoticonEmoticon