Java Interview Questions Answers – Page 2

Java Interview Questions Answers – Page 2
Love and Share

6. Why is Java considered Architectural neutral?

Answer:  The Java is considered as Architectural Neutral because its compiler compiles a java program and converts it into bytecode i.e  a highly optimized set of instructions designed to be executed by the JVM.

Because of bytecode, a java program can run on a wide variety of platforms. JVM is platform dependent i.e its implementation differs from platform to platform, but these all JVMs can execute the same Java bytecode.

Hence, it is java bytecode due to which java is considered as “Architectural Neutral”.


7. Why is Java said to be portable?

Answer:  A. Java is considered as Portable because of Bytecode, it the key that makes Java language most secure and Portable.  The output of a Java compiler is Non-Executable Code i.e Bytecode. Bytecode is executed by Java run-time system, which is called the Java Virtual Machine (JVM).


8. What are class variables in Java?

Answer:  It contains  3 types of variable:-
1 Local variable (applicable Access Modifier is Final only (In terms of the main method))
2 Instance variable (we can any no. objects)
3 Class/Static variable (only one copy is made per class)


9. How to declare variables in Java?

Answer:  It contains  3 types of variable:-
1 Local variable (applicable Access Modifier is Final only (In terms of the main method))
2 Instance variable (we can any no. objects)
3 Class/Static variable (only one copy is made per class)


10. When Should we use static methods in JAVA?

Answer:  Define static methods are usually used in the following scenarios:

– If you are writing utility classes and they are not supposed to be changed.

– If a method doesn’t modify the state of an object, or not using any instance variables.

– If the method does not use any fields (or only static fields) of a class.
– If any operation is not dependent on instance creation.
– If there is some code that can easily be shared by all the instance methods, extract that code into a static method.

– It is also used If the method is not using any instance variable.
–  Static methods can not be overridden. So use it, If you are sure that the definition of the method will never be changed or overridden.

– If the function of method will remain static across class hierarchy


11. Why can’t we initialize a static variable inside a method?

Answer:  We cannot declare a static variable within the main method in Java because Static variables have to be declared at a class level. A static variable means that this variable belongs to the entire class and not any particular instance. So In java, only class variables can be declared as static.

Any variable declared within a method is supposed to be local to the method (It gets stored in the method stack in the JVM, which is one per thread), and static variables are common to all objects of a class (obviously a class can be invoked by many threads. So, Static variables are class variables and don’t make sense to be limited in scope to a method.

Love and Share

Leave a Reply

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