Top 8 Programming Interview Questions

This is for all the people-cum-coders who are trying to apply for a custom based Java job!!
Oh man, this is gonna of such a huge importance, you’ll really thank me!!😉
Alright let’s begin, I’ve found out for you guys the Top 8 questions which are frequently asked in any interview for programming point of view!!
Here we go!!

Name the Basic DataTypes in Java:-

boolean, byte, char, int float, double, long, short

Why is Java profile Independent?

If a child class inherits the property from
multiple classes is known as multiple
inheritance. Java does not allow to extend
multiple classes. The problem with multiple
inheritance is that if multiple parent classes have
a same method name, then at runtime it
becomes difficult for the compiler to decide
which method to execute from the child class.
Therefore, Java doesn’t support multiple
inheritance. The problem is commonly referred
to as Diamond Problem.

What methods does the standard object class have?

Every class is a descendant, direct or indirect of the Object class. Its methods are:

• protected Object clone() throws CloneNotSupportedException

Creates and returns a copy of this object

• public boolean equals(Object obj)

Indicates whether some other object is “equal to” this one

• protected void finalize() throws Throwable

Called by the garbage collector on an object when garbage collection determines that

there are no more references to the object.

• public final Class getClass()

Returns the runtime class of an object

• public int hashCode()

Returns a hash code value for the object

• public String toString()

Returns a string representation of the object

Threading specific:

• public final void notify()

Wakes up one thread waiting on the object. Which thread is woke depends on the OS

implementation

• public final void notifyAll()

Wakes up all threads waiting on this object, although which one will process first depends

on the OS implementation

• public final void wait()

Puts the calling thread in an indefinite wait state until any other thread calls notify or

notifyAll

• public final void wait(long timeout)

Puts the calling thread in a wait state until timeout runs out or any other thread calls

notify or notifyAll

• public final void wait(long timeout, int nanos)

Puts the calling thread in a wait state until timeout and nanos run out or any other thread

calls notify or notifyAll

How can you make a Class Immutable?

Might be a difficult question. Naming a couple of things from the following list should be accepted!

1. Declare the class as final so it can’t be extended.

2. Make all fields private so that direct access is not allowed.

3. Don’t provide setter methods for variables.

4. Make all mutable fields final so that it’s value can be assigned only once.

5. Initialize all the fields via a constructor performing a deep copy.

6. Perform cloning of objects in the getter methods to return a copy rather than

returning the actual object reference.

Which types of basic collections do you know?

Hint: The Collection framework provides interfaces and classes to manage a group of the same type e.g. ArrayList

1.ArrayList

A dynamic array. It’s size is not predefined. Elements can be added to and removed

from it during runtime. Unordered container. Not synchronized (thread-safe)

2.LinkedList

A LinkedList is a collection of elements where every elements points to the next

element in the list. Every element in a LinkedList holds a reference to the next element

in the sequence. A LinkedList can be used to depict a Queue (FIFO) or even a Stack

(LIFO)

3.HashMap

A HashMap stores data in the form of key-value pairs where key and value are objects.

Every key in a HashMap must be unique (no duplicates). Values can be duplicate for

different keys. Unordered container.

4.HashSet

A HashSet stores its elements by hashing. The order is undefined. Elements are unique

(no duplicates). Internally, HashSet creates a HashMap (with dummy values) with the

values added to the HashSet as keys to the HashMap.

5.TreeMap

A TreeMap stores key-value pairs in a sorted ascending order. The retrieval of an

element is faster than with an unordered data structure.

6.TreeSet

A TreeSet stores its elements in a sorted manner. It is a Set, therefore no duplicate

elements are allowed. Elements are stored in a sorted and ascending order. Elements

are sorted by keys. TreeSet is a good choice if large amounts of sorted information

needs to be accessed quickly.

7.PriorityQueue

A PriorityQueue is a Queue that sorts it’s elements according to their natural ordering or

by a Comparator provided at creation of the PriorityQueue. The PriorityQueue sorts it’s

elements every time a new element is added. The element on top can be dequeued

and therefore removed from the PriorityQueue.

What is synchronization in Java, and how can you use it?

Synchronization is a way of controlling the access of a method or a block by multiple threads. Only one thread can enter a method or a block which has been declared as synchronized. Synchronization is one of the way to achieve thread safety.

What are the two different ways of creating threads in Java?

There are two ways to create the threads in Java. Naming the two ways should be

accepted as a correct answer.

1) By extending java.lang.Thread class.

2.) By implementing java.lang.Runnable interface.

You have thread T1, T2 and T3. How will you ensure that T3 will run after T1 and T2.

This can be achieved by using the join() method which blocks further code

execution until the thread on which it is called has finished.

Find the middle element of a custom Linkedlist in a single pass.

Hint: You do not have access to the size of the list. Use an elements .next() method to access

the next element in the list. Remember, you may iterate over it only once!

In order to find middle element of linked list we need to find length first but since

we can only traverse the LinkedList list one time, two pointers can be used one

which increments on each iteration while the other increments every second

iteration. So when the first pointer will point to the last element of linked list, the

second will be pointing to the middle element.

Leave a comment