DronaBlog

Friday, August 17, 2018

Java Interview Questions and Answers - Part 3


Are you preparing for Java interview and looking for material for preparation? Are you also interested in knowing what kinds of questions are normally asked during Java interview? If so, then this article provides detailed questions and answers about Java. Good luck for your interview.

Q1: What is marker interface in Java?
Answer:  The interface with no defined methods is called marker interface. Such interface acts as a marker which tells the compiler that the objects of the classes implementing the interfaces with no defined methods need to be treated differently.

For example, java.io.Serializable, java.lang.Cloneable etc. are marker interfaces 
  • Marker interface also called as ‘tag’ interface as they tag the derived classes into a specific category based on usage.
  • We can write custom marker interface. 

Q2: What is method in Java?
Answer:
  • A Java method in java is a set of statements that are grouped together to perform an operation
  • It can be called at any point in the program using the method's name.
  • The method is a subprogram which works on the data.
  • Method has return type, so it either return some value or it may return void. 

     For example, in the sample code below area() is method. 
    class Triangle 
        public int area(int param1, int param2) {
        return (1/2*param1*param2);
     }

Q3: What is method overloading and method overriding?
Answer:

  1. Method overloading means multiple methods in the same class with the same name but different method signatures. We can define the same operation in different ways for different data.

          For example -
       class Shape {
            public void calculateArea(int param) {…}
            public void calculateArea(int length, int width){}
        }
     
      2. Method overriding means two methods with same name and signatures, one in the parent class and the other one in the child class. We can define the same operation in different ways for different object types.

             For example - In the example below, method name 'area(int length)' is same. The signature of method in parent and child class is also same. Here, the child class Square overrides 'area' method from parent class - Shape.
       class Shape{
             public void area(int length) {…}
        }

       class Square extends Shape {
             public void area(int length) { …}
        }

Q4: Is Java 100% Object Oriented?
Answer: The Java technology is based on Object Oriented concepts. However, it does not follow 100% Object Oriented. Java uses eight primitive data types such as int, float, double , boolean, byte, char, long, short which are not objects.  Hence it is not fully Object Oriented.

Q5: What is an Object in Java?
Answer: Object is an entity which has state and behavior. State is presented through fields and behavior is presented through methods.  Person, Table, TV, Home etc . are examples of an object.

For example, assume person as an object.
It has state - weight, height, gender, eye color, hair color etc.
It has behavior - walk, speak, listen etc.


                                   



No comments:

Post a Comment

Please do not enter any spam link in the comment box.

Understanding Survivorship in Informatica IDMC - Customer 360 SaaS

  In Informatica IDMC - Customer 360 SaaS, survivorship is a critical concept that determines which data from multiple sources should be ret...