Reader Sreenath Ravva shared these questions from an Oracle Enterprise Manager interview. This is a historical account, not an official Oracle question bank or a prediction of a current interview. The answers below make the original questions useful for revising Java, concurrency, JDBC and project discussions.
I have tried to put links to web pages where you can refer for answers to below interview questions. They may not be enough to cover the topic, but they will give you a start.
1. First Round (Telephonic)
1. Can you please just start telling about yourself and your project?
Introduce your current role, the application’s purpose and the part you owned. Describe one technical decision, why you chose it and an outcome you can support. Keep confidential project details out of the answer.
https://www.youtube.com/watch?v=CumOvDWnUDY
2. What is abstraction and encapsulation in java ?
Abstraction exposes the behavior callers need; encapsulation controls access to an object’s state and implementation. An interface can describe an abstract contract, while private fields and methods help enforce an implementation’s invariants.
- howtodoinjava.com/object-oriented/understanding-abstraction-in-java/
- howtodoinjava.com/object-oriented/encapsulation-in-java-and-its-relation-with-abstraction/
3. Method Overloading rules? Can we overload the super class method in sub class. Discussion happened with the example.
Overloading uses the same method name with different parameter lists. A subclass can add an overload of an inherited accessible method. Return type alone cannot distinguish overloads, and selection uses compile-time argument types.
- howtodoinjava.com/2013/07/15/what-is-polymorphism-in-java
- https://stackoverflow.com/questions/10901259/java-overloading-rules
4. Method Overriding rules?
Overriding replaces inherited instance-method behavior with a compatible signature and return type. It cannot reduce access or broaden checked exceptions. Static methods are hidden, and final methods cannot be overridden.
5. Widening and narrowing in java? Discussion happened with example?
Widening conversions generally move to a broader primitive type without an explicit cast, though some numeric conversions can lose precision. Narrowing usually requires a cast and can lose range or precision. Reference casts are checked against the object’s runtime type.
6. Can I have only try block in code?
An ordinary try statement needs a catch or finally block. A try-with-resources statement can omit both because its resource specification provides the cleanup behavior.
Since Java 7, try-with-resources closes eligible resources automatically. Prefer it for JDBC and other AutoCloseable resources rather than relying on a finalizer.
try (FileInputStream f = new FileInputStream("temp.json"))
{
System.out.println("test");
}
7. Threads : producer and consumer problem?
Use a bounded BlockingQueue: producers put work into it and consumers take work out. Blocking coordinates empty/full conditions and provides backpressure. Define how producers finish and consumers shut down instead of relying on an endless loop.
- http://en.wikipedia.org/wiki/Producer-consumer_problem
- //howtodoinjava.com/java-5/how-to-use-blockingqueue-and-threadpoolexecutor-in-java/
8. Why wait(), notify() and notifyAll() are defined in Object class?
Every object can act as a monitor with an associated wait set, so monitor coordination methods belong to Object. A thread must own that object’s monitor before calling wait, notify or notifyAll. Waiting releases that monitor; it does not release every lock the thread holds.
- //howtodoinjava.com/2013/03/04/core-java-interview-questions-series-part-2/
- http://stackoverflow.com/questions/17840397/concept-behind-putting-wait-notify-methods-in-object-class
9. Can we override wait() or notify() methods?
In Object.java, methods getClass(), notify(), notifyAll() and three wait() methods are final, so you can’t override them.
10. Difference between wait(), sleep() and yield()?
wait releases the owned object monitor and waits for notification, interruption or timeout. sleep pauses the current thread without releasing monitors. yield is a scheduler hint that may be ignored. None guarantees that a particular other thread will run next.
11. Explain about join() method in thread class
join waits for the target thread to terminate, with optional timeout variants. When an untimed join returns normally, the joining thread can observe the completed thread’s actions through the happens-before guarantee. Handle interruption rather than ignoring it.
http://stackoverflow.com/questions/18479771/java-multithreading-concept-and-join-method
2. Second Round (Face to Face)
1. Can you just start telling about yourself and your project?
Use the same concise introduction as in the first round, then be ready to explain the project architecture, your responsibilities and a decision the interviewer asks about. Keep the account consistent across rounds.
Refer to first question in above list.
2. Have you faced out of memory error? If yes how you fixed ? Tell different scenarios why it comes?
Explain a real incident if you have one; otherwise describe your diagnostic approach honestly. Read the OutOfMemoryError message to identify heap, Metaspace, direct memory or thread-creation pressure. Use suitable dumps, GC logs and resource metrics before assuming the solution is a larger heap.
3. Database connection leakage?
A connection leak occurs when borrowed connections are not returned to the pool. Close Connection, Statement and ResultSet resources reliably, inspect pool active/idle counts and use leak diagnostics to identify the borrowing code.
4. Write a program to swap two numbers with out using third variable?
XOR can swap two distinct integer variables without a third variable. Arithmetic swapping can overflow, and XOR can obscure intent or fail when both locations alias the same array element. State the puzzle constraints; ordinary production code is clearer with a temporary variable.
import java.io.*;
public class SwappingNumberWithoutThirdVariable
{
public static void main(String[] args)throws IOException
{
int a = 0 ,b = 1;
System.out.println("a = "+a);
System.out.println("b = "+b);
//Beginning of Swapping
a = a + b;
b = a - b;
a = a - b;
//End of Swapping
System.out.println("The numbers after swapping are");
System.out.println("a = "+a);
System.out.println("b = "+b);
}
}
5. Write a program to sort an array and remove duplicates?
Sort the array, then scan once and retain only values that differ from the previous retained value. Discuss whether the input may be modified and how an empty array is handled. Sorting dominates the usual comparison-based time cost.
http://stackoverflow.com/questions/17967114/how-to-remove-duplicates-from-an-array-in-java
6. Write a program on Singleton?
An enum constant or initialization-on-demand holder can provide a JVM-managed singleton. Explain lazy initialization, constructor access and the intended scope. A Spring singleton is one bean instance per container and bean definition, not necessarily one instance across a whole distributed application.
//howtodoinjava.com/design-patterns/singleton-design-pattern-in-java/
7. I have two arrays which contains integer. Write a program to merge those two arrays and remove duplicate elements? At the end, I need one array which should have unique elements?
For sorted unique output, combine both arrays and insert values into a TreeSet, or sort and deduplicate a combined array. For encounter order, use a LinkedHashSet. Choose the data structure after clarifying the required output order.
http://stackoverflow.com/questions/5057156/merging-lists-into-a-single-array-with-unique-elements
8. Write a program to fetch the data from a table using JDBC and result set?
Obtain a connection, create a PreparedStatement, bind parameters and execute the query. Traverse the ResultSet with next() and read named columns. Use try-with-resources for all three resources and handle SQL errors without returning a connection leak.
//howtodoinjava.com/2013/11/24/jdbc-select-query-example/
9. How to get data from HashMap?
Use get(key) with a key whose equals and hashCode contract matches the inserted key. A null result may mean either no mapping or a mapped null in HashMap; containsKey distinguishes them when that distinction matters.
//howtodoinjava.com/java/interviews-questions/how-hashmap-works-in-java/
10. Difference between Vector and ArrayList?
Vector synchronizes individual methods; ArrayList does not. Both are resizable indexed lists. Individual synchronization does not make a sequence of operations or an iteration atomic, so shared usage still needs an appropriate coordination strategy.
//howtodoinjava.com/java/collections/useful-java-collection-interview-questions/
11. Difference between sleep and wait?
sleep affects the current thread and retains held monitors. wait operates on an owned monitor, releases that monitor while waiting and requires reacquisition before returning. Use wait in a condition loop because wakeups do not prove the condition is true.
//howtodoinjava.com/2013/03/08/difference-between-sleep-and-wait/
12. Also asked some sql queries.
Practice joins, aggregation, NULL handling, indexes and transaction isolation using a small schema. For each query, explain why it returns the intended rows and which database-specific assumptions it makes.
13. Write a program to print Fibonacci series?
Maintain the previous two values and update them for each requested term. State whether the sequence starts at 0 or 1, handle a zero-length request and choose a numeric type that can represent the required range. An iterative solution uses constant auxiliary space.
public class FibonacciSampleCode
{
public static void main(String[] args)
{
FibonacciSampleCode fs = new FibonacciSampleCode();
fs.fibonacci();
}
public void fibonacci()
{
int numb1 = 1;
int numb2 = 1;
int temp = 0;
Scanner input=new Scanner(System.in);
System.out.println("How Many Terms? (Up To 45)");
int x=input.nextInt();
x=x-2;
System.out.println(numb1);
System.out.println(numb2);
for (int i = 0; i < x; i++)
{
System.out.println(numb1 + numb2 + " ");
temp = numb1;
numb1 = numb2;
numb2 = temp + numb2;
}
}
}
14. Can we write try catch block like below code?
No. Java requires blocks with braces for try and catch bodies. The snippet below illustrates invalid syntax, unlike an if statement that may accept a single statement without braces.
try
{
try
{
}
}
catch()
{
}
//howtodoinjava.com/2012/12/07/why-try-catch-finally-blocks-require-braces/
15. What is the use of finally block?
finally normally runs when control leaves the associated try/catch, including by return or exception. It is not guaranteed after JVM termination or a process crash. Avoid returning from finally because it can replace an earlier return value or suppress an exception.
16. What is the use of final keyword?
A final variable cannot be reassigned after initialization; the referenced object may still be mutable. A final method cannot be overridden, and a final class cannot be subclassed. These are separate restrictions, not a general guarantee of immutability.
http://en.wikipedia.org/wiki/Final_%28Java%29
17. Can i declare class as static?
A member class can be static, which means it has no implicit enclosing instance. A top-level class cannot be static. A static nested class can still create ordinary instances and have instance fields.
http://stackoverflow.com/questions/2376938/why-cant-a-java-class-be-declared-as-static
18. What is static method and static variable?
A static field belongs to its declaring class, and a static method is invoked without an instance receiver. It cannot directly access instance fields without an object reference. Static mutable state still needs synchronization when shared between threads.
http://stackoverflow.com/questions/7815664/static-method-and-static-variable-java
19. Can I declare a class as private?
A nested member class can be private. A top-level class cannot: top-level access is public or package access. Choose visibility based on where the type is meant to be used.
http://docs.oracle.com/javase/tutorial/java/javaOO/classdecl.html
20. What happens if i write return in try block? Will finally executes? What happens if write system.exit(), will finally block executes?
A return from try normally runs finally before the method returns. A return inside finally can replace the earlier result. System.exit initiates JVM shutdown, so ordinary stack-unwinding finally blocks are not guaranteed to execute.
http://stackoverflow.com/questions/65035/does-finally-always-execute-in-java
21. Why you want to change the company?
https://www.linkedin.com/uas/login?session_redirect=https%3A%2F%2Fwww.linkedin.com%2Fgroups%2FHow-do-you-answer-Why-1792709.S.54631120
Give an honest, professional reason for considering the role, such as the responsibilities, technical work or growth you are seeking. Do not invent experience or criticize a previous employer.
Use this account as a practice set. Explain the behavior, discuss an edge case and connect it to work you actually performed; memorizing the original interviewer’s wording is less useful than being able to handle a follow-up.
Happy Learning!!
Thanks a lot sir, i love reading your blog and You have explained every topic in a detail way.
Sir, can you please post spring interview questions?
I am asked to write a generics code to add two number.using generics,when i tried,it can’t be perform any arithmetic operation on two generics type passed arguments in a method.
For a possible solution if i will check instance of on passed arguments,then perform arithmetic operation,then what will be the generic output type in that case.
Any guidance.
something like below:
class AddOp<T extends Number>{ public <T> T add(T e1,T e2){ if(e1 instanceof Integer){ return e1.intValue()+e2.intValue(); } } }Very good side i have ever seen
Thanks sunil
Your site has very good information and very well presented. But when we are trying to read the page, every few seconds the page just scrolls to an advertisement and we lose track of the part we are reading. It is EXTREMELY annoying and to be honest outright insulting. I know you would like some ad revenue, but why can’t you display the ads on the side?
Hi Venu, Thanks for the feedback. I am using adsense and developermedia for ads. Both are reputed and trustworthy. It is perhaps any rouge ad from any advertiser. Please send me screenshot whenever you face such behavior, i will block that ad. Thanks again for your patience.
superb boss keep happy learning and happy teaching…………………..
Can I have only try block in code?
No. You need either catch block or finally block along with try block, at least.
It’s false. Because there is try-with-resources in java 7.
try(FileInputStream f = new FileInputStream("ttt")){ System.out.println("t"); }it works ok
Thanks for your valuable comment. I will update the post.
Thanks a lot.
As usual one more great article from you.
I love yours blog, I guess i could find here almost everything :)
hi Lokesh..for an 7+ yrs. Exp. Java resource..what would be the topics that needs to be concentrated on while preparing for an interview…
thnx sir for this lovely article keep posting more
Please share testing and Spring Interview questions.
Testing is not my forte. Spring interview questions are in my TODO list already.
Its very usefull … Please share spring questions also..
I will soon ask my readers for their experience in spring interview questions, and consolidate them.
thnk u for given these questions and can u post questions on spring also………..
Excellent. Thanks for putting together and sharing.
thanks