Java Interview Questions Set 3
Categories: Java 8(JDK1.8)
What is the default value of the local variables?
The local variables are not initialized to any default value, neither primitives nor object references.
What are the various access specifiers in Java?
In Java, access specifiers are the keywords which are used to define the access scope of the method, class, or a variable. In Java, there are four access specifiers given below.
a) Public The classes, methods, or variables which are defined as public, can be accessed by any class or method.
b) Protected Protected can be accessed by the class of the same package, or by the sub-class of this class, or within the same class.
c) Default Default are accessible within the package only. By default, all the classes, methods, and variables are of default scope.
d) Private The private class, methods, or variables defined as private can be accessed within the class only.
What is the purpose of static methods and variables?
The methods or variables defined as static are shared among all the objects of the class. The static is the part of the class and not of the object. The static variables are stored in the class area, and we do not need to create the object to access such variables. Therefore, static is used in the case, where we need to define variables or methods which are common to all the objects of the class.
For example, In the class simulating the collection of the students in a college, the name of the college is the common attribute to all the students. Therefore, the college name will be defined as static.
What are the advantages of Packages in Java?
There are various advantages of defining packages in Java.
a) Packages avoid the name clashes.
b) The Package provides easier access control.
c) We can also have the hidden classes that are not visible outside and used by the package.
d) It is easier to locate the related classes.
What is the output of the following Java program?
class Test
{
public static void main (String args[])
{
System.out.println(10 + 20 + "Javatpoint");
System.out.println("Javatpoint" + 10 + 20);
}
}
The output of the above code will be
30Javatpoint
Javatpoint1020