Common and advanced Java interview questions

(Last Updated On: June 20, 2010)

There are all from Career Cup

I: Interviewer M: Myself
I: What is singleton pattern?
M: Explained about singleton pattern and constructor to be private and there will be a method (Static) which will return the object.
I: How can i achieve Thread safe in this implementation?
M: In Java we need to use “synchronized” keyword in order to make that method thread safe.
I: If there are Million threads accessing this method, as you used synchronized all the other threads has to wait, which makes the application to run slower. So how can you achieve thread safe with out using synchronization?

n my 1st Phone interview:
What is Linked List?
Interface and abstract class
Write java code for prime no:
Threads
Garbage collector
Vector and Arraylist
HashMap and HashTable
Externalizable interface
What is difference between String and StringBuffer classes.
Write code for the following mode of string comparision. The method should return true if the second string contains all the characters of the first string (order is not important) and you should consider duplicates.
For example, first string is aabc, true for kbaac, abac.. false for abc.
Dont worry about the complexity. I guess the person just wants to see if I can write code. Also I think I got the correct idea in less than 30 seconds but putting it on the board took around 10 minutes.
What does a static method mean in Java
What does Synchronized keyword before a method do in Java?
for a try/catch/finally block when is the finally block not executed.
Why is access to static variables not allowed from non-static methods in java?
Why would you use an interface over an abstract class?
What is Garbage Collection in Java. How is it implemented? What kind of algorithms does the garbage collector use? How does it know that references can be collected? What are advantages and disadvantages of garbage collection?
What is the difference between Inheritance and Interfaces? Under what conditions would you prefer one over the other?
Name a few differences between C# and Java.
What log file can you refer to for determining if a JUnit test failed?
What is final, finalize and finally? Where do you use ‘final’ keyword. What is the effect of making a method final?
Difference between final & finally
Different ways to pass parameters to a function (by value, by reference, by pointer). for the following cases.
6.1 Basic data type (int, char etc)
6.2 Array of integers
6.3 an object of Structure
6.4 an object of a class
Discussed about possiblities of passing constant argument values (by reference, by pointer etc) & their syntax
What is the effect by keeping a constructor private? (in terms of inheritance)
Say you are using a map in your program, how would you count the number of times the program use put() and the get() function? How would you do that if its using multiple maps in the program? How would you do that if the map is sent as a parameter in a method? How would you do it if the multiple maps are passed into the methods (as parameters), and are use differently? (it went on forever….)
What does the “Static” keyword do? Do you know any other similar keywords? What are they?
What do you know about Java generics
what is object reflection in Java
How does Java achieve synchronization? Given an class with Synchronized method A and B, a normal method C, there are 2 threads, one instance, can these two threads call A at the same time? call A and B at the same time? A & C at the same time?
Difference between int and Integer in Java.
What are the interfaces in Collection classes that you worked with?
(Collection, Set, Map, List, Queue)
Had my first and second phone interview with Amazon. I was dropped. This site has been a great help towards my preparation and most questions are based on what you find here.
Posting my Questions is a small way of saying Thanks!
Interview 1:
1. What is polymorphism.
2. Design an OO parking lot. What classes and functions will it have. It should say, full, empty and also be able to find spot for Valet parking. The lot has 3 different types of parking: regular, handicapped and compact.
3. Coding: I have an integer array where every number appears even number of times and only one appears odd times. Find the number.
(I said hashtable and he asked me to write code with Hashtable)
4. What data structure would you use to look up phone numbers for customer names.
(I said Hashtable. Asked why hashtable, why not a tree. I said HT has O(1). Asked is order always 1, when more than O(1) in HT.
Second Interview:
1. Starter: Describe your college projects.
2. OO Design: Design a deck of cards. What classes, data structures will you use? How will you shuffle the cards? How will you divide (deck) among players. What class/function do you need to denote players and where will you add them? What class/function do you need to deck? What if I need to add 2 jokers to the deck of 52 cards.
3. Data Structures: How will you use a hashtable to find data in a tree. (Then he rephrased) suppose I have a hashtable, I want to store the data in a tree instead of a bucket. How will I do it. What complexity to find an element.
4. Bits & Bytes: Find if a binary representation of a number is palindrome. The function should work irrespective of number of bytes for an integer. Suppose if our machine is 4 bytes for an int, how will you use the program for 8 byte machine.
5. Unix: Suppose I have 100’s of html files in many directories. I want to find the files having phone numbers.
b) Suppose I have 2 files having phone numbers, find the repeating phone numbers. (I said sort and grep). Then he asked what if the lines cannot be sorted.
All the best guys. I think the second interview was challenging since the interviewer was prodding until he heard a leave me alone. So it means that though they are based on questions in cc, be prepared for extensions. I think this site is all you need to prepare for Amazon interview.
What is the flaw in the design of Java.lang.Stack class of Java
what is the difference between template in C++ and generics in java
What is the use of volatile variable.
Explain it in Singleton class also for lazy initialization.
How to implement your own HashMap in java
give the algorithm for checking anagram
What is the difference between sleep and yield method
What is the limitation of singleton pattern
Tell the situation where we can use nested classes.
What was the problem in implementing double checked locking before java 1.5.
Why Java language designer has chosen to make all the variable inside an Interface as public ,static and final.
What are the various ways in singleton pattern can be made to fail and how to avoid them ?
What are the various ways in singleton pattern can be made to fail and how to avoid them ?
Can Java constructor has private or protected access type?
If you want to implement a class includes firstname and lastname, then implement equals() and hashcode()
Talk about collection framework of Java.
What will happen if we add the same object to the Set?
About Junit. If I have 2 test cases A and B where A depends on B then how wil I run these test cases using Junit
Write a function to e reverse the order of words in a sentence using constant extra space
eg: “Here I am” would be “am I Here”.
I tried to solve this problem by taking the space as delimiter and inserting each word rfrom the string into a stringbuffer.
sb.insert(0,word)
Finally the stringbuffer would have the reversed string.
But this does not satisfy the constant extra space condition as the size of stringbuffer is still dependant on string size.
Write a program to replace string ‘us’ with ‘them’ from the following String . Do not replace ‘Us’ as well as any string containing us
$_=”Us? It usually rains when bus comes to us”;Please write your inputs on this
What is the importance of the keyword static in java
What is reflection
implement a counting semaphore in Java.
What are the advantages of using spring?
When to use composition and when to use inheritance. Explain with example. Do interfaces have an advantage over inheritance?
What are the advantages of Java over c++ and vice versa
What is multiple inheritance?Why does Java not have it?How can we implement it in Java
What is JIT?
Why does java allow only one public class in one .java file?
What is the difference betn hashmap and hashset?
Can we call non static function from within static function?
String a=”abhay”
String b=”deol”
System.out.println(a+b);
======================
String a=new String(“abhay”);
String b=new String (“deol”);
System.out.println(a+b);
========================
Which is more efficient? why?
Difference betn abstract class and interface
Question about java Generics. What it is? Functions and Generics questions. Best if you study it well.
Explain how memory leaks occur in java. How would you check for them?
What is the default visibility of an instance variable in java? What is package visibility?
You have been asked to deploy a number of robotic domestic helpers (RDH) into a large room to remotely clean it as they move around it. This room is currently rectangular in shape and must be navigated by the RDH’s in order to successfully clean the room.
A RDH position and location is represented by a combination of x and y co-ordinates and a letter representing one of the four cardinal compass points. The room is divided up into a grid to simplify navigation. An example position might be 0, 0, N, which means the RDH is in the bottom left corner and facing North.
In order to control a RDH the operator sends a simple string of letters. The possible letters are ‘L’, ‘R’ and ‘M’. ‘L’ and ‘R’ makes the robot spin 90 degrees left or right respectively, without moving from its current spot. ‘M’ means move forward one grid point, and maintain the same heading.
Assume that the square directly North from (x, y) is (x, y+1).
INPUT:
The first line of input is the upper-right coordinates of the arena, the lower-left coordinates are assumed to be 0,0.
The rest of the input is information pertaining to the RDH’s that have been deployed. Each RDH has two lines of input. The first line gives the RDH’s initial deployed position, and the second line is a series of instructions telling the RDH how to navigate around the room.
The position is made up of two integers and a letter separated by spaces, corresponding to the x and y co-ordinates and the RDH’s orientation.
Each RDH will be finished sequentially, which means that the second RDH won’t start to move until the first one has finished moving.
OUTPUT
The output for each RDH should be its final co-ordinates and heading.
INPUT AND OUTPUT
Test Input:
7 6
2 4 E
MMRMMRMRRM
3 4 N
LMLMLMLMM
Expected Output:
4 2 E
43e3 5 N
Given a sorted array of n integers that has been rotated left or right by k places, give an algorithm that searches and finds an element in the array in log n time.
Example Input: 8 9 0 1 2 4 5 6 (sorted array rotated right by 2 places)
Questions over spring and hibernate. Explain Spring framework and hibernate concepts. discuss using spring and hibernate together.
Explain what is in a deployment descriptor file?
give an example of a deployment descriptor
Get all information about all threads running. And also where exactly a particular thread is blocked during execution.
Share knowledge about JVM parameters/variables.
How will you go around building a pool of threads in Java? Best way to maintain the thread count once a thread in the pool finishes execution?
How do you get the request URL in a servlet?
What will happen if you do not use hashcode() to override a equals()?
What is the difference between >> and >>>?
Why would you use the JSPs and JavaBeans in a web application
Explain what is a weak reference. How do you declare a weak reference? Why would you implement a weak reference – versus a strong reference?
What is the purpose of the session() in a servlet?
Can you have mutliple instances of a database
running in hibernate?
what is the great advantage of using hibernate(or any ORM) vs. direct peristence

What does the web server contain? what does the application server contain? Basically, explain how the tomcat server works different then like a JBOSS server
Implement a Singleton
Can you describe the differences between java 5 and java 6. Also, can you describe some of the newer enchancements in java from java 1.4 to java 5.
Can you identify 10 different Java APIs and explain what each is used for
Interview for a mid-level java developer on one of their product R&D teams:
Can you explain the use of web application frameworks in java development. Discuss, how they work, why they are used, what they have done to revolutionize java development in the last few years. Can you explain the difference between Spring and Struts. Can you explain the similarity between the two.
How do you write a de-constructor in Java? Explain how is it used in programming and how Java handles this issue.
How does the Collections API handle collisions? How does the API handling making the collection “some-what” collision safe?
Explain- which collection does this affect and the best way to deal with it.
Also explain, the most common senario that causes this issue- hint* one is in threading(how can this be caused).
What is Spring IOC?
Explain hashcode() and equals() methods? When would you override these methods? What does the hashcode() method in the Object class do?
How do you write a ping routine in Java
What is final, finalize and finally? Where do you use ‘final’ keyword. What is the effect of making a method final?
Different ways to pass parameters to a function (by value, by reference, by pointer). for the following cases.
6.1 Basic data type (int, char etc)
6.2 Array of integers
6.3 an object of Structure
6.4 an object of a class
Discussed about possiblities of passing constant argument values (by reference, by pointer etc) & their syntax
usask on August 25, 2008
Say you are using a map in your program, how would you count the number of times the program use put() and the get() function? How would you do that if its using multiple maps in the program? How would you do that if the map is sent as a parameter in a method? How would you do it if the multiple maps are passed into the methods (as parameters), and are use differently? (it went on forever….)
how a marker interface gets its functionality and when we implements a marker interface how it got invoked
How many objects are created when we create String class object using new operator?
What are the two major components of JDBC?
what is webservices
What is final, finalize and finally? Where do you use ‘final’ keyword. What is the effect of making a method final?
Explain what is defined in the .jar, .ear, and .web files for a J2EE application?
http://www.allinterview.com/companyqa/Mind-Tree/interview-questions/Core-Java/page1.html
Question how to handle a singleton service locator. when multiple threads are trying to get the singleton object in same time Rank Answer Posted By
Interview Question Submitted By :: Yadav.kar
I also faced this Question!!
© ALL Interview .com
Answer Singleton service means only one service provide at a
time.When multiple threads are trying to get the singleton
object in the same time.we can get an error. because only
one object provide in complete application.
0 Sitaram

Answer Two ways to create threads
1)by creating thread class
class classname extends Thread
{

}
2)By converting class to thread ie using Runnable interface
0 Gowramma M

Question how a marker interface gets its functionality and when we implements a marker interface how it got invoked Rank Answer Posted By
Interview Question Submitted By :: Yadav.kar
I also faced this Question!!
© ALL Interview .com
Answer The marker interfaces would have got the functionalities
from the super class or interface . Marker interfaces are
called as tagged interfaces which wraps a specific
functionality with its name.It doesn’t contains any method.
0 Bhargavi

Answer Marker interface are a design pattern which is used to mark
the classes for doing some specific actions. Marker
interface are just to mark the classes. For example
Serializable is a marker interface. So jvm looks for the
classes which are marked as serializable to serialize the
objects. It tells the compiler that the marked items
should be treated differently. Since it is a older design
pattern. Now a days Annotations has been introduced, so we
can replace the usage of marker interface with annotations.
0 Somasundar M

Answer Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures:

private void writeObject(java.io.ObjectOutputStream out)
throws IOException
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException;
0 Yadav

Question How many objects are created when we create String class object using new operator? Rank Answer Posted By
Interview Question Submitted By :: Guest
I also faced this Question!!
© ALL Interview .com
Answer Two object will create.
0 Guest

Answer yes, two Objects are created one Object is string constent
pool and second one is in heap memory
0 Koneti

Answer using new operator, we can create same object ‘n’ no of
times of the same string class coz jvm would never verify
for the existing object wenever we create the object using
new operator
0 Shalini

Answer yes two objects will be created when you use new operator
for a String. The reason for this is at first it creates an
object in HEAP and then verifies in the String Constant
Pooling if it is not available then creates the new one in
pool. so totally two object will be created.
0 Mahesh

Question What are the two major components of JDBC? Rank Answer Posted By
Interview Question Submitted By :: Gopalraop
I also faced this Question!!
© ALL Interview .com
Answer 1.Driver Manager
2.Connection
0 Priyanka

Answer The two major components of JDBC are
1.Driver
2.DriverManager
0 Gajendra
[Techmatics]

Answer 1.connection pool
2.Data source
0 Geetanjali
[Techmatics]

Answer JDBC Driver and JDBC API

JDBC DRIVER is different for each db , API is same
for all the DB . API is the one Application
programmers can use and which will communicate to the
underlaying Db through Driver
0 Sukesh
[Techmatics]

Question what is webservices Rank Answer Posted By
Interview Question Submitted By :: Gopalraop
I also faced this Question!!
© ALL Interview .com
Answer Softaware system designed to support interoperable machine
to machine transacation over network.
0 Natarajan

Answer Service available over the network.
0 Sangith
[Techmatics]

Back to Questions Page

HOW DO YOU START A PROFITABLE TRADING BUSINESS? Read more NOW >>>

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!
This entry was posted in Quant Development and tagged , , , , on by .

About caustic

Hi i there My name is Bryan Downing. I am part of a company called QuantLabs.Net This is specifically a company with a high profile blog about technology, trading, financial, investment, quant, etc. It posts things on how to do job interviews with large companies like Morgan Stanley, Bloomberg, Citibank, and IBM. It also posts different unique tips and tricks on Java, C++, or C programming. It posts about different techniques in learning about Matlab and building models or strategies. There is a lot here if you are into venturing into the financial world like quant or technical analysis. It also discusses the future generation of trading and programming Specialties: C++, Java, C#, Matlab, quant, models, strategies, technical analysis, linux, windows P.S. I have been known to be the worst typist. Do not be offended by it as I like to bang stuff out and put priorty of what I do over typing. Maybe one day I can get a full time copy editor to help out. Do note I prefer videos as they are much easier to produce so check out my many video at youtube.com/quantlabs