Examveda

Choose the appropriate code that counts the number of non-zero(non-null) elements in the sparse array.

A.

public int count()
{
        int count = 0;
        for (List cur = this.next; (cur != null); cur = cur.next)
        {
            count++;
        }
        return count;
}

B.

public int count()
{
        int count = 0;
        for (List cur = this; (cur != null); cur = cur.next)
        {
            count++;
        }
        return count;
}

C.

public int count()
{
        int count = 1;
        for (List cur = this.next; (cur != null); cur = cur.next)
        {
            count++;
        }
        return count;
}

D.

public int count()
{
    int count = 1;
    for (List cur = this.next; (cur != null); cur = cur.next.next)
    {
        count++;
    }
    return count;
}

Answer: Option A


This Question Belongs to Data Structure >> Arrays In Data Structures

Join The Discussion

Related Questions on Arrays in Data Structures