A2Z UPDATES: Java Virtual Machine JVM Architecture Based Interview Questions and Answers PDF File

Tuesday, 18 June 2013

Java Virtual Machine JVM Architecture Based Interview Questions and Answers PDF File

In this post contain about Java Virtual Machine JVM Architecture based interview questions and answers:in this post contain these searches like Java Virtual Machine Architecture based interview questions and answers,interview questions and answers for JVM(Java Virtual Machine) Architecture,interview questions and answers based on JVM Architecture&PDF file.
Java Virtual Machine(JVM) Architecture Based Interview Questions and Answers

Java Virtual Machine(JVM) Architecture Based Interview Questions and Answers: 

In this page contain interview questions and answers based on Architecture of JVM,Java Virtual Machine(JVM) Architecture based interview questions and answers,here we cover most frequently asked questions in JVM so just follow this Java Virtual Machine(JVM) Architecture Based Interview Questions and Answers..

1)What is JVM(Java Virtual Machine)?
Ans: A Java Virtual Machine(JVM) is a Runtime environment required for execution of a Java application. Every Java Application runs inside a Runtime instance of some concrete implementation of Abstract Specifications of JVM. Its  is JVM which is crux of “Platform Independent” nature of the Language. 
    2)What is a JVM Consisted of?
Ans: Each time a Java Application is executed then an instance of JVM(Java Virtual Machine),responsible for its running ,is created .A JVM(Java Virtual Machine)instance is described in terms of subsystems, memory areas, data types, and instructions. The block diagram given below, depicts a view of Internal Architecture of JVM

Java Virtual Machine(JVM) Architecture:

JVM Architecture based Interview Questions and Answers
Java Virtual Machine(JVM) have  various sub components internally what they just follow the above diagram.
1)Class Loader Sub System
2)Run Time Data Area
3)Native Methods Interface
4)Native Method Library
5)Execution Engine.
-->


1)Class Loader Sub System:
Java Virtual Machine(JVM) Class Loader sub system perform 3 major tasks
They are:
A)Class Loader loads .Class file into Memory.
B)Class Loader verify s Byte Code Instructions.
C)Class Loader Allots Memory required for the program.
2) Run Time Data Area:

a) Method Area: This Method Area stores Class code and Methodcode.

b)Heap:In this Heap Objects are create.

c)Java Stacks: Java Stacks are the places where the Java Methods are Executed. A Java Stack contains Frames. On each Frame, a seperate method is Executed.

d)Program Counter Registers: The Program Counter Registers store Memory Address of the Instructions to be executed by the Micro Processor.

e)Native Methods Stacks: The Native Method Stacks are places where Native Methods(EX: C Language Programs) executed. Native Method is a Function,which is written in another language other than Java.

3) Native Method Interface: The Native Method Interface is a program that connects Native Methods Libraries('C' HeaderFiles) with JVM for executing Native Methods.

4) Native Method Library: It holds the Native Libraries Informations.

5) Execution Engine: Execution Engine contains Interpreter and JIT Compiler, which convert Byte Code into Machine understandable Code(Machine Code). JVM uses Optimization Techniques to decide which part of code to be Interpreted and which part of code to be used with JIT Compiler. The HotSpot represents the Block of code executed by JIT Compiler. 
                                     
Each JVM(Java Virtual Machine)has a class loader subsystem which loads classes and interfaces with fully qualified names.Each JVM has an execution engine too , which executes all instructions contained by Methods of a loaded class.While executing a Java program,a JVM(Java Virtual Machine) requires memory for storing byte codes, objects ,local variables, Method arguments, return values,intermediate computational results and JVM does that memory management on several Runtime data areas.The specification of Runtime data areas is quite abstract.This abstract nature of JVM specification helps different designers to provide implementation on wide variety of OS and as per choice of the designers.
Some implementations may have a lot of memory in which to work, others may have very little. Some implementations may be able to take advantage of virtual memory, others may not. Each instance of the Java Virtual Machine has one Method area and one heap. These areas are shared by all threads running inside the Virtual Machine. When the Virtual Machine loads a class file, it parses information about a type from the binary data contained in the class file. It places this type information into the Method area. As the program runs, the Virtual Machine places all objects the program instantiates onto the heap. 
When a new thread is created, it gets its own PC register (program counter) and Java stack. If the thread is executing a Java Method (not a native Method), the value of the PC register indicates the next instruction to execute. A thread's Java stack stores the state of Java (not native) Method which includes its local variables, the parameters with which it was invoked, its return value (if any), and intermediate calculations. The state of native Method invocations is stored in an implementation-dependent way in native Method stacks, in registers or other implementation-dependent memory areas. The Java stack is composed of stack frames (or frames). 
A stack frame contains the state of one Java Method invocation. When a thread invokes a Method, the Java Virtual Machine pushes a new frame onto that thread's Java stack. When the Method completes, the Virtual Machine pops and discards the frame for that Method. In JVM ,the instruction set uses the Java stack for storage of intermediate data values.The stack-based Architecture of the JVM's instruction set optimizes code done by just-in-time and dynamic Compilers.

   3)What is Java Class Files Magic Number?

Ans: A Magic Number of a class file a unique Identifier for Tools to quickly differentiate class files from non class files.The First Four Bytes of each Java Class file has th Magic Value as 0xCAFEBABE.

   4)What is Class Loader and its Responsibilities?

Ans:

The Class loader is a subsystem of a JVM which is responsible, predominantly for loading classes and interfaces in the system. Apart from this, a class loader is responsible for the following activities:-Verification of imported types (classes and interfaces)-Allocating memory for class variables and initializing them to default values. Static fields for a class are created and these are set to standard default values but they are not explicitly initialized. The Method tables are constructed for the class.-Resolving symbolic references from type to direct references The class loaders can be of two types: a bootstrap or primordial class loader and user defined class loader Each JVM has a bootstrap class loader which loads trusted classes, including classes from Java API. JVM specs do not tell how to locate these classes and is left to implementation designers.A Java application with user defined class loader objects can customize class loading. These load untrustworthy classes and not an intrinsic part of JVM. They are written in Java, converted to class files and loaded into the JVM and installed like any other objects.
    5)What is Heap and Stack?
Ans:
The heap is the part of memory of JVM(Java Virtual Machine) where all objects reside. The stack is consisted of stack Frames. When a thread invokes a Method, The JVM(Java Virtual Machine) pushes a new frame onto that thread's Java Stack. Each stack frame is consisted of operand stack and the local variable array. All Arguments, Local Variables, intermediate computations and Return Values if any are kept in these  stack corresponding to the Method invoked. The stack frame on the top of the stack is called the active stack frame, which is the current place of execution. When the Method completes, the Virtual Machine pops and discards the frame for that Method.
    6)How JVM Performs Garbage Collections?
Ans:
One of the most frequently asked questions(FAQ's) during interviews and it seeks a precise and clear understanding of the concept. Whenever a reference to an object on heap lies dangling or no longer in use by an active program then it becomes eligible for being garbage collected by JVM(Java Virtual Machine).JVM(Java Virtual Machine) specifications do not force any specific kind of garbage collection algorithm though there are several algorithms like Reference Counting, Tracing ,compacting, Copying, generational etc. in place. It is very important that Garbage Collection should be efficient and non interfering in execution of Java programs. There is a trade off between ease of Implementation Vs better Performance while implementing Garbage Collection feature for a Java Virtual Machine (JVM). Since J2SE1.2 JVM(Java Virtual Machine) incorporated different Algorithms and that are combined using Generational Collection.This mechanism can be compared with a container or bucket kind of thing, where new object or infant/young objects are kept another as tenured and the third is permanent. The objects lying in young container are more often checked and quick to go in garbage state while this phenomenon decreases with tenure. So when objects are old enough or survive enough to be part of tenure zone then they become less likely to be Garbage Collected. The JVM specific classes and Method objects are part of permanent zone and are never garbage collected. So it is quite likely that non reachable object still remains out of bound of garbage collection due to how much time an object has survived for as mentioned above, how much heap space JVM(Java Virtual Machine) is consuming for its running, if it is low then whole process of Garbage Collections slows down.
    7) How JVM Performs Thread Synchronizations?
Ans:
JVM associates a lock with an object or a class to achieve mutilthreading. A lock is like a token or privilege that only one thread can "possess" at any one time. When a thread wants to lock a particular object or class, it asks the JVM.JVM responds to thread with a lock maybe very soon, maybe later, or never. When the thread no longer needs the lock, it returns it to the JVM.

If another thread has requested the same lock, the JVM passes the lock to that thread.If a thread has a lock,no other thread can access the locked data until the thread that owns the lock releases it. The JVM uses locks in conjunction with monitors. A monitor is basically a guardian in that it watches over a sequence of code, making sure only one thread at a time executes the code.

Each monitor is associated with an object reference. It is the responsibility of monitor to watch an arriving thread must obtain a lock on the referenced object. When the thread leaves the block,it releases the lock on the associated object.A single thread is allowed to lock the same object multiple times.Java Virtual Machine(JVM) maintains a count of the number of times the object has been locked. An unlocked object has a count of zero. When a thread acquires the lock for the first time, the count is incremented to one. Each time the thread acquires a lock on the same object, a count is incremented.

Each time the thread releases the lock, the count is decremented. When the count reaches zero, the lock is released and made available to other threads. In Java language terminology, the coordination of multiple threads that must access shared data is called synchronization. The language provides two built-in ways to synchronize access to data: with synchronized statements or synchronized Methods. The Java Virtual Machine(JVM) does not use any special op codes to invoke or return from synchronized Methods. When the JVM(Java Virtual Machine) resolves the symbolic reference to a Method, it determines whether the Method is synchronized. If it is, the JVM acquires a lock before invoking the Method. For an instance Method, the JVM acquires the lock associated with the object upon which the Method is being invoked. For a class Method, it acquires the lock associated with the class to which the Method belongs. After a synchronized Method completes, whether it completes by returning or by throwing an exception, the lock is released.
Two op codes, monitor enter and monitor exit are used by Java Virtual Machine(JVM) for accomplishing this task.

When monitor enter is encountered by the Java Virtual Machine, it acquires the lock for the object referred to by object ref on the stack. If the thread already owns the lock for that object, a count is incremented. Each time monitor exit is executed for the thread on the object, the count is decremented. When the count reaches zero, the monitor is released.
    8) How to Profile Heap Usage?
Ans: Try using -Xaprof to get a profile of the allocations(Objects and Sizes) of your application.
Also Try –agentlib :  hprof=heap=all (or other option, try -agentlib:hprof=help for a list)
    9) What Is 64 Bit Java?
Ans:
A 64-Bit version of Java has been available to Solaris SPARC users since the 1.4.0 release of J2SE. A 64-Bit capable J2SE is an implementation of the Java SDK (and the JRE along with it) that runs in the 64-Bit environment of a 64-Bit OS on a 64-Bit processor. The primary advantage of running Java in a 64-Bit environment is the larger address space.

This allows for a much larger Java heap size and an increased maximum number of Java Threads, which is needed for certain kinds of large or long-running applications. The primary complication in doing such a port is that the sizes of some native data types are changed. Not surprisingly the size of pointers is increased to 64 bits. On Solaris and most Unix platforms, the size of the C language long is also increased to 64 bits. Any native code in the 32-bit SDK implementation that relied on the old sizes of these data types is likely to require updating.

Within the parts of the SDK written in Java things are simpler, since Java specifies the sizes of its primitive data types precisely. However even some Java code needs updating, such as when a Java int is used to store a value passed to it from a part of the implementation written in C.
    10) What is the Difference between JVM & JRE?
Ans: A Java Runtime Environment(JRE) is a prerequisite for Running Java Application on any computer. A JRE contains a (JVM)Java Virtual Machine, all standard, Core java Classes and Runtime Libraries.It does not contain any development Tools such as Compiler,Debugger, etc.. JDK(Java Development Kit) is awhole Package required to Java Development which essential contails JRE+JVM and Tools required to compile and Debug,Execute Java Applications.
11)  Why Java based GUI intensive program has Performance Issues? 
Ans:
GUI(Graphical User Interface) intensive Java application mostly run underlying OS specific native libraries which is time and more CPU cycles consuming.
The overall Performance of a Java Application Depends on 4 Factors:
1) The Design of the Application.
2) The Speed at which the Virtual Machine Executes the Java Byte Codes.
3) The Speed at which the Libraries that perform basic Functions tasks execute.
4) The Speed of the underlying hardware and Operating System(OS).

The Virtual Machine is responsible for byte code execution, storage allocation, thread synchronization, etc. Running with the Virtual Machine are native code libraries that handle input and output through the operating system, especially graphics operations through the window system. Programs that spend significant portions of their time in those native code libraries will not see their performance on HotSpot improved as much as programs that spend most of their time executing byte codes.
12)  What is Difference between Interpreted Code and Compiled Code?
Ans:
An Interpreter produces a result from a program, while a Compiled  produces a program written in assembly language and in case of Java from bytecodes.The Scripting Languages Like JavaScript,Python etc. require Interpreter to execute them.So a program written in scripting language will directly be executed with Interpreter installed on that computer,if it is absent then this program will not execute.While in case of Compiled code,an assembler or a Virtual Machine in case of Java is required to convert Assembly level code or Bytecodes into Machine level instructions/commands.Generally, interpreted programs are slower than Compiled programs, but are easier to Debug and revise.
13)  Should one Pool Object to help Garbage Collector? Should one call System.gc() periodically?
Ans: No,Pooling Objects will cause them to live longer than neccesary.The Garbage Collection(GC) methods will be much more efficient if you let it do the Memory Management.The Strong Advice is taking out Object Pools.
Donot call System.gc(), HotSpot will make the determination of when its appropriate and will generally do a much better job.

Java Virtual Machine(JVM) Architecture Explanation with Video:

In this video explain about Java Virtual Machine(JVM) Architecture,here very easily we can understand about JVM(Java Virtual Machine) so concentrate on this video you can learn JVM architecture..
click here : JVM Architecture PDF File Download

  
                                                  for more questions coming soon......

1 comment :