Saturday, July 12, 2008

Tricky Questions in Struts / J2EE / SQL :

Tricky Questions in J2EE, Struts and SQL :

Sometimes, during the interview, when you answer almost all questions theoretically, the direction of the interview would change and the interviewer would start asking tricky questions. These types of questions have no definite answer in theory and it comes more of practical scenarios and real-time experiences. I am going to discuss such type of questions and hope this is useful to readers such as you.

During the J2EE or Java interview, you should be able to answer little bit of SQL also. So be prepared some basics or even tricky queries, so that you give the interviewer the feeling that you would manage any circumstances in your development team.

Obviously these types of questions cannot be answered from books. So I want to share these with you.

1. How can you find the nth smallest or greatest number using SQL?

SELECT A.NUMBER FROM EMP A
WHERE (SELECT COUNT(B.NUMBER) FROM EMP B WHERE B.NUMBERl > A.NUMBER) = 2;


The above query finds the 3rd greatest number. Substitute 5 in place of 2 if you want to find the 6th greatest number. Substitute < in place of > to get the nth smallest number.

Above query is a bit complex to understand by looking at it concept-wise. The query does a simple thing. It fetches the number from the table, such that there are 2 more bigger numbers than that. The outer query picks up a number and the sub-query finds the number of elements that are greater than the number picked by the outer query. Such number is the third highest number. (Consider numbers 3, 4, 23, 143, 15, 20; Here, there are 2 numbers greater than 20; they are 23 and 143. So the third greatest number is 20).

Since this is a correlated sub-query, it is executed distinctly for every row in the table. So number of times the sub-query is executed is dependent on the number of rows in the table. When the number of rows increases, obviously the performance of the query comes down.

2. How can you display multiple related rows as a single row?

For example assume the following rows in EMP table:

Name Value
---------------
John A
John B
John C

The above rows should be displayed as
John A B C

The solution to the above problem is query using self-join:

SELECT A.NAME, A.VALUE, B.VALUE, C.VALUE FROM EMP A, EMP B, EMP C
WHERE (A.VALUE <> B.VALUE) AND (B.VALUE <> C.VALUE)


3. How can you avoid a page from being cached in JSP?

When you feel that the JSP contains all dynamic content and the content should not be cached, particularly when clicking Back and Forward buttons in the browser. So when you prevent the page from being cached, the page always displays the contents interact dynamically whenever the URL is typed again or when Back is pressed.

This can be achieved by setting appropriate page headers in JSP.
<%
response.setHeader("Cache-Control","no-store"); Headers are set with appropriate values
response.setHeader("Pragma\","no-cache");
response.setDateHeader ("Expires", 0); // The page get expired immediately.
%>


4. How can you efficiently display huge amount of information in a single JSP (HTML) page?

First, classify the information that you want to display, as categories. Put each category under DIV tag with suitable id. Also make the ‘cursor:hand’ style property to the headings of these tags. This heading when clicked, runs a small javascript code that displays only one DIV, ie., displaying only one heading and hiding the rest of them. At any time, all the topic-headings are visible to the userm, only the contentes are either visible or hidden. When the user clicks on the second heading, the selected DIV tag is set visible and others are hidden. The visible and hidden properties are set with the help of suitable CSS style properties.

5. How can you import a JSP content into an Excel (.XLS) format?

The normal JSP content can be downloaded as an excel sheet by setting appropriate headers and document type with the document name. The simple example is given as follows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@
page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<HTML>
<head>
<%response.setContentType("application/vnd.ms-excel"); %>
<%response.setHeader("Content-disposition","attachment;filename=Demo.xls");%>
<title>excelReport</title>
</head>
<body>
<table>
<tr>
<td> One </td>
<td> Two </td>
</tr>

</table>
</body>
</HTML>


In the above example, the content type is set to application that is ms-excel type. Also that the content is disposed in the browser in the form of an attachment in XLS type with the name provided under filename.

If you feel that the above article is helpful or if you have any queries, write to me to swathikalakshmi@gmail.com

I would come with some more tricky questions in the next week.

Bye.
Swathika.

11 comments:

prem said...

hey hii...great work buddy..
do keep up d gud wrk....evn if u dnt get many comments...but b rest assured ur blogs are helpful 2 many...:)
thnQ

Arul Nehru said...

hi friend, u r doing great work. its help us.Thank u so much for ur effort.and Keep it up :)

Lucy said...

nice work buddy

Sreedhar said...

hi friend u r doing great work

seetharam said...

hi,
It is very useful for the guys freshers like me..

many thanks and go a head

Ramya said...

Hi ,
Your blog is really useful... keep it going!!!

Thamizhvanan said...

SELECT A.NAME, A.VALUE, B.VALUE, C.VALUE FROM EMP A, EMP B, EMP C
WHERE (A.VALUE <> B.VALUE) AND (B.VALUE <> C.VALUE)


The above is not giving proper output what we expect.please check

ramesh..challagundla said...

Really its very useful and thanks a lot

Anonymous said...

Hi

Tks very much for post:

I like it and hope that you continue posting.

Let me show other source that may be good for community.

Source: Sample interview questions

Best rgs
David

Interview questions answers pdf said...

Struts Interview Questions and Answers
http://allinterviewquestionsandanswerspdf.blogspot.in/2016/06/40-top-struts-interview-questions-and.html

Xplore IT Corp said...

Hey Nice Blog!! Thanks For Sharing!!!Wonderful blog & good post.Its really helpful for me, waiting for a more new post. Keep Blogging!
advanced java training in coimbatore
best java training in coimbatore