Sunday, 17 May 2015

Core Java interview questions

  1. What is encapsulation?

    Encapsulation means keeping together the implementation (code) and the data it manipulates (variables). In Java a class contains the member variables (instance variables) and the code in methods. In a properly encapsulated Java class method defines how member variables can be used.

    Read more about encapsulation here.

  2. What is abstraction?

    Abstraction means hiding the complexity and only showing the essential features of the object. So in a way, Abstraction means abstracting/hiding the real working and we, as a user, knowing only how to use it.

    Real world example would be a vehicle which we drive with out caring or knowing what all is going underneath.

    Read more about abstraction here.

  3. Difference between abstraction and encapsulation?

    Abstraction is more about hiding the implementation details. In Java abstraction is achieved through abstract classes and interfaces.

    Encapsulation is about wrapping the implementation (code) and the data it manipulates (variables) with in a same class. A Java class, where all instance variables are private and only the methods with in the class can manipulate those variables, is an example of encapsulated class.

    Read more about abstraction here and encapsulation here.

  4. What is Polymorphism?

    Polymorphism, a Greek word, where poly means many and morph means change, thus it refers to the ability of an object taking many forms. The concept of polymorphism is often expressed as "One interface, multiple methods".
    Where one general class may have the generic method and the derived classes (classes which extend the general class) may add their own specific method with implementation. At the time of execution "One interface" i.e. the general class will take "many forms" i.e. references of the derived classes.

    Read more about polymorphism here.

  5. What is inheritance?

    Inheritance is a mechanism, by which one class acquires, all the properties and behaviors of another class. The class whose members are inherited is called the Super class (or base class), and the class that inherits those members is called the Sub class (or derived class).
    The relationship between the Super and inherited subclasses is known as IS-A relationship.

    Read more about inheritance here.

  6. What Is a Class?

    In object-oriented terms class provides a blue print for the individual objects created from that class. Once a class is defined it can be used to create objects of that type.

    Read more about class here.

  7. What Is an Object?

    In object-oriented terms object is an instance of the class, which gets its state and related behavior from the class. When a new class is created essentially a new data type is created. This type can be used to declare objects of that type.
    An object stores its state in fields and exposes its behavior through methods.

    Read more about object here.

  8. What is method overloading?

    When two or more methods with in the same class or with in the parent-child relationship classes have the same name, but the parameters are different in types or number the methods are said to be overloaded. This process of overloaded methods is known as method overloading.

    Read more about method overloading here.

  9. What is method overriding?

    In a parent-child relation between classes, if a method in subclass has the same signature as the parent class method then the method is said to be overridden by the subclass and this process is called method overriding.

    Read more about method overriding here.

  10. What are JVM, JRE and JDK?

    JVM

    JVM meaning Java Virtual Machine is an abstract layer between a Java program and the platform that Java Program is running on.
    JVM is platform dependent and different implementations of JVMs are available for specific platforms.

    JRE

    JRE meaning Java Runtime Environment provides the libraries, the Java Virtual Machine, and other components to run applets and applications written in the Java programming language.

    JDK

    JDK meaning Java Development Kit is a superset of the JRE, and contains everything that is in the JRE, plus development tools such as the compilers and debuggers necessary for developing applets and applications.

    Read more about JVM, JRE and JDK here.

  11. What is bytecode in Java or Why java is called platform independent?

    Java programs are both compiled and interpreted which means -
    When we do javac javaprogram, the java source code is compiled into a .class file which is actually a bytecode. The generated bytecode is portable and it's format is same across platforms.
    After compilation, the interpreter reads the generated byte code & transforms it according to the native platform.
    The compiled bytecode doesn't run on CPU directly, JVM sits in between and interpret the bytecode into readable machine language for the CPU. So Java program is platform independent but JVM is platform dependent and different implementations of JVMs are available for specific platforms.


  12. Java is pass by value or pass by reference?

    Java is pass by value for both primitive types as well as for objects.

    Read more about Java pass by value or pass by reference here

  13. What is package in Java?

    A package is a namespace that organizes a set of related classes and interfaces. Packages also help in  preventing naming conflicts.
    Packages also provide visibility control mechanism. You can define classes and class members inside a package that are not visible to the classes in other packages.

    Read more about Package in Java here

  14. What are access modifiers in Java?

    Access level modifiers are used to control the visibility of the class or the members of the class i.e. fields and methods.
    Types of Access Modifiers

    • private - private modifier specifies that the member can only be accessed in its own class.
    • default (or package-private) - If no specifier is used (which is known as default access) member is visible only within its own package.
    • protected - protected modifier specifies that the member can only be accessed within its own package and by a subclass of its class in another package.
    • public - public modifier specifies that member is visible to all classes everywhere.

    Read more about Access Modifiers here

  15. What all access modifiers can be used with a class?

    At the class level only two of the modifiers can be used, public or default.
    If a class is declared with the modifier public, that class is visible to all classes everywhere.
    If a class has no modifier (the default), it is visible only within its own package.

    Read more about Access Modifiers here

  16. What all access modifiers can be used with class fields?

    All the four types of access modifiers - public, protected, default, private can be used with variables declared in the class.

    • If a field is declared as public then it is visible to all classes in the same package or other packages.
    • If a fields is declared with no access specifier (default) then it can be accessed by any class in the same package.
    • If a field is defined as protected then it is accessible to any class in the same package or to any subclass (of the class where field is declared) in different package.
    • If a field is defined private then that field can only be accessed in its own class.


  17. What all access modifiers can be used with class methods?

    All the four types of access modifiers - public, protected, default, private can be used with methods of the class and access modifier for the methods work the same way as for the fields.


  18. What all access modifiers can be used with constructors?

    All the four types of access modifiers - public, protected, default, private can be used with constructors of the class.

    • In case constructor of the class is private then the object of that class can be created by that class only.
    • In case constructor is marked as protected then a subclass in different package can access the protected constructor.
    • If a constructor is declared with no access specifier (default) then it can be accessed by any class in the same package.
    • If a constructor is declared as public then it is visible to all classes in the same package or other packages.


  19. What is automatic numeric promotion in Java?

    In Java numeric promotion happens in case of primitive types when those primitives are used in an expression.
    As exp.

    byte a = 100;
    byte b = 50;
    int i = a * b;
    
    In the above code a * b will exceed the range of its byte operand (range of byte is -128 to 127). In these types of situations Java will automatically promote the byte, short or char to int when evaluating an expression.

    Read more about automatic numeric promotion here

  20. What is constructor?

    In Java there is a special method provided to initialize objects when they are created. This special method which is used for automatic initialization is called Constructor.
    Constructor has the same name as the class in which it is created and defined just like a method, that is, constructor's syntax is similar to a method

    Read more about constructor here.

  21. What is default constructor in Java?

    When a constructor is not explicitly defined for a class, then Java creates a default no-arg constructor for a class that is called default constructor.

    Read more about constructor here.

  22. What is Parameterized Constructor in Java?

    If we want our object's fields to be initialized with specific values, we can do it by adding parameters to the constructor.

    Read more about constructor here.

  23. What is constructor chaining in Java?

    In case when we have a hierarchy of classes (inheritance) the constructors of the classes are invoked in an order. That order is known as constructor chaining.
    For Exp - If class A is super-class and there is Class B which is subclass of A. In that case if a new instance of class B is created, order in which constructors of Class A and Class B are executed is from super-class to subclass.

    Read more about constructor chaining here.

  24. What is constructor overloading in Java?

    Like method overloading there is also an option to have multiple constructors within the same class where the constructors differ in number and/or types of parameters, that process is known as Constructor overloading.

    Read more about constructor overloading here.

  25. Are constructors from the super class inherited in the sub-class?

    No constructors are not inherited in Java.


  26. What is this in java?

    this in java is a reference to the current object on which the method or constructor was invoked. this can be used inside any method or constructor to refer to the current object.

    Read more about this here.

  27. What is super in java?

    The super keyword in java is essentially a reference variable that can be used to refer to its immediate parent class.
    Usage of super -

    • Invoke the constructor of the super class.
    • Accessing the variables and methods of parent class.

    Read more about super here.

  28. What is automatic numeric promotion in Java?

    In Java numeric promotion happens in case of primitive types when those primitives are used in an expression.
    As exp.

    byte a = 100;
    byte b = 50;
    int i = a * b;
    
    In the above code a * b will exceed the range of its byte operand (range of byte is -128 to 127). In these types of situations Java will automatically promote the byte, short or char to int when evaluating an expression.

    Read more about automatic numeric promotion here.

  29. What is interface in Java?

    Interfaces help in achieving full abstraction in Java, as using interface, you can specify what a class should do, but how class does it is not specified.
    Interfaces look syntactically similar to classes, but they differ in many ways -

    • Interfaces don't have instance variables.
    • In interfaces methods are declared with out any body. They end with a semicolon.
    • Interface can't be instantiated.
    • Interfaces don't have constructors.
    • An interface is implemented by a class not extended.
    • An interface can extend multiple interfaces.

    Read more about interfaces here.

  30. Can an Interface be final?

    No, interface can't be final. The whole idea of having an interface is to inherit it and implement it. Having it as final means it can't be subclassed.

    Read more about interfaces here.

  31. Is it possible to declare an interface method static?

    Not before Java 8, from Java 8 it is possible to have interface static methods.

    Read more about interface here and interface static method here.

  32. What are interface default/defender methods?

    With the release of Java 8, it is now possible to add default method in interfaces. With the addition of default method to an interface, addition of new method, to even an interface will not break the pre-existing code.
    An interface default method is defined the same way a method will be defined in a class. One difference is that in interface default method is preceded by the keyword default
    As Exp.

    public interface MyInterface {
     int method1();
     // default method, providing default implementation
     default String displayGreeting(){
      return "Hello from MyInterface";
     }
    }
    

    Read more about interface default method here.

  33. Can an Interface implement another Interface?

    No, an interface can't implement another interface. Though, interface can extend another interface.

    Read more about extending interface here.

  34. Can an Interface extend another Interface?

    Yes,interface can extend another interface.

    Read more about extending interface here.

  35. Is it possible to have final method in an interface?

    No, whole idea of interface is to have abstract methods for which implementation is provided by the implementing classes. Making a method as final in interface will mean it can't be overridden which will mean implementing class can't provide an implementation for that mthod.

    Read more about interfaces here.

  36. Is it possible to define a class inside an interface?

    Yes. An interface can have an inner class. That class will be accessed by using InterfaceName.ClassName.


  37. What is a nested interface?

    An interface or a class can have another interface. Such an interface is known as nested interface or a member interface.When a nested interface is used outside, it must be used as a fully qualified name i.e. must be qualified by the name of the class or interface of which it is a member.

    Read more about nested interface here.

1 2 3 4

No comments:

Post a Comment