Test01
Home ] Up ]

 

CSIS 250 2003 Fall Test01

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

 

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

 

const int MAX_SIZE    = 25;

const int TEST_COUNT  = 19;

const int MAX_VALUE   = 15;

const int UNDEFINED   = -911;

 

 

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

a)        deleted=0;

b)        deleted--;

c)        deleted++;

d)        deleted=1;

e)        None of the above

 

int deleteDupes(int a[], int &n)

  {

  int p, deleted=0;

 

  do

    {

    p = -1;

 

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

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

        {

        p = i+1;

        deleteAtPos(a, n, p);

        ...

        deleted++;

        break;

        }

 

    } while (p != -1);

 

  return deleted;

  }

 

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

a)        a[n] = UNDEFINED;

b)        a[n+1] = UNDEFINED;

c)        a[n-1] = UNDEFINED;

d)        a[1] = UNDEFINED;

e)        None of the above

 

 

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

  {

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

      return false;

    else

      {

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

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

 

      ...

      a[n-1] = UNDEFINED;

      n--;

      return true;

      }

  }

 

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

a)        swap(a[i], a[i-1]);

b)        swap(a[i], a[i+1]);

c)        swap(a[j], a[i+1]);

d)        swap(a[i], a[j+1]);

e)        None of the above

 

 

void sortBubble(int a[], int n)

  {

  int swaps;

  do

    {

    swaps = 0;

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

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

        {

        ...

        swap(a[i], a[i+1]);

        swaps++;

        }

 

    } while (swaps!=0);

 

  }

 

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

a)        insert(a, n, x);

b)        insert(a, n-1, x);

c)        insert(a, m-1, x);

d)        insert(a, m, x);

e)        None of the above

 

 

void populate(int a[], int n)

  {

  int m=0;

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

    {

    int x = rand()%(MAX_VALUE+1);

    ...

    insert(a, m, x);

    }

  }

 

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

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

b)        return;

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

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

e)        None of the above

 

 

void shuffle(int a[], int n)

  {

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

    {

    int pick = rand()%n;

    ...

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

    }

  }

 

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

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

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

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

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

e)        None of the above

 

 

bool isSorted(int a[], int n)

  {

  ...

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

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

      return false;

 

  return true;

  }

 

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

a)        temp = x;

b)        int x = temp;

c)        int temp = x;

d)        int temp;

e)        None of the above

 

 

void swap(int &x, int &y)

  {

  if (x != y)

    {

    ...

    int temp = x;

    x = y;

    y = temp;

    }

  }

 

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

a)        a[1] = x;

b)        a[2] = x;

c)        a[3] = x;

d)        a[n] = x;

e)        None of the above

 

 

bool insert(int a[], int &n, int x)

  {

  if (0 == n)

      {

      ...

      a[n] = x;

      n++;

      return true;

      };

 

  if (MAX_SIZE == n)

      return false;

 

  if (n > 0)

      {

      a[n] = x;

      n++;

 

      for (int i=n-1; i>=1; i--)

        {

        if (a[i] >= a[i-1]) return true;

        swap(a[i], a[i-1]);

        }

 

      return true;

      }

 

  return false;

  }

 

9.       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; i++)

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

e)        None of the above

 

 

void initialize(int a[], int n)

  {

  ...

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

    a[i] = UNDEFINED;

  }

 

10.   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; i++)

d)        for (int i=1; i<=n-2; 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;

  }

 

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

 

const int MAX_SIZE    = 10;

const int TEST_COUNT  = 25;

const int MAX_VALUE   = 15;

const int UNDEFINED   = -911;

 

class COList

  {

  private:

    int a[MAX_SIZE];

    int n;

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

  public:

    COList(void);

    void display(void) const;

    void initialize(void);

    bool insert(int x);

    bool isSorted(void) const;

    void shuffle(void);

    void sortBubble(void);

    void populate(int n);

    COList(char ch);

    friend bool isEqual(const COList &thisList,

      const COList &thatList);

    bool isEqual(const COList &thatList) const;

    bool operator ==(const COList &thatList)

     const;

    COList(const COList &givenList);

    int searchSequential(int x) const;

    friend COList fUnion(const COList &thisList,

     const COList &thatList);

    COList fUnion(const COList &thatList) const;

    COList operator +(const COList &thatList)

      const;

    COList operator *(const COList &thatList)

      const;

    COList operator -(const COList &thatList)

      const;

    friend ostream & operator << (ostream &bob,

      const COList &aList);

    bool hasDistinct(void) const;

    bool deleteAtPos(int p);

    int deleteDupes(void);

    int deleteDupes2(void);

    int deleteDupes3(void);

    int deleteDupes3r(void);

  };

 

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

a)        int tList;

b)        tList = 0;

c)        tList = empty;

d)        n = 0;

e)        None of the above

 

 

COList COList::operator +

  (const COList &thatList) const

  {

  ...

  COList tList;

  int i;

 

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

    if (tList.searchSequential(this->a[i])

      == UNDEFINED)

      tList.insert(this->a[i]);

 

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

    if (tList.searchSequential(thatList.a[i])

      == UNDEFINED)

      tList.insert(thatList.a[i]);

 

  return tList;

  }

 

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

a)        int tList;

b)        tList = 0;

c)        tList = empty;

d)        n = 0;

e)        None of the above

 

 

COList COList::fUnion(const COList &thatList)

  const

  {

  ...

  COList tList;

  int i;

 

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

    if (tList.searchSequential(this->a[i])

      == UNDEFINED)

      tList.insert(this->a[i]);

 

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

    if (tList.searchSequential(thatList.a[i])

      == UNDEFINED)

      tList.insert(thatList.a[i]);

 

  return tList;

  }

 

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

a)        int tList;

b)        tList = 0;

c)        tList = empty;

d)        n = 0;

e)        None of the above

 

 

COList fUnion(const COList &thisList,

  const COList &thatList)

  {

  ...

  COList tList;

  int i;

 

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

    if (tList.searchSequential(thisList.a[i])

      == UNDEFINED)

      tList.insert(thisList.a[i]);

 

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

    if (tList.searchSequential(thatList.a[i])

      == UNDEFINED)

      tList.insert(thatList.a[i]);

 

  return tList;

 

  }

 

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

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

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

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

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

e)        None of the above

 

 

int COList::deleteDupes3r(void)

  {

  int p1, p2, deleted;

 

  deleted = 0;

  p1 = 0;

  p2=p1+1;

 

  while (p2 <= n-1)

    if (a[p1] == a[p2])

        {

        p2++;

        deleted++;

        }

      else

        {

        p1++;

        a[p1] = a[p2];

        p2++;

        }

 

  n = n - deleted;

 

  ...

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

    a[i] = UNDEFINED;

 

  return deleted;

  }

 

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

a)        while (p2 <= 1)

b)        while (p2 <= -1)

c)        while (p2 <= n-1)

d)        while (p1 <= n-1)

e)        None of the above

 

 

int COList::deleteDupes3(void)

  {

  if (n<=1)

    return 0;

 

  int deleted, distinctCount=1, p1 = 0, p2=p1+1;

  ...

  while (p2 <= n-1)

    if (a[p1] == a[p2])

        a[p2++] = UNDEFINED;

      else

        {

        a[++p1] = a[p2++];

        distinctCount++;

        }

 

  deleted = n - distinctCount;

  n = distinctCount;

  return deleted;

  }

 

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

a)        while (p<=2)

b)        while (p<=n-2)

c)        while (p<=-2)

d)        while (p<=n)

e)        None of the above

 

 

int COList::deleteDupes2(void)

  {

  int p=0, deleted=0;

  ...

  while (p<=n-2)

    if (a[p+1] == a[p])

        deleteAtPos(p+1), deleted++;

      else

        p++;

 

  return deleted;

  }

 

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

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

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

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

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

e)        None of the above

 

 

int COList::deleteDupes(void)

  {

  int p, deleted=0;

 

  do

    {

    p = -1;

 

    ...

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

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

        {

        p = i+1;

        this->deleteAtPos(p);

        deleted++;

        break;

        }

 

    } while (p != -1);

 

  return deleted;

  }

 

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

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

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

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

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

e)        None of the above

 

 

bool COList::deleteAtPos(int p)

  {

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

      return false;

    else

      {

      ...

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

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

 

      a[n-1] = UNDEFINED;

      n--;

      return true;

      }

  }

 

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

a)        if (this->a[i] == this->a[i-1])

b)        if (this->a[i] == this->a[i+1])

c)        if (this->a[i] == this->a[i+2])

d)        if (this->a[i] == this->a[i])

e)        None of the above

 

 

bool COList::hasDistinct(void) const

  {

  for (int i=0; i<=this->n-2; i++)

    ...

    if (this->a[i] == this->a[i+1])

      return false;

 

  return true;

  }

 

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

a)        for (int i=0; i<=MAX_SIZE; i++)

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

c)        for (int i=2; i<=MAX_SIZE-1; i++)

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

e)        None of the above

 

 

ostream & operator << (ostream &bob,

  const COList &aList)

  {

  bob << "a[" << aList.n << "]: ";

  ...

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

    bob << aList.a[i] << ' ';

 

  return bob;

  }

 

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

a)        thisList.insert(x);

b)        tempList.insert(x);

c)        thatList.insert(x);

d)        firstList.insert(x);

e)        None of the above

 

 

COList COList::operator -(const COList &thatList) const

  {

  COList tempList;

  int x;

 

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

    {

    x = this->a[i];

    if (thatList.searchSequential(x) == UNDEFINED)

      if (tempList.searchSequential(x) == UNDEFINED)

        ...

        tempList.insert(x);

    }

 

  return tempList;

  }

 

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

a)        if (thisList.searchSequential(x) != UNDEFINED)

b)        if (tList.searchSequential(x) != UNDEFINED)

c)        if (tempList.searchSequential(x) != UNDEFINED)

d)        if (thatList.searchSequential(x) != UNDEFINED)

e)        None of the above

 

 

COList COList::operator *(const COList

  &thatList) const

  {

  COList tempList;

  int x;

 

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

    {

    x = this->a[i];

    ...

    if (thatList.searchSequential(x) != UNDEFINED)

      if (tempList.searchSequential(x) == UNDEFINED)

        tempList.insert(x);

    }

 

  return tempList;

  }

 

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

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

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

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

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

e)        None of the above

 

 

int COList::searchSequential(int x) const

  {

  ...

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

    if (x == a[i])

      return i;

 

  return UNDEFINED;

  }

 

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

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

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

c)        for (int i=2; i<=MAX_SIZE-1; i++)

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

e)        None of the above

 

 

COList::COList(const COList &oldList)

  {

  this->n = oldList.n;

  ...

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

    this->a[i] = oldList.a[i];

  }

 

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

a)        if (this->n = thatList.n)

b)        if (this->n === thatList.n)

c)        if (this->n > thatList.n)

d)        if (this->n != thatList.n)

e)        None of the above

 

 

bool COList::operator ==(const COList &thatList) const

  {

  ...

  if (this->n != thatList.n)

    return false;

 

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

    if (this->a[i] != thatList.a[i])

       return false;

 

  return true;

  }

 

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

a)        if (this->n = thatList.n)

b)        if (this->n === thatList.n)

c)        if (this->n > thatList.n)

d)        if (this->n != thatList.n)

e)        None of the above

 

 

bool COList::isEqual(const COList &thatList) const

  {

  ...

  if (this->n != thatList.n)

    return false;

 

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

    if (this->a[i] != thatList.a[i])

       return false;

 

  return true;

  }

 

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

a)        if (thisList.n == thatList.n)

b)        if (thisList.n != thatList.n)

c)        if (thisList.n = thatList.n)

d)        if (thisList.n > thatList.n)

e)        None of the above

 

 

bool isEqual(const COList &thisList, const COList &thatList)

  {

  ...

  if (thisList.n != thatList.n)

    return false;

 

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

    if (thisList.a[i] != thatList.a[i])

       return false;

 

  return true;

  }

 

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

a)        this->initialize();

b)        n = 0;

c)        n-1 = 0;

d)        int n;

e)        None of the above

 

 

COList::COList(char ch)

  {

  ...

  this->initialize();

  int n = rand()%(MAX_SIZE+1);

  (*this).populate(n);

  }

 

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

a)        if (x != y)

b)        if (x = y)

c)        if (x == y)

d)        if (x > y)

e)        None of the above

 

 

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

  {

  ...

  if (x != y)

    {

    int temp = x;

    x = y;

    y = temp;

    }

  };

 

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

a)        k = 0;

b)        swaps++;

c)        return swaps;

d)        swaps = 0;

e)        None of the above

 

 

void COList::sortBubble(void)

  {

  int swaps;

  do

    {

    swaps = 0;

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

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

        {

        swap(a[i], a[i+1]);

        ...

        swaps++;

        }

 

    } while (swaps!=0);

 

  }

 

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

a)        pick = rand()%n;

b)        pick = rand()%n + 2;

c)        int pick = rand()%n;

d)        int pick = rand() + n;

e)        None of the above

 

 

void COList::shuffle(void)

  {

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

    {

    ...

    int pick = rand()%n;

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

    }

 

  }

 

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

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

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

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

d)        for (int i=1; i<=m; i++)

e)        None of the above

 

 

void COList::populate(int n)

  {

  ...

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

    {

    int x = rand()%(MAX_VALUE+1);

    (*this).insert(x);

    }

  }

 

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

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

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

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

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

e)        None of the above

 

 

bool COList::isSorted(void) const

  {

  ...

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

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

      return false;

 

  return true;

  }

 

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

a)        a[i] = UNDEFINED;

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

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

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

e)        None of the above

 

 

void COList::initialize(void)

  {

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

    ...

    a[i] = UNDEFINED;

 

  n = 0;

  }

 

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

a)        a[n] = x;

b)        a[1] = x;

c)        a[2] = x;

d)        a[3] = x;

e)        None of the above

 

 

bool COList::insert(int x)

  {

  if (0 == n)

      {

      ...

      a[n] = x;

      n++;

      return true;

      };

 

  if (MAX_SIZE == n)

      return false;

 

  if (n > 0)

      {

      a[n] = x;

      n++;

 

      for (int i=n-1; i>=1; i--)

        {

        if (a[i] >= a[i-1]) return true;

        swap(a[i], a[i-1]);

        }

 

      return true;

      }

 

  return false;

 

  }

 

36.   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; i++)

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

e)        None of the above

 

 

void COList::display(void) const

  {

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

  ...

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

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

 

  cout << endl;

  }

 

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

a)        initialize();

b)        n=0;

c)        n=1;

d)        n=2;

e)        None of the above

 

 

COList::COList(void)

  {

  ...

  initialize();

  }