Sunday, June 25, 2017

Java OOP/Core Java Questions

1. What is JVM and is it platform independent:
    JVM is Java Virtual Machine and is responsible for converting byte code into machine readable code.

2. Difference between JVM and JDK and JRE:
    JVM is used to execute bytecode            JDK is for dev          
    JRE  = JVM  +  java binary libraries
    If u wanna run Java program, u need JR

3. Which class is the superclass of all classes?
    java.lang.Object is the root class for all the java classes and we don’t need to extend it.

3.5 Difference between Extends / Implements
    Implements is for implementing an interface (can implement multiple interfaces)
    Extends is for extending a class(can only extends one )
    Interface method generally doesn't have implementation.

4.Why Java doesn’t support multiple inheritance(extends a class )?
    Because of Diamond Problem
    Diamond Problem:
     
    Let's say we have a method in A.    B and C both override that method.   Should D take B's implementation or C's implementation

 5. What is path and classpath:
    path is sys environment
    classpath is used for Java to locate class files

6. What is overload and overriding:
    Overload means same method signature with different parameter
    Override, one in parent class and another in child class. Use to override method in parent class

7. Can we overload main method:
    yes, we can have multiple method named with main, but only public static void main(String args[])
    will be treated as main method

8. Can we have more than one public class in one java source file
    no, only one public Java class is allowed in one java file

9. What is final/finally/finalize keyword:
    For class: to make sure no other class can extend it, String class is final and can't be extended
    For method: child class cannot override parent's method
    For variable: make sure variable can only be assigned once, however, the state of the variable can
    be changed. For example, we can assign a final variable to an object only once, but the object  
    variable can be changed later on
     Java interface variables are by default final and static
 
    Finally block: put the code which will always be executed even if any exception thrown by the try
                            catch block.
    Finalize: Tell JVM it is good to do GC

10. What is static keyword:
    For method: A static method can only access that class' static variable and invoke
    For variable: can be used within class level to make it global. all objects will share the same variable

10.5 What is super keyword:
    Access super class method you have overridden in child class
    Invoke superclass's consturctor

11. What is Interface:
    Provide a way to achieve abstraction in java and used to define the contract for the subclasses to implement. Interface method doesn't have implementation

12. What is an abstract class:
    Abstract classes have some default method implementations for subclasses.
    An abstract class can have abstract method(no body) and real method(with body)
    Abstract class cannot be instantiated

13. Abstract class VS Interface:
    Abstract class can have method implementation while Interfaces can't
    A class can implements >1 interfaces while can only extends one abstract class
    Abstract uses abstract to define abstract class while interface uses interface to define interface

14. Can Interface implement/extend another interface:
    Interface can implement >2 interfaces

15. Four elements of OOP
    EA has a good IP

    Encapsulation:    hide data and implementation from outside by using setter/getter
    Abstraction:        It is a concept. One class should not know detail of another to use it.

    Inheritance:         Extends/Implement. Classes can be derived from other classes.
    Polymorphism:   Overload/Override. One name can have multiple implementation

16. What is default constructor?
    Compiler automatically creates default no-args constructor for classes. If there are other constructors defined, the compiler won't create default constructor.

17. Can we have try (possible with finally)without catch block:
    Yes.

18 What is GC?
   Look through heap memory and delete objects not in use.

19 What is mock object?
    Mocking or mock objects is a unit testing strategy.  Replace code chunk with dummy codes. For example, if you want to test a method in class A. Class A depends on class B/C/D, you can create some dummy objects b,c,d to testing class A instead of implementing class B/C/D

20. How to create immutable objects?
  1. Don’t provide any methods that modify the object’s state (known as mutators).
  2. Ensure that the class can’t be extended.
  3. Make all fields final.
  4. Make all fields private. This prevents clients from obtaining access to mutable objects referred to by fields and modifying these objects directly.
  5. Make defensive copies. Ensure exclusive access to any mutable components.
21. How sets avoid duplicates internally ?

It roughly works like this
if (!collection.contains(element))
    collection.add(element);
And the contains method, would use equals/hashcode.
In TreeSet, the elements are stored in a Red-Black Tree, 
In HashSet, uses a HashMap to traverse buckets and use equals() to check equality

22. What is TreeMap

The TreeMap class implements the Map interface by using a tree. A TreeMap provides an efficient means of storing key/value pairs in sorted order, and allows rapid retrieval.
A tree map guarantees that its elements will be sorted in an ascending key order

No comments:

Post a Comment