Wednesday, 5 July 2017

Core Java Interview Questions!!

 

Ques 1) Define what is the difference between an Inner Class and a Sub-Class?
Answer:                  Inner Class                                                  Sub-Class
  • Inner classes are in the same file.                    Sub-classes can be in another file, maybe in                                                                                    another package.
  • Inner class lies within the same class.             Sub-class may may reside as a child class in                                                                                  inheritance.
  • Inner classes have the methods they want.      Sub-classes have the methods of their parent                                                                                  class.


Ques 2) Can a class be a super class and a sub-class at the same time? Give example.
Answer: A sub class has an 'is a' relationship with its super class. This means that a sub class is a               special kind of its super class. When we talk in terms of objects, a sub class object can                   also be treated as a super class object. And hence, we can assign the reference of a sub               class object to a super class variable type. However, the reverse is not true.
        The following statements illustrate this concept by creating a Student object and                            assigning it to a Person variable. However, when we try to assign the reference of a Person            object to a Student variable, we get a compilation error.
            Person p=new Student("Sai", 19,100);
           Student s=new Person("Ram",18);//error
      This 'is a' relationship finds application in other areas too. For example, we can pass a                   Student object to a method which requires a Person object and we can return a Student object       from a method whose return type is Person.
      public Person someMethod () {
      Student s=new Student ("Sai", 19,100);
      return s; // allowed
      }

      public void needAPerson ( Person p ) {
      // code
      }

     public void aMethod() {
     needAPerson ( new Student("Sai", 19,100) ) ; // allowed
      }


Ques 3) When we should use serialization?
Answer:  
  • To persist data for future use.
  • To send data to a remote computer using such client/server Java technologies as RMI or socket programming.
  • To "flatten" an object into array of bytes in memory.
  • To exchange data between applets and servlets.
  • To store user session in Web applications.


Ques 4) Identify five classes of java.util.concurrent package?
Answer:  
  • ArrayBlockingQueue <E>
  • Executors
  • Exchanger <V>
  • CyclicBarrier
  • ThreadLocalRandom



Ques 5)Explain how HashMap works internally?
Answer:  hashCode() - HashMap provides put(key, value) for storing and get(key) method                 for retrieving Values from HashMap. When put() method is used to store (Key,                       Value) pair, HashMap implementation calls hash code on Key object to calculate a              hash that is used to find a bucket where Entry object will be stored.


Ques 6) Explain difference between Abstract classes & interfaces?
Answer:                      Abstract Classes                                     Interfaces
  •  An abstract class may contain non-final variables.      Variables declared in a Java interface                                                                                              are by default final.
  • It can have class members like private, protected.     Members of a Java interface are public                                                                                          by default.
  • It can be extended using keyword “extends”.               It can be implemented using keyword                                                                                            “implements” .

Ques 7) Define what’s the base class in Java from which all classes are derived?
Answer: Base Class is a class, in an Object Oriented Programming language, from which other                         classes are derived. It facilitates the creation of others classes that can be reuse the code                       implicitly inherited from the base class.

Ques 8) Define what’s meant by anonymous class?
Answer: Anonymous classes enable you to make your code more concise. They enable you to declare               and instantiate a class at a same time. They are like local classes except that they do not have               a name. Use them if you need to use a local class only once.
              It can be created by two ways:- class(may be abstract or concrete) and interface.


Ques 9) Can we have any other return type than void for main method?
Answer: No, the return type of main()method must be void only. Any other type is not acceptable.


Ques 10) Can we declare the main method of our class as private?
Answer: Yes, we can declare the main method of our class as private. It complies without any errors,                  but in run-time, it says main method is not public.




Check your score, if you feel any need for Core Java training, you can contact us on:
Ph: 9999201478
Website: www.gyansetu.in



No comments:

Post a Comment