Question: What will be the output of the following java code snippet?
class Test{
public static void main(String []args){
int x ;
if(args[] != null){
x = 7;
}
System.out.println(x);
}
}
Answer: Java code snippet will give you compilation error because x is not initialized when we are printing it. At compile time if condition is not tested.
Question: Explain Garbage Collection in java?
Answer: Objects are created by Java's "new" operator, and memory for new objects is allocated on the heap at run time. Garbage collection is the process of automatically freeing objects that are no longer referenced by the program. This frees the programmer from having to keep track of when to free allocated memory, thereby preventing many potential bugs and headaches.
Question: What is the output of System.out.println(null)?
Answer: It gives you compilation error The method println(char[]) is ambiguous for the type PrintStream.
Question: What will be the output of following java code snippet?
public class Test {
public static void main(String[] args) {
HashSet s = new HashSet();
for(short i = 0; i<100;i++){
s.add(i);
s.remove(i-1);
}
System.out.println(s.size());
}
}
Answer: Output will be 100 because when we do remove(i-1) it is type casted to Integer which it doesnt find. So it doesnt remove any element.Hence the size is 100.
Question: Is JVM platform independent?
Answer: No,JVM(Java Virtual Machine) is not platform independent.
To Read More click here
Question: How will you define a page as an error page so that all the exceptions are sent to this page?
Answer: Set isErrorPage = true in <%@ page%> directive
Question: How will you send your exception to the error page?
Answer: set errorPage= "error page path" in <%@ page%> directive
Question: What do you mean by isThreadSafe attribue of page directive?
Answer: It means whether thread safety is implemented in the JSP file. The default value is true, which means that the JSP container can send multiple, concurrent client requests to the JSP page. You must write code in the JSP page to synchronize the multiple client threads. If you use false, the JSP container sends client requests one at a time to the JSP page.
Question: How many types of directives are there?
Answer: Three types page ,include and taglib.
Question: How will you get the exception object in a jsp?
Answer: When you set isErrorPage= true you get the exception object
To Read More click here
Question: What is Servlet?
Answer: A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications access via a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers
Question: Difference between RequestDispatcher.forward() and Response.sendRedirect()?
Answer: Forward happens on the server side where are sendRedirect happens on the client side.in the forward the processing is transfered to another resource and it does the processing but the
url remains the same. But in case of sendRedirect the URL changes and second resource does the processing. In redirect there is a round trip so performance is slow.forward is server side redirect and sendRedirect is client side redirect. 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 And then returns to the calling method. When a sendRedirect 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. Client can disable sendRedirect.
Question: Explain Servlet Lifecycle?
Answer: The lifecycle of a servlet begins when it is loaded into Application Server memory and ends when the servlet is terminated or reloaded.Instantiation and initialization:-init() method is called,Servicing requests service() method is called,Termination destroy() method is called
To Read More click here
Question: What is Prepared Statement?
Answer: This special type of statement is derived from class Statement.If you need a Statement object to execute many times, it will normally make sense to use a PreparedStatement object instead. The advantage to this is that in most cases, this SQL statement will be sent to the DBMS right away, where it will be compiled. As a result, the PreparedStatement object contains not just an SQL statement, but an SQL statement that has been precompiled. This means that when the PreparedStatement is executed, the DBMS can just run the PreparedStatement’s SQL statement without having to compile it first.
PreparedStatement updateSales = con.prepareStatement("UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ?");
Question: Where is Prepared Statement compiled?
Answer: SQL statement will be sent to the DBMS right away, where it will be compiled.
Question: Why prepared statement is faster?
Answer: It is faster because it is precompiled.
To Read More click here
Question: How will you avoid multiple threads access the singleton method getInstance?
Answer: We will need to synchronize the getInstance() method
Question: What is Front Controller Design Pattern?
Answer: The Front Controller pattern defines a single component that is responsible for processing application requests. A front controller centralizes functions such as view selection, security, and templating, and applies them consistently across all pages or views. Consequently, when the behavior of these functions need to change, only a small part of the application needs to be changed: the controller and its helper classes.
To Read More click here
Question: What is Index? explain clustered and non clustered index?
Answer: Index is to make query faster.Indexes are of two types. Clustered indexes and non-clustered indexes. When you create a clustered index on a table, all the rows in the table are stored in the order of the
clustered index key. So, there can be only one clustered index per table. Non-clustered indexes have their own storage separate from the table data storage. The row located
could be the RowID or the clustered index key, depending up on the absence or presence of clustered index on the table.If you create an index on each column of a table, it improves the query performance, asthe query optimizer can choose from all the existing indexes to come up with an efficient execution plan. At the same time, data modification operations (such as INSERT,UPDATE, and DELETE) will become slow, as every time data changes in the table, all the indexes need to be updated. Another disadvantage is that, indexes need disk space,
the more indexes you have, more disk space is used.
Answer: Joins allow database users to combine data from one table with data from one or more other tables (or views, or synonyms). Tables are joined two at a time making a new table containing all possible combinations of rows from the original two tables.
Question: Explain inner and outer joins?
Inner joins: Chooses the join criteria using any column names that happen to match between the two tables.
Left Outer Join :- With left outer join you get only those rows of right table that meet the condition but all the rows of the left table
Right Outer Join:- With rightouter join you get only those rows of left table that meet the condition but all the rows of the right table
To Read More click here