CSC 171 Sample Quiz 6 C Language Programming for Engineers

CSC 171 C Language Programming for Engineers
Student Name Answer Key
Sample Quiz 6
Section ___ Date _______
Arrays and Object - Oriented Programming
Select the correct answer or enter the correct answer in the space provided.
(1)
Assuming the following array declaration and initialization, write an expression that
references the array element with a value of 6 .
int x[2][3] = {{2, 4, 6}, {8, 10, 12}};
(a) x[2][2] (b) x[3][2] (c) x[2][1]
(2)
Give the C ++ keyword that is used to restrict access to data attributes and member
functions.
(a) private
(c) catch
(3)
(d) x[0][2] (e) x[1][2]
(b) friend
(d) public
For the following class declaration, identify the extractor member function that can be
used to return the value of a data attribute within the class.
class Complex
{
private:
double x, y; // z = x + iy ( complex number )
public:
Complex(double re, double im) // constructor function
{ x = re; y = im;
}
double getR() const
{ return x; }
// extractor member function
void setI(double im)
{ y = im; }
// mutator member function
friend ostream& operator<<(ostream& out, const complex& z)
{
out << z.x << " + " << z.y << "i";
return out;
}
};
© Copyright 2012 by P.E.P.
CSC 171 C Language Programming for Engineers
Student Name Answer Key
(4)
Sample Quiz 6
Section ___ Date _______
For the following class declaration, identify the mutator member function that can be
used to return the value of a data attribute within the class.
class Complex
{
private:
double x, y; // z = x + iy ( complex number )
public:
Complex(double re, double im) // constructor function
{ x = re; y = im;
}
double getR() const
{ return x; }
// extractor member function
void setI(double im)
{ y = im; }
// mutator member function
friend ostream& operator<<(ostream& out, const complex& z)
{
out << z.x << " + " << z.y << "i";
return out;
}
};
(5)
For the following class declaration, identify the constructor member function that
can be used to declare and initialize instances of the class.
class automobile
{
private:
int doors;
string style; // coupe, hatchback, sedan, ...
public:
automobile(int d, string s) // class constructor
{ doors = d;
style = s;
}
string getStyle() const
{ return style; }
// extractor member function
void setDoors(int d)
{ doors = d; }
// mutator member function
friend ostream& operator<<(ostream& out, const automobile& a)
{ out << a.doors << " door " << a.style;
return out;
}
};
© Copyright 2012 by P.E.P.
CSC 171 C Language Programming for Engineers
Student Name Answer Key
(6)
Sample Quiz 6
Section ___ Date _______
The following Unified Modeling Language ( UML ) diagram represents an object called
Foo . Give the name of all public data members and public member functions for the
object Foo .
Foo
-a: int
+b: double
+Foo()
+c() : char
-d() : bool
public member function:
public data member:
public member function:
(7)
Foo()
b
c()
Explain in your own words what value is stored in variable temp below.
vector<double> X;
vector<double>::iterator point;
double temp = 0.0;
// ... after X is initialized ...
if ( X.size() > 0 )
for ( point = X.begin(); point != X.end(); point++)
temp += *point;
temp will contain the sum of the elements in the vector X
(8)
Given the following example of code, which expressions evaluate to the address of
memory that contains an int with a value of 6 ?
int x = 6,
*ptr = &x,
a[2] = {6};
answer:
(9)
ptr and a
For the following declaration, give the command necessary to dynamically allocate an
array of 10 int values with the variable m pointing at the start of the array.
int *m;
answer:
(10)
m = new int[10];
Give the name of the C ++ keyword that provides support for combining data attributes
and member functions in one construction.
(a) private
(c) class
© Copyright 2012 by P.E.P.
(b) friend
(d) public