jvm interview questions for experienced

Java is the most widely used programming language in the current IT industry. One major reason for the vast number of beginners and professionals in the field of programming is the career potential that Java knowledge comes with. This article is dedicated to the same purpose. Here is a complete guide on how to help you crack the most frequently asked Core Java Interview questions.

JVM Interview Questions and Answers 2019 Part-1 | JVM Interview Questions | Wisdom Jobs

Differences between JDK, JRE and JVM

JDK

JRE

JVM

Java Development Kit

Java Runtime environment

java virtual machine

JDK is required for java development.

JRE provides environment for running/executing programs.

JVM is the virtual machine on which java code executes.

JVM is responsible for converting byte code into machine specific code.

We need JDK in your system for>

  • developing,

  • compiling and

  • running Java programs.

JDK contains-

  • JRE and

  • JVM

You need to have JRE in your system for >

  • running Java programs.

JRE contains-

  • JVM,

  • class libraries and

  • other supporting libraries.

In short

JDK = JRE + JVM

In short

JRE = JVM + class libraries (rt.jar) + other libraries (if any).

For more explanation -Xms and -Xmx JVM parameters and differences

  • -Xms (minimum heap size which is allocated at initialization of JVM),
  • -Xmx (maximum heap size that JVM can use. )
  • -XX:MaxPermSize: It’s maximum value of Permanent Space that JVM can allot up to.
  • publicclass FindNumberOfAvailableProcessorsInSystemExample{ publicstaticvoid main(String[] args) { /** * first we will get the java Runtime object using the * Runtime classs getRuntime() method in java. */ Runtime runtime = Runtime.getRuntime(); /** * Now, Find out total Number Of Processors Available to * JVM (Java virtual machine) In System * We will use native method availableProcessors in java. */ intnumberOfProcessors = runtime.availableProcessors(); System.out.println(“total Number Of Processors Available to “ + “JVM (Java virtual machine) In your System = “+numberOfProcessors); } } /* output total Number Of Processors Available to JVM (Java virtual machine) In your System = 4 */
  • totalMemory() method of Runtime class returns the total amount of memory in the JVM (Java virtual machine).
  • totalMemory() is the native method in java, so value returned by this method depends on the host environment.
  • publicclass TotalAmountOfMemoryInJavaVirtualMachineExample{ publicstaticvoid main(String[] args) { /** * first we will get the java Runtime object using the * Runtime classs getRuntime() method in java. */ Runtime runtime = Runtime.getRuntime(); /** * totalMemory() method of Runtime class returns the total * amount of memory in the JVM (Java virtual machine). * totalMemory() is the native method, so value returned by this * method depends on the host environment. */ longtotalMemoryInJVM = runtime.totalMemory(); System.out.println(“Total amount of memory in the “ + “JVM (Java virtual machine) in bytes = “+ totalMemoryInJVM); } } /* output Total amount of memory in the JVM (Java virtual machine) in bytes = 257425408 */
  • freeMemory() method of Runtime class returns the total amount of free memory in the JVM (Java virtual machine).
  • freeMemory() is the native method in java.
  • /** * Example/program to Find Free Memory Available In * JVM (Java virtual machine) In your System */ publicclass FreeMemoryAvailableInJavaVirtualMachineExample{ publicstaticvoid main(String[] args) { /** * first we will get the java Runtime object using the * Runtime classs getRuntime() method in java. */ Runtime runtime = Runtime.getRuntime(); /** * freeMemory() method of Runtime class returns the total * amount of free memory in the JVM (Java virtual machine). * * freeMemory() is the native method. * * Calling gc (garabage collector) may result in * increasing free memory in java. */ longfreeMemoryAvailableInJVM = runtime.freeMemory(); System.out.println(“Total amount of free memory available in the “ + “JVM (Java virtual machine) in bytes = “+ freeMemoryAvailableInJVM); } } /* output Total amount of free memory available in the JVM (Java virtual machine) in bytes = 253398664 */
  • maxMemory() method of Runtime class returns maximum amount of memory that the JVM (Java virtual machine) will try to use.
  • maxMemory() is the native method in java.
  • /** * Example/program to Find Maximum Memory That * JVM (Java virtual machine) AttemptsToUseExample */ publicclass FindMaximumMemoryThatJVMAttemptsToUseExample{ publicstaticvoid main(String[] args) { /** * first we will get the java Runtime object using the * Runtime classs getRuntime() method in java. */ Runtime runtime = Runtime.getRuntime(); /** * maxMemory() method of Runtime class returns maximum amount * of memory that the JVM (Java virtual machine) will try to use * * maxMemory() is the native method. * */ longmaximumMemoryThatJVMAttemptsToUse = runtime.maxMemory(); System.out.println(“Maximum Memory That “ + “JVM (Java virtual machine) will try to use “ + “in bytes = “+ maximumMemoryThatJVMAttemptsToUse); } } /* output Maximum Memory That JVM (Java virtual machine) will try to use in bytes = 3797417984 */
  • halt() method of Runtime class
  • halt method terminates the running JVM (Java virtual machine) forcibly.
  • halt method never returns normally in java.
  • Two important points about halt method in java >
    • 1) halt method does not allows even already registered ShutdownHook to be executed (shown in program 2 below) in java And
    • 2) halt method even does not run the uninvoked finalizers if finalization-on-exit has been enabled in java.
    /** * Example/program to terminate * JVM (Java virtual machine) using halt method */ publicclass TerminateJVMExampleUsingHaltMethod{ publicstaticvoid main(String[] args) { System.out.println(“Halt method program – to terminate JVM (Java virtual machine)”); /** * first we will get the java Runtime object using the * Runtime classs getRuntime() method in java. */ Runtime runtime = Runtime.getRuntime(); /** * halt method of Runtime class * terminates the running JVM (Java virtual machine) forcibly. * halt method never returns normally. */ runtime.halt(1); //pass status as 1 System.out.println(“JVM (Java virtual machine) halted”); //This line wont be printed } } /* output Halt method program – to terminate JVM (Java virtual machine) */
    /** * Example/program to terminate * JVM (Java virtual machine) * Using Halt Method And Show registered * ShutdownHooks Are Not Executed */ publicclass TerminateJVMUsingHaltMethodAndShowShutdownHooksAreNotExecuted{ publicstaticvoid main(String[] args) { System.out.println(“halt method program – to terminate JVM (Java virtual machine)”); Runtime runtime = Runtime.getRuntime(); //Register/ add ShutdownHook, it wont get called after //JVM is shutDown using halt method. Runtime.getRuntime().addShutdownHook(new Thread(){ publicvoid run() { System.out.println(“Executing shutdown hook…”); System.out.println(“shutdown hook executed successfully”); } }); //call halt method runtime.halt(1); System.out.println(“JVM (Java virtual machine) halted”); //This line wont be printed } } /* output halt method program – to terminate JVM (Java virtual machine) */
  • exit method of Runtime class
  • Exit method terminates the running JVM (Java virtual machine) by initiating the shutdown sequence in java.
  • exit method never returns normally in java.
  • Two important phases of shutdown sequence initiated by exit method in java >
    • 1) exit method does allows to run already registered ShutdownHook to be executed (shown in program 4 below) And
    • 2) halt method runs the uninvoked finalizers if finalization-on-exit has been enabled.
    /** * Example/program to terminate * JVM (Java virtual machine) using exit method */ publicclass TerminateJVMExampleUsingExitMethod{ publicstaticvoid main(String[] args) { System.out.println(“Exit method program – to terminate JVM (Java virtual machine)”); /** * first we will get the java Runtime object using the * Runtime classs getRuntime() method in java. */ Runtime runtime = Runtime.getRuntime(); /** * exit method of Runtime class * terminates the running JVM (Java virtual machine) by * initiating the shutdown sequence. * */ runtime.exit(1); //pass status as 1 to exit method System.out.println(“JVM (Java virtual machine) halted”); //This line wont be printed } } /* output Exit method program – to terminate JVM (Java virtual machine) */
    /** * Example/program to terminate * JVM (Java virtual machine) * Using exit Method And Show registered * ShutdownHooks Are Executed */ publicclass TerminateJVMUsingExitMethodAndShowShutdownHooksAreExecuted{ publicstaticvoid main(String[] args) { System.out.println(“Exit method program – to terminate JVM (Java virtual machine)”); Runtime runtime = Runtime.getRuntime(); //Register/ add ShutdownHook, it will get called after //JVM is shutDown using exit method. Runtime.getRuntime().addShutdownHook(new Thread(){ publicvoid run() { try{ System.out.println(“Executing shutdown hook…”); }catch (Exception e){ e.printStackTrace(); } System.out.println(“shutdown hook executed successfully”); } }); //call exit method runtime.exit(1); System.out.println(“JVM (Java virtual machine) exited”); //This line wont be printed } } /* output Exit method program – to terminate JVM (Java virtual machine) Executing shutdown hook… shutdown hook executed successfully */

    The HotSpot JVM is a virtual machine that is responsible for running Java programs. It is used by many different Java applications and provides a way for Java programs to be executed on a variety of different platforms. The HotSpot JVM is also responsible for managing the memory and resources of the Java program, as well as providing a way for the program to interact with the underlying operating system.

    Yes, it is possible to use multiple interpreters or compilers within one JVM instance. This can be useful if we want to support multiple languages within our application or if we want to be able to dynamically compile and execute code at runtime. Having multiple interpreters or compilers available can also help improve performance by allowing us to choose the best tool for the job at hand.

    JVM uses a security manager to help ensure that no unauthorized operations are performed by programs running inside its environment. The security manager defines a set of permissions that programs are allowed to have, and it enforces these permissions. If a program tries to perform an operation that is not allowed by the security manager, then it will be blocked.

    Dynamic memory allocation is the process of allocating memory to a program while it is running. This is in contrast to static memory allocation, which allocates memory to a program at compile time. Dynamic memory allocation is necessary in order to support programs that need to use more memory than can be statically allocated to them.

    – The Stack: This is where local variables are stored. – The Heap: This is where objects are stored. – The Method Area: This is where class information is stored. – The PC Registers: This is where the JVM stores information about the currently executing instruction. – The Native Method Stack: This is where information about native methods is stored.

    What is the difference between JVM, JRE and JDK?

    JVM – JVM, which stands for Java Virtual Machine, is a virtual machine that understands and runs java bytecodes.

    When you compile a java program, the output is a .class file which contains bytecodes. JVM understands bytecodes and can execute the .class Java files.

    There are specific implementations of the JVM for specific platforms – Windows, Linux etc. The same Java .class file can be run for any of the JVM implementations. This is how Java programming language is platform independent and make the Java programs portable i.e write once, run anywhere.

    JRE – JRE, which stands for Java Runtime Environment, provides an implementation of the JVM, supporting libraries and other components required to run Java programs. JRE also provides components that enable two kinds of deployment of Java programs. Java plug-in, which enables java programs to run on browsers and Java Web Start, which deploys standalone java applications.

    JDK – JDK, which stands for Java Development Kit, contains the JRE plus tools such as compilers, debuggers etc. which are required for developers to develop Java programs.

    FAQ

    How can I prepare for Java experienced interview?

    The JVM consists of three distinct components: Class Loader. Runtime Memory/Data Area. Execution Engine.

    How JVM works step by step?

    Which topics to prepare for Java interviews?
    1. Java Fundamentals.
    2. Data Structure and Algorithms.
    3. Object-Oriented Concepts.
    4. Multithreading, concurrency, and thread basics.
    5. Java Collections Framework.
    6. Date type conversion and fundamentals.
    7. Array.
    8. Garbage Collection.

    Related Posts

    Leave a Reply

    Your email address will not be published. Required fields are marked *