Java Interview Questions & Answers – Part3

java interview Q&Ans

1)Difference between Application server and Web server.

Web serverApplication server
Web server serves HTTP contentApplication server serves HTTP, RMI/RPC etc.
Webserver is suitable for static content.Application server supports dynamic content also.
Webserver does not contain inbuilt web server component.Application server contains inbuilt web server component.
IIS, Jetty, Tomcat, Apache etc… Is webserverSharePoint, JBOSS, EJB, WAS etc… Is application server.
Webserver does not add any functionalityApplication server adds functionality
To applications.

2)Difference between http and https.

HTTPHTTPS
URL begins with “http://”URL begins with “https://”
It uses port 80 for communicationIt uses port 443 for communication
UnsecuredSecured
Operates at Application LayerOperates at Transport Layer
No encryptionEncryption is present
No certificates requiredCertificates required

3)Difference between Static and Non-Static variable/Method.

StaticNon-static
Static members are one per classNon-static Variables are 1 per instance.
Static members are accessed by their class name which encapsulates themNon-static members are accessed by object reference.
Static methods can be accessed directly from the classNon-static methods (or instance methods as I like to call them) have to be accessed from an instance.
static members can’t use non-static methods without instantiating an objetNon-static members can use static members directly.
static constructor is used to initialize static fieldsNon-static fields normal instance constructor is used.

4)Difference between String and StringBuffer.

StringStringBuffer
String is used to manipulate character strings that cannot be changed (read-only and immutable).StringBuffer is used to represent characters that can be modified.
String is slow when performing concatenations because concatenating a string created new object.StringBuffer is faster when performing concatenations.
String is immutableStringBuffer is mutable
String is not synchronizedStringBuffer is synchronized

5)Difference between Method Overloading and Overriding.

Method OverloadingMethod Overriding
Arguments Must changeArguments Must not change
Return type Can changeReturn type Can’t change except for covariant returns
Exceptions Can changeExceptions Can reduce or eliminate. Must not throw new or broader checked exceptions
Access Can changeAccess Must not make more restrictive (can be less restrictive)
Invocation Reference type determines which overloaded version is selected. Happens at compile time.Invocation Object type determines which method is selected. Happens at runtime

6)Difference between abstract class and interface.

Abstract ClassInterfaces
An abstract class can provide complete, default code and/or just the details that have to be overridden.An interface cannot provide any code at all, just the signature.
In case of abstract class, a class may extend only one abstract class.A Class may implement several interfaces.
An abstract class can have non-abstract methods.All methods of an Interface are abstract.
An abstract class can have instance variables.An Interface cannot have instance variables.
An abstract class can have any visibility: public, private, protected.An Interface visibility must be public (or) none.
If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.
An abstract class can contain constructors.An Interface cannot contain constructors.
Abstract classes are fast.Interfaces are slow as it requires extra indirection to find corresponding method in the actual class.

 

For More Interview Questions Click Here – Java Interview Questions

Leave a Reply

Your email address will not be published. Required fields are marked *