In: Computer Science
which question is incomplete these question given like they are and i have to answer them. These are Java questions.
Q1. //Write an iterative method to count long book in the linked list. Assume that book has method
// Boolean isLong () that returns true if book is long and returns false otherwise.
Public int countLongBooks ()
{
Q2. //Recursive method to add a new node (with myBook as data) at the rear of the list
Public void addToRearRec(Book myBook, Node first)
Q3. //Recursive method returns longest Book in the non-empty linked list. First refers to the first node in the linked l
// list.
Public Book longestBook ( Node first)
{
Q4. Determine the outcome of the following code if NumberFormatException happened within the try block.
Try
{
//some code
}
Catch (Number FormatException e)
{
System.out.println(“Example ne”);
}
Catch (stringIndexOutOfBoundsException e)
{
System.out.println(“Example two”);
}
Finally
{
System.out.println(“Finally is dine”);
}
System.out.println(“The next”);
Q5. Determine the outcome of m(4) invocation?
Public void m(int n)
{
If. (n <= 1
System.out.print(“A”);
Else
{
m(n-1);
m(n-2);
}
}
Q 6.
Write “C” next the statements that are correct?
a). Interface is java class that cannot be instantiated.
b). Interface is java class containing constants / or abstract methods.
c). Interface is an abstract class.
d). Interface is java structure containing constants and / or abstract methods.
Q7. Complete code for recursive method which returns shortest person in the nonempty list.Assume that only first n elements in the list have been assigned their values.person class has method getHight().
Public person shortestPersonRec(Person[] list, int n)
{
In Question 1, Write an iterative method to count long book in the linked list, it is mentioned that assume that book has method Boolean isLong () that returns true if book is long and returns false otherwise. But in the example method it is given like this
Public int countLongBooks ()
{
in which firstly, alphabet 'p' of public is written in upper case which is wrong and the same mistake is in all other questions, secondly, it was mentioned previously that the method should be isLong() with return type boolean but the method name in example given is countLongBooks() with return type int. This may create confusion.
In Question 2, Recursive method to add a new node (with myBook as data) at the rear of the list
Public void addToRearRec(Book myBook, Node first), it is mentioned that the new node should be added with myBook as data. Now in linear linked list each node always contains two section 1) Info / data part and 2) Next address part. There cannot be a node in linked list with only data part. But if it was asked to create a new node object as data then it is correct.
In Question 3, Recursive method returns longest Book in the non-empty linked list. First refers to the first node in the linked list, Public Book longestBook ( Node first){
There is no problem with this question. The question is clear and complete.
In Question 4,
Determine the outcome of the following code if NumberFormatException happened within the try block.
Try
{
//some code
}
Catch (Number FormatException e)
{
System.out.println(“Example ne”);
}
Catch (stringIndexOutOfBoundsException e)
{
System.out.println(“Example two”);
}
Finally
{
System.out.println(“Finally is dine”);
}
System.out.println(“The next”);
It was asked to determine whether NumberFormatException will occur in the try block or not but no code is written inside the try block. Exceptions occurs at runtime when the user enters any incorrect input at runtime. Therefore, Question - 4 is incomplete. Moreover, lot of syntax error is there in the code snippet like try, catch finally should be written in lower case and instead of class name stringOutOfBoundException, it should be ArrayIndexOutOfBoundsException.
Question 5 and 6 is also ok only few syntax errors are there.