Java Interview Questions
0of 0 votesWrite a function to find a key in Hash map if the value associated to that key is given as input. As a followup, swap key & value assuming that value is not equal to any of the existing keys.
0of 0 votesExplain how you would implement a multi-map in Java without using any collections?
-1of 1 voteWhat is system.gc() and runtime.gc() ?
0of 0 votesCreate a method which accept a integer as a argument and print on console. This method is accessed by multiple threads. If two or more threads call the method with same value then only one thread should allow to print the value other threads should wait. If values are different then all threads should allow to print the value.
0of 0 votesHow can you make thread safe to 3rd party library javaclass.
1of 1 voteImplement circular queue in Java such that:
1. It should work in multithreaaded environment.
2. If one thread performing EnQueue operation and if queue are full then it should wait untill other not emptied the queue.
3. If some thread tring to do Dequeue operation and if queue is empty, then that thread should wait untill other thread can fill atleast one element in queue.
0of 0 votesWrite a Program such that
1. Thread T1 and T2 are doing job J1 and J2 respectively.
2. Job J2 can't be start by J2 untill job J1 is done by J1.
3. Write code without using Join() method.
0of 0 votesWhat design pattern does AOP use?
0of 0 votesYou have two class A and B in a jar file and you have no source code with you. Write a class C which will rewrite the behaviour of the methods in A and B. You are not allowed to write any other class or interface.
0of 0 votesWrite program of thread pool.
0of 0 votesWhy do we have both checked exception and Runtime Exception? What will happen if we have only one of a kind?
0of 0 votesWhat is stale object in Java? How will you handle it? For example: You have a Class A as shown below
public class A{
A(){
// code for database connection
}
// code for other method
}
Now,you are trying to create a object A by its constructor. To initialize constructor it throws some exception to create database connection.
A obj = new A().
obj.amethod() ;
If obj.amethod() ; will be executed successfully or not ?
How will you stop your code not to use obj?
0of 0 voteswrite a class which exposes only 20 of its Objects containing two methods borrowObject and returnObject .Code must be thread safe.Also write a method to get the number of Live Objects(Objects currently in use by other classes).
0of 0 votesWrite a code in java to design connection pool. You have to pass the parameter like pool size, time out etc.
How will you call the connection from pool?Write the code in Java?
How will you return the connection to pool once time out or connection not in use. You also have to write the exception handling mechanism to print any exception.
0of 0 votesWrite a java program to read a file and get different words and also print number of occurences of each word.
0of 0 votesWrite a java program to count the words from the file Othello.txt
- Have the stop words library to filter the words like (to, the, and). (We do not want to count these words)
- Measure the time taken to execute the code.
Small input (From File) : othello.txt
Output (In Console) :
Welcome - 1
bhive -1
community - 2
Join -1
win - 1
exciting - 1
prizes -1
Time Taken: 100 milliseconds
Bigger Input (From File) : othello.txt
Does your code execute within 10 seconds?.
0of 0 votesQ: The New operator...how does it work, what are the steps?
A: I just said it creates a new memory in the heap and the reference points to it. He seemed satisfied.
-2of 2 votesIn a 5*4 matrix what is the most optimal way of traversal and compare the time complexity for different solution ?
0of 0 votesCreate a Session Manager class that iterates thru Session objects throw stale sessions out - How would you automatically purge Session objects that have not been active for 30 seconds or (n) seconds? The answer needs to handle millions of sessions.
0of 0 votesLength is given as input.Print all possible permutations of numbers between 0-9.
Eg: if input length=4
all possible combinations can be 0123, 1234, 5678,9864,...etc all combinations of length from in all numbers between 0-9
0of 0 votesLet's say you have a fixed thread pool of size 1, internally how does this threadPool work? How is that the same thread gets used again and again.
-------
My answer: The run() methods from various classes are loaded on to a task Queue. As and when the tasks get added to to this queue, the thread keeps on acting on this queue.
Something like:
Queue qTask = new Queue();
qTask.enqueue(object1.method1());
qTask.enqueue(object2.method2());
...public void run(){ while(true){ if (qTask == null or empty){ wait() } //Take tasks out of qTask }
0of 0 votesWhat are the disadvantages/limitations of a ConcurrentHashmap in JAVA?
0of 0 votesYou have two sorted list A and B.
A = [1, 3, 4, 6,8,10, 17, 34]
B = [2, 8, 17, 33, 44, 66, 89, 100, 123]
Write a program to print those numbers which are
1) in A and not in B
2) in B and not in A
Eg: After print: 1 , 3 , 4 , 6 , 10, 33, 34, 44,, 66, 89, 100, 123
I was asked to write this in JAVA.
0of 0 votesIf an N X N matrix is given, print it in spiral order.
Example: Below is 5 X 5 matrix
i l o v e
d i n t e
n i e e p
a v w r i
m a x e c
Print in spiral order. Output is iloveepicexamandinterview
0of 0 votesThere is a SRT file having timestamp and dialogue .
eg . hh:mm:ss , dialogue .
Suppose the movie runs ahead of dialogue . How or what will the approach to make it equivalent with the movie running .
For eg : The movie time is 02: 58:59 and the dialogue is 5 sec delayed.
0of 0 votesSpecial Property Numbers:
Eg--> You have a number 8987656 or 4565676
The difference between consecutive numbers is either 1 or -1.
You are given a range, you need to print the numbers with this special property.
0of 0 votesThis is on Additive Number Property
Additive Number examples:
123459 (1+2=3, 4+5=9)
314538 (3+1=4, 5+3=8)
122436 (12+24=36)
You are given a range, you need to print all the additive numbers.
0of 0 votesYou are given a 2-D array with same number of rows and columns. You have to determine the longest snake in the array. The property to find the snake is the difference between the adjacent(left, right, up or down) should be either 1 or -1. If there are more than one snakes with maximum length, the output should print both of them.
Example-->
The given array elements are as follows:
4 7 9 8
5 6 5 4
6 7 8 5
10 9 7 6
The longest snakes are 7->6->5->4->5->6 and 7->6->7->6->5->4
0of 0 votesWhat all design pattern you know. I said i know only singleton.
They asked me to explain Singleton design pattern.
0of 0 votesWhat are wait, notify and notifyAll methods?
Explain one scenario where do you use these methods?
