Showing posts with label Core Java. Show all posts
Showing posts with label Core Java. Show all posts

Core Java Interview Questions



  • What is JVM? 

 JVM(Java Virtual Machine) is a program whose responsible is to take the .class file and convert the byte code instructions into machine code, and then make it executed by the processor.

  • What is a class loader?

Part of JVM which loads the .class file into JVM's memory. 

  • Where does the memory is allocated to the class?

when we run a java program class is stored in a runtime memory area called Method Area

  • What are Native functions?

These are the functions that are copied form C,C++ header files. 

  • Why Java is a simple programming language?

It contains no.of predefined functions, which makes a developer to fell free to write the code.  
  •  What are object oriented concepts? 
The concepts like class, object, encapsulation, inheritance, and polymorphism are object oriented concepts. 
  •  What is a class?
 Class is an encapsulated body that consists of variables and methods. 
  • What is the difference between Method and Function?
Method/Functions is described as a set of instructions that performs certain task. In Procedure oriented language they are called as Functions, where as in Object oriented language they are called as Methods.
  •  What is the difference between Public, Private and Protected?
Public, Private and Protected are the three access specifiers. If we declare a class as public then the members of that class can be accessed from any other classes. If we declare a class as private, then the members of that class cannot be accessed by any other class. If we declare a class as protected it can be accessed by only some classes.
  • Which is the default package in java?
java.lang is the default package in java. 
  • What is a static block?
The block of code which is created with a static keyword, this block of code is executed before the main() method execution.
eg: static{
        ;;;;;;;;;;;;;;;;;;
        ;;;;;;;;;;;;;;;;;;
       } 
  • What are the different ways to accept input from keyboard? 
There are three different ways to accept input form keyboard:
using BufferedReader
using command line arguments and
  •  What is the error shows by the compiler if the main method is not available in program?
 Exception in thread "main" java.lang.NoSuchMethodError: main
  • Does Java supports overriding?
No, because overriding leads to confusion in JVM. 
  • Can we write a java program without using a class?
NO, Since java is a pure Object oriented language we cannot write a java program without class. 
  • What is an API?
An API(Application Programming Interface) is a document that contains the specifications that are to be followed while writing a program. It can be in any format like .pdf, .html, .doc, .txt etc..,
  • What is meant by a Garbage Collector? Can we invoke it manually?
Garbage Collector is a program, that deletes the unused objects of a class. Yes, we can invoke manually as "gc();".
  • What is the difference between Assignment and Initialization?
The process of assigning the value to the variable is called Assignment. We can assign a value to a variable any number of times. The process of assigning the value to the variable at the time of declaration is called as Initialization. Initialization can be done only once.
  • What is the difference between equals() and "=="?
When we compare two values using equals() it compares their values, but when we use == it compares the hash values of the given values.
  • What is the output of the following code?
              System.out.println(1+2+"3");
              System.out.println("1"+2+3);
Output: 33
             123
  • Which of the following is the most restricted access specifier?
                     protected 
                     friend
                     static 
                     Default
 Protected
  •  What is the difference between a constructor and a method?

A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator.
A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator. 
  • What is the purpose of garbage collection in Java, and when is it used?
The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used.
  • What is the difference between StringBuffer and StringBuilder?
  • Which of the following is primarily accessed by the java compiler?(static block , static method, static variable.)
When we compile the program JVM first looks for the static variables , then methods followed by blocks and allocates memory for them respectively.
  •  What is the difference between Instance variables and Static variables(Class Variables)?

Instance Variables
Static Variables/Class Variables
A separate copy is available to each object
Single copy in memory is shared by all the objects
Instance variables are created and stored in Heap Memory.
Static variables are stored in the Method area.
  
  • What is the difference between Instance Methods and Static Methods?
Instance methods are able to access both Instance variables as well as Static variables, where as the Static methods are able to access only Static variables.

  • Why Instance variables are not accessed by the static methods?
JVM creates the objects after the execution of Static methods. So, instance variables of the objects are not accessed by the static methods.
  •  What is inheritance?
Deriving subclass from the super class is called as Inheritance.
  •  Why Java does not support Multiple inheritance?
Multiple inheritance in java leads to confusion in programmer. Since Java is known for simplicity, multiple inheritance is omitted in Java.
  •  How can we gain multiple inheritance in Java?
We can gain multiple inheritance by using interfaces. i.e, by extending a class and by implementing the interface.
  • Can we create the super class object in the subclass?
Yes, we can create the super class object in the subclass.
  • What is polymorphism?
Polymorphism means, a single object exhibiting different properties.
  • What is meant by an abstract class?
An abstract class is the class which consists of both abstract methods and concrete methods.
  • What is the difference between Interface and Abstract class?
Interface is the one which consists of only abstract methods, where as the abstract class consists of both abstract methods as well as concrete methods.
  • Can we create the object to the abstract class?
No, we cannot create the object to the abstract class.
  • What is meant by Interface?
Interface is typical class which consists of only abstract methods, the class implementing that interface should define all the methods in that interface.
  • What is meant by type casting?
The process of converting one datatype into another is called as Typecasting. 



Control Statements

In java there are two types of Statements. They are:
  • Sequential Statements (statements which are executed one by one.)
  • Control Statements (Statements which are executed repeatedly and randomly)
When we come to control statements, these are again divided into 3. they are:
  • Conditional or Decision Making statements(if-then, if-then-else, switch)
  • Looping Statements(for, while, do-while)
  • Branching Statements(break, continue, return)

if..else statement:

This statement performs the task based on the condition. The statements present inside the curly braces {} represents a task. The following is the syntax of this statement.

if-else statement
fig: if-else statement

switch statement:

This statement consists of a set of cases which we have to choose according to our requirement. This statement is mainly used in menu driven programs. The following figure shows the syntax:

Switch statement
fig: Switch statement

break statement:

This statement is used to break the flow of execution. This is used in 3 places, they are:
  • used in switch statement to come out of it.
  • used in loops to come out of it.
  • used in nested blocks to go to the end of the block.

continue statement:

This statement is used to skip the iteration of loops. The following program shows the use of continue statement. 

 continue statement
fig: continue statement

return statement:

Return statement is used to exit form the method. This statement has two forms, one which returns a value and one which doesn't.        

while loop:

The statements present in this loop executes continuously as long as the condition specified in the loop is true. The following is the syntax for while loop.

while statement
fig: while statement

do...while loop:

The difference between while and do-while is that, In while loop, first the condition is checked and then the block of code is executed, where as in do-while first the block of code is executed and then the condition is evaluated. This means in do-while the block of statements are executed atleast once. The following is the syntax of do-while.

dowhile statement
fig: doWhile statement

for loop :

This loop iterates over a range of values, the following is the syntax of for loop.

for loop
fig: for loop

for-each loop:

This loop is mainly used in collections. This loop repeatedly executes the statements for each element of a collection. The no.of iterations of this loop is equal to no.of elements in the collection. The following is the syntax. 
                                                       
for each loop
fig: for each loop





Abstract Classes

      An Abstract class is the one that consists of both abstract as well as concrete methods. Even if a class have only one abstract method, we declare that class as abstract class. The following is the example of abstract class.

Abstract Class Example
Fig1: Abstract Class Example
        In the above example we have one abstract method as well as one concrete method in a class, so we have declared that class as Abstract. All abstract classes does not contain abstract methods. We can declare a class as abstract even if it contains no abstract methods. The following is the example of such class.

Abstract Class without abstract methods
Fig2: Abstract Class without abstract methods

       We cannot create an object to the abstract class, but we can create a reference variable which can hold the object of the class that inherits its properties. If we are trying to inherit the properties of an abstract class, we must provide the implementation to all the abstract methods of that class, if any.                               
                

Interfaces

         An Interface is one which contains the set of abstract methods, or Interface is similar to class which contains the set of variables and methods. Interfaces are declared using the interface keyword, and may only contain method signature and constant variable declaration. An interface may never contain method definition. 
        All the methods in the interface are abstract, even if we did not declare the method as abstract, by default Java compiler declares the methods as abstract. The variables in the interface are declared as static and final. The following is the syntax of interface:

Interface Syntax
Fig1: Interface Syntax
         It is not mandatory to provide variables in the interface, it is based on the requirement. Now, let us see an example on interface.

Interface Example
Fig2: Interface Example
         When we observe the above interface, there we haven't declared variables as Static & final and methods as abstract. Now, when we compile this interface compiler automatically declares the variables as public, static and final, and methods as public abstract

The following are the rules of Interfaces:  
  • All interface methods are implicitly public and abstract.
  • All variables that are defined in an interface must be public , static and final
  • Interface methods must not be static
  • Interface methods cannot be marked as final.
  • An interface can extend one or more interfaces.
  • An interface cannot extend anything other than interface.
  • An interface cannot implement another interface or class. 
           Anybody can release the interface, Once if the interface is released anybody can provide the implementation. We will provide implementation to the interface using a class. Providing the implementation means, defining(writing the method body) all the abstract methods present in that interface. When a class implements an interface it must define all the abstract methods of that interface. If we don't want to provide the definition to all the methods, then we should declare that class as abstract, otherwise compiler reports an error message.            

           We cannot create an object to the interface, but we can create a reference variable. This reference variable can hold the object of a class which provides the implementation of that interface.                  
          

Inheritance

      The process of acquiring the members of one class to another class is called as Inheritance or Deriving the new class form the old class or the existing class is called as Inheritance. In this concept the derived class acquires all the features of the old/existing class from which it is derived. This inheritance is achieved in the java programming by the usage of extend keyword. Suppose when classA is inherited from classB, then classA is called as subclass and classB is called as super class/parent class. classA gets the copy of classB,which provides all its features. The following is a syntax to extend a class.
  • public class <class_name1> extends <class_name2>
The main advantage of inheritance in Java programming is that, a programmer reuses the code of the super class without rewriting it. So, the size of code becomes less and the programmer feels easy to write the code. Let us see a simple example:

Inheritance Example
Fig1: Inheritance Example
If you observe the above example, in the main method we have created an object to the sub class i.e., to the Hire class. By creating the object to the Hire class we are able to access the super class method using subclass object.

Generally when we create the object to the subclass we can inherit the properties of all its super classes. The following figure clarifies you.


Inheritance Example
Fig2: Inheritance example
As shown in the above figure, every class by default inherits the properties of Object class either directly or indirectly.


What exactly the JVM do in the above program is, It first checks for the method display() in Hire class as it is not found it moves to the super class and check for the method there. As the method is found there it executes it. If it is not found there it moves to object class and checks for the method.

Now, observe the following program..
Inheritance Example
Inheritance Example

Here, If you observe the above program, we are creating the object of subclass and storing it in the super class(Sample) reference variable "s". This program produces the following error message.