Java interview questions and answers


156 Java interview questions and answers

8 Things you should always remember before go for an interview


What do you known  about Java?
             Java is a high-level programming language originally developed by sun microsystem. Supported oops concepts and it  provides high security. Java is a platform independent programming language(Linux,Windows,Unix...). Now it is owned by Oracle Corporation.

What is Java technology and why we need it?
                Java is Object based programming language first released by Sun Microsystems in 1995. Java is platform independent language, secure , reliable and robust. In present world lots of applications and websites working on Java technology, to use that application we need to install Java.

What is platform independent?
            A Java program can run on any hardware platform like windows,Linux,Unix.
Example: If we write a java program in windows machine  we can execute in another operating system like a Linux machine.
"Write Once, run anywhere"(WORA) Java quotation.




Why we are using java rather than other technologies?
            Java is platform independent language and supported OOPs concepts and provides more security rather than other technologies.

How many platforms supported java programming language?
            Java programming language is platform independent, so it supports a variety of platforms like Windows, Linux, Solaris, Ubuntu, etc…

What is the difference between class and classpath?
            The classpath and path are OS level environment variables. The classpath is describing location of .class files and path is describing the location of .exe files.

What is JIT compiler?
JIT(Just in time complier): It is converting byte code to executable code providing high performance. The java code will be executed both interpreter and JIT compiler simultaneously it reduces compilation time.

Why we need command line arguments in java?
            For customization we use command line arguments.

Is java a pure object oriented programming language?
            In java uses primitive data types so java is not a pure object-oriented programming language.

What are the features of java?
            The features of java are supports OOPs, platform-independent, secure, robust, high performance, portable.

 Why java doesn’t support pointers?
            The pointer is a reference to a memory location. The readability is the big problem and pointers are leads to memory leaks.

 What is aggregation in java?
            If a class has an entity type reference is known as aggregation. The aggregation represents HAS-A relationship. The code reusability is the main use of aggregation.

List of few java keywords unlike C, C++ keywords?
            Import
            Super
            Finally

What is the base class of all classes?
            The base class is java.lang.Object

How to declare constant variables in java?
            By using static and final keywords we can declare constant variables.
Example: static final int number =10;


JVM:
            The JVM is a platform-dependent and it is an abstract machine. The JVM executes only byte code. The Java Virtual Machine is known as JVM.
JDK:
            The JDK is a software development environment used for developing Java applications. It includes the Java Runtime Environment (JRE), and development tools. The Java Development Kit is known as JDK.
JRE:
            The Java Runtime Environment is known as JRE. The JRE provides runtime environment and it is an implementation of JVM. The JRE is a software environment it consists of a set of library files and its related files.

What is high performance?
            Java contains JIT (Just-In-Time) compiler. It compiles byte code in time only. The instructions are directly sent to a processor.

List of java IDE’s?
            Eclipse
            Netbeans
            JDeveloper, etc...

What is java quotation?
            Write once run anywhere.

What is call by value?
            In java if we call the method passing a value is known as call by value.
Example:   sample.add(50);

What is recursion?
            Recursion is a process of few statements calling itself. A method calling itself is known as recursion in java.
Example:
public class Factorial{
static int fact(int number){
if (number==0)
return 1;
else
return number=number*fact(number-1);     //recursion  method is calling it self
}
     public static void main(String []args){
        int number2= fact(5);    
        System.out.println(number2);
     }
}


Is java is a pure object-based programming language?
                 In java we are using primitive type data types, so is not a pure object-oriented programming language.

What is an object-based programming language?
                The object-based programming language has followed all features of OOPs concepts except inheritance.


What is a constructor in java?
            A constructor is a special method that is used to initialize that is used to initialize an object. The java constructor is a special method it looks like a method but it is not a method.

What is the return type of constructor?
            Constructor doesn’t have any return type even void.

How many types of constructors in java?
            There are two types of constructors in java user defined constructor and default constructor.

What is user default constructor?
            The default constructor JVM will provide.

What is a user defined constructor?
            The parameterized constructor is known as the user-defined constructor.

What is “this keyword“in java?
            In java, this is a reference variable and it refers to a current object.

What is the use of this keyword in java?
              In java “this” is an instance variable and used in the constructor. In java, this keyword can be passed as an argument in the method call.
Program:
public class Variables{
    public int x = 0;
    public int y = 0;
    //constructor
    public Variables(int x, int y) {
        this.x = x;              //this is used here
        this.y = y;
}
void display( ){
        System.out.println(+x);
        System.out.println(+y);

}
     public static void main(String []args){
        Variables v = new Variables(10,10);            //we are usd this so here 10,10 values are passed to constructor
        v.display();                   //calling display( ) to print output
     }
}

Why we are using “new keyword” in java?
            If we new keyword allocated memory at runtime and by using we are creating an object in java.

Define about super keyword?
            In java super is a reference variable If we overload a constructor into child class then it is immediately refers to parent class object. The super( ) is used in constructors.
Example:
public class Basicphone {
    public void features() {
        System.out.println("Few features available");
    }
}
public class Smartphone extends Basicphone {

                                // overrides features from Basicphone class
    public void features() {
        super.features();                                     //super refers to parent class
        System.out.println("Advanced features available");
    }
    public static void main(String[] args) {
        Smartphone s = new Smartphone();
        s.features();   
    }
}

Main() method


Is main( ) method compulsory in a java class?
            In java class main( ) method  is not necessary, if we compile a class without main( ) method there is no error in compile time but run time you well get an error.

public static void main(String args[])                 

Is write this order only or we can change order?
            We can change an order but must should declare void beside main( ) method because main() method id doesn’t return anything.

What is the return type of main( ) method?
            In java main( ) method nothing will return so it declared as void.

Can we overload main( ) method in java?
            We can overload main( ) method in java.

What type of error will get if we apply final to main( ) method?
We can apply final to main( ) method and there is no error in compile time and run time.

Why main( ) method is static in java?
            In java main( ) method is a class method. Initially, JVM creates an object and call main( ) method it leads to big problem for memory allocation, so main( ) method is static in java.


Variables:


Which type of variables a class can consist of?
            The java class consists of class variables, local variables and instance variables.

What is a local variable?
            The local variables are defined inside class and inside method. The local variables are declared and initialized inside of a method and it will destroy when a method has completed.
Example: Constructors or blocks variables are local variables.

What is an instance variable?
            The instance variables are created inside class and outside method.  The instance variables are allocated memory at run time.

What is a class variable?
            The class variables are created inside class and outside method. The class variables are also known as static variables. The class variable is declared with static keyword and allocates memory at compile time.
Program:
public class Variables{
static int i=10;                                //class variables
int j=15;                                         //instance variables
     public static void main(String []args){
            int k=20;                                             //local variable
            System.out.println(“Different types of variables here”);
     
}
}


Data types:


What is the default values of byte, float, double data type in java?
            The default value of byte data type is 0 and float data type value is 0.0f and double is 0.0d.

List of primitive types in java?
            The primitive types are byte, char, short, int, long, float, double, boolean.

What is type casting?
            Type casting means converting one data type to another data type.
Example: int a = 20;
                byte b = (byte)a;


What are the differences between String and StringBuffer and StringBuilder?
String:
            String is immutable and content is fixed. If we concat new string to old string then it creates a new object. The String class overrides equals( ) method of Object class. To compare two strings equals( ) method is useful.
StringBuffer:
            StringBuffer is mutable and content is not- fixed. The StringBuffer is fast and consumes less memory. The StringBuffer is synchronized i.e. thread safe. It is less efficient than StringBuilder. The StringBuffer class doesn’t override equals ( ) method of Object class.
StringBuilder:
            StringBuilder is mutable and content is not- fixed. The StringBuilder is non- synchronized i.e. not thread safe. It is more efficient than StringBuffer.


Why java doesn’t support multiple inheritance?
            To reduce complexity and simplify language java doesn’t support multiple inheritance and another one is Diamond problem.

What is up casting in java?
            The reference variable of parent class object  refers to child class object is known as up casting.

Which relation inheritance follows?
            Inheritance follows IS-A relationship.

Can we extend more than one class into a single class?
            No, because java doesn’t support multiple inheritance.

What is downcasting?
            The reference variable of child class object refers to parent class is known as downcasting.

What are the differences between static binding and dynamic binding?

Static Binding
Dynamic Binding
The object is determined at compile time is known as static binding.
The object is determined at run time is known as dynamic binding.
The static binding is also known as early binding.
The dynamic binding is also known as late binding.
The Overloaded methods are bonded using static binding.
The overridden methods are bonded using dynamic binding at runtime.


What is marker interface in java?
            An interface that has no member is known as maker interface. An interface with no fields and methods and variables is called as a marker interface. A maker interface is an empty interface. In another word a tagged object is getting extra functionality is known as a marker interface.
Example: Serializable, Clonnable.

How to make variable constant in java?
            By using final and static keywords we can declare constant variables in java. The final variables are declared outside of main() method and inside of class. If we declare inside of main() method compile time error will occur.
Example:
public class Sample{
public  final static int A = 110;
     public static void main(String []args){        
        System.out.println("Final varible value is :"+A);
     }
}


What is the default imported package in java?
                java.lang.package default imported package for all classes without any declaration.

List of three pre-defined classes in java.lang package?
Object
String
StringBuffer
Math
Vector
Thread
System





1 comments:


EmoticonEmoticon