Saturday, February 23, 2013

Java Questions and Answers Part 2

As you know, Java is one of most popular object-oriented programming languages and has been in use since its release. It is interesting that test automation can be written in Java in certain automated testing tools/ frameworks. View the videos, Java Interview Questions and Answers - 1 and Java Interview Questions and Answers - 2. Or read on.

1. How can you add GUI elements to a Java application?
Java has an API called swing to provide GUI components such as textboxes, checkboxes, radiobuttons, comboboxes and so on. These components are available on importing javax.swing.*. The process to add a GUI element to a Java application involves creating a new object of the component class, adding the object to the container and attaching an event handler to the object.

2. How can you accept user input to a Java application?
This can be done by importing the java.util.Scanner class. The Scanner class has methods like nextInt, nextDouble, nextLine and so on to accept inputs of different data types from the user into a variable.

3. What methods are available to search within a string?
The String class in Java has a vast number of methods. There are many methods to search for characters within a string. Some of these methods include charAt, contains, indexOf, lastIndexOf, startsWith and endsWith.

4. How do you draw graphics in an application?
Java has a built-in Graphics object. This object has methods such as drawArc, drawImage, drawLine, drawRect (empty rectangle)/ fillRect (solid rectangle), drawPolygon/ fillPolygon and so on that can be invoked to draw the respective lines and shapes in any desired color.

5. How can you work with files and folders?
First, import the library. Then create an object of the File class and point it to the required file or directory. This object will have the file's properties (e.g. length, isDirectory, lastModified) and methods (e.g. listFiles, createNewFile, getPath). A Scanner object can be created for the File object and used to fetch the contents in the file.

6. How do you handle exceptions?
There may be code which if executed may throw an exception. Such code is put inside a try block, which is followed by a catch block containing the exception handler. If the code throws an exception when it is executed, the control flows to the catch block. The try block may have one or more lines of code. Each line of code may have its own try block. There can be one or more catch blocks to handle different types of exceptions. All such catch blocks immediately follow the try block.

7. What is recursion?
A recursive method is one that calls itself. See the example below. For a given integer input that is greater than one, this method sums all integers from the input down to one. This method has the base case for input equal to one. The recursion exists where sumAll method calls itself in the last line.
public static int sumAll(int n){
    if (n==1)
        return 1;
        return n + sumAll(n-1);}// recursion is in this line of code

8. What is the benefit of using a List instead of an array?
The Collections class has several useful methods that work on a List but not on an array. Examples of these methods are addAll, binarySearch, copy, fill, max, min, reverse, shuffle and sort.

9. What is the difference between Stack and PriorityQueue classes?
A new element can be added to the Stack by the push method. The new element is always added at the top of the Stack. In an existing Stack, the top element can be removed by the pop method. The Stack has other methods to add and remove elements at specified positions within the Stack.
In a PriorityQueue, a new element is added by the offer method. This new element is always inserted at the bottom of the queue. In an existing queue, the top element can be removed by using the poll method. The queue does not have methods to add and remove elements at specified positions within the queue.

10. How can different code in a program be executed concurrently?
This can be done using threads. A Java application can have multiple threads of execution running at the same time. A thread can be created by writing a class that implements the Runnable interface which means implementing the run method. Thereafter, a new thread can be created by instantiating a new Thread passing the new object of this class as parameter. A Thread object has a number of methods like setPriority and start.

No comments:

Post a Comment