We are hosting this site in another domain. Checkout 

http://www.techpitcher.com

This blog contains questions asked in interviews. Questions are related to Java, Struts and Web Technology.

I have listed all the questions that I have been asked in some interview or I have asked to someone. Please go through the index given to right. 

This Blog can help one to test your knowledge just before taking some interview.

Share your interview question to make it a better collection.



Describe String in Java.
What are the differences between the == operator and the equals() method?
What is String.intern()?
Which one of the following is more efficient?

Describe OOPs concepts.
What is Polymorphism in java?
What is an abstract class?
What is an interface?

What do you know about Containers?
Difference between HashTable and HashMap?
What are the differences between Vector and ArrayList?

What is Garbage Collection ?
What are the advantages and disadvantages of reference counting in garbage collection over tracing algorithm?


Garbage Collector

We are hosting this site in another domain. Checkout

http://www.techpitcher.com


What is Garbage Collection ?
Memory for the new object created in java is allocated in heap at runtime. When an object is no longer referenced by the program, the heap space it occupies must be recycled so that the space is available for subsequent new objects.

Garbage collection is the process of automatically freeing objects that are no longer referenced by the program.

During object cleaning, free memory in the heap may not be continuous, free memory may be between live objects. It’s job of GC to fragment heap memory.

There are many advantages of having garbage collector

1. It increases productivity. One need not worry about memory issues.
2. It ensures program integrity. Garbage collection is an important part of Java's security strategy. Java programmers are unable to accidentally (or purposely) crash the Java virtual machine by incorrectly freeing memory.

A potential disadvantage of a garbage-collected heap is that it adds an overhead that can affect program performance. The Java virtual machine has to keep track of which objects are being referenced by the executing program, and finalize and free unreferenced objects on the fly. This activity will likely require more CPU time than would have been required if the program explicitly freed unnecessary memory. In addition, programmers in a garbage-collected environment have less control over the scheduling of CPU time devoted to freeing objects that are no longer needed.


What are the advantages and disadvantages of reference counting in garbage collection over tracing algorithm?
Reference counting and tracing algorithm are both techniques used for garbage collection. Reference counting is a garbage-collection technique that maintains a per-object count of the number of pointers to that object. When the count reaches zero, the object must be dead and can be collected. When an object is garbage collected, the other object's count referring to this is decremented by 1. In this way, garbage collection of one object can lead to subsequent garbage collection of other objects.

The main advantage of reference counting over tracing is that an object is reclaimed as soon as it is dead and without long pauses or delays. This is well suited for smaller applications with limited memory and in chunks. This characteristic makes it suitable for real time environment where the responsiveness needs to be high. They are typically run in conjunction with other methods to increase over all precision.

There are two main disadvantages for it over tracing. First one being that it can not detect cycles. This method totally relies on reference count and an object which is directly or indirectly(by parent child relationship) refers to itself will never be garbage collected because their count is destined to stay as non-zero. The other disadvantage is the frequent updates that needs to happen to reference count of all the objects every time an object is garbage collected or a new assignment has happened. This is typically inefficient and includes a lot of extra storage traffic too.

You can also find links related to these topics on ads section provided by Google.
Please provide your comment to make this forum better.

Containers

We are hosting this site in another domain. Checkout

http://www.techpitcher.com/java/container.html


1. What do you know about Containers?
There are two types of container library available in java .
i. Collection: Collection category only holds one item in each location. Which includes List and Set. ArrayList is type of list and HashSet is type of Set.
The name is misleading, because entire container libraries are often called “collections”.
a. List (Interface)
Order is most important feature of List
List allows insertion and deletion of element in the middle.We can traverse List in both directions.
1.ArrayList:
ArrayList allows random access of elements.
It is slow when inserting and removing elements from the middle.
2. LinkedList :
Provides optimal sequential access.
Inexpensive insertions and deletions from the middle of the List.
Relatively slow for sequential access.
b. Set (Interface)
Element in set are unique.
Set doesn’t guarantee that it will maintain any order.
1. HashSet:
Provides fast lookup
ii. Map: The Map holds key-value pairs, rather like a mini database. We can lookup on the basis of key.
1. HashMap:
Implementation based on Hash table.
Provides constant time performance for inserting and location pairs.
-----------------------------------------------------------------------------
2. Difference between HashTable and HashMap?
The HashTable is almost equal to HashMap except following points.
  1. HashTable is synchronized, while HashMap is not.
  2. HashTable doesn’t permit null as key while in HashMap it is allowed.
  3. HashTable guarantee that order of the table will remain constant over time while in HashMap it is not.
-----------------------------------------------------------------------------

3. What are the differences between Vector and ArrayList?
The two classes are very similar, however there are few differences, which can give one a clear idea about their usages.Vector is synchronized. Content in vector is thread-safe. ArrayList is not synchronized hence its content is not thread-safe. Synchronization is not at free of cost. It affects vector’s performance. So if we don’t require thread-safe data, we should use ArrayList.
Internally, both the ArrayList and vector hold data in Array. When we add a new element into ArrayList or Vector, and the object will need to expand its internal array if it runs out of memory. Vector doubles the size of Array by default, while the ArrayList increases its size by 50 percent. Vector does have slight advantage since they allow to reset increment size.Addition and deletion operation in both classes takes same time.
Addition at the end of container can be performed in constant time O(1). Deletion and Addition are more expansive when the element is added/removed from middle, it takes O(n-i). Here n is number of elements in the container and i is index of the element.
-----------------------------------------------------------------------------

You can also find links related to these topics on ads section provided by Google.
Please provide your comment to make this forum better.

String

We are hosting this site in another domain. Checkout

http://www.techpitcher.com/java/string.html


1. Describe String in Java.
Strings are immutable. A string can’t be altered once created. Applying a method on string won’t change string’s value; rather it will create a new string. String is also final, and so can’t be sub classed. When you assign one String variable to another, no copy is made. Even when you take a substring there is no new String created.

Creating Strings
String can be created by assigning string literals:
String stringLiteral = "Java";
The text between double quotes is a string literal. By assigning a string literal, you can avoid using the new keyword. In fact, this special shortcut syntax was designed to improve String performance: Each JVM only keeps one copy of each string literal. The following code creates a single string literal that both String references point to:
String firstString = "I am a String Literal";
String secondString = "I am a String Literal"; // Points to the same object as firstString
String thirdString = new String("I am another String Literal"); // By using constructor, will create a new Objet in JVM

Note: You almost always want to initialize String references with literals to avoid creating unnecessary objects in the JVM? This can really add up if you are creating hundreds of identical Strings.
-----------------------------------------------------------------------------

2. What are the differences between the == operator and the equals() method?

Equals method compare values of the receiver and sender object. It returns true if the values are same. While == operator is a fundamental operator, it compares reference of the sender and receiver object.

E.g.
String s1 = "Hello";
String s2 =
"Hello";
String s3 =
new String("Hello");

System.out.println(s1 == s2); // true
System.out.println(s1 == s3); // false
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // true

From the example, we can see that reference and value for s1 & s2 are same. At the same time s3 is a newly created instance which has same value as of s1 and s2 but reference is different.

-----------------------------------------------------------------------------

3. What is String.intern()?

String.intern returns a canonical representation for the string object. A pool of strings, initially empty, is maintained privately by the class String. All literal strings and string-valued constant expressions are stored in this pool.
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.
By using intern one can get better performance as compared to equals method in String class. equals does the character by character that you probably want.
There are some disadvantages of intern. If we start using intern with every string then pool size will increase and could lead to memory problem. Secondly in case of bigger pool, finding a string could be more costly that having character by character comparison.
-----------------------------------------------------------------------------

4. Which one of the following is more efficient?

a. String str1 = "Hello " + "Guest";
b. String str2 = (new StringBuffer().append("Hello ")).append("Guest").toString();

str1 is resolved at compile time, while str2 is resolved at runtime time with an extra StringBuffer and String. The version that can be resolved at compile time is more efficient. It avoids the overhead of creating a String and an extra StringBuffer, as well as avoiding the runtime cost of several method calls.
-----------------------------------------------------------------------------



You can also find links related to these topics on ads section provided by Google.
Please provide your comment to make this forum better.

OOP Concepts

We are hosting this site in another domain. Checkout

http://www.techpitcher.com/java/oops.html


1. Describe OOPs concepts.
The four main concepts are involved in OOP:
  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism

Abstraction is hiding of information from others. In case of Java, its hiding implementation of a method/ object. For example if an interface is available to outer world, so end user won’t know about implementing class details. This is just a kind of abstraction. Abstraction could be anywhere in java, wherever we hide some information, we call it abstraction.

Encapsulation is combining data and method along with implementation hiding within the class. Implementation hiding is done with the help of access modifiers (public, protected, package, private).

Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. Although inheritance may sometimes imply (especially in Java, where the keyword for inheritance is extends) that you are going to add new methods to the interface, that’s not necessarily true. The second and more important way to differentiate your new class is to change the behavior of an existing base-class method. This is referred to as overriding that method.

Polymorphism is something like one name many forms. Polymorphism is also known as dynamic binding or late binding or run-time binding.

----------------------------------------------------------------------------------

2. What is Polymorphism in java?
The meaning of Polymorphism is something like one name many forms. Polymorphism is also known as dynamic binding or late binding or run-time binding. In java polymorphism exists in three distinct forms
Method overloading: Method overloading can be done on the basis of number of argument in a method , or order of arguments or class of the function. Overloading can’t be done on the basis of return type. This is because sometime methods are called only for their side effect and their return value might be ignored.
E.g.


public class Test {
// First method
public void display(){}
// Second method
public void display(String msg){}
// Third method: Not allowed
public String display(){}
}

In the above example first and second display methods are overloaded on the basis of number of arguments while we can’t define third display method with same arguments and different return type as compiler won’t be able to differentiate that which method is called.

Method overriding through inheritance: Overloading methods in extended class which are defined in base class. E.g.


public class ExtendedTest extends Test {
// Overloaded Method
public void display(String msg){}
public static void main(String[] args) {
ExtendedTest eTest =
new ExtendedTest();
eTest.display(
"ExtendedTest");
Test test =
new Test();
test.display(
"Test");
}
}

That means the method "display" in the "ExtendedTest" class is given more preference than " display " in the "Test" class for any " ExtendedTest " object. This is what is called overriding the methods. If there is no “display” method defined in “ExtendedTest” class then object from “ExtendedTest” will use method defined in “Test” class. That means compiler doesn’t have information at compile time that which method to call. This decision is takes at run time. That is why this is called late binding or runtime binding.

Method overriding through the Java interface:
This could be the case when we have one interface implemented by more than one java class. Let’s assume that there is an interface ITest and classes TestA & TestB both implement this interface. Any statement like

ITest test1 = new TestA();
Or
ITest test2 = new TestB();
can’t be resolved at compile time. This could be resolved at runtime.
----------------------------------------------------------------------------------
3. What is an abstract class?
You create an abstract class when you want to manipulate a set of classes through this common interface. All derived-class methods that match the signature of the base-class declaration will be called using the dynamic binding mechanism.
A class containing abstract methods is called an abstract class. Class does not contain implementation for Abstract methods. If a class contains one or more abstract methods, the class itself must be qualified as abstract. (Otherwise, the compiler gives you an error message.)
----------------------------------------------------------------------------------
4. What is an interface?
The interface keyword takes the abstract concept one step further. You could think of it as a “pure” abstract class. It allows the creator to establish the form for a class: method names, argument lists, and return types, but no method bodies. An interface can also contain fields, but these are implicitly static and final. An interface provides only a form, but no implementation. An interface says, “This is what all classes that implement this particular interface will look like.”

You can also find links related to these topics on ads section provided by Google.
Please provide your comment to make this forum better.