Java Questions
Java Questions
- Applet is a dynamic and interactive program that runs inside a web page displa
yed by a java capable browser.
What is the difference between applications and applets?
- a)Application must be run on local machine whereas applet needs no explicit in
stallation on local machine. b)Application must be run explicitly within a java-
compatible virtual machine whereas applet loads and runs itself automatically in
a java-enabled browser. d)Application starts execution with its main method whe
reas applet starts execution with its init method. e)Application can run with or
without graphical user interface whereas applet must run within a graphical use
r interface.
How does applet recognize the height and width?
- Using getParameters() method.
When do you use codebase in applet?
- When the applet class file is not in the same directory, codebase is used.
What is the lifecycle of an applet?
- init() method - Can be called when an applet is first loaded start() method -
Can be called each time an applet is started. paint() method - Can be called whe
n the applet is minimized or maximized. stop() method - Can be used when the bro
wser moves off the applet s page. destroy() method - Can be called when the browse
r is finished with the applet.
How do you set security in applets?
- using setSecurityManager() method
What is JDBC?
- JDBC is a set of Java API for executing SQL statements. This API consists of a
set of classes and interfaces to enable programs to write pure Java Database ap
plications.
What are drivers available?
- a) JDBC-ODBC Bridge driver b) Native API Partly-Java driver c) JDBC-Net Pure J
ava driver d) Native-Protocol Pure Java driver
What is the difference between JDBC and ODBC?
- a) OBDC is for Microsoft and JDBC is for Java applications. b) ODBC can t be dir
ectly used with Java because it uses a C interface. c) ODBC makes use of pointer
s which have been removed totally from Java. d) ODBC mixes simple and advanced f
eatures together and has complex options for simple queries. But JDBC is designe
d to keep things simple while allowing advanced capabilities when required. e) O
DBC requires manual installation of the ODBC driver manager and driver on all cl
ient machines. JDBC drivers are written in Java and JDBC code is automatically i
nstallable, secure, and portable on all platforms. f) JDBC API is a natural Java
interface and is built on ODBC. JDBC retains some of the basic features of ODBC
.
What are the types of JDBC Driver Models and explain them?
- There are two types of JDBC Driver Models and they are: a) Two tier model and
b) Three tier model Two tier model: In this model, Java applications interact di
rectly with the database. A JDBC driver is required to communicate with the part
icular database management system that is being accessed. SQL statements are sen
t to the database and the results are given to user. This model is referred to a
s client/server configuration where user is the client and the machine that has
the database is called as the server. Three tier model: A middle tier is introdu
ced in this model. The functions of this model are: a) Collection of SQL stateme
nts from the client and handing it over to the database, b) Receiving results fr
om database to the client and c) Maintaining control over accessing and updating
of the above.
What are the steps involved for making a connection with a database or how do yo
u connect to a database?
a) Loading the driver : To load the driver, Class. forName() method is used. Cla
ss. forName( sun. jdbc. odbc. JdbcOdbcDriver ); When the driver is loaded, it regist
ers itself with the java. sql. DriverManager class as an available database driv
er. b) Making a connection with database: To open a connection to a given databa
se, DriverManager. getConnection() method is used. Connection con = DriverManage
r. getConnection ( jdbc:odbc:somedb , user , password ); c) Executing SQL statements : To
execute a SQL query, java. sql. statements class is used. createStatement() meth
od of Connection to obtain a new Statement object. Statement stmt = con. createS
tatement(); A query that returns data can be executed using the executeQuery() m
ethod of Statement. This method executes the statement and returns a java. sql.
ResultSet that encapsulates the retrieved data: ResultSet rs = stmt. executeQuer
y( SELECT * FROM some table ); d) Process the results : ResultSet returns one row at
a time. Next() method of ResultSet object can be called to move to the next row
. The getString() and getObject() methods are used for retrieving column values:
while(rs. next()) { String event = rs. getString( event ); Object count = (Integer)
rs. getObject( count );
What type of driver did you use in project?
- JDBC-ODBC Bridge driver (is a driver that uses native(C language) libraries an
d makes calls to an existing ODBC driver to access a database engine).
What are the types of statements in JDBC?
- Statement: to be used createStatement() method for executing single SQL statem
ent PreparedStatement To be used preparedStatement() method for executing same S
QL statement over and over. CallableStatement To be used prepareCall() method fo
r multiple SQL statements over and over.
What is stored procedure?- Stored procedure is a group of SQL statements that fo
rms a logical unit and performs a particular task. Stored Procedures are used to
encapsulate a set of operations or queries to execute on database. Stored proce
dures can be compiled and executed with different parameters and results and may
have any combination of input/output parameters.
How to create and call stored procedures?- To create stored procedures: Create p
rocedure procedurename (specify in, out and in out parameters) BEGIN Any multipl
e SQL statement; END; To call stored procedures: CallableStatement csmt = con. p
repareCall( {call procedure name(?,?)} ); csmt. registerOutParameter(column no. , da
ta type); csmt. setInt(column no. , column name) csmt. execute();
What is servlet?
- Servlets are modules that extend request/response-oriented servers, such as ja
va-enabled web servers. For example, a servlet might be responsible for taking d
ata in an HTML order-entry form and applying the business logic used to update a
company s order database.
What are the classes and interfaces for servlets?
- There are two packages in servlets and they are javax. servlet and
What is the difference between an applet and a servlet?
- a) Servlets are to servers what applets are to browsers. b) Applets must have
graphical user interfaces whereas servlets have no graphical user interfaces.
What is the difference between doPost and doGet methods?
- a) doGet() method is used to get information, while doPost() method is used fo
r posting information.
b) doGet() requests can t send large amount of information and is limited to 240
-255 characters. However, doPost()requests passes all of its data, of unlim
ited length.
c) A doGet() request is appended to the request URL in a query string and this
allows the exchange is visible to the client, whereas a doPost() request passes
directly over the socket connection as part of its HTTP request body and the ex
change are invisible to the client.
What is the life cycle of a servlet?
- Each Servlet has the same life cycle: a) A server loads and initializes the se
rvlet by init () method. b) The servlet handles zero or more client s requests thr
ough service() method. c) The server removes the servlet through destroy() metho
d.
Who is loading the init() method of servlet?
- Web server
What are the different servers available for developing and deploying Servlets?
- a) Java Web Server b) JRun g) Apache Server h) Netscape Information Server i)
Web Logic
How many ways can we track client and what are they?
- The servlet API provides two ways to track client state and
they are: a) Using Session tracking ; b) Using Cookies ; (c) URL rewriting ; (d)
Hidden Files
What is session tracking and how do you track a user session in servlets?
- Session tracking is a mechanism that servlets use to maintain state about a se
ries requests from the same user across some period of time. The methods used fo
r session tracking are: a) User Authentication - occurs when a web server restri
cts access to some of its resources to only those clients that log in using a re
cognized username and password. b) Hidden form fields - fields are added to an H
TML form that are not displayed in the client s browser. When the form containing
the fields is submitted, the fields are sent back to the server. c) URL rewritin
g - every URL that the user clicks on is dynamically modified or rewritten to in
clude extra information. The extra information can be in the form of extra path
information, added parameters or some custom, server-specific URL change. d) Coo
kies - a bit of information that is sent by a web server to a browser and which
can later be read back from that browser. e) HttpSession- places a limit on the
number of sessions that can exist in memory. This limit is set in the session. m
axresidents property.