Set - 10

Question 36 :

Is exit a keyword in Java?

Answer :

No. To exit a program explicitly you use exit method in System object.


Question 37 :

What happens if you don't initialize an instance variable of any of the primitive types in Java?

Answer :

Java by default initializes it to the default value for that primitive type. Thus an int will be initialized to 0, a Boolean will be initialized to false.


Question 38 :

What will be the initial value of an object reference which is defined as an instance variable?

Answer :

The object references are all initialized to null in Java. However in order to do anything useful with these references, you must set them to a valid object, else you will get NullPointerException everywhere you try to use such default initialized references.


Question 39 :

What are the different scopes for Java variables?

Answer :

The scope of a Java variable is determined by the context in which the variable is declared. Thus a java variable can have one of the three scopes at any given point in time.
1. Instance : - These are typical object level variables, they are initialized to default values at the time of creation of object, and remain accessible as long as the object accessible.
2. Local : - These are the variables that are defined within a method. They remain accessible only during the course of method execution. When the method finishes execution, these variables fall out of scope.
3. Static: - These are the class level variables. They are initialized when the class is loaded in JVM for the first time and remain there as long as the class remains loaded. They are not tied to any particular object instance.


Question 40 :

What is the default value of the local variables?

Answer :

The local variables are not initialized to any default value, neither primitives nor object references. If you try to use these variables without initializing them explicitly, the java compiler will not compile the code. It will complain about the local variable not being initialized..