Monday, August 21, 2017

Java Programming Interview Questions Series 01 - OOP Concepts



1. What are the main features in OOP?
               There are four main features in OOP. 
                            1. Inheritance
                            2. Polymorphism
                            3. Encapsulation
                            4. Abstraction
                As an Object-Oriented language, Java supports all the above features. 

2. What is the difference between parameter and argument?
               A Parameter is a variable which is defined by a method. It has no value in it but it will receive a value when the method is called.
                  An Argument is a value that is passed to the method when it is called.

        

3. What are the two methods to pass an argument to a method?
                1. Call-by-value: A copy of an argument value is passed to the method. Therefore modification of that argument value will not affect the original argument value.
                2. Call-by-reference: Reference to the argument is passed to the method. Therefore modification will affect the argument value.

4. What is method overloading?
                If two or more methods in a class having the same name but different parameters, it is called as method overloading. There are two types of overloading.
                              1. Method overloading by changing the data type of the argument
                              2. Method overloading by changing number of argument.

5. What is meant by default constructor?
               A constructor is a special method in a class that is used to initialize an object. Every class has a constructor. If we don't declare a constructor for a class, the compiler will create a default constructor for that class but it has no arguments. The default constructor has no meaningful implementation in it. 

6. Can we overload constructors?
             Yes. Constructors are also working as a normal method. To construct objects in different ways, we can overload constructors. 

7. What is the difference between constructor and a normal method?
               Constructors have special attributes rather than normal method. 
                              - Constructors must have the same name as the class.
                              - Constructors return the current state of a class. But constructor signature does not have any return type. 
                              - Constructors in java cannot be abstract, synchronized or static.
                              - They are only called once for an object.
               A normal method can be called many times and can be static, final, abstract. And it can return a value or can be void. 

8. What is meant by garbage collection?
               Normally the object will be destroyed automatically from the memory by the JVM. If there is no reference to an object, then it is assumed that the particular object is no longer needed and the memory will release the occupied memory. That technique is called Garbage Collection.

9. What are the advantages of Garbage collection?
           The programmer does not need to dereferencing an object. Because JVM does it automatically. Therefore memory efficiency will increase and decrease the chance to a memory leak.

10. What are Access Control Modifiers and Non-Access Modifiers?
             Basically, modifiers are keywords, that are used to change the meaning of a definition.
                              1. Access Control Modifiers
                                   Private    : Visible to the class only.
                                   Protected : Visible to the package and all subclasses.
                                   Default    : Visible to the package only.
                                   Public      : Visible to the world.
                          2. Non-Access Modifiers
                                   Static Used in classes and methods which can be accessed without an instance of a class.
                                   Final : Act as constant. Then, class, method or variable cannot be changed.
                                   Synchronized : Used in Threads. Then only one thread can be accessed to that method at a given point in time.
                                   Volatile : Used in Threads. It tells the compiler that the particular volatile variable can be changed unexpectedly in other segments of the program.
                                   Transient : Used in instance variables. Then it's value doesn't persist when an object is serialized.
                                   Abstract : Used to create abstract classes and methods.

11. Why constructors cannot be static?
               The need of a constructor is to initialize the contents of an instance of the class. But static methods do not have an instance. They stay in the place called class area. Therefore they can be executed without an instance. Therefore static constructor does not make any sense.

12. Why the Main method is static?
               Static classes and methods can be accessed without an instance of a class. The entry point of any Java program is the Main method. Therefore it is declared as static because it is called before any object of the class is created.

13. What is static initialization block?
              This is used to initialize static data member. Static block executes before the Main method. Static initialization block can be viewed as 'static constructor.'

14. What is static variable and instance variable?
              Static variables are defined as a class member and they can be accessed without any object of that class (Like static methods) and they can be accessed using its class name. They have only one single storage.
             Instance variables are initialized when an object of the class is initialized. And they can be accessed using the name of the object. Instance variables get new memory each time a new object is created. 

15. Why a non-static variable cannot be referenced from a static context?
              Non-static variables are created when an instance of the particular class is created. It has no particular storage in memory like static variables. Therefore if you try to access a non-static variable without any instance, the compiler will complain because the particular variable is not created yet.











0 comments:

Post a Comment

Copyright © iTecTricks | Powered By Blogger

Design by Shehan Vanderputt