Let us get started. Question 11.
How can you add GUI elements to a Java application?
Java has an API called swing to provide GUI components such as text boxes,
check boxes, radio buttons, combo boxes and so on.
These components are available on importing javax.swing.*
The process to add a GUI element to a Java application is
1) Create a new object of the component class.
2) Add the object to the container and
3) Attach an event handler to the object.
Question 12. 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.
Question 13. 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.
Examples of these methods include charAt,
contains, indexOf, lastIdexOf, startsWith and endsWith.
How do you draw graphics in a Java application?
Java has a built-in graphics object. This object has methods such as drawArc, drawImage,
drawLine, drawRect (for an empty rectangle), fillRect (for a solid rectangle),
drawPolygon, fillPolygon and so on.
These methods can be invoked to draw the respective lines and shapes in any desired color.
How can you work with files and folders in Java?
First, import the java.io.File library.
Then create an object of the File class.
We can point the File object to the required file or directory.
This object will have file's properties and methods.
A Scanner object can be created for the File object and used to fetch the contents in the file.
Question 16. In Java, how do you handle exceptions?
This is done with try and catch blocks. So 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 also.
There can be one or more catch blocks to handle different types of exceptions.
All such catch blocks immediately follow the try block.
Question 17. What is recursion?
A recursive method is one that calls itself.
Let us look at the example below.
Now here for a given integer input that is greater than 1,
this method sums all the integers from the input down to one.
let us say for this method, we give
the input argument as 2. So, it is going to add up all the numbers from 1 to 2. That is
1 + 2 is 3.
So, first if the input is 2, it checks if
2 is equal to 1? No. It goes to the else block. return 2 + sumAll (2-1),
which means the sumAll method calls itself. And this time it calls itself
with an input argument of 1.
Now, when the input argument is 1, it returns 1. So basically it returns 2 + 1. That is 3.
Now, there are a couple of items to note here. If n = 1, return 1.
This is the base case, which is where the recursion ends.
And here since we have the function calling itself, there is a recursion in this line of code.
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 do not work on an array.
Some of these methods are
addAll, binarySearch, copy, fill, max, min, reverse, shuffle and sort.
How are Stack and PriorityQueue classes different?
First, the Stack.
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
The Stack has other methods to add and remove elements at specified positions within the Stack.
Now, the PriorityQueue class. 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.
How can different code in a program be executed concurrently? That means at the same time.
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 Runnable
Which means we have to implement the run method of this interface.
Thereafter, a new thread can be created by instantiating a new Thread object.
A Thread object has a number of methods like setPriority and start.