Home > Net >  Are these 2 Java exam questions incorrect?
Are these 2 Java exam questions incorrect?

Time:02-10

I recently did an exam related to Java that had 2 questions that in my opinion were wrong. I was hoping some of you could help me out.

The questions are:

1)

public class Cell {
    public Cell(){
    }
    public char content; //The character in this cell.
    public Cell next;  // Pointer to the cell to the right of this one.
    public Cell prev;  // Pointer to the cell to the left of this one.
}

The following method is supposed to move one node to the right in a doubly linked list of Cells but it contains a flaw. The global variable 'currentCell' is a pointer to a Cell in the list.

public void moveRight(){
    if (currentCell.next == null){
        Cell newCell = new Cell();
        newCell.content = '';
        newCell.next = null;
        newCell.prev = currentCell;
        currentCell.next = newCell;
        currentCell = currentCell.next;
     }
 }

True or False: If 'currentCell' is at the head of the list this moveRight() method will perform its duties correctly.

For me, this question is False, as the moveRight method will not do anything because currentCell.next is NOT null. Its duties (move to the right) will not be successful. I would like to know what the community thinks, because according to the exam this should be True?

2.

True or False: An interface contains method declarations as well as implementations.

I believe this should be True. As stated by Oracle (2021), “An interface declaration can contain method signatures, default methods, static methods, and constant definitions. The only methods that have implementations are default and static methods.”

Yet, the exam stated that the correct answer is False.

I hope someone could clarify this further for me.

Thank you!

CodePudding user response:

The problem with the method is not about the != null check: that will be true when currentCell is the last element of the list, and in that case there will be nothing to move (it cannot be moved right).

2.

The question should technically be True for newer versions of Java (after 2014 default methods implementation in Java 8), but (personally) I think the question was more about the essence of interfaces, that are used typically to abstract away from implementations, providing just method declarations.

  •  Tags:  
  • Related