1.
Identify the missing statement
in the function that follows.
a.
if (n/i == 0)
b.
if (n*i == 0)
c.
if (n%i == 0)
d.
if (n*i == 0)
e.
none of the above
bool
isPrime1(int n)
{
if (1 == n) return false;
if (2 == n) return true;
for (int i=2; i<=n-1; i++)
if (n%i == 0)
...
return false;
return true;
}
2.
Identify the missing statement
in the function that follows.
a.
for (int i=2;
i<=n/2; i++)
b.
for (int i=2; i=n/2; i++)
c.
for (int i=2; i<n/n; i++)
d.
for (int i=2; i>=n/2; i++)
e.
none of the above
bool
isPrime2(int n)
{
if (1 == n) return false;
if (2 == n) return true;
for (int
i=2; i<=n/2; i++)
...
if (n%i == 0)
return false;
return true;
}
3.
Identify the missing statement
in the function that follows.
a.
for (int i=2; i>sqrt(n); i++)
b.
for (int i=2; i>=sqrt(n); i++)
c.
for (int i=2; i=sqrt(n); i++)
d.
for (int i=2;
i<=sqrt(n); i++)
e.
none of the above
bool
isPrime3(int n)
{
if (1 == n) return false;
if (2 == n) return true;
for (int
i=2; i<=sqrt(n); i++)
...
if (n%i == 0)
return false;
return true;
}
For the questions that follow, assume
the following declarations:
const int ARRAY_SIZE = 10;
const int UNDEFINED = -9999;
const int MAX_VALUE = 99;
const int TEST_COUNT = 10;
class CSortedList
{
private:
int m_array[ARRAY_SIZE];
int m_count;
int m_min;
int m_max;
public:
CSortedList(void);
CSortedList(char ch);
CSortedList(int n);
void display(void) const;
bool insert(int x);
bool isSorted(void)
const;
void sortBubble1(void);
void sortBubble2(void);
void
sortInsertion1(void);
void shuffle(void);
int searchSeq(int x)
const;
int searchBinary(int x)
const;
int searchBinaryLM(int x)
const;
void displayDistinct(void)
const;
void
displayDistinctWithCounts(void) const;
bool remove(int x);
int getAt(int p);
int getCount(void);
bool operator ==(CSortedList
list2);
};
4.
Identify the missing statement
in the function that follows.
a.
int
i=0;
b.
int
i=this->m_count;
c.
i++;
d.
for (int i=0; i<this->m_count;
i--)
e.
none of the
above
bool
CSortedList::operator ==(CSortedList list2)
{
if (this->m_count != list2.m_count)
return false;
for (int
i=0; i<this->m_count; i++)
...
if (this->m_array[i] !=
list2.m_array[i])
return false;
return true;
}
5.
Identify the missing statement
in the function that follows.
a.
while (true)
b.
while
(!sorted)
c.
while (false)
d.
while (sorted=true)
e.
none of the above
void CSortedList::sortInsertion1(void)
{
int i, temp;
bool sorted = false;
while
(!sorted)
...
{
sorted = true;
for (i=m_count-2; i>=0; i--)
{
if (m_array[i]
> m_array[i+1])
{
temp = m_array[i];
m_array[i] = m_array[i+1];
m_array[i+1] = temp;
sorted = false;
}
}
}
}
6.
Identify the missing statement
in the function that follows.
a.
if (p < m_count)
b.
if (p >= 0)
c.
if ((p < m_count) || (p >= 0))
d.
if ((p <
m_count) && (p >= 0))
e.
none of the above
int
CSortedList::getAt(int p)
{
if ((p <
m_count) && (p >= 0))
...
return this->m_array[p];
else
return
UNDEFINED;
};
7.
Identify the missing statement
in the function that follows.
a.
return 1;
b.
return 0;
c.
return this->m_count;
d.
return this->m_count-1;
e.
none of the above
int
CSortedList::getCount(void)
{
return
this->m_count;
...
};
8.
Identify the missing statement
in the function that follows.
a.
this->m_count
= 0;
b.
count = 0;
c.
m_count
= 1;
d.
this->m_count = 2;
e.
none of the above
CSortedList::CSortedList(int
n)
{
this->m_count
= 0;
...
int x;
for (int i=0; i<n; i++)
{
x = rand()%(MAX_VALUE+1);
this->insert(x);
}
}
9.
Identify the missing statement
in the function that follows.
a.
m_array[i]
= m_array[i-1];
b.
m_array[i]
= m_array[i+1];
c.
m_array[i+1] = m_array[i];
d.
m_array[i-1] = m_array[i];
e.
none of the above
bool
CSortedList::remove(int x)
{
int p = searchSeq(x);
if (-1 == p)
return false;
for (int i=p; i<=m_count-2; i++)
m_array[i] = m_array[i+1];
...
this->m_count--;
return true;
}
10.
Identify the missing statement
in the function that follows.
a.
p++;
b.
p--;
c.
p=p+1;
d.
p=p+2;
e.
none of the above
int
CSortedList::searchBinaryLM(int x) const
{
int p;
p = this->searchBinary(x);
if (-1 == p)
return -1;
while((p>0) &&
(this->m_array[p] ==
this->m_array[p-1]))
p--;
...
return p;
}
11.
Identify the missing statement
in the function that follows.
a.
mid = m_count/2;
b.
mid = lower/2;
c.
mid = upper/2;
d.
mid = (lower-upper)/2;
e.
none of the
above
int
CSortedList::searchBinary(int x) const
{
int lower, upper, mid;
lower = 0;
upper = this->m_count-1;
while (lower <= upper)
{
mid = (lower+upper)/2;
...
if (x == this->m_array[mid])
return mid;
if (x < this->m_array[mid])
upper = mid-1;
if (x > this->m_array[mid])
lower = mid+1;
}
return -1;
}
12.
Identify the missing statement
in the function that follows.
a.
if (x <
this->m_array[i])
b.
if (x < i)
c.
if (x <= i)
d.
if (x > this->m_array[i])
e.
none of the above
int
CSortedList::searchSeq(int x) const
{
for (int i=0; i<=this->m_count-1; i++)
{
if (x == m_array[i])
return i;
if (x < this->m_array[i])
...
break;
}
return -1;
}
13.
Identify the missing statement
in the function that follows.
a.
this->m_count = 1;
b.
this->m_count
= 0;
c.
this->m_count = 2;
d.
this->m_count = 3;
e.
none of the above
CSortedList::CSortedList(void)
{
this->m_count
= 0;
...
this->m_min = UNDEFINED;
this->m_max = UNDEFINED;
}
14.
Identify the missing statement
in the function that follows.
a.
this->m_count
= 0;
b.
this->m_count = 1;
c.
this->m_count = 2;
d.
this->m_count = 3;
e.
none of the above
CSortedList::CSortedList(char
ch)
{
this->m_count
= 0;
...
int x, n;
if ('r' == ch)
{
n = rand()%ARRAY_SIZE+1;
for (int i=0; i<n; i++)
{
x =
rand()%(MAX_VALUE+1);
this->insert(x);
}
}
}
15.
Identify the missing statement
in the function that follows.
a.
cout
<< i << ' ';
b.
cout
<< this->m_array[i] << ' ';
c.
cout
<< this->m_array << ' ';
d.
cout
<< this->m_array[i+1] << ' ';
e.
none of the above
void CSortedList::display(void) const
{
cout << "SortedList(" << this->m_count
<< ")= ";
for (int i=0; i<this->m_count; i++)
cout << this->m_array[i] << ' ';
...
cout << endl;
}
16.
Identify the missing statement
in the function that follows.
a.
x = this->m_array[m_count];
b.
this->m_array[m_count] = 0;
c.
this->m_array[m_count]++;
d.
this->m_array[m_count]
= x;
e.
none of the above
bool
CSortedList::insert(int x)
{
if (ARRAY_SIZE == this->m_count)
return false;
else
{
this->m_array[m_count] = x;
...
this->m_count++;
this->sortInsertion1();
return true;
}
}
17.
Identify the missing statement
in the function that follows.
a.
for (j=0; j<=this->m_count-2;
j--)
b.
for (j=0;
j<=this->m_count-2; j++)
c.
for (j=0; j=this->m_count-2;
j++)
d.
for (j=0; j>=this->m_count-2;
j++)
e.
none of the above
void CSortedList::sortBubble1(void)
{
int j;
for (int i=1; i<=this->m_count-1; i++)
{
for (j=0; j<=this->m_count-2; j++)
...
{
if (this->m_array[j]
> this->m_array[j+1])
{
int temp = this->m_array[j];
this->m_array[j] = this->m_array[j+1];
this->m_array[j+1] = temp;
}
}
}
}
18.
Identify the missing statement
in the function that follows.
a.
while (sorted)
b.
while (sorted=false)
c.
while
(!sorted)
d.
while (sorted=true)
e.
none of the above
void CSortedList::sortBubble2(void)
{
int i, temp;
bool sorted = false;
while
(!sorted)
...
{
sorted = true;
for (i= 0; i<=m_count-2;
i++)
{
if (m_array[i]
> m_array[i+1])
{
temp = m_array[i];
m_array[i] = m_array[i+1];
m_array[i+1] = temp;
sorted = false;
}
} //end for
}//end while
}
19.
Identify the missing statement
in the function that follows.
a.
for (int i=0; i<=this->m_count-2;
i--)
b.
for (int i=0; i<this->m_count-2;
i++)
c.
for (int i=0; i<=this->m_count;
i++)
d.
for (int i=0;
i<=this->m_count-2; i++)
e.
none of the above
bool CSortedList::isSorted(void) const
{
for (int
i=0; i<=this->m_count-2; i++)
...
if (this->m_array[i] >
this->m_array[i+1])
return false;
return true;
}
20.
Identify the missing statement
in the function that follows.
a.
for (int i=0; i<=this->m_count-1;
i--)
b.
for (int i=0; i>=this->m_count-1;
i++)
c.
for (int i=0; i<=this->m_count-2;
i++)
d.
for (int i=0; i<=this->m_count+1;
i++)
e.
none of the
above
void CSortedList::shuffle(void)
{
int picked, temp;
for (int
i=0; i<=this->m_count-1; i++)
...
{
picked = rand()%this->m_count;
temp = this->m_array[0];
this->m_array[0] = this->m_array[picked];
this->m_array[picked] =
temp;
}
}
For the questions that follow, assume
the following declarations:
const int MAX_SIZE = 20;
const int UNDEFINED = -9999;
const int MAX_VALUE = 50;
const int TEST_COUNT = 20;
const int INFINITY =
32000;
const int sValues[] = {55, 12, 34, 71, 98,
45, 20};
const int S_SIZE = sizeof(sValues)/sizeof(sValues[0]);
class CNode
{
private:
int m_key;
CNode *m_next;
public:
CNode(void);
void display(void);
CNode(int x);
CNode(char ch);
friend class CDSortedList;
friend ostream & operator <<
(ostream & bob, const
CDSortedList &aList);
friend ostream & operator <<
(ostream & bob, const
CNode &aNode);
};
class CDSortedList
{
private:
int m_count;
CNode *m_first;
void init(void);
public:
CDSortedList(void);
void display(void);
bool insert(int x);
CDSortedList(int n);
CDSortedList(char ch);
void removeAll(void);
~CDSortedList(void);
friend void add(const
CDSortedList &aList,
const
CDSortedList &bList, CDSortedList &cList);
friend ostream & operator
<<
(ostream &
bob, const CDSortedList &aList);
CNode* addByPos(int pos);
void displayRev(void);
void shuffle(void);
void analyze(void);
};
21.
Identify the output given by
the code segment that follows.
a.
4
b.
28
c.
24
d.
14
e.
none of the above
const int sValues[] = {55, 12, 34, 71, 98,
45, 20};
cout << sizeof(sValues) << endl;
22.
Identify the output given by
the code segment that follows.
a.
4
b.
28
c.
24
d.
14
e.
none of the above
const int sValues[] = {55, 12, 34, 71, 98,
45, 20};
cout << sizeof(sValues[0]) << endl;
23.
Identify the missing statement
in the function that follows.
a.
p1 = this;
b.
p1 = i;
c.
p1 = &i;
d.
p1 =
this->addByPos(i+1);
e.
none of the above
void CDSortedList::analyze(void)
{
int countUp=0, countDn=0, countEq=0;
for (int i=0; i<this->m_count-1; i++)
{
CNode *p1, *p2;
p1 = this->addByPos(i);
...
p2 = this->addByPos(i+1);
if (p1->m_key < p2->m_key)
countUp++;
if (p1->m_key > p2->m_key)
countDn++;
if (p1->m_key == p2->m_key)
countEq++;
}
cout << "Count Up = " << countUp <<
endl;
cout << "Count Dn = " << countDn <<
endl;
cout << "Count Eq = " << countEq <<
endl;
}
24.
Identify the missing statement
in the function that follows.
a.
p1 =
this->addByPos(pos1);
b.
p1 = pos1;
c.
p1 = @pos1;
d.
p1 = this(pos1);
e.
none of the above
void CDSortedList::shuffle(void)
{
for (int i=0; i<this->m_count*100; i++)
{
int pos1, pos2;
pos1 = rand()%this->m_count;
pos2 = rand()%this->m_count;
CNode *p1, *p2;
...
p1 = this->addByPos(pos1);
p2 =
this->addByPos(pos2);
int temp = p1->m_key;
p1->m_key = p2->m_key;
p2->m_key = temp;
}
}
25.
Identify the missing statement
in the function that follows.
a.
cout << q;
b.
cout << this->q;
c.
cout << *q;
d.
cout << i;
e.
none of the above
void CDSortedList::displayRev(void)
{
cout << "SortedRevr[" << this->m_count
<< "]= ";
for (int i = this->m_count-1; i>=0; i--)
{
CNode *q = this->addByPos(i);
...
cout << *q;
}
cout << endl;
}
26.
Identify the missing statement
in the function that follows.
a.
p = m_next;
b.
p = p->m_next;
c.
p = p++;
d.
p = p--;
e.
none of the above
CNode* CDSortedList::addByPos(int pos)
{
if (this->m_count == 0)
return NULL;
if (pos < 0)
return NULL;
if (pos >= this->m_count)
return NULL;
CNode *p;
p = this->m_first->m_next;
while (pos > 0)
{
pos--;
...
p = p->m_next;
}
return p;
}
27.
Identify the missing statement
in the function that follows.
a.
return;
b.
return bob;
c.
return -1;
d.
return NULL;
e.
none of the above
ostream & operator << (ostream & bob,
const CNode &aNode)
{
bob << aNode.m_key << ' ';
...
return
bob;
}
28.
Identify the missing statement
in the function that follows.
a.
bob << p;
b.
bob << endl;
c.
bob << p++;
d.
bob << p*;
e.
none of the
above
ostream & operator << (ostream & bob,
const CDSortedList &aList)
{
bob << "SortedList[" << aList.m_count
<< "]= ";
CNode *p;
p = aList.m_first->m_next;
while (p->m_next != NULL)
{
...
bob << *p;
p = p->m_next;
}
bob << endl;
return bob;
}
29.
Identify the missing statement
in the function that follows.
a.
delete *cia1;
b.
delete
cia1;
c.
delete cia2;
d.
delete *cia2;
e.
none of the above
CDSortedList::~CDSortedList(void)
{
cout << "Destructor was called\n";
CNode *cia1, *cia2;
cia1 = this->m_first;
while (cia1 != NULL)
{
cia2 = cia1->m_next;
...
delete cia1;
cia1 = cia2;
}
}
30.
Identify the missing statement
in the function that follows.
a.
delete
cia1;
b.
delete *cia1;
c.
delete cia2;
d.
delete *cia2;
e.
none of the above
void CDSortedList::removeAll(void)
{
CNode *cia1, *cia2;
cia1 = this->m_first;
while (cia1 != NULL)
{
cia2 = cia1->m_next;
...
delete cia1;
cia1 = cia2;
}
this->init();
}
31.
Identify the missing statement
in the function that follows.
a.
m_next = p2;
b.
p1 = p2;
c.
p2->m_next = p1;
d.
p1->m_next
= p2;
e.
none of the above
void CDSortedList::init(void)
{
CNode *p1 = new CNode;
CNode *p2 = new CNode;
this->m_first = p1;
...
p1->m_next
= p2;
p2->m_next = NULL;
p1->m_key = -INFINITY;
p2->m_key = +INFINITY;
this->m_count = 0;
}
32.
Identify the missing statement
in the function that follows.
a.
insert(sValues);
b.
this->insert(sValues[i]);
c.
insert(i);
d.
this->insert(sValues[0]);
e.
none of the above
CDSortedList::CDSortedList(char ch)
{
this->init();
if (('r' == ch) || ('R' == ch))
{
int n =
rand()%(MAX_SIZE+1);
for (int i=0; i<n; i++)
this->insert(rand()%(MAX_VALUE+1));
}
if (('c' == ch) || ('C' == ch))
{
for (int i=0; i<S_SIZE; i++)
...
this->insert(sValues[i]);
}
}
33.
Identify the missing statement
in the function that follows.
a.
this->init();
b.
this->init(0);
c.
this->init(NULL);
d.
init(NULL);
e.
none of the above
CDSortedList::CDSortedList(int n)
{
...
this->init();
for (int i=0; i<n; i++)
this->insert(rand()%(MAX_VALUE+1));
}
34.
Identify the missing statement
in the function that follows.
a.
p = p2;
b.
p2 = p;
c.
this->p->m_next = p2;
d.
p->m_next =
p2;
e.
none of the above
bool CDSortedList::insert(int x)
{
if (this->m_count >= MAX_SIZE)
return false;
CNode *p, *p1, *p2;
p = new CNode(x);
if (NULL == p)
return false;
p1 = this->m_first;
p2 = p1->m_next;
while(true)
{
if ((x >= p1->m_key) &&
(x <= p2->m_key))
{
p1->m_next =
p;
...
p->m_next = p2;
this->m_count++;
return true;
}
else
{
p1 = p1->m_next;
p2 = p1->m_next;
}
}
}
35.
Identify the missing statement
in the function that follows.
a.
p = m_next;
b.
p->m_next = p;
c.
this->p = p->m_next;
d.
p = NULL;
e.
none of the
above
void CDSortedList::display(void)
{
cout << "List[" << this->m_count <<
"] = \n";
CNode *p;
p = this->m_first;
while (p != NULL)
{
p->display();
...
p = p->m_next;
cout << endl;
}
cout << endl;
}
36.
Identify the missing statement
in the function that follows.
a.
this->init(0);
b.
this->init();
c.
this->init(NULL);
d.
init(NULL);
e.
none of the above
CDSortedList::CDSortedList(void)
{
...
this->init();
}
37.
Identify the missing statement
in the function that follows.
a.
m_next = p;
b.
this->m_next = p;
c.
this->m_next = *p;
d.
this->m_next
= NULL;
e.
none of the above
CNode::CNode(char ch)
{
if (('r' == ch) || ('R' == ch))
{
this->m_key =
rand()%(MAX_VALUE+1);
...
this->m_next = NULL;
}
else
{
this->m_key = UNDEFINED;
this->m_next
= NULL;
}
}
38.
Identify the missing statement
in the function that follows.
a.
m_next = p;
b.
this->m_next = p;
c.
this->m_next
= NULL;
d.
this->m_next = *p;
e.
none of the above
CNode::CNode(int x)
{
this->m_key = x;
...
this->m_next
= NULL;
}
39.
Identify the missing statement
in the function that follows.
a.
m_next = p;
b.
this->m_next
= NULL;
c.
this->m_next = p;
d.
this->m_next = *p;
e.
none of the above
CNode::CNode(void)
{
this->m_key = UNDEFINED;
...
this->m_next
= NULL;
}
40.
Identify the missing statement
in the function that follows.
a.
cout << this << " ";
b.
cout << *this << " ";
c.
cout <<
this->m_key << " ";
d.
bob << this->m_key << " ";
e.
none of the above
void CNode::display(void)
{
...
cout <<
this->m_key << " ";
}