Interview qns & Ans

Types (Hierarchy) of Java Class Loaders

Java class loaders can be broadly classified into below categories:
  • Bootstrap Class Loader  - (loads class from JRE)
    Bootstrap class loader loads java’s core classes(usually part of java runtime environment) like java.lang, java.util etc.
  • Extensions Class Loader  -  (loads classes from this ext folder)
    JAVA_HOME/jre/lib/ext contains jar packages that are extensions of standard core java classes. 
  • System Class Loader - (loads from java classpath)
    Java classes that are available in the java classpath are loaded using System class loader.
1. How ConcurrentHashMap Internal Works?
A: ConcurrentHashMap allows concurrent threads to read the value without locking. default 16 segments (Inner class Segment) will be maintained internally.so at max 32 threads can work at a time. It means each thread can work on a each segment during high concurrency and atmost 32 threads can operate at max which simply maintains 32 locks to guard each bucket of the ConcurrentHashMap.

Inserting (Put) Element in ConcurrentHashMap:
1st calculate the hash of key 
After getting the hashVal we can decide the Segment.
since it is concurrency, synchronized block on each Segment.
on comparison of key's hash values and equals, either updates the existing value or creates new entry object and puts at calculated index. null insertion is not possible in ConcurrentHashMap as key or value.

Getting (get) Element From ConcurrentHashMap :
When we are getting an element from ConcurrentHashMap we are simply passing key and hash of key is getting calculated and finds the  segment(i.e Entry).
No need to put any lock when getting the element from ConcurrentHashMap.
At a time any number of threads are applicable for read operation without locking the ConcurrentHashMap object which is not there in HashMap.
---------------------------------------------------------
2.HashSet Vs LinkedHashSet Vs TreeSet

HashSet -
1. It internally uses HashMap for storing objects.
2. HashSet can be used, if you dont want to maintain insertion order but want to store unique objects.
3. O(1) complexity for insertion, removing and retrieving objects.
4. HashSet performance is better than LinkedHashSet and TreeSet
5. it allows one null object

LinkedHashSet -
1. It internally uses LinkedHashMap for storing objects.
2. LinkedHashSet can be used, if you want to maintan insertion order of elements.
3. O(1) complexity for insertion, removing and retrieving objects.
4. LinkedHashSet performance is slower than TreeSet and almost similar or slower than HashSet. because of it internally uses linkelist datastructure.
5. it allows one null object

TreeSet - 
1. It internally uses TreeMap for storing objects.
2. Treeset can be used, if you want to sort the elements based on some comparator.
3. O(log n) complexity for insertion, removing and retrieving objects.
4. TreeSet Performance is better than LinkedHashSet and slower than HashSet. because it has to sort the elements after each element insertion and removal of operations.
5. TreeSet not allow a any null objects. If you insert null objects into TreeSet, it throws NullPointerException
---------------------------------------------------------
3. Java Cursors
A.  1. Enumeration(1.0)  - only forward reads on collections
     2. Iterator(1.2)  -  support forward read and remove elements from collection.
Enhanced for loop uses Iterator object internally and do the same.
we can customize the our own iterator by implementing iterator interface and override iterator() method. so we can give external iteration of objects.
     3. ListIterator(1.2) - 
- Bi-Directional Itrator,It supports both Forward Direction and Backward Direction                   iterations.
-used to iterate elements one-by-one from a List implemented object.
-It extends Iterator interface.It supports all four operations: CRUD (CREATE, READ,                  UPDATE and DELETE). 
-Forward Direction Iteration methods (hasNext(), next(), nextIndex())
-Backward Direction Iteration methods(hasPrevious(), previous(), previousIndex())
-It is not a Universal Java Cursor.
-Compare to Spliterator, it does NOT support Parallel iteration of elements.
-Compare to Spliterator, it does NOT support better performance to iterate large                   volume of data.
    4. Spliterator (1.8) - 
-  supports parallelprogramming functionality
- We can use it for both Collection API and Stream API classes
- We can NOT use this Iterator for Map implemented classes.
- It uses tryAdvance() method to iterate elements individually in multiple Threads to support Parallel Processing.
- It uses forEachRemaining() method to iterate elements sequentially in a single Thread.
- It uses trySplit() method to divide itself into Sub-Spliterators to support Parallel Processing.
- Spliterator supports both Sequential and Parallel processing of data.
Spliterator itself does not provide the parallel programming behavior. However, it provides some methods to support it. Developers should utilize Spliterator interface methods and implement parallel programming by using Fork/Join Framework (one good approach).


---------------------------------------------------------
4. How Java iterator works internally?
A.  On create Iterator object on list object 
ex: Iterator<String> namesIterator = names.iterator();
on calling hasNext , next methods on iterator, Iterator’s Cursor points to the first element in the List. and as again same process will go, cursor moves from one element to next element.
After reading the final element, hasNext method returns false.

--------------------------------------------------------------------------------
JAVA 8 Fetures:
Feature NameDescription
Lambda expressionA function that can be shared or referred to as an object.
Functional InterfacesSingle abstract method interface.
Method ReferencesUses function as a parameter to invoke a method.
Default methodIt provides an implementation of methods within interfaces enabling 'Interface evolution' facilities.
Stream APIAbstract layer that provides pipeline processing of the data.
Date Time APINew improved joda-time inspired APIs to overcome the drawbacks in previous versions
OptionalWrapper class to check the null values and helps in further processing based on the value.
Nashorn, JavaScript EngineAn improvised version of JavaScript Engine that enables JavaScript executions in Java, to replace Rhino.


Java 8 Explained: Using Filters, Maps, Streams and Foreach to apply Lambdas to Java Collections!

Java 8 Stream API: map, filter, foreach

The new java.util.stream package has been added to JDK which allows us to perform filter/map/reduce-like operations with the collections in Java 8.
The Stream API would allow us to declare either sequential or parallel operations over the stream of data:
List<Person> persons = … 

// sequential version
Stream<Person> stream = persons.stream();
 
//parallel version 
Stream<Person> parallelStream = persons.parallelStream(); 
The java.util.stream.Stream interface serves as a gateway to the bulk data operations. After the reference to a stream instance is acquired, we can perform the interesting tasks with the collections.

Filter

Filtering a stream of data is the first natural operation that we would need. Stream interface exposes a filter method that takes in a Predicate (http://javadocs.techempower.com/jdk18/api/java/util/function/Predicate.html ) SAM that allows us to use lambda expression to define the filtering criteria:
List<Person> persons = …
Stream<Person> personsOver18 = persons.stream().filter(p -> p.getAge() > 18);
Now the thing is that often you want to filter a List just like in the example above. Then you can easily convert the list to a stream, filter and collect the results back to a list if necessary.

Map

Assume we now have a filtered data that we can use for the real operations, say transforming the objects. The map operations allows us to apply a function (http://javadocs.techempower.com/jdk18/api/java/util/function/Function.html ), that takes in a parameter of one type, and returns something else. First, let’s see how it would have been described in the good ‘ol way, using an anonymous inner class:
Stream<Student> students = persons.stream()
      .filter(p -> p.getAge() > 18)
      .map(new Function<Person, Student>() {
                  @Override
                  public Student apply(Person person) {
                     return new Student(person);
                  }
              });
Now, converting this example into a lambda syntax we get the following:
Stream<Student> students = persons.stream()
        .filter(p -> p.getAge() > 18)
        .map(person -> new Student(person));
And since the lambda that is passed to the map method just consumes the parameter without doing anything else with it, then we can transform it further to a method reference:
Stream<Student> students = persons.stream()
        .filter(p -> p.getAge() > 18)
        .map(Student::new);


MetaSpace  vs PermGen :

PremGen: MetaData information of classes was stored in PremGen (Permanent-Generation) memory type before Java 8. PremGen is fixed in size and cannot be dynamically resized. It was a contiguous Java Heap Memory.

MetaSpace: Java 8 stores the MetaData of classes in native memory called 'MetaSpace'. It is not a contiguous Heap Memory and hence can be grown dynamically which helps to overcome the size constraints. This improves the garbage collection, auto-tuning, and de-allocation of metadata.








-------------------------------------------------------------------------------------------------

Q) What are NFR items?
Ans) 
Non-Functional Requirement (NFR) defines the quality attribute of a software system. They judge the software system based on Responsiveness, Usability, Security, Portability and other non-functional standards that are critical to success of the software system. 
Non-functional Requirements allows you to impose constraints or restrictions on the design of the system across the various agile backlogs. Example, the site should load in 3 seconds when the number of simultaneous users are > 10000. Description of non-functional requirements is just as critical as a functional requirement.

Types of Non-functional Requirement






  • A non-functional requirement defines the performance attribute of a software system.
  • Types of Non-functional requirement are Scalability Capacity, Availability, Reliability, Recoverability, Data Integrity, etc.
  • Example of Non Functional Requirement is Employees never allowed to update their salary information. Such attempt should be reported to the security administrator.
  • Functional Requirement is a verb while Non-Functional Requirement is an attribute
  • The advantage of Non-functional requirement is that it helps you to ensure good user experience and ease of operating the software
  • The biggest disadvantage of Non-functional requirement is that it may affect the various high-level software subsystems.
--------------------------------------------------------------
Technical Discussion with HSBC Client(UK)

1.what are your roles and responsibilities in your project?
A) working as Full stack developer and individual contributor. 

2.tell about GIT, why GIT, difference with others
A) GIT is centralized repository for a project. developer can work on his own copy of repository(as well branch on forking) and push his code and merge into project branch  

3.in a agile process, how you will prepare design docs
A) based on requirements , update on DFD and Design document on project level(high level). 

4.How you will Design the project(steps to design)
A)  depends on the requirements, design can be modular based(ex: springboot+Micro services) or mono project (very simple maven project).

5.in a Project/technical architectural point of view, what are parameters you will be consider in development , deployment etc.,
A) easy to manage, easy to learn, functionality segregation,  Performance improvement based ...etc.,

6.what are the differences of SOAP and Rest webservices
A) then main difference is  SOAP requires contract, where as REst will work on Endpoint url.

7.what are different http methods, when to use?
A) GET-> to fetch records, POST->To save record, PUT->To update record

8.how you will deploy your project? have you involved in devops roles?
A)through CI/CD Tools ex: jenkins, bamboo etc., . yes I have involved in Devops roles.

9.with whom you will get review your work and how you will consider review comments?
A)  get review with Senior technical person or peer to peer review.
as per coding standards or applying sonar quality gate rules
-----------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment

JAVA NOTES

  click here  👉  Interview  Preparation : complete notes sql queries notes ============================================================ Con...