Java Variable Questions and Answers

1. What are different types of Varibales in Java?
Answer: Below are the different types of variables in Java.
Instance or non static variables,
Static or class variables,
Local or method variables,
parameters.

2. What are local variables in Java?
Answer:  A local variable is one that exists inside a method or a block of code and exists as long as that method or block is executing. Once the program reaches the end of the method (or block), the local variable disappears from memory. The next time the method is called, a completely new version of the local variable comes into existence. One of the most common types of local variables is a parameter. This variable need to be initialized before using it. The local or method level variables are not initialized to any default value, neither primitives nor object references.

3. Is local variable thread safe in java?
Answer:  Yes. All local variables defined in your program will be allocated memory in the stack. So, When you create a thread it will have its own stack created. Two threads will have two stacks and one thread never shares its stack (that is, local variables) with other thread.

4. What is inline value in Java?
Answer:  Inline value represents the literals in a Java program.
For example, String str = "Hello", here the string "Hello" is a inline value.

5. What are Instance variables?
Answer:  Instance variables are variables that are defined at the class level. Instance variables need not have to be initialized since they are automatically initialized to its default value during object creation.

6. How the object references are initialized when it is a instance variable?
Answer: Object references are initialized to null in case of instance variables.

7. Is Instance variables thread safe in Java?
Answer: No

8. What is the advantage of using arrays over variables ?
Answer:  Arrays provide a structure wherein multiple values can be accessed using single reference and index. This helps in iterating over the values using loops.

9. What is Constant?
Answer: The variable should be declared with static and final modifiers. static ensures that only one variable exists for all instances of the class and final makes its value not changeable once initialized.
static final int MATH_MARKS = 50;

10. What are static variables?
Answer: Static variables are class level variable where all instances of the class refer same variable. When an instance update its value, all the other objects will see the new value. They are loaded at run time when the respective Class is loaded.

11. Explain static keyword in Java.
Answer:  The static keyword can be applied to,
static variables,
static methods,
static block,
static inner class,
and interface static method (introduced in Java 8).

12. What is a static class in Java?
Answer:  A Class can be made static only if it is a nested class (class within a class). The nested static class can be accessed without having an object of outer class. It can access static data members of outer class including private. Static nested class cannot access non-static data member or method.

If you have the static member inside static nested class, it can be accessed directly and you don't have to create instance of static nested class.

13. What is a static block?
Answer:  A static block, is a block of code inside a Java class that will be executed when a class is first loaded into the JVM. Mostly the static block will be used for initializing the variables.
Static block will be called only one while loading and it cannot have any return type also not have
'this' or 'super' keyword. Java class can have more than one static block. It will be executed in the
same order as it appears.

14. Can we overload static methods in java?
Answer: Yes, we can overload static methods in java.

15. Can we override static methods in java?
Answer:  No, we can not override static methods in java.

16. Can we execute a program without main () method?
Answer:  Yes, it is possible in previous versions of Java, not since JDK 1.7 using static block.

No comments:

Post a Comment