Вы находитесь на странице: 1из 4

University of Zululand Department of Computer Science

SCPS211 : Data Structures and Algorithm Analysis


C++ Programming Introduction

Test 1 MEMO

03 March 2016

Surname & Initials:

90 minutes
StudNo:

1. Suppose x is 5 and y is 7. Choose the value of the


following expression:
(x != 7) && (x <= y)

a. false
c. true

b. 0
d. null

6. Assume all variables are properly declared.


What is the output of the following C++ code?
num = 100;
while (num <= 150)
num = num + 5;
cout << num << endl;

a. 150
c. 160
2. Suppose that x is an int variable. Which of the
following expressions always evaluates to true?
a. (x >0) &&(x <= 0)
c. (x >=0)||(x==0)

b. (x >0)||(x <= 0)
d. (x>0)&&(x == 0)

b. 155
d. 165

7. What is the output of the following C++ code?


int j;
for (j = 10; j <= 10; j++)
cout << j << " ";
cout << j << endl;

10 11
3. What is the output of the following C++ code?
int x =
int y =
int z;
if (x >
z =
else
z =
cout <<
<<

35;
45;
y)
x + y;
y x;
x << " " << y << " "
z << endl;

8. Which of the following is true about a do...while


loop?
a. The body of the loop is executed at least once.
b. The logical expression controlling the loop is
evaluated before the loop is entered.
c. The body of the loop may not execute at all.
d. It cannot contain a break statement

35 45 10
4. Which, if any, of the following will cause a syntax
error if you are attempting to compare x to 5?
a. if (x == 5)
c. if (x = 5)

b. if (x >= 5)
d. none of these

5. What is the value of x after the following statements


execute?
int x;
x = (5 <= 3 && 'A' < 'F') ? 3 : 4

9. Consider the declaration:


enum sports {BASKETBALL, FOOTBALL,
HOCKEY, BASEBALL, SOCCER};
Which of the following statements is true?
a. SOCCER-- = BASEBALL
b. BASEBALL++ = SOCCE
c. FOOTBALL <= SOCCER
10. In C++, you can create aliases to a previously defined
data type by using the ____ statement.
a. alias
c. typedef

b. using
d. namespace

11. Which of the following statements declares the 14. Which of the following class definitions is correct in
studentGrade variable?
C++?
a. enum studentGrade {A, B, C, D, F};
a. class studentType
b. enum int {A, B, C, D, F} studentGrade;
{ public:
c. enum studentGrade {A, B, C, D, F}
grades;
d. enum grades {A, B, C, D, F}
studentGrade;

12. Suppose that gamma is an array of 50 components of


type int and j is an int variable. Which of the following
for loops sets the index of gamma out of bounds?
a. for (j = 0; j <= 49; j++)
cout << gamma[j] << " ";
b. for (j = -1; j < 50; j++)
cout << gamma[j] << " ";
c. for (j = 0; j < 50; j++)
cout << gamma[j] << " ";
13. Which of the following statements is used to simplify
the accessing of all globalType namespace members?
a. using globalType;
b. using namespace globalType:all
c. using namespace globalType::all

d. using namespace globalType;

//For questions 15, 16 & 17 refer to the


//following class definition:
class rectangleType
{
public:
void setLengthWidth(double x, double y);
//Postcondition: length = x; width = y;
void print() const;
//Output length and width;
double area();
//Calculate & return area of rectangle;
double perimeter();
//Calculate and return the parameter;
rectangleType();
//Postcondition: length = 0; width = 0;
rectangleType(double x, double y);
//Postcondition: length = x; width = y;
private:
double length;
double width;
};

//Assume the following declaration


rectangleType bigRect(14.0,10.0);

void setData(string, double, int);


private: string name;
};

b. class studentType
{ public:
void setData(string, double, int);
void print() const;
private:
string name;
double gpa;
}

c. class studentType
{ public
void setData(string, double, int);
private string name;
};
d. studentType class
{

public:
void setData(string, double, int);
private: string name;

};

15.

Which of the following statements is correct?

a. bigRect.setLengthWidth();

b. bigRect.setLengthWidth(3.0,2.0);
c. bigRect.length = 14.0;

16. Which of the following statements is correct?


a. rectangleType.print();
b. rectangleType::print();

c. bigRect.print();
d. bigRect::print();

17. A class object can be ____. That is, it is created


each time the control reaches its declaration, and
destroyed when the control exits the surrounding block
a. static

b. automatic
c. local
d. public

SECTION B (15 marks)

22. Assuming that these declarations have been made:

For questions 18 to 20, briefly explain each of


the given concepts.

int x, y, *p, *q;


indicate whether each of the following assignment
statements is legal or illegal. If illegal, give a
reason or explanation why .
a) p = *&x;
Illegal

18. A data structure

A data structure is a particular way of


storing and organizing data in a computer
so that it can be used efficiently

// cannot convert int value (on right) to pointer int*


(on the left)
b) y = *&q;

Illegal

// cannot convert pointer int* (on right) to int value


(on the left)
19. A dangling reference problem

A dangling reference problem arises when


an object is deleted without modifying the
value of the pointer. The pointer still points
to the memory location of the deallocated
memory.
Attempting to dereference the pointer will
cause an error.
20. A destructor

A destructor is a code construct that is


automatically called when its associated
object is deleted
It can specify special processing to occur,
such as the deletion of pointer-linked
memory objects

c) p = &x;

d) p = &*&y;

e) x = &*q;

Legal

Legal

Illegal

// cannot convert pointer int* (on right) to int value


(on the left)
f) p =*&q;

g) p = &*q;

Legal

Legal

21. Give one difference between a class from a


struct.

By default,the members of a struct are


public.

h) q = **&p;

// cannot convert int (on right) value to pointer int*


(on the left)
i) x = *&y;

By default, the members of a class are


private.

Illegal

Legal

SECTION C (10 marks)


23. Consider a program that uses two parallel double arrays low[12] and high[12] to store the lowest and the
highest temperatures for each month of the year. The program uses a number of functions to compute the
following values: the average high temperature, the average low temperature, and the highest temperature
and the lowest temperature for the year.
Write the C++ implementations of the following two functions:
(a) The function averageLow: This function calculates and returns the average low temperature for the year.

double averageLow(double low[], int noOfMonths)


{
int i;
int sum = 0;
for (i = 0; i < noOfMonths; i++)
sum = sum + low[i];
return sum/noOfMonths;
}
(4)

(b) Function indexHighTemp: This function determines and returns the index of the highest high temperature
for the year.
(6)

int indexHighTemp(double high[], int noOfMonths)


{
int i;
int highIndex = 0;
for (i = 1; i < noOfMonths; i++)
if (high[ [highIndex] < high[ [i])
highIndex = i ;
return highIndex;
}