• Slider Banner

    Prepare for

    Technical Interview

    Over .1 Million+ Interview Questions for different profiles

    Read More
  • Slider Banner

    Join the

    Programming World

    Learn about top programming languages, tools and frameworks.

    Read More
  • Slider Banner

    Prepare with

    Certification MCQ

    Practice for certification from 1000+ multiple choice questions.

    Read More

How do you run headless mode browsers in Protractor?

Headless browser doesn’t require a graphics driver at all, They just get commands and execute them directly in memory. To achieve this, you need to specify headless in configuration.js under capabilities.

Chrome

capabilities: {
  browserName: 'chrome',

  chromeOptions: {
	 args: [ "--headless", "--disable-gpu", "--window-size=800,600" ]
   }
}

Firefox

capabilities: {
  browserName: 'firefox',

  'moz:firefoxOptions': {
	 args: [ "--headless" ]
   }
}

How to implement Read Write Lock in Java?

Use below code:

package com.test.filehandling;

import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ThreadSafeArrayList<E>
{
	private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
	private final Lock readLock = readWriteLock.readLock();
	private final Lock writeLock = readWriteLock.writeLock();
	
	private final List<E> list = new ArrayList<E>();
	
	public void set(E o)
	{
        writeLock.lock();
        try
        {
            list.add(o);
            System.out.println("Adding element by thread: "+Thread.currentThread().getName());
        }
        finally
        {
            writeLock.unlock();
        }
	}
	
	public E get(int i)
	{
		readLock.lock();
        try
        {
            System.out.println("Printing elements by thread: "+Thread.currentThread().getName());
            return list.get(i);
        }
        finally
        {
            readLock.unlock();
        }
    }
	
    public static void main(String[] args)
    {
        ThreadSafeArrayList<String> threadSafeArrayList = new ThreadSafeArrayList<String>();
        threadSafeArrayList.set("1");
        threadSafeArrayList.set("2");
        threadSafeArrayList.set("3");
 
        System.out.println("Printing the First Element: "+threadSafeArrayList.get(0));
    }
}

What are the differences between HashMap and TreeMap?

Below are the difference:

Point of Distinction HashMap TreeMap
Time Complexity (Big O notation) Get, Put, ContainsKey and Remove method O(1) O(1)
Definition Java HashMap is a hashtable based implementation of Map interface. Java TreeMap is a Tree structure-based implementation of Map interface.
Iterator Order Random Sorted according to either natural order of keys or comparator (as specified during construction) 
Null Keys / Value HashMap allows a single null key and multiple null values. TreeMap does not allow null keys but can have multiple null values.
Homogeneous/ Heterogeneous HashMap allows heterogeneous elements because it does not perform sorting on keys. TreeMap allows homogeneous values as a key because of sorting.
Performance HashMap is faster than TreeMap because it provides constant-time performance that is O(1) for the basic operations like get() and put(). TreeMap is slow in comparison to HashMap because it provides the performance of O(log(n)) for most operations like add(), remove() and contains().
Interface HashMap implements Map, Cloneable, and Serializable interface. TreeMap implements NavigableMap, Cloneable, and Serializable interface.
Data Structure The HashMap class uses the hash table. TreeMap internally uses a Red-Black tree, which is a self-balancing Binary Search Tree.
Comparison Methods It uses equals() method of the Object class to compare keys. The equals() method of Map class overrides it. It uses the compareTo() method to compare keys.
Functionality HashMap class contains only basic functions like get(), put(), KeySet(), etc.  TreeMap class is rich in functionality, because it contains functions like: tailMap(), firstKey(), lastKey(), pollFirstEntry(), pollLastEntry().
Requirement for Keys Equals() and hashCode() needs to be overwritten. comparator needs to be supplied for keys implementation, otherwise natural order will be used to sort the keys.
Application General purpose, fast retrieval, non-synchronized. ConcurrentHashMap can be used where concurrency is involved.  Algorithm where sorted or Navigable feature are required. For example, find among the list of employees whose salary is next to given employee, range search, etc.

What will be the output of below Java code?

Example Code 1: 

public interface  A  
{	
    public void m1();
    public void m2();
}

class B implements A
{
    public void m1()
    {	
    }
}

Output: compile-time error:- The type B must implement the inherited abstract method A.m2()

Example Code 2: 

public interface A  
{
    public void m1();
    public void m2();
}

abstract class B implements A
{
    public void m1()
    {
        System.out.println("Vinay");
    }
}

class C extends B
{
    public void m2()
    {
        System.out.println("Method2 value from the class C");
    }
    public void m1()
    {
        System.out.println("Method1 value from the class C");
    }

    public static void main(String[] args)
    {
        C c=new C();
        c.m1();
        c.m2();

        B b=new C();
        b.m1();
        b.m2();

        A a=new C();
        a.m1();
        a.m2();
    }
}

Output:

Method1 value from the class C

Method2 value from the class C

Method1 value from the class C

Method2 value from the class C

Method1 value from the class C

Method2 value from the class C

What is a constructor overloading in Java?

In Java, constructor overloading is a technique of adding any number of constructors to a class each having a different parameter list. The compiler uses the number of parameters and their types in the list to differentiate the overloaded constructors.

class Demo
{
    int i;

    public Demo(int a)
    {
        i = k;
    }
    
    public Demo(int a, int b)
    {
        //body
    }
}

In above example, Demo() constructor is overloaded.

What is a copy constructor in Java?

Copy constructor is a member function that is used to initialize an object using another object of the same class. Though there is no need for copy constructor in Java since all objects are passed by reference. Moreover, Java does not even support automatic pass-by-value.

What is object cloning in Java?

Object cloning in Java is the process of creating an exact copy of an object. It basically means the ability to create an object with a similar state as the original object. To achieve this, Java provides a method clone() to make use of this functionality.

This method creates a new instance of the class of the current object and then initializes all its fields with the exact same contents of corresponding fields. To object clone(), the marker interface java.lang.Cloneable must be implemented to avoid any runtime exceptions. One thing you must note is Object clone() is a protected method, thus you need to override it.

What is composition in Java?

Composition is again a specialized form of Aggregation and we can call this as a "death" relationship. It is a strong type of Aggregation. Child object does not have their lifecycle and if parent object deletes all child object will also be deleted.

Let's take again an example of a relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room can not belongs to two different houses if we delete the house room will automatically delete.

What do you mean by aggregation in Java?

An aggregation is a specialized form of Association where all object has their own lifecycle but there is ownership and child object can not belong to another parent object. Let’s take an example of Department and teacher. A single teacher can not belong to multiple departments, but if we delete the department teacher object will not destroy. 

What is an association in Java?

Association is a relationship where all object have their own lifecycle and there is no owner. Let's take the example of Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle.

These relationships can be one to one, one to many, many to one and many to many.

Subscribe to