Books Related to Java Technology

Friday, July 24, 2009

Saigun Technology Pvt. Ltd. Written test paper and interview questions with answers

Core Java


What is the most important feature of Java?
Java is a platform independent language.
What do you mean by platform independence?
Platform independence means that we can write and compile the java code in one platform like (Windows) and we can execute the class in any other supported platform like (Linux, Solaris etc).
Is JVM platform independent?
JVM is not platform independent. It is a platform specific run time implementation provided by the vendor.
What is a JVM?
JVM is Java Virtual Machine which is a run time environment for the compiled java class files.
Difference between JRE/JVM/JDK? To See in Details Click on this Link:-
JDK (Java Development Kit)

JDK= JDK tools + JRE

JRE = JVM + Java Packages Classes(like util, math, lang, awt,swing etc)+runtime libraries.

JDK contains tools required to develop the Java programs, and JRE to run the programs. The tools include compiler (javac.exe), Java application launcher (java.exe), Appletviewer, etc…

JAVA Compiler converts java code into byte code. and java application launcher opens a JRE, loads the class, and invokes its main method.

If you want to write and compile your own programs, you need JDK. If you just want to run your java programs, JRE is sufficient. JRE is targeted for execution of Java files

You can create a Java file (with the help of Java packages), compile a Java file and run a java file. JDK is mainly targeted for java development.


JRE (Java Runtime Environment)
Java Runtime Environment contains JVM, class libraries, and other supporting files. It does not contain any development tools such as compiler, debugger, etc. Actually JVM runs the program, and it uses the class libraries, and other supporting files provided in JRE. If you want to run any java program, you need to have JRE installed in the system.


JVM(Java Virtual Machine)
The Java Virtual Machine provides a platform-independent way of executing code, programmers can concentrate on writing application, without having to be concerned with how or where it will run.
What is Externalizable? - Externalizable is an Interface that covers Serializable Interface. It sends data into Streams in Compressed Format. It has two methods, that is: writeExternal (ObjectOuput out) and readExternal (ObjectInput in).
What is a pointer and does Java support pointers?
Pointer is a reference deal to a memory location. Improper treating of pointers leads to memory escapes and reliability issues hence Java doesn't support the usage of pointers.
Does Java support multiple inheritance?
Java doesn't support multiple inheritance.
Is Java a pure object oriented language?
Java is not a pure object oriented language because it uses primitive data types.
What is difference between Path and Classpath?
Path and Classpath are operating system level environment variables. Path is used define where the system can find the executables (.exe) files and Classpath is used to specify the location .class files.
Can there be an abstract class with no abstract methods in it? - Yes
1. Can an Interface be final? - No
What are local variables?
Local variables are those which are declared within a block of code like methods. Local variables should be initialized before accessing them.
Can an Interface have an inner class? - Yes.
public interface xyz{
static int i=0;
void ss();

class abc{
abc(){
int j;
System.out.println("In side the interface");
};

public static void main(String abc[]){
System.out.println("Interface is in");
}
}
}
Can we define private and protected modifiers for variables in interfaces? - No
What modifiers are allowed for methods in an Interface? - Only public and abstract modifiers are allowed for methods in interfaces.
What is a local, member and a class variable? - Which variable declared within a method that is called “local” variables, which variable declared within the class (not within any methods) are called “member” variables (global variables) and which variables declared within the class (not within any methods and are defined as “static”) are called class variables.
What are the different identifier states of a Thread? - The different identifiers of a Thread are: R - Running or runnable thread, S - Suspended thread, CW - Thread waiting on a condition variable, MW - Thread waiting on a monitor lock, MS - Thread suspended waiting on a monitor lock
What are some alternatives to inheritance? - Delegation is an alternative to inheritance. Delegation means that you include an instance of another class as an instance variable, and forward messages to the instance. It is often better than inheritance because it forces you to think about each message you forward, because the instance is of a known class, rather than a new class, and because it doesn’t force you to accept all the methods of the super class: you can provide only the methods that really make sense. On the other hand, it makes you write more code, and it is more difficult to re-use (because it is not a subclass).
What is instance variables?
Instance variables are those variable which is defined at the class level. Instance variables need not be initialized before using them as they are automatically initialized to their default values.
What is constant variable in Java?
The constant variable should be declared as static and final. So only one copy of the variable exists for all instances of the class and the value can't be changed.
Should a main method be mandatory declared in all java classes?
No it is not required. main method should be defined only if the source class is a java application.
What is the return type of the main method?
Main method doesn't return anything therefore it declared void.
Why is the main method declared static?
main method is called by the JVM yet before the instantiation of the class hence it is declared as static.
What is the argument of main method?
The main method accepts an array type of String object as an argument.
Can main method be overloaded?
Yes. You can have any number of main methods with different method signature and implementation in the class.
Can a main method be declared final?
Yes. Any inheriting class will not be able to have it's own default main method.
Why isn’t there operator overloading? - Because C++ has proven by example that operator overloading makes code almost impossible to maintain. In fact there very nearly wasn’t even method overloading in Java, but it was thought that this was too useful for some very basic methods like print(). Note that some of the classes like DataOutputStream have unoverloaded methods like writeInt() and writeByte().
How do I convert a numeric IP address like 192.168.10.204 into a hostname like javasks.blogspot.com?
String hostname = InetAddress.getByName("192.168.10.204").getHostName();
Why do threads block on I/O? - Threads block on I/O (that is enters in the waiting state) so that other threads may execute while the I/O operation is performed.
What is synchronization and why is it important? - With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object’s value. This often leads to significant errors.
Is null a keyword? - No, The null value is not a keyword.
How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters? - Unicode requires 16 bits and ASCII require 7 bits. while the ASCII character set uses only 7 bits, it is usually represented as 8 bits. UTF-8 represents characters using 8, 16, and 18 bit patterns. UTF-16 uses 16-bit and larger bit patterns.
What are wrapped classes? - Wrapped classes are classes that allow primitive types to be accessed as objects.
What is a native method? - A native method is a method that is implemented in a language other than Java.
What is the catch or declare rule for method declarations? - If a checked exception may be thrown within the body of a method, the method must either catch the exception or declare it in its throws clause.
What is the range of the char type? - The range of the char type is 0 to 2^16 - 1.
------------------------------------------------------------------------------------------------------------------------------
















JSP

What is JSP and why it used in java?
JSP is a framework which is the part of MVC model, it stand for view which show on the front end of the application, it is a normally HTML pages with Java code pieces embedded in them. JSP pages are saved to *.jsp files. A JSP compiler is used in the background to generate a Servlet from the JSP page.
Differentiate between and response.sendRedirect(url),?.
The element forwards the request object holding the client request information from one JSP file to another file.
sendRedirect sends HTTP temporary redirect response to the browser, and browser creates a new request to go the redirected page.
Differentiate between custom JSP tags and beans?
Custom JSP tag is a tag you defined. To use custom JSP tags, you need to define three things:
1. The tag handler class that specifies the tag\'s behavior
2. The tag library descriptor file that maps the XML element names
3. the JSP file that uses the tag library
JavaBeans are Java utility classes you defined.
to declare a bean and use
to set value of the bean class and use
to get value of the bean class.
What are comments in JSP?
1: <%-- JSP Comment --%>
2:
3: <% // Java comments %>
4: <% /* Java comments*/ %>
What is JSP technology?
JSP is a standard Java extension that is defined on top of the servlet Extensions. The goal of JSP is the simplified creation and management of dynamic Web pages.
What is JSP page?
A text-based document that contains two types of text: static template data, which can be showed in any text-based format such as HTML, SVG, WML, and XML, and JSP elements, which construct dynamic content.
Describe implicit objects?
Implicit objects are objects that are created by the web container and contain information related to a particular request, page, or application. They are:
* request
* response
* pageContext
* session
* application
* out
* config
* page
* exception
How many JSP scripting elements and what are they?
There are three scripting language elements:
*declarations
*scriptlets
*expressions
Why are JSP pages the preferred API for creating a web-based client program?
Because no plug-ins or security policy files are needed on the client systems(applet does).
Differentiate between and <%@ include file = ... >?.
: This is like a function call from one jsp to another jsp.
<%@ include file = ... >: . In this case in the included file changes, the changed content will not included in the output. This approach is used when the code from one jsp file required to include in multiple jsp files.
Is JSP is extensible technology?
YES. JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries.
Can we use the constructor, instead of init(), to initialize servlet?
Yes , of course we can use the constructor instead of init(). The original reason for init() was that ancient versions of Java couldn’t dynamically invoke constructors with arguments, so there was no way to give the constructur a ServletConfig.
How can a servlet refresh automatically if some new data has entered the database?
You can apply a client-side Refresh or Server Push.
The code in a finally clause will never fail to execute, right?
Using System.exit(1); in try block will not allow finally code to execute.
How many messaging models do JMS provide for and what are they?
JMS provide for two messaging models,
1:publish-and-subscribe and
2:point-to-point queuing.
What type of information is needed to create a TCP Socket?
The Local Systems IP Address and Port Number. And the Remote System’s IPAddress and Port Number.
What Class.forName will do while loading drivers?
It is used to create an instance of a driver and register it with the DriverManager.
How to Retrieve Warnings?
A warning can be reported on a Connection object, a Statement object (including PreparedStatement and CallableStatement objects), or a ResultSet object. Each of these classes has a getWarnings method, which you must invoke in order to see the first warning reported on the calling object.
How many types of JSP scripting elements are there and what are they?
There are three scripting language elements:
declarations, scriptlets and expressions.
In the Servlet 2.4 specification SingleThreadModel has been deprecated, why?
Because it is not practical to have such model. Whether you set isThreadSafe to true or false, you should take care of concurrent client requests to the JSP page by synchronizing access to any shared objects defined at the page level.
What are stored procedures? How is it useful?
"A stored procedure is a set of statements/commands which reside in the database". The biggest advantage is of course speed. Also certain kind of data manipulations are not achieved in SQL. Stored procs provide a mechanism to do these manipulations. Stored procs are also useful when you want to do Batch updates/exports/houseKeeping kind of stuff on the database.
How do I include static files within a JSP page?
Static resources should always be admitted using the JSP include directive. This way, the inclusion is performed just once during the translation phase. Note that you should always supply a relative URL for the file attribute. Although you can also include static resources using the action, this is not advisable as the inclusion is then performed for each and every request.
Why does JComponent have add() and remove() methods but Component does not?
because JComponent is a subclass of Container, and can contain other components and jcomponents.
How can I implement a thread-safe JSP page?
This is done by adding the directive <%@ page isThreadSafe="false" % > within your JSP page.
If the browser has disabled cookies. How can we enable session tracking for JSP pages ?
We know that session tracking uses cookies by default to associate a session identifier with a unique user. If the browser does not support cookies, or if cookies are disabled, you can still enable session tracking using URL rewriting. URL rewriting essentially includes the session ID within the link itself as a name/value pair.
What is a output comment?
The JSP engine handles an output comment as uninterrupted HTML text, returning the comment in the HTML output sent to the client. You can see the comment by viewing the page source from your Web browser.
What is a Hidden Comment?
A comments that documents the JSP page but is not sent to the client. The JSP engine ignores a hidden comment, and does not process any code within hidden comment tags.
JSP Syntax
<%-- comment --%>
What is a Declaration
A declaration declares one or more variables or methods for use later in the JSP source file. A declaration must contain at least one complete declarative statement. You can declare any number of variables or methods within one declaration tag, as long as they are separated by semicolons. <%! declarations %>
<%! int i = 0; %> <%! int a, b, c; %>
What is a Scriptlet
A scriptlet can contain any number of language statements, variable or method declarations, or expressions that are valid in the page scripting language.Within scriptlet tags, you can
1.Declare variables or methods
2.Write expressions valid in the page scripting language
3.Use any of the JSP implicit objects
What are implicit objects? List them?
Certain objects that are available for the use in JSP documents without being declared first. These objects are parsed by the JSP engine and inserted into the generated servlet.
Difference between forward and sendRedirect?
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 completly 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.
What are the different scope values for the ?
The four different scope values for are
1. page
2. request
3.session
4.application
Explain the life-cycle mehtods in JSP?
The JspPage interface declares only two mehtods - jspInit() and jspDestroy() that must be implemented by all JSP pages regardless of the client-server protocol. However the JSP specification has provided the HttpJspPage interfaec specifically for the JSp pages serving HTTP requests. This interface declares one method _jspService(). The jspInit()- The container calls the jspInit() to initialize the servlet instance.It is called before any other method, and is called only once for a servlet instance. The jspservice()- The container calls the _jspservice() for each request, passing it the request and the response objects. The jspDestroy()- The container calls this when it decides take the instance out of service. It is the last method called n the servlet instance.
---------------------------------------------------------------------------------------------------------------------------------















JDBC
How can you establish connection?
To establish a connection you need to have the appropriate driver connect to the DBMS.
The following line of code we use:
String url = “jdbc:odbc:Fred”;
Connection con = DriverManager.getConnection(url, “Fernanda”, “J8?);
Define different types of Statements?
There are three types of statements
• Regular statement
• prepared statement
• callable statement
What are the components of JDBC?
The two major components of JDBC are Connection Pooling and Data Sources.
Define metadata?
Metadata is data about data.
In JDBC, there are two types
1. Describing information about the Result set object. i
2. describes about the database connection
Differentiate between local and global transaction?
A transaction is atomic unit of Work.
Transactions can be divided into two categories.
1.Local Transactions: These transactions are confined to the objects which reside inside one particular JVM.
2.Global Transactions:These transactions may encapsulate objects which are distributed on various JVM's.
What is the technique to call a Stored Procedure from JDBC?
The first step is to create a CallableStatement object. As with as a Statement an and PreparedStatement objects, this is done with an open Connection object.
E.g.CallableStatement cs = con.prepareCall("{call SHOW_SUPPLIERS}");
ResultSet rs = cs.executeQuery();
Why JDBC objects generate SQLWarnings?
Connections, Statements and ResultSets all have a getWarnings method that allows retrival.The prior ResultSet warnings are cleared on each new read and prior Statement warnings are cleared with each new execution.
What are the components of JDBC?
The two major components of JDBC is One implementation interface for the database manufacturers, the other implementation interface for application and applet writers.
How web server is connected with appliction server?
All the application server requires to the information about the installed web server's port and name, through these entries it creates a connector and stored. This connector is used to communicate with Application server.
Differentiate between Rowset and Resultset?
RowSets are a part of JDBC 2.0 API. Essentially a row set is a JavaBean that contains database data. The implementation is store the data offline, or it can simply wrap a connection to make a result set look like a JavaBean. You could even use a row set to access data communicating over HTTP to a servlet, which provides the data for the bean.
How can we insert images into a Mysql database?
This code is snip shows the basics:
File file = new File(fPICTURE);
FileInputStream fis = new FileInputStream(file);
PreparedStatement ps =
ConrsIn.prepareStatement("insert into dbPICTURE values (?,?)");
ps.setString(1,file.getName());
ps.setBinaryStream(2,fis,(int)file.length());
ps.close();
fis.close();
Define advantage of using a PreparedStatement?
For SQL statements that are executed repeatedly, using a PreparedStatement object would almost always be s faster than using a Statement object. This is because creating a PreparedStatement object by explicitly giving the SQL statement causes the statement to be precompiled within the database immediately.
Typically, PreparedStatement objects are used for the SQL statements that take parameters.
How does the JDBC work?
The Java Database Connectivity (JDBC) is used to whenever a Java application should communicate with a relational database for which a JDBC driver exists.
Main JDBC classes:
• DriverManager. Manages a list of database drivers.
• Driver. The database communications link, handling all communication with the database.
• Connection. Interface with all the methods for contacting a database
• Statement. Encapsulates an SQL statement which is passed to the database to be parsed, compiled, planned and executed.
• ResultSet. The answer/result from a statement. A ResultSet is a fancy 2D list which encapsulates all outgoing results from a given SQL query.
--------------------------------------------------------------------------------------------------------------------------





MySQL
What is Primary Key?
A primary key is a single column or multiple columns is defined to have unique values that can be used as a row identifications.
What is Transaction?
MySQL server is introduced the transaction concept to allow users to group one or more SQL statements into a single transaction, so that the effects of all the SQL statements in a transaction can be either all committed or all rolled back. A transaction is a logical unit of work requested by a user to be applied to the database objects.
What are the indexes?
An index is an internal structure which is provided quick access to rows of a table based on the values of more than one columns.
What is a foreign key?
A foreign key is a constraint associates one or more columns in a table with an identical set of columns on which a primary key has been defined in another table. A foreign key may refer to the primary key of another table or same table.
What is the advantages of MySQL against oracle?
MySQL are many advantages in comparison to Oracle.
• MySQL is Open a source, which can be available any time
• MySQL has no cost of development purpose.
• MySQL has most of features , which oracle provides
• MySQL day by day updating with the new facilities.
• Good for small application.
• Easy to learn and to become master.
• MySQL has a good power these days.
-------------------------------------------------------------------------------------------------------------------------------









Servlet
Question: What are the lifecycle methods of Servlet?
Answer: The interface javax.servlet.Servlet, defines the three life-cycle methods. These are:
public void init(ServletConfig config) throws ServletException
public void service( ServletRequest req, ServletResponse res) throws ServletException, IOException
public void destroy()
The container manages the lifecycle of the Servlet. When a new request come to a Servlet, the container performs the following steps.
1. If an instance of the servlet does not exist, the web container
* Loads the servlet class.
* Creates an instance of the servlet class.
* Initializes the servlet instance by calling the init method. Initialization is covered in Initializing a Servlet.
2. The container invokes the service method, passing request and response objects.
3. To remove the servlet, container finalizes the servlet by calling the servlet's destroy method.
Question: What are the type of protocols supported by HttpServlet?
Answer: It extends the GenericServlet base class and provides an framework for handling the HTTP protocol. So, HttpServlet only supports HTTP and HTTPS protocol.
Question: What are the directory Structure of Web Application?
Answer: Web component follows the standard directory structure defined in the J2EE specification.
Directory Structure of Web Component
/
index.htm, JSP, Images etc..
Web-inf

web.xml
classes

servlet classes
lib

jar files
Question: What is ServletContext?
Answer: ServletContext is an Interface that defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file. There is one context per "web application" per Java Virtual Machine. (A "web application" is a collection of servlets and content installed under a specific subset of the server's URL namespace such as /catalog and possibly installed via a .war file.)
Question: What is meant by Pre-initialization of Servlet?
Answer: When servlet container is loaded, all the servlets defined in the web.xml file does not initialized by default. But the container receives the request it loads the servlet. But in some cases if you want your servlet to be initialized when context is loaded, you have to use a concept called pre-initialization of Servlet. In case of Pre-initialization, the servlet is loaded when context is loaded. You can specify 1
in between the tag.
Question: What mechanisms are used by a Servlet Container to maintain session information?
Answer: Servlet Container uses Cookies, URL rewriting, and HTTPS protocol information to maintain the session.
Question: What do you understand by servlet mapping?
Answer: Servlet mapping defines an association between a URL pattern and a servlet. You can use one servlet to process a number of url pattern (request pattern). For example in case of Struts *.do url patterns are processed by Struts Controller Servlet.
Question: What must be implemented by all Servlets?
Answer: The Servlet Interface must be implemented by all servlets.
Question: What are the differences between Servlet and Applet?
Answer: Servlets are server side components that runs on the Servlet container. Applets are client side components and runs on the web browsers. Servlets have no GUI interface.
Question: What are the uses of Servlets?
Answer: * Servlets are used to process the client request.
* A Servlet can handle multiple request concurrently and be used to develop high performance system
* A Servlet can be used to load balance among serveral servers, as Servlet can easily forward request.
Question: What are the objects that are received when a servlets accepts call from client?
Answer: The objects are ServeltRequest and ServletResponse . The ServeltRequest encapsulates the communication from the client to the
server. While ServletResponse encapsulates the communication from the Servlet back to the client.

2 comments:

  1. thanks bro...whats your phone no....is it interview Q or written Q???
    Thanks Once Again

    ReplyDelete