Java Interview Questions

Jumbo Java FAQs

Newbie Java Questions:

  1. If Runnable interface is better than Thread class, than why we are using Thread class? What is the need for Thread class?
  2. Why we are calling System.gc() method to garbage collection of unused object, if garbage collection is automatically done in Java by daemon thread in background process with regular interval?
  3. What is the significance of Marker interface? Why are we using, even though it has no method?
  4. Why we are always doing rs.next() in first line of while loop in retrieving data from database through result set?
  5. Please give me the details of synchronization? And which are the methods and elements used in it and why only that methods and variables?
  6. Why we are not using Java in real time based application, but instead we are using C or C++?
  7. Detail difference between 4 types of driver and their use in different different applications?
  8. Is Java code with native methods platform-independent?
  9. Why is the compiler platform-independent, while JVM is platform-dependent?
  10. Mention different type of compilers and interpreters in Java?

 

XML interview questions

 

  1. What is the difference between SAX parser and DOM parser?
  2. What is the difference between Schema and DTD?
  3. How do you parse/validate the XML document?
  4. What is XML Namespace?
  5. What is Xpath?
  6. What is XML template?
  7. How would you produce PDF output using XSL’s?
  8. What are the steps to transform XML into HTML using XSL?
  9. What is XSL?
  10. What is XSLT?

Java Web Development Interview Questions

 

  1. Can we use the constructor, instead of init(), to initialize servlet? - Yes , of course you can use the constructor instead of init(). There’s nothing to stop you. But you shouldn’t. 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. That no longer applies, but servlet containers still will only call your no-arg constructor. So you won’t have access to a ServletConfig or ServletContext.
  2. How can a servlet refresh automatically if some new data has entered the database? - You can use a client-side Refresh or Server Push.
  3. 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.
  4. How many messaging models do JMS provide for and what are they? - JMS provide for two messaging models, publish-and-subscribe and point-to-point queuing.
  5. What information is needed to create a TCP Socket? - The Local System?s IP Address and Port Number. And the Remote System’s IPAddress and Port Number.
  6. What Class.forName will do while loading drivers? - It is used to create an instance of a driver and register it with the DriverManager. When you have loaded a driver, it is available for making a connection with a DBMS.
  7. How to Retrieve Warnings? - SQLWarning objects are a subclass of SQLException that deal with database access warnings. Warnings do not stop the execution of an application, as exceptions do; they simply alert the user that something did not happen as planned. 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
8.                     SQLWarning warning = stmt.getWarnings();
9.                     if (warning != null)
10.                {
11.                          while (warning != null)
12.                          {
13.                                     System.out.println("Message: " + warning.getMessage());
14.                                     System.out.println("SQLState: " + warning.getSQLState());
15.                                     System.out.print("Vendor error code: ");
16.                                    System.out.println(warning.getErrorCode());
17.                                     warning = warning.getNextWarning();
18.                          }
19.                }
  1. How many JSP scripting elements are there and what are they? - There are three scripting language elements: declarations, scriptlets, expressions.
  2. 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.
  3. What are stored procedures? How is it useful? - A stored procedure is a set of statements/commands which reside in the database. The stored procedure is pre-compiled and saves the database the effort of parsing and compiling sql statements everytime a query is run. Each database has its own stored procedure language, usually a variant of C with a SQL preproceesor. Newer versions of db’s support writing stored procedures in Java and Perl too. Before the advent of 3-tier/n-tier architecture it was pretty common for stored procs to implement the business logic( A lot of systems still do it). 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 db. The overhead of a JDBC Connection may be significant in these cases.
  4. How do I include static files within a JSP page? - Static resources should always be included using the JSP include directive. This way, the inclusion is performed just once during the translation phase. Do 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.
  5. 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.
  6. How can I implement a thread-safe JSP page? - You can make your JSPs thread-safe by having them implement the SingleThreadModel interface. This is done by adding the directive <%@ page isThreadSafe=”false” % > within your JSP page.

Java GUI Design Interview Questions

  1. What advantage do Java’s layout managers provide over traditional windowing systems? – Java uses layout managers to lay out components in a consistent manner across all windowing platforms. Since Java’s layout managers aren’t tied to absolute sizing and positioning, they are able to accomodate platform-specific differences among windowing systems.
  2. What is the difference between the paint() and repaint() methods? – The paint() method supports painting via a Graphics object. The repaint() method is used to cause paint() to be invoked by the AWT painting thread.
  3. How can the Checkbox class be used to create a radio button? – By associating Checkbox objects with a CheckboxGroup
  4. What is the difference between a Choice and a List? – A Choice is displayed in a compact form that requires you to pull it down to see the list of available choices. Only one item may be selected from a Choice. A List may be displayed in such a way that several List items are visible. A List supports the selection of one or more List items.
  5. What interface is extended by AWT event listeners? – All AWT event listeners extend the java.util.EventListener interface.
  6. What is a layout manager? – A layout manager is an object that is used to organize components in a container
  7. Which Component subclass is used for drawing and painting? – Canvas
  8. What are the problems faced by Java programmers who dont use layout managers? – Without layout managers, Java programmers are faced with determining how their GUI will be displayed across multiple windowing systems and finding a common sizing and positioning that will work within the constraints imposed by each windowing system
  9. What is the difference between a Scrollbar and a ScrollPane? (Swing) – A Scrollbar is a Component, but not a Container. A ScrollPane is a Container. A ScrollPane handles its own events and performs its own scrolling.

Java And Perl Interview Questions

  1. Q1: How can we store the information returned from querying the database? and if it is too big, how does it affect the performance? In Java the return information will be stored in the ResultSet object. Yes, if the ResultSet is getting big, it will slow down the process and the performance as well. We can prevent this situation by give the program a simple but specific query statement.
  2. Q2: What is index table and why we use it? Index table are based on a sorted ordering of the values. Index table provides fast access time when searching.
  3. Q3: In Java why we use exceptions? Java uses exceptions as a way of signaling serious problems when you execute a program. One major benefit of having an error signaled by an exception is that it separates the code that deals with errors from the code that is executed when things are moving along smoothly. Another positive aspect of exceptions is that they provide a way of enforcing a response to particular errors.
  4. Q4: Write a code in Perl that makes a connection to Database.
5.           #!/usr/bin/perl
6.           #makeconnection.pl
7.            
8.           use DBI;
9.                     my $dbh = DBI->connect('dbi:mysql:test','root','foo')||die "Error opening database:
10.      $DBI::errstrn";
11.                print"Successful connect to databasen";
12.       
13.                $dbh->disconnect || die "Failed to disconnectn";
  1. Q5: Write a code in Perl that select all data from table Foo?
15.      #!usr/bin/perl
16.      #connect.pl
17.       
18.      use DBI;
19.      my ($dbh, $sth, $name, $id);
20.      $dbh= DBI->connect('dbi:mysql:test','root','foo')
21.                || die "Error opening database: $DBI::errstrn";
22.                $sth= $dbh->prepare("SELECT * from Foo;")
23.                          || die "Prepare failed: $DBI::errstrn";
24.                $sth->execute()
25.                          || die "Couldn't execute query: $DBI::errstrn";
26.                while(( $id, $name) = $sth->fetchrow_array)
27.                {
28.                          print "$name has ID $idn";
29.                }
30.                $sth->finish();
31.       
32.      $dbh->disconnect

33.                || die "Failed to disconnectn";

2 Comments »

  1. Market interface means interface which have no methods. Then why we call the Runnable interface as a marker interface, even though it has run() method?

    Comment by Krishan — January 24, 2007 @ 5:31 am

  2. What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy?

    Comment by Krishan — January 24, 2007 @ 5:33 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.