Saturday, June 20, 2020

Java Interview @ Fresher Level - 3

Hi friends,

In this post, I'm sharing interview questions asked @ fresher level.

You can also go through my other fresher level interview posts:



Question 1:

Name the types of  SQL databases.

Answer:

There are multiple SQL databases. Few are listed below:

  • MySQL
  • SQL Server
  • Oracle 
  • Postgres


Question 2:

What is the difference between MySQL and SQL Server?

Answer:

There are multiple differences between MySQL and SQL server databases:

  • MySQL is an open source RDBMS [owned by Oracle] , whereas SQL Server is a microsoft product.
  • MySQL supports more programming languages than supported by SQL server. e.g.: MySQL supports Perl, Scheme, Tcl, Eiffel etc.  which are not supported by SQL server.
  • Multiple storage engine[InnoDB, MyISAM] support makes MySQL  more flexible than SQL Server.
  • While using MySQL, RDBMS blocks the database while backing up the data. And the data restoration process is time-consuming due to execution of multiple SQL statements. Unlike MySQL, SQL server does not block the database while backing up the data.
  • SQL server is more secure than MySQL. MySQL allows database file to be accessed and manipulated by other processes at runtime. But SQL server  does not allow any process to access or manipulate it's database files or binaries.
  • MySQL doesn't allow to cancel a query mid-execution. On the other hand, SQL server allows us to cancel a query execution mid-way in the process.     



Question 3:

Why should we never compare Integer using == operator?

Answer:

Java 5 provides autoboxing/unboxing.
So, we store int to wrapper class Integer. But we should not use == operator to compare Integer objects. e.g.:

Integer i = 127;
Integer j = 127;

i == j will give true.

Integer ii =  128;
Integer jj = 128;

ii == jj  will give false.

It is so because, Integer.valueOf() method caches int values ranging from -127 to 127. So, between this range, it will return same object.
After that, it will create new object.

Note:
Integer i = 127;
Integer j = new Integer(128);

Now, == operator will give false. As , new operator will create a new object.

 

Question 4:

Why to use lock classes from concurrent API when we have synchronization?

Answer:

Lock classes in concurrent API provide fine-grained control over locking.

Interfaces: Lock, ReadWriteLock
Classes: ReentrantLock, ReentrantReadWriteLock



Question 5:

What are the methods for fine-grained control?

Answer:

ReentrantLock provides multiple methods for more fine-grained control:

  • isLocked()
  • tryLock()
  • tryLock(long milliseconds, TimeUnit tu)
The tryLock() method tries to acquire the lock without pausing the thread. That is, if the thread couldn't acquire the lock because it was held by another thread, then it returns immediately instead of waiting for the lock to be released.

We can also specify a timeout in the tryLock() method to wait for the lock to be available:

lock.tryLock(1, TimeUnit.SECONDS);

The thread will now pause for one second and wait for the lock to be available. If the lock couldn't be acquired within 1 second , then the thread returns.



Question 6:

What is ReentrantReadWriteLock?

Answer:

ReadWriteLock consists of a pair of locks - one for read and one for write access. The read lock may be held by multiple threads simultaneously as long as the write lock is not held by any thread.

ReadWriteLock allows for an increased level of concurrency. It performs better compared to other locks in applications where there are fewer writes than reads. 


Question 7:

What is the difference between Lock and synchronized keyword?

Answer:

Following are the differences between Lock and synchronized keyword:

  • Having a timeout trying to get access to a synchronized block is not possible. Using lock.tryLock(long timeout, TimeUnit tu), it is possible.
  • The synchronized block must be fully contained within a single method. A lock can have it's calls to lock() and unlock() in separate methods.


Question 8:

Why to use Executor framework?

Answer:

We can use executor framework to decouple command submission from command execution.

Executor framework gives us the ability to create and manage threads. 

There are 3 interfaces defined in executor framework:

  • Executor
  • ExecutorService
  • ScheduledExecutorService




Question 9:

How many groups of collection interfaces are there?

Answer:

There are two groups of collection interfaces:

  • Collection
    • List
      • ArrayList
      • Vector
      • LinkedList
    • Queue
      • LinkedList
      • PriorityQueue
    • Set
      • HashSet
      • LinkedHashSet
      • SortedSet
        • TreeSet
  • Map
    • HashMap
    • HashTable
    • SortedMap
      • TreeMap



Question 10:

What is the definition of Iterable interface?

Answer:

public interface Iterable<T>{

    public Iterator<T> iterator();
}


That's all for this post.
Thanks for reading!!



No comments:

Post a Comment

CAP Theorem and external configuration in microservices

 Hi friends, In this post, I will explain about CAP Theorem and setting external configurations in microservices. Question 1: What is CAP Th...