Sunday, June 25, 2017

Java Spring/Hibernate Questions

1.  Pros and Cons about Sping
    Cons:
         Huge, I wouldn't put 3000 jars in my hobby project
         Slower than other DI, like juice
         getting more and more complex in annotation and XML
     Pros:
         decoupling
         readable

2. What are the common types of Dependency Injection:
    Constructor-based:
        Use for mandatory injection

    Setter-based:
        invoking a no-argument constructor or no-argument static factory method to instantiate your bean.
        Use for optional injection
    1. Inject differently
    2. Constructor guarantees injection while setter injection can be failed
    3. Setter injection can detect circular dependency by throwing ObjectCurrentlyInCreationException
 

http://javarevisited.blogspot.com/2012/11/difference-between-setter-injection-vs-constructor-injection-spring-framework.html

3. What is the scope of Java Spring bean?
singleton: one object/ container     prototype/request: one object/ bean is requested or HTTP requested


Java GC/Memory Model Questions

GC
1. What are the six reachability states in Java?
    Strongly reachable objects
    Softly reachable objects
    Weakly reachable objects
    (Weak Refer: a reference that doesn't protect the referenced object from collection)
    Resurrect-able reachable objects
    Phantomly reachable objects
    Unreachable reachable objects

    https://stackoverflow.com/questions/299659/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java

2. How does GC work and when do JVM perform GC
heap regions for garbage collection in java
java garbage collection
What is Eden: newly created objects will be putted here
What is survivor: objects which survived Eden GC
Why there are two survivors: Only put things to old generation if objects survives many GC

What are the major GCs:
Minor GC:  Clean eden, move Eden to survivors
Full GC:     Clean entire heap space

When to collet:
Minor  When don't have enough space to allocate new space in Eden
Major:  New candidate for Old generation is bigger than free space in Old generation

What to collect:
    Go through GC roots. Any unreachable objects will be marked as OK to collect

What happened:
    stop some threads and finalize some objects




Java Multithread Questions

1. What is the difference between Callable and Runnable?
    Runnable is Old and has been there for a while.
    People knows how to run at the beginning. Once she runs away, you don't get back from her.
    Callable can return a result and cannot throw a checked exception

2. How to detect deadlock in Java?
    Use ThreadMXBean class. It has a method called findDeadlockedThreads.

3. Difference between static variable and volatile variable in Multithread java
    Declaring a static variable in Java, means that there will be only one copy, no matter how many objects of the class are created. However, threads may have locally cached values of it.
    When a variable is volatile and not static, there will be one variable for each Object.

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