Industrial Training

TECH MAHINDRA Core Java Campus Interview Question Answer


How can I get to print the stacktrace for an exception occuring within my JSP page?

By printing out the exception’s stack trace, you can usually diagonse a problem better when debugging JSP pages. By looking at a stack trace, a programmer should be able to discern which method threw the exception and which method called that method. However, you cannot print the stacktrace using the JSP out implicit variable, which is of type JspWriter. You will have to use a PrintWriter object instead. The following snippet demonstrates how you can print a stacktrace from within a JSP error page:
<%@ page isErrorPage="true" %>
<%
out.println(" ");
PrintWriter pw = response.getWriter();
exception.printStackTrace(pw);
out.println(" ");
%>


How do you pass an InitParameter to a JSP?

The JspPage interface defines the jspInit() and jspDestroy() method which the page writer can use in their pages and are invoked in much the same manner as the init() and destory() methods of a servlet. The example page below enumerates through all the parameters and prints them to the console. <%@ page import="java.util.*" %>
<%!
ServletConfig cfg =null;
public void jspInit(){
ServletConfig cfg=getServletConfig();
for (Enumeration e=cfg.getInitParameterNames(); e.hasMoreElements();) {
String name=(String)e.nextElement();
String value = cfg.getInitParameter(name);
System.out.println(name+"="+value);
}
}
%>


How can my JSP page communicate with an EJB Session Bean?

The following is a code snippet that demonstrates how a JSP page can interact with an EJB session bean: <%@ page import="javax.naming.*, javax.rmi.PortableRemoteObject, foo.AccountHome, foo.Account" %> <%! //declare a "global" reference to an instance of the home interface of the session bean AccountHome accHome=null; public void jspInit() { //obtain an instance of the home interface InitialContext cntxt = new InitialContext( ); Object ref= cntxt.lookup("java:comp/env/ejb/AccountEJB"); accHome = (AccountHome)PortableRemoteObject.narrow(ref,AccountHome.class); } %> <% //instantiate the session bean Account acct = accHome.create(); //invoke the remote methods acct.doWhatever(...); // etc etc... %>


What is the Collections API?

The Collections API is a set of classes and interfaces that support operations on collections of objects


What is the List interface?

The List interface provides support for ordered collections of objects.


What is the Vector class?

The Vector class provides the capability to implement a growable array of objects


What is an Iterator interface?

The Iterator interface is used to step through the elements of a Collection


Which java.util classes and interfaces support event handling?

The EventObject class and the EventListener interface support event processing



What is the GregorianCalendar class?

The GregorianCalendar provides support for traditional Western calendars


What is the Locale class?

The Locale class is used to tailor program output to the conventions of a particular geographic, political, or cultural region


What is the SimpleTimeZone class?

The SimpleTimeZone class provides support for a Gregorian calendar


What is the Map interface?

The Map interface replaces the JDK 1.1 Dictionary class and is used associate keys with values


What is the highest-level event class of the event-delegation model?

The java.util.EventObject class is the highest-level class in the event-delegation class hierarchy


What is the Collection interface?

The Collection interface provides support for the implementation of a mathematical bag - an unordered collection of objects that may contain duplicates


What is the Set interface?

The Set interface provides methods for accessing the elements of a finite mathematical set. Sets do not allow duplicate elements


What is the purpose of the enableEvents() method?

The enableEvents() method is used to enable an event for a particular object. Normally, an event is enabled when a listener is added to an object for a particular event. The enableEvents() method is used by objects that handle events by overriding their event-dispatch methods.


What is the ResourceBundle class?

The ResourceBundle class is used to store locale-specific resources that can be loaded by a program to tailor the program’s appearance to the particular locale in which it is being run.


What is the difference between yielding and sleeping?

When a task invokes its yield() method, it returns to the ready state. When a task invokes its sleep() method, it returns to the waiting state.


When a thread blocks on I/O, what state does it enter?

A thread enters the waiting state when it blocks on I/O.


When a thread is created and started, what is its initial state?

A thread is in the ready state after it has been created and started.


What invokes a thread’s run() method?

After a thread is started, via its start() method or that of the Thread class, the JVM invokes the thread’s run() method when the thread is initially executed.


What method is invoked to cause an object to begin executing as a separate thread?

The start() method of the Thread class is invoked to cause an object to begin executing as a separate thread.


What is the purpose of the wait(), notify(), and notifyAll() methods?

wait(),notify(), and notifyAll() methods are used to provide an efficient way for threads to wait for a shared resource. When a thread executes an object’s wait() method, it enters the waiting state. It only enters the ready state after another thread invokes the object’s notify() or notifyAll() methods.

Hi I am Pluto.