Test02
Home ] Up ]

 

CSIS 250 2003 Spring T02

Incorrect choices could be due to logical, execution, or syntax errors.

 

Assume the following declarations/definitions for the problems that follow.

 

// constants

const int MAX_COUNT = 10;

const int MAX_VALUE = 5;

 

1.       Identify the missing statement/line in the following function.

a)        if x = a[i]

b)        if (x = a[i])

c)        if (x == a[i])

d)        if (a[i] = x)

e)        None of the above

 

bool searchSeq(int a[], int n, int x)

  {

  for (int i=0; i<=n-1; i++)

    ...

 

    if (x == a[i])

        return true;

 

  return false;

  }

 

2.       Identify the missing statement/line in the following function.

a)        for (int i=1; i<n-1; i++)

b)        for (int i=0; i<=n+1; i++)

c)        for (int i=0; i<n-1; i++)

d)        for (int i=1; i<=n-1; i++)

e)        None of the above

 

bool areDistinct(int a[], int n)

  {

  if (n <= 1)

      return true;

    else

      {

      ...

 

      for (int i=1; i<=n-1; i++)

        if (searchSeq(a, i, a[i]))

            return false;

 

      return true;

      }

  }

 

3.       Identify the missing statement/line in the following function.

a)        for (int i=1; i<=n-1; i++)

b)        for (int i=0; i<=n-1; i++)

c)        for (int i=0; i<n-1; i++)

d)        for (int i=0; i<=n+1; i++)

e)        None of the above

 

void populate(int a[], int &n)

  {

  n = rand()%(MAX_COUNT+1);

  ...

 

  for (int i=0; i<=n-1; i++)

    a[i] = rand()%(MAX_VALUE+1);

  }

 

4.       Identify the missing statement/line in the following function.

a)        for (int i=0; i<=n-1; i--)

b)        for (int i=1; i<=n-1; i++)

c)        for (int i=1; i<=n+1; i++)

d)        for (int i=0; i<n-1; i++)

e)        None of the above

 

void display(int a[], int n)

  {

  cout << "a[" << n << "]=";

  ...

 

  for (int i=0; i<=n-1; i++)

    cout << a[i] << ' ';

 

  cout << endl;

  }

 

5.       Identify the missing statement/line in the following function.

a)        a[i-1] = a[i+1];

b)        a[i] = a[i+1];

c)        a[i-1] = a[i];

d)        a[i+1] = a[i];

e)        None of the above

 

bool deleteAtPos(int a[], int &n, int id)

  {

  if ((id<0) || (id>=n)) 

      return false;

 

  for (int i=id; i<=n-2; i++)

    ...

    a[i] = a[i+1];

 

  n--;

  return true;

  }

 

6.       Identify the missing statement/line in the following function.

a)        if (a[i] == a[j])

b)        if (a[i] != a[j])

c)        if (a[i] = a[j])

d)        if (a[j] != a[i])

e)        None of the above

 

void purgeDupes(int a[], int &n)

  {

  if (1==n || 0==n)

      return;

 

  int i = 1;

  while (i <= n-1)

    {

    bool matchFound = false;

    for (int j=0; j<=i-1; j++)

      {

      ...

      if (a[i] == a[j])

        {

        matchFound = true;

        break;

        }

      }

 

    if (matchFound)

        deleteAtPos(a,n,i);

      else

        i++;

 

    }

  }

 

7.       Identify the missing statement/line in the following function.

a)        if (a[j] != a[i])

b)        if (a[i] == a[j])

c)        if (a[i] != a[j])

d)        if (a[i] = a[j])

e)        None of the above

 

void purgeDupes2(int a[], int &n)

  { 

  int i, j;

 

  i=0;

  while (i <= n-2)

    {

    j=i+1;

    while (j<=n-1)

      ...

      if (a[i] == a[j])

          deleteAtPos(a, n, j);

        else

          j++;

 

    i++;

    }

  }

 

Assume the following declarations/definitions for the problems that follow.

 

const int MAX_COUNT = 10;

const int MAX_VALUE = 5;

const int UNDEFINED = -9;

 

 

class CArray

  {

  private:

    int m_a[MAX_COUNT];

    int m_n;

    void swap(int &x, int &y);

  public:

    CArray(void);

    CArray(int m);

    void display(void) const;

    void displayMultiple(int c) const;

    void populate(void);

    bool areDistinct(void) const;

    bool searchSeq(int x) const;

    CArray(char ch);

    void sortBubble(void);

    void displayAll(void) const;

    bool isSorted(void) const;

    void shuffle(void);

    void frag(void);

    void defrag(void);

    void sortSelection(void);

    friend bool areEqual(const CArray &array1,

      const CArray &array2);

    bool isEqualTo(const CArray &array2) const;

    bool operator ==(const CArray &array2)

      const;

  };

 

bool areEqual(const CArray &array1,

  const CArray &array2);

 

8.       Identify the missing statement/line in the following function.

a)        CArray ta1 = array1;

b)        CArray ta1 = this;

c)        CArray ta1 = *this;

d)        CArray ta1 = CArray(’r’);

e)        None of the above

 

bool CArray::operator ==(const CArray &array2) const

  {

  if ((m_n == 0) && (array2.m_n == 0))

    return true;

 

  if (m_n != array2.m_n)

    return false;

 

  ...

  CArray ta1 = *this;

  CArray ta2 = array2;

 

  ta1.sortSelection();

  ta2.sortSelection();

 

  for (int i=0; i<=ta1.m_n-1; i++)

    if (ta1.m_a[i] != ta2.m_a[i])

      return false;

 

  return true;

  }

 

9.       Identify the missing statement/line in the following function.

a)        CArray ta1 = this;

b)        CArray ta1 = array1;

c)        CArray ta1 = CArray(’r’);

d)        CArray ta1 = *this;

e)        None of the above

 

bool CArray::isEqualTo(const CArray &array2) const

  {

  if ((m_n == 0) && (array2.m_n == 0))

    return true;

 

  if (m_n != array2.m_n)

    return false;

 

  ...

  CArray ta1 = *this;

  CArray ta2 = array2;

 

  ta1.sortSelection();

  ta2.sortSelection();

 

  for (int i=0; i<=ta1.m_n-1; i++)

    if (ta1.m_a[i] != ta2.m_a[i])

      return false;

 

  return true;

  }

 

10.   Identify the missing statement/line in the following function.

a)        CArray ta1 = this;

b)        CArray ta1 = array1;

c)        CArray ta1 = CArray(’r’);

d)        CArray ta1 = *this;

e)        None of the above

 

bool areEqual(const CArray &array1, const CArray &array2)

  {

  if ((array1.m_n == 0) &&

      (array2.m_n == 0))

    return true;

 

  if (array1.m_n != array2.m_n)

    return false;

 

  ...

  CArray ta1 = array1;

  CArray ta2 = array2;

 

  ta1.sortSelection();

  ta2.sortSelection();

 

  for (int i=0; i<=ta1.m_n-1; i++)

    if (ta1.m_a[i] != ta2.m_a[i])

      return false;

 

  return true;

  }

 

11.   Identify the missing statement/line in the following function.

a)        for (int i=1; i<=m_n; i++)

b)        for (int i=1; i<=size-1; i++)

c)        for (int i=1; i<=size+1; i++)

d)        for (int i=1; i<=size-1; i--)

e)        None of the above

 

void CArray::sortSelection(void)

  {

  int size = m_n;

  while (size > 1)

    {

    int maxP = 0;

    ...

    for (int i=1; i<=size-1; i++)

      if (m_a[i] > m_a[maxP])

          maxP = i;

 

    swap(m_a[maxP], m_a[size-1]);

    size--;

    }

  }

 

12.   Identify the missing statement/line in the following function.

a)        while (true)

b)        while (p > 1)

c)        while (n < MAX_COUNT)

d)        for (j=0; j<p; j++)

e)        None of the above

 

void CArray::defrag(void)

  {

  int p, q, i;

  ...

 

  while (true)

    {

    p = -1;

    for (i=0; i<=MAX_COUNT-1; i++)

      {

      if (m_a[i] == UNDEFINED)

          {

          p = i;

          break;

          }

      }

 

    if (p==-1)

      return;

 

    q = -1;

    for (i = p+1; i<=MAX_COUNT-1; i++)

      {

      if (m_a[i] != UNDEFINED)

        {

        q = i;

        break;

        }

      }

 

    if (q == -1)

      return;

 

    swap(m_a[p], m_a[q]);

    }

  }

 

13.   Identify the missing statement/line in the following function.

a)        pick  = MAX_COUNT;

b)        pick  = rand()%MAX_COUNT + 1; 

c)        pick  = rand()%MAX_COUNT - 1;

d)        pick  = rand();

e)        None of the above

 

void CArray::frag(void)

  {

  for (int i=1; i<=MAX_COUNT*1000; i++)

    {

    ...

 

    int pick  = rand()%MAX_COUNT;

    swap(m_a[pick], m_a[0]);

    }

  }

 

14.   Identify the missing statement/line in the following function.

a)        y = x;

b)        y = temp;

c)        temp = y ;

d)        y = y + x;

e)        None of the above

 

void CArray::swap(int &x, int &y)

  {

  int temp;

  temp = x;

  x = y;

  ...

 

  y = temp;

  };

 

 

15.   Identify the missing statement/line in the following function.

a)        m_a[pick] = m_a[0];

b)        swap(m_a[pick], m_a[0]);

c)        m_a[0] = m_a[pick];

d)        swap m_a[pick], m_a[0];

e)        None of the above

 

void CArray::shuffle(void)

  {

  for (int i=1; i<=m_n*100; i++)

    {

    int pick  = rand()%m_n;

    ...

 

    swap(m_a[pick], m_a[0]);

    }

  }

 

16.   Identify the missing statement/line in the following function.

a)        if (m_a[i] = m_a[i+1])

b)        if (m_a[i] < m_a[i+1])

c)        if (m_a[i] <= m_a[i+1])

d)        if (m_a[i] != m_a[i+1])

e)        None of the above

 

bool CArray::isSorted(void) const

  {

  for (int i= 0; i<= m_n-2; i++)

    ...

 

    if (m_a[i] > m_a[i+1])

        return false;

 

  return true;

  }

 

17.   Identify the missing statement/line in the following function.

a)        for (int i=1; i<=MAX_COUNT; i++)

b)        for (int i=0; i<=MAX_COUNT; i++)

c)        for (int i=0; i<MAX_COUNT; i++)

d)        for (int i=0; i<MAX_COUNT; i--)

e)        None of the above

 

void CArray::displayAll(void) const

  {

  cout << "array[" << m_n << "]=";

 

  ...

 

  for (int i=0; i<MAX_COUNT; i++)

    cout << m_a[i] << ' ';

 

  cout << endl;

  }

 

18.   Identify the missing statement/line in the following function.

a)        m_a[i+1] = m_a[i-1];

b)        m_a[i+1] = m_a[i];

c)        m_a[i] = m_a[i+1];

d)        m_a[i-1] = m_a[i];

e)        None of the above

 

void CArray::sortBubble(void)

  {

  if (m_n <= 1)

      return;

 

  bool somethingChanged;

  int i;

 

  do

    {

    somethingChanged = false;

    for (i=0; i<=m_n-2; i++)

      {

      if (m_a[i] > m_a[i+1])

          {

          int temp = m_a[i+1];

          ...

 

          m_a[i+1] = m_a[i];

          m_a[i] = temp;

          somethingChanged = true;

          }

      }

    } while (somethingChanged == true);

  }

 

19.   Identify the missing statement/line in the following function.

a)        m_a[i+1] = m_a[i-1];

b)        m_a[i+1] = m_a[i];

c)        m_a[i] = m_a[i+1];

d)        m_a[i-1] = m_a[i];

e)        None of the above

 

CArray::CArray(char ch)

  {

  if (('r'==ch) || ('R'==ch))

      {

      m_n = rand()%(MAX_COUNT+1);

 

      ...

 

      for (int i=0; i<=m_n-1; i++)

        m_a[i] = rand()%(MAX_VALUE+1);

 

      for (i=m_n; i<MAX_COUNT; i++)

        m_a[i] = UNDEFINED;

      }

    else

      m_n = 0;

  }

 

20.   Identify the missing statement/line in the following function.

a)        m_a[i+1] = m_a[i-1];

b)        m_a[i+1] = m_a[i];

c)        m_a[i] = m_a[i+1];

d)        m_a[i-1] = m_a[i];

e)        None of the above

 

bool CArray::areDistinct(void) const

  {

  if (m_n <= 1)

      return true;

    else

      {

      ...

 

      for (int i=1; i<=m_n-1; i++)

        for (int j=0; j<=i-1; j++)

          if (m_a[i] == m_a[j])

              return false;

 

      return true;

      }

  }

     

21.   Identify the missing statement/line in the following function.

a)        if (x = a[i-1])

b)        if (x = a[i])

c)        if (x = m[i])

d)        if (x = m_a[i])

e)        None of the above

 

bool CArray::searchSeq(int x) const

  {

  for (int i=0; i<=m_n-1; i++)

    ...

 

    if (x == m_a[i])

        return true;

 

  return false;

  }

 

22.   Identify the missing statement/line in the following function.

a)        m_a[i] = rand()%(MAX_VALUE+1);

b)        a[i] = rand()%(MAX_VALUE1);

c)        a[i] = rand()%(MAX_VALUE-1);

d)        a[i] = rand()+(MAX_VALUE+1);

e)        None of the above

 

CArray::CArray(int m)

  {

  m_n = m;

  for (int i=0; i<=m_n-1; i++)

    ...

 

    m_a[i] = rand()%(MAX_VALUE+1);

 

  for (i=m_n; i<MAX_COUNT; i++)

    m_a[i] = UNDEFINED;

 

  }

 

23.   Identify the missing statement/line in the following function.

a)        a[i] = rand()%(MAX_VALUE1);

b)        a[i] = rand()%(MAX_VALUE-1);

c)        m_n = rand()%(MAX_COUNT+1);

d)        a[i] = rand()+(MAX_VALUE+1);

e)        None of the above

 

void CArray::populate(void)

  {

  ...

 

  m_n = rand()%(MAX_COUNT+1);

  for (int i=0; i<=m_n-1; i++)

    m_a[i] = rand()%(MAX_VALUE+1);

  }

 

24.   Identify the missing statement/line in the following function.

a)        a[i] = rand()%(MAX_VALUE1);

b)        a[i] = rand()%(MAX_VALUE-1);

c)        m_n = rand()%(MAX_COUNT+1);

d)        a[i] = rand()+(MAX_VALUE+1);

e)        None of the above

 

void CArray::displayMultiple(int c) const

  {

  for (int i=1; i<=c; i++)

    ...

 

    display();

  };

 

25.   Identify the missing statement/line in the following function.

a)        cout << a[i] << ' ';

b)        cout << m_a[i-1] << ' ';

c)        cout << m_a[i] << ' ';

d)        cout << m_a[i+1] << ' ';

e)        None of the above

 

void CArray::display(void) const

  {

  cout << "array[" << m_n << "]=";

 

  for (int i=0; i<=m_n-1; i++)

    ...

 

    cout << m_a[i] << ' ';

 

  cout << endl;

  }

 

26.   Identify the missing statement/line in the following function.

a)        m_a[i] = m_a[i+1];

b)        m_a[i-1] = m_a[i];

c)        m_a[i] = UNDEFINED;

d)        m_a[i+1] = m_a[i];

e)        None of the above

 

CArray::CArray(void)

  {

  m_n=0;

  for (int i=0; i<MAX_COUNT; i++)

    ...

 

    m_a[i] = UNDEFINED;

 

  cout << "Default constructor for CArray called\n";

  }

 

27.   Identify the range of output from the following program segment.

a)        Between 3 and 7

b)        Between 0 and 9

c)        Between 1 and 6

d)        Between 2 and 6

e)        None of the above

 

  int pick  = rand()%MAX_COUNT;

  cout << pick << endl;