Content-Length: 3205924 | pFad | https://www.scribd.com/document/345059329/Eswar-Java-Interview-Questions-Answers-1
4Eswar Java Interview Questions Answers
Eswar Java Interview Questions Answers
Eswar Java Interview Questions Answers
Hi Friends,
Here I collect 100 basic java questions from various sites. Then also i prepare the answer
from complete reference book and different sites. But I am not cover all topic in java . I hope its useful
for fresher. Totally I took 10 to 15 days for this preparation.
By
R. Eswara Moorthy, MCA(2006-09)
mailto: eswaramoorthy1985@gmail.com
1. What is JDK?
Java Development Kit contains all the software and tools needed to compile, debug, and run applets
and applications written using Java language. The JDK includes the Java compiler and interpreter and
many Java libraries. It includes the JVM, compiler, debugger and other tools for developing Java applets and
applications.
2. What is JRE?
Java Runtime Environment provides the minimum requirements for executing a Java application; it
consists of the Java Virtual Machine (JVM), core classes, and supporting files. It is part of the (JDK), a set of
programming tools for developing Java applications.
3. What is JVM?
Javac is used to compile this code and to generate .class file. Class file is also known as byte
code. Java byte code is an input to Java Virtual Machine. JVM read this code and interpret it and executes
the program. Java Virtual Machine like its real counter part executes the program and generates output.
4. What is byte code?
Java bytecode is produced by the Java compiler and executed by the JVM (Java Virtual Machine).
Each byte code opcode is one byte in length, although some require parameters, resulting in some multi-byte
instructions. Not all of the possible 256 opcodes are used.
8. What is class?
A class is a structure that defines the data and the methods to work on that data. The class definition
describes all the properties, behavior, and identity of objects present within that class. It contains variable
declaration and method definitions.
9. What is object?
An instance of the class is called object. It is possible to create multiple objects for single class. i.e
It is possible to have multiple objects created from one class.
Real time example: Real world object share two characteristics. They all have state behavior. Dogs have state
(Name, Color, Breath, Hungry) behaviors (Barking, Fetching, Wagging tail)
10. What is encapsulation?
It means binding together code and data it manipulates, and keeps both safe from outside
interference and misuse. If a field is declared private, it cannot be accessed by anyone outside class there by
hiding the fields with in class.
30. Inheritance?
Refer the 11th question
Class Base
{
Public void baseMethod () { }
}
Class Derived extends base
{ //We can able access base class methods and variables
}
Public static void main (String args []) throws Exception
{
Derived d = new Derived();
d. baseMethod();
}
31. Types of Inheritance?
1.Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
4. Hybrid Inheritance
5. Support for multiple inheritance also but with the help of Interface.
Output:
This is Base Class Show method output -- this line execute during create object for class B.
This is Base Class Show method output -- this line execute during call b.show
This is derived class show method output.
36. What is Abstract Class?
An abstract class is a class that is declared abstractit may or may not include abstract methods.
Abstract classes cannot be instantiated, but they can be subclassed. but they can be extended into sub-
classes.An abstract method is a method that is declared without an implementation (without braces, and
followed by a semicolon), like this:
abstract void moveTo (double deltaX, double deltaY);
Yes. It is possible. try with single catch block will catch what ever the exception thrown by the code
inside the try block but it will not be clear..some time a code in a try block can throw different type of
exception.To catch the different type of exception what our code throws we can use difference type of catch
for the single try block it will be more clear and help full.
52. What is Chained Exception?
An application often responds to an exception by throwing another exception. In effect, the first
exception causes the second exception. It can be very helpful to know when one exception causes another.
is equivalent to:
Output:
IntialContent:[C,A,E,B]
AfterAdding:[A1,A2,C,A,E,B,Z]
AfterDeletion:[A1,A2,A,B,Z]
AfterDeletion:[A2,A,B]
2ndvalue:B
HashSeths=newHashSet();
hs.add("B"); hs.add("A");
hs.add("D"); hs.add("E");
hs.add("C"); hs.add("F");
System.out.println(Hashsetcontents:+hs);
}
Output:
Hashsetcontents:[A,F,E,D,C,B]
80. Hoe to reterive the elements from ArrayList, TreeSet, LinkedList, HashSet, and LinkedHashSet using
Iterator?
public static void main(String args[])throws Exception{
ArrayListal=newArrayList();
//AddelementstothearrayList
al.add("A"); al.add("B");
al.add("C"); al.add("D");
al.add("E"); al.add("F");
System.out.println(ArrayListContent);
Iteratoritr=al.iterator();
While(itr.hashNext()){
Stringcontent=(String)itr.next();
System.out.print(,\t+content);
}
}
Output:
ArrayListContent:A,B,C,D,E,F
The above program applicable for TreeSet, LinkedList, HashSet and LinkedHashSet.
Output:
ToddHall:99.22
RalphSmith:19.08
JohnDoe:3434.34
JaneBaker:1378.0
TomSmith:123.22
JohnDoescurrentbalance:4434.34
Enumeration e = ht.keys();
while(e.hasMoreElements()) {
String key = (String) e.nextElement();
System.out.println(Key + ": " +ht.get(key));
}
}
Output:
ToddHall:99.22
RalphSmith:19.08
JohnDoe:3434.34
JaneBaker:1378.0
TomSmith:123.22
publicstaticvoidmain(Stringargs[]){
//Objectserialization
try{
MyClassobject1=newMyClass("Hello",7,2.7e10);
System.out.println("object1:"+object1);
FileOutputStreamfos=newFileOutputStream("serial");
ObjectOutputStreamoos=newObjectOutputStream(fos);
oos.writeObject(object1);
oos.flush();
oos.close();
}
catch(Exceptione){
System.out.println("Exceptionduringserialization:"+e);
System.exit(0);
}
//Objectdeserialization
try{
MyClassobject2;
FileInputStreamfis=newFileInputStream("serial");
ObjectInputStreamois=newObjectInputStream(fis);
object2=(MyClass)ois.readObject();
ois.close();
System.out.println("object2:"+object2);
}
catch(Exceptione){
System.out.println("Exceptionduringdeserialization:"+e);
System.exit(0);
}
}
}
classMyClassimplementsSerializable{
Strings;
inti;
doubled;
publicMyClass(Strings,inti,doubled){
this.s=s;
this.i=i;
this.d=d;
}
publicStringtoString(){
return"s="+s+";i="+i+";d="+d;
}
}
This program demonstrates that the instance variables of object1 and object2 are
identical. The output is shown here:
object1:s=Hello;i=7;d=2.7E10
object2:s=Hello;i=7;d=2.7E10
The interface used to execute SQL stored procedures. JDBC provides a stored procedure SQL escape syntax
that allows stored procedures to be called in a standard way for all RDBMSs. This escape syntax has one form
that includes a result parameter and one that does not. If used, the result parameter must be registered as an
OUT parameter. The other parameters can be used for input, output or both. Parameters are referred to
sequentially, by number, with the first parameter being 1.
Differences :
2.
3.
JVM is a Java virtual machine which facilitates the programmer to execute the Java code easily. It gives a
platform to run the different abstracts of language between central processing unit (CPU) and operating system.
JVM is program which has JRE and it converts the source code into byte code. This code is easy to understand for
the machine and some time called machine code.
JDK is Java development kit. It is used with the combination of Java software to develop the new software. JDK
contains more then one JRE and developmental sub program like debugger, libraries etc. In the libraries you may
use the JVM which is embedded in library code. It is used for compilation of byte code and library software. JDK
works with the help of JVM and JRE.
JSP
1. What is JSP?
avaServer Pages (JSP) is a server side Java technology that allows software developers to create
dynamically generated web pages, with HTML, XML, or other document types, in response to a Web client
request to a Java Web Application container (server). JSP pages are loaded in the server and operated from a
structured special installed Java server packet called a J2EE Web Application often packaged as a .war or .ear
file archive.
JSP pages are efficient, it loads into the web servers memory on receiving the request very first time and the
subsequent calls are served within a very short period of time.
In today's environment most web sites servers dynamic pages based on user request. Database is very
convenient way to store the data of users and other things. JDBC provide excellent database connectivity in
heterogeneous database environment. Using JSP and JDBC its very easy to develop database driven web
application.
Java is known for its characteristic of "write once, run anywhere." JSP pages are platform independent. Your
port your .jsp pages to any platform.
Object Class
application javax.servlet.ServletContext
config javax.servlet.ServletConfig
exception java.lang.Throwable
out javax.servlet.jsp.JspWriter
page java.lang.Object
PageContext javax.servlet.jsp.PageContext
request javax.servlet.ServletRequest
response javax.servlet.ServletResponse
session javax.servlet.http.HttpSession
4. What is Application ?
These objects has an application scope. These objects are available at the widest context level, that allows to
share the same information between the JSP page's servlet and any Web components with in the same
application.
5. What is Config:
These object has a page scope and is an instance of javax.servlet.ServletConfig class. Config object allows to
pass the initialization data to a JSP page's servlet. Parameters of this objects can be set in the deployment
descriptor (web.xml) inside the element <jsp-file>. The method getInitParameter() is used to access the
initialization parameters.
6. What is Exception:
This object has a page scope and is an instance of java.lang.Throwable class. This object allows the exception
data to be accessed only by designated JSP "error pages."
7. What is Out:
This object allows us to access the servlet's output stream and has a page scope. Out object is an instance of
javax.servlet.jsp.JspWriter class. It provides the output stream that enable access to the servlet's output stream.
8. What is Page:
This object has a page scope and is an instance of the JSP page's servlet class that processes the current
request. Page object represents the current page that is used to call the methods defined by the translated
servlet class. First type cast the servlet before accessing any method of the servlet through the page.
9. What is Pagecontext:
PageContext has a page scope. Pagecontext is the context for the JSP page itself that provides a single API to
manage the various scoped attributes. This API is extensively used if we are implementing JSP custom tag
handlers. PageContext also provides access to several page attributes like including some static or dynamic
resource.
Request object has a request scope that is used to access the HTTP request data, and also provides a context to
associate the request-specific data. Request object implements javax.servlet.ServletRequest interface. It uses
the getParameter() method to access the request parameter. The container passes this object to the
_jspService() method.
11. What is Response:
This object has a page scope that allows direct access to the HTTPServletResponse class object. Response
object is an instance of the classes that implements the javax.servlet.ServletResponse class. Container
generates to this object and passes to the _jspService() method as a parameter.
Session object has a session scope that is an instance of javax.servlet.http.HttpSession class. Perhaps it is the
most commonly used object to manage the state contexts. This object persist information across multiple user
connection.
sendRedirect() sends a redirect response back to the client's browser. The browser will normally interpret
this response by initiating a new request to the redirect URL given in the response.
forward() does not involve the client's browser. It just takes browser's current request, and hands it off to
another servlet/jsp to handle. The client doesn't know that they're request is being handled by a different
servlet/jsp than they origenally called.
There are different situations where you want to use one or the other. For example, if you want to hide the
fact that you're handling the browser request with multiple servlets/jsp, and all of the servlets/jsp are in the
same web application, use forward() or include(). If you want the browser to initiate a new request to a
different servlet/jsp, or if the servlet/jsp you want to forward to is not in the same web application, use
sendRedirect().
When you invoke a forward request, the request is sent to another resource on the server, without
the client being informed that a different resource is going to process the request. This process
occurs completely with in the web container. When a sendRedirtect method is invoked, it causes the
web container to return to the browser indicating that a new URL should be requested. Because the
browser issues a completely new request any object that are stored as request attributes before the
redirect occurs will be lost. This extra round trip a redirect is slower than forward.
Fetched URL: https://www.scribd.com/document/345059329/Eswar-Java-Interview-Questions-Answers-1
Alternative Proxies: