Java Interview Questions For Beginners

Q1. Why Java is called platform independent? 

Java is platform independent because a compiled java dot class file could be executed on different  platform (that is operating system) without re-compiling on those platforms. We can compile a java source  code on one operating system and can execute the object code (a dot class file) on any other operating  system provided the target operating system is installed with Java Runtime Environment.

Q2. How Java is platform independent? 

Java achieves platform independence using a two tier system of compilation and execution. Java compiler  compiles the Java source code and generates java dot class file/s in a specific format called byte code.  This dot class file has to be submitted to the Java Virtual Machine (JVM) of the target system for  execution; JVM interprets the dot class file according to the underlying operating system and executes  the java program. 

Sun-micro-system launched different versions of virtual machines for different operating system and they  were made freely available. It is the JVM that makes the Java independent of operating system.

Related Posts

Q3. Describe JDK, JRE, JVM  

JDK – It is Java Development Kit, contains Java compiler, Java Runtime Environment and Standard java  runtime library along with the other necessary tool (Applet viewer, rmic ) to develop standard java  applications. For developing Java application a programmer need JDK. 

JRE – Java Runtime Environment, it contains everything that is necessary for executing a java compiled  code, JRE contains JVM (Java Virtual Machine), Runtime classes, library, Java class loader. 

JRE could be downloaded independently or it is available within JDK.  JVM- Java Virtual Machine is subpart of JRE, it contains Just in time compiler to interpret the byte code  according to the underlying operating system. JVL also contains garbage collector and exception handler.

Q4. State some basic features of Java. 

∙ Java is platform independent. 

∙ It is Object Oriented programming language. 

∙ Java library is open source. 

∙ No license fee is required for commercial development. ∙ Standard library contains rich set of classes for various purposes.

Q5. State 4 differences between Java and C++. 

∙ Java is platform independent, C++ is not 

∙ Java does not support global scope, C++ does. 

∙ In Java program we cannot declare a pointer but in C++ we can. 

∙ In Java Objects are passed always ‘by reference’ but in C++ Objects could be passed ‘by reference’ or ‘by value’. 

Q6. Define class with an example. 

In object-oriented programming, a class is an extensible template for creating objects, providing initial  values for state (member variables) and implementations of behavior (member functions, methods). In object oriented programming a class is written to represent a user defined type by providing the  behavior in the member function and state in the member variables. 

In Java class is declared using the class keyword as follows:

class MyRational 
{ 
//declaration of member variables 
 private int numerator; 
 private int denominator; 
  
 public Rational(int n, int d) //constructor function  { 
numerator = n; 
denominator = d!=0?d:1; 
 }//end of constructor function 
 public double getArgument(){ 
return Math.atan((double)denominator/numerator); 
 } 
//some other function to represent the behavior of Rational type   
}//end of class definition 

Q7. Discuss the significance of static keyword in Java. 

If a class member (both variable and function) is declared as static then the member becomes  independent of the object that is static members are not in any way related to the object of the class and  they can be accessed using the class name without an object instance.

class AClass{ 
public static void fun(){ 
System.out.println(“Hello World”); 
} 
public void test(){ 
System.out.println(“Hello India”); 
} 
}

In the above example function fun could be called using the class name only as follows:  AClass.fun(); 

But for function test we need object of class A Class because that is not a static member.  It is not possible to access a static member from within a non-static member due to the object  dependency of the non-static members. 

Q8. What are the different qualifiers for class members available  in Java? Briefly mention their characteristics. 

In Java there are 4 types of qualifiers for the class members:  

1. private  

2. public  

3. protected 

4. default 

When a member within a class is declared with the private keyword the member cannot be accessed  outside of the class and is accessible only from within the class. 

When a class member is declared as public then the member is accessible from anywhere, from another  function within the class as well as from a function in another class in the same or different package. 

When a class member is declared as protected then the member is accessible from a function within the  same class or from a function belonging to the inherited class in the same or different package or from a  function within any class of the same package. 

If a member is not declared as private, protected or public then it is by default taken as ‘default’, default is  not a keyword like private, public or protected. A default member is accessible from a function within  same class, from a function within any class in same package but they are not accessible from a function in  a class not belonging to the same package.

Q9. Mention the usage of final key word in Java. 

In java the keyword final could be used with three different ways, first with a variable, second with a  member function and third with a class. 

If a variable is declared with the final keyword then the content of the variable could not be changed. 

If a member method of a class is declared with the final keyword then the method definition is final that is  we cannot override the method in child class. 

If a class is declared as final then the class cannot be inherited.