Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Quizzes CS 371p
/* -----------------------------------------------------------------------
1. Show the cycle for 3.
What is the cycle length?
[Collatz]
(2 pts)
3, 10, 5, 16, 8, 4, 2, 1
8
*/
/* -----------------------------------------------------------------------
2. Which of the following is true? Maybe more than one.
[Collatz]
(2 pts)
a. (n / 2), with n even, always produces an even
b. (n / 2), with n even, always produces an odd
c. (3n + 1), with n odd, always produces an even
d. (3n + 1), with n odd, always produces an odd
c.
*/
/* -----------------------------------------------------------------------
1. List any TWO pieces of advice.
[The Harvard Guide to Collegiate Happiness]
(2 pts)
meet the faculty
take a mix of courses
study in groups
write, write, write
speak another language
manage time
participate in extracurricular activities
*/
/* -----------------------------------------------------------------------
2. List any TWO pieces of advice.
[Advice for Computer Science College Students]
(2 pts)
write
learn C
learn microeconomics
excel in non-CS classes
take programming-intensive classes
stop worrying about jobs going to India
get a good summer internship
*/
/* -----------------------------------------------------------------------
1. What is the output of the following program?
[Collatz]
(2 pts)
5
11
*/
#include <iostream> // cout, endl
using namespace std;
int f (int n) {
return n + (n >> 1) + 1;}
int main () {
cout << f(3) << endl;
cout << f(7) << endl;
return 0;}
/* -----------------------------------------------------------------------
2. In the context of Project #1: Collatz, what is f() computing?
[Collatz]
(2 pts)
For odd n it's computing (3n + 1) / 2.
(3n + 1) / 2
3n/2 + 1/2
n + n/2 + 1/2
n + n/2 + 1, because n is odd
n + (n >> 1) + 1
*/
/*
CS371p: Quiz #4 (5 pts)
*/
/* -----------------------------------------------------------------------
1. What does the Sapir-Whorf hypothesis assert?
[Sec. 1.2.3, Pg. 5]
(1 pt)
It may be possible for an individual working in one language to
imagine thoughts or utter ideas that cannot in any way be translated,
or even understood by individuals operating in a different linguistic
framework.
*/
/* -----------------------------------------------------------------------
2. Given positive integers, b and e, let m = e / 2. If b < m, then
max_cycle_length(b, e) = max_cycle_length(m, e). True or False?
[Collatz]
(1 pt)
True
Consider b = 10, e = 100.
Then m = 100 / 2 = 50.
max_cycle_length(10, 100) = max_cycle_length(50, 100)
All the numbers in the range [10, 49] can be mapped to numbers in the
range [50, 100] by one or more doublings, so none of the numbers in the
range [10, 49] could have a larger cycle length than the numbers in the
range [50, 100].
*/
/* -----------------------------------------------------------------------
3. Describe the difference between a unit test and an acceptance test.
(2 pts)
a unit test tests the return value and the side effects of an individual
function or method
an acceptance test tests the input/output behavior of the whole program
*/
/*
CS371p: Quiz #5 (5 pts)
*/
/* -----------------------------------------------------------------------
1. Many languages permit a group of objects (e.g types, functions, etc.)
working together to be combined into a unit. What is that feature
called in C++?
[Sec. 2.1, Pg. 27]
(1 pt)
namespace
*/
/* -----------------------------------------------------------------------
2. Which of the following practices demonstrates effective pair
programming?
[All I Really Needed to Know about Pair Programming I Learned in
Kindergarten]
(1 pt)
a. Each partner writing separate parts.
b. Each partner writing both parts and then submitting the best.
c. Each partner writing both parts and then submitting the best
combination.
d. Sharing a monitor and keyboard while coding.
e. One partner writing the interface and tests, the other the
implementation.
d.
*/
/* -----------------------------------------------------------------------
3. In C++ are assertions enabled or disabled by default?
How do you enable them or disable them?
(2 pts)
enabled
compile with -NDEBUG
*/
/*
CS371p: Quiz #6 (5 pts)
*/
/* -----------------------------------------------------------------------
1. What is the output of the following program?
You MUST get the newlines right to get full credit.
Write the word "blank" to indicate a blank line.
(4 pts)
m1 f1 f2 m2 m4 m5
m1 f1 m3 m4 m5
*/
class Quiz6 {
static void f (boolean b) {
System.out.print("f1 ");
if (b)
throw new RuntimeException("abc");
System.out.print("f2 ");}
public static void main (String[] args) {
try {
System.out.print("m1 ");
f(false);
System.out.print("m2 ");}
catch (RuntimeException e) {
System.out.print("m3 ");}
finally {
System.out.print("m4 ");}
System.out.print("m5");
System.out.println();
try {
System.out.print("m1 ");
f(true);
System.out.print("m2 ");}
catch (RuntimeException e) {
System.out.print("m3 ");}
finally {
System.out.print("m4 ");}
System.out.println("m5");}}
/*
CS371p: Quiz #7 (5 pts)
*/
/* -----------------------------------------------------------------------
1. What does CRC stand for?
[Sec. 3.5, Pg. 55]
(1 pt)
component, responsibility, collaborators
*/
/* -----------------------------------------------------------------------
2. Some programming languages provide a way to specify that a data field
is immutable. How does Java do that? How does C++?
[Sec. 4.3.2, Pg. 83]
(1 pt)
final, const
*/
/* -----------------------------------------------------------------------
3. In C++, for the '>>' operator that is defined on 'int', what is
the l-value / r-value nature of the return, the left argument, and
the right argument?
On istream?
(2 pts)
r-value, r-value, r-value
l-value, l-value, l-value
*/
/*
CS371p: Quiz #8 (5 pts)
*/
/* -----------------------------------------------------------------------
1. Which of the following is true about strong aggregation, also known as
composition?
[UML Class Diagrams]
(2 pts)
a. filled diamond
lifetime of the part is dependent on lifetime of the whole
b. filled diamond
lifetime of the part is not dependent on lifetime of the whole
c. no diamond
no dependency between lifetime of the part and lifetime of the whole
d. unfilled diamond
lifetime of the part is dependent on lifetime of the whole
e. unfilled diamond
lifetime of the part is not dependent on lifetime of the whole
a.
*/
/* -----------------------------------------------------------------------
2. What is the output for the following?
[Australian Voting]
(2 pts)
1
4
Red
Green
Blue
Orange
1 2 3 4
2 1 3 4
2 3 1 4
1 2 3 4
3 4 1 2
4 3 2 1
Red
Green
*/
/*
CS371p: Quiz #9 (5 pts)
*/
/* -----------------------------------------------------------------------
1. In C++, what four methods constitute the orthodox canonical class form?
[Sec. 5.6.1, Pg. 115]
(2 pts)
default constructor
copy constructor
copy assignment operator
destructor
*/
/* -----------------------------------------------------------------------
2. What is the output of the following program?
(2 pts)
3 4 false
4 4 true
*/
#include <iostream> // cout, endl
int main () {
using namespace std;
cout << boolalpha; // 'bool' outputs as 'true' or 'false'
{
int i = 2;
int j = ++i;
j++;
cout << i << " ";
cout << j << " ";
cout << (&i == &j) << endl;
}
{
int i = 2;
int& j = ++i;
j++;
cout << i << " ";
cout << j << " ";
cout << (&i == &j) << endl;
}
return 0;}
/*
CS371p: Quiz #10 (5 pts)
*/
/* -----------------------------------------------------------------------
1. Twitter recently switched parts of its server infrastructure from Ruby
on Rails to what other language, because it better matched their needs
for long running threads, high performance under heavy loads, and more
robust code via compile-time type checking.
[Why Undergraduates Should Learn the Principles of Programming
Languages]
(1 pt)
Scala
*/
/* -----------------------------------------------------------------------
2. Features of the financial contract language MLFi, for example, were
inspired by what other language that, while not currently pervasive,
is often examined in programming languages courses.
[Why Undergraduates Should Learn the Principles of Programming
Languages]
(1 pt)
Haskell
*/
/* -----------------------------------------------------------------------
3. What was the conclusion of the article?
[Why Undergraduates Should Learn the Principles of Programming
Languages]
(2 pts)
programming languages embody many central concepts of cs
studying programming languages provides insights to all aspects of cs
courses should present those concepts in a broader context than a single
langauge
*/
/*
CS371p: Quiz #11 (5 pts)
*/
/* -----------------------------------------------------------------------
1. What is the output of the following program?
(4 pts)
3 true
3 false
*/
#include <iostream> // cout, endl
int main () {
using namespace std;
cout << boolalpha; // bool outputs as true or false
{
int i = 2;
int* p = &i;
int** pp = &p;
int** qq = pp;
++**qq;
cout << i << " ";
++qq;
cout << (pp == &p) << endl;
}
{
int i = 2;
int* p = &i;
int** pp = &p;
int**& qq = pp;
++**qq;
cout << i << " ";
++qq;
cout << (pp == &p) << endl;
}
return 0;}
/*
CS371p: Quiz #12 (5 pts)
*/
/* -----------------------------------------------------------------------
1. Define the method arraycopy(), iteratively, such that it mimics
System.arraycopy().
(4 pts)
*/
import java.util.Arrays;
class Quiz12 {
public static void arraycopy (long[] a, int i, long[] b, int j, int s) {
if (j < i) {
int n = 0;
while (n != s) {
b[j + n] = a[i + n];
++n;}}
else {
int n = s;
while (n != 0) {
--n;
b[j + n] = a[i + n];}}}
public static void main (String[] args) {
{
final long[] a = {2, 3, 4};
arraycopy(a, 0, a, 1, 2);
assert Arrays.equals(a, new long[]{2, 2, 3});
}
{
final long[] a = {2, 3, 4};
arraycopy(a, 1, a, 0, 2);
assert Arrays.equals(a, new long[]{3, 4, 4});
}}}
/*
CS371p: Quiz #13 (5 pts)
*/
/* -----------------------------------------------------------------------
1. What is the output of the following program?
(4 pts)
A(int) A(int) A(int) A() A()
~A() ~A() ~A() ~A() ~A()
*/
#include <iostream> // cout, endl
using namespace std;
struct A {
A () {cout << "A() ";} // default constructor
A (int) {cout << "A(int) ";} // int constructor
A (const A&) {cout << "A(A) ";} // copy constructor
~A () {cout << "~A() ";} // destructor
A& operator = (const A&) {cout << "=(A) "; return *this;}}; // copy assignment operator
int main () {
{
A a[5] = {2, 3, 4};
cout << endl;
}
cout << endl;
return 0;}
/*
CS371p: Quiz #14 (5 pts)
*/
/* -----------------------------------------------------------------------
1. What is the output of the following program?
(4 pts)
A()
A() A() A() A() A()
=(A) =(A) =(A) =(A) =(A)
~A() ~A() ~A() ~A() ~A()
~A()
*/
#include <algorithm> // fill
#include <iostream> // cout, endl
using namespace std;
struct A {
A () {cout << "A() ";} // default constructor
A (int) {cout << "A(int) ";} // int constructor
A (const A&) {cout << "A(A) ";} // copy constructor
~A () {cout << "~A() ";} // destructor
A& operator = (const A&) {cout << "=(A) "; return *this;}}; // copy assignment operator
int main () {
{
A x;
cout << endl;
A* a = new A[5];
cout << endl;
fill(a, a + 5, x);
cout << endl;
delete [] a;
cout << endl;
}
cout << endl;
return 0;}
/*
CS371p: Quiz #15 (5 pts)
*/
/* -----------------------------------------------------------------------
1. What is the output of the following program?
(4 pts)
A(int)
A(A) A(A)
A(A)
A(A) A(A) ~A()
~A() ~A() ~A() ~A() ~A()
*/
#include <iostream> // cout, endl
#include <vector> // vector
using namespace std;
struct A {
A () {cout << "A() ";} // default constructor
A (int) {cout << "A(int) ";} // int constructor
A (const A&) {cout << "A(A) ";} // copy constructor
~A () {cout << "~A() ";} // destructor
A& operator = (const A&) {cout << "=(A) "; return *this;}}; // copy assignment operator
int main () {
{
A v(3);
cout << endl;
vector<A> x(2, v);
cout << endl;
vector<A> y(1, v);
cout << endl;
y = x;
cout << endl;
}
cout << endl;
return 0;}
/*
CS371p: Quiz #16 (5 pts)
*/
/* -----------------------------------------------------------------------
1. What is the output of the following program?
(4 pts)
-40
12
*/
#include <iostream> // cout, endl
using namespace std;
template <typename T, std::size_t N>
struct Allocator {
char a[N];
...};
int& view (char& c) {
return *reinterpret_cast<int*>(&c);}
int main () {
assert(sizeof(double) == 8);
Allocator<double, 100> x;
x.allocate(5);
x.allocate(3);
cout << view(x.a[0]) << endl;
cout << view(x.a[96]) << endl;
return 0;}
/*
CS371p: Quiz #17 (5 pts)
*/
/* -----------------------------------------------------------------------
1. What are the two reasons to use inheritance?
[Sec. 8.1.2, Pg. 163]
(2 pts)
code reuse (implementation)
concept reuse (interface)
*/
/* -----------------------------------------------------------------------
2. In regards to inheritance describe the two types of object-oriented
languages?
[Sec. 8.2, Pg. 164]
(2 pts)
all classes extend directly or indirectly a common root
all classes do not extend directly or indirectly a common root
*/
/*
CS371p: Quiz #18 (5 pts)
*/
/* -----------------------------------------------------------------------
1. What distinguishes refinement overriding from replacement overriding?
[Sec. 10.7, Pg. 217]
(2 pts)
the overriding method replaces the base version
the overriding method runs in a addition to the base version
*/
/* -----------------------------------------------------------------------
2. Fill in the TWO blanks below:
[The Open-Closed Principle]
(2 pts)
Software entities (classes, modules, functions, etc.) should be open
for <BLANK>, but closed for <BLANK>.
extension
modification
*/
/*
CS371p: Quiz #19 (5 pts)
*/
/* -----------------------------------------------------------------------
1. The keyword "static" as a modifier of a "local variable" causes what
to NOT change?
(1 pt)
scope
*/
/* -----------------------------------------------------------------------
2. The keyword "static" as a modifier of a "global variable" causes what
to change?
(1 pt)
scope
*/
/* -----------------------------------------------------------------------
3. What is the output of the following program?
Alternatively, the line might not compile.
If a line doesn't compile, which line is illegal.
(2 pts)
true
false
*/
#include <iostream> // cout, endl
template <typename T>
struct A {
static int si;};
template <typename T>
int A<T>::si;
int main () {
using namespace std;
cout << boolalpha; // bool outputs as true or false
cout << ( A<int>::si == A<double>::si) << endl;
cout << (&A<int>::si == &A<double>::si) << endl;
return 0;}
/*
CS371p: Quiz #20 (5 pts)
*/
/* -----------------------------------------------------------------------
1. Fill in the TWO blanks below:
[Sec. 11.2, Pg. 224]
(2 pts)
In a statically typed object-oriented programming language, the
legality of a message-passing expression is determined at compile time
based on the <BLANK> class of the receiver and not on its current
<BLANK> value.
static
dynamic
*/
/* -----------------------------------------------------------------------
2. Fill in the TWO blanks below:
[The Liskov Substitution Principle]
(2 pts)
Functions that use pointers or references to <BLANK> must be able to
use objects of <BLANK> without knowing it.
base classes
derived classes
*/
/*
CS371p: Quiz #21 (5 pts)
*/
/* -----------------------------------------------------------------------
1. In the implementation of vector, what was the reason for adding an
allocator template argument?
(2 pts)
so that the user can specify their own allocator type
*/
/* -----------------------------------------------------------------------
2. In the implementation of vector, what was the reason for adding an
allocator constructor argument?
(2 pts)
so that the user can create an allocator object with a constructor other
than the default constructor
*/
/*
CS371p: Quiz #22 (5 pts)
*/
/* -----------------------------------------------------------------------
1. In C++, what is the type of 'this' in a non-static non-const method of
type T?
Be very precise.
(1 pt)
T* const
*/
/* -----------------------------------------------------------------------
2. In C++, what is the type of 'this' in a non-static const method of
type T?
Be very precise.
(1 pt)
const T* const
*/
/* -----------------------------------------------------------------------
3. Where can the keyword 'mutable' be used?
What does it mean?
Be very precise.
(2 pts)
non-static, non-const, data member declaration
can be changed in a non-static const method
*/
/*
CS371p: Quiz #23 (5 pts)
*/
/* -----------------------------------------------------------------------
1. What mechanism can be used in a fashion that is very nearly equivalent
to multiple inheritance, but avoids many of the semantic problems.
[Sec. 13.5, Pg. 271]
(1 pt)
inner classes
*/
/* -----------------------------------------------------------------------
2. Describe something that can be done with inheritance that can't be done
with composition.
[Sec. 14.2.3, pg. 282]
(1 pt)
substitution
*/
/* -----------------------------------------------------------------------
3. Describe what this Darwin creature does in the long run if there are no
other creatures in the game.
(2 pts)
0: if_empty 3
1: left
2: go 0
3: hop
4: go 0
moves counterclockwise around the grid
*/
/*
CS371p: Quiz #24 (5 pts)
*/
/* -----------------------------------------------------------------------
1. A piece of software that fulfills its requirements and yet exhibits any
or all of the following three traits has a bad design.
List any two.
[The Dependency Inversion Principle]
(2 pts)
1. It is hard to change because every change affects too many other parts
of the system.
(Rigidity)
2. When you make a change, unexpected parts of the system break.
(Fragility)
3. It is hard to reuse in another application because it cannot be
disentangled from the current application.
(Immobility)
*/
/* -----------------------------------------------------------------------
2. In C++ there are five causes for the default constructor to not be
automatically generated. List any two.
(1 pt)
defining any constructor
containing a const
containing a reference
containing a user-defined type with no public default constructor
having a superclass with no public default constructor
*/
/* -----------------------------------------------------------------------
3. In C++ there are four automatically generated methods that
exhibit refinement overriding. List any two.
(1 pt)
default constructor
copy constructor
copy assignment operator
destructor
*/
/*
CS371p: Quiz #25 (5 pts)
*/
/* -----------------------------------------------------------------------
1. Name the two mechanisms through which interfaces can be separated.
[The Interface Segregation Principle]
(2 pts)
delegation
multiple inheritance
*/
/* -----------------------------------------------------------------------
2. Define operator != only in terms of ! and ==.
Define the operators <=, >, and >= only in terms of ! and <.
(2 pts)
*/
#include <cassert> // assert
#include <iostream> // cout, endl
using namespace std;
class A {
friend bool operator == (const A& lhs, const A& rhs) {
return lhs._i == rhs._i;}
friend bool operator < (const A& lhs, const A& rhs) {
return lhs._i < rhs._i;}
private:
int _i;
public:
A (int i) {
_i = i;}};
template <typename T>
bool operator != (const T& lhs, const T& rhs) {
return !(lhs == rhs);}
template <typename T>
bool operator <= (const T& lhs, const T& rhs) {
return !(rhs < lhs);}
template <typename T>
bool operator > (const T& lhs, const T& rhs) {
return (rhs < lhs);}
template <typename T>
bool operator >= (const T& lhs, const T& rhs) {
return !(lhs < rhs);}
int main () {
A x = 2;
A y = 3;
assert(!(x == y));
assert(x != y);
assert(x < y);
assert(x <= y);
assert(!(x > y));
assert(!(x >= y));
cout << "Done." << endl;
return 0;}
/*
CS371p: Quiz #26 (5 pts)
*/
/* -----------------------------------------------------------------------
2. In C++ constructors, refinement overriding is forced. True or False?
In Java constructors, refinement overriding is forced. True or False?
(2 pts)
true
true
*/
/* -----------------------------------------------------------------------
2. In C++ a static 'const double' data member must be initialized in its
definition. True or False?
In Java a static 'final double' data member must be initialized in the
static initializer block. True or False?
(2 pts)
true
false
*/
/*
CS371p: Quiz #27 (5 pts)
*/
/* -----------------------------------------------------------------------
1. Describe the difference in binding time between overloading and
overriding.
[Sec. 15.0, Pg. 287]
(1 pt)
compile time vs. run time
*/
/* -----------------------------------------------------------------------
2. What two concepts characterize two broad categories of overloading?
[Sec. 15.1, Pg. 289]
(1 pt)
type signature and scope
*/
/* -----------------------------------------------------------------------
3. What is the output of the following?
(2 pts)
A() A()
A(A) =(A) ~A()
~A() ~A()
*/
#include <iostream> // cout, endl
using namespace std;
struct A {
A () {cout << "A() ";} // default constructor
A (const A&) {cout << "A(A) ";} // copy constructor
~A () {cout << "~A() ";} // destructor
A& operator = (A) {cout << "=(A) "; return *this;}}; // copy assignment operator
int main () {
{
A x;
A y;
cout << endl;
y = x;
cout << endl;
}
cout << endl;
return 0;}
/*
CS371p: Quiz #28 (5 pts)
*/
class Shape {
private int _x;
private int _y;
public ... equals (...) {...}
...}
class Circle extends Shape {
private int _r;
...}
/* -----------------------------------------------------------------------
1. Define Circle.Circle(...) in terms of Shape.Shape(...).
(2 pts)
*/
public Circle (int x, int y, int r) {
super(x, y);
_r = r;}
/* -----------------------------------------------------------------------
2. Define Circle.equals(...) in terms of Shape.equals(...).
(2 pts)
*/
public boolean equals (Object rhs) {
if (rhs instanceof Circle)
return super.equals(rhs) && (_r == ((Circle) rhs)._r);
return false;}
/*
CS371p: Quiz #29 (5 pts)
*/
class Shape {
private:
int _x;
int _y;
public:
virtual ... equals (...) {...}
...};
class Circle : public Shape {
private:
int _r;
...};
/* -----------------------------------------------------------------------
1. Define Circle::Circle(...) in terms of Shape::Shape(...).
(2 pts)
*/
Circle (int x, int y, int r) :
Shape(x, y) {
_r = r;}
/* -----------------------------------------------------------------------
2. Define Circle::equals(...) in terms of Shape::equals(...).
(2 pts)
*/
bool equals (const Shape& that) const {
if (const Circle* const p = dynamic_cast<const Circle*>(&that))
return Shape::equals(*p) && (_r == p->_r);
return false;}
/*
CS371p: Quiz #30 (5 pts)
*/
/* -----------------------------------------------------------------------
1. In Java dynamic binding is enabled by default.
Under what FOUR conditions will static binding occur instead?
(2 pts)
final class
final method
private method
static method
*/
/* -----------------------------------------------------------------------
2. In C++ static binding is enabled by default.
Dynamic binding is enabled with the keyword "virtual".
Under what FOUR conditions will static binding occur anyway?
(2 pts)
on an object
with an explicit reference to the base class
in a constructor of the base class
in the destructor of the base class
*/
/*
CS371p: Quiz #31 (5 pts)
*/
/* -----------------------------------------------------------------------
1. In Java, what are the consequences of making a method "abstract"?
Be precise.
(2 pts)
the class must be declared abstract
derived classes must define the method or be declared abstract
definition of the method in the class becomes prohibited
*/
/* -----------------------------------------------------------------------
2. In C++, what are the consequences of making a method "abstract"?
Be precise.
(2 pts)
the class becomes abstract
derived classes must define the method or become abstract
definition of the method in the base class becomes optional
if defined it must be defined outside the body of the class
*/
/*
CS371p: Quiz #32 (5 pts)
*/
/* -----------------------------------------------------------------------
1. What is the output of the following program?
(4 pts)
A() A(A) A(A) ~A()
~A() ~A()
*/
#include <algorithm> // swap
#include <iostream> // cout, endl
#include <vector> // vector
using namespace std;
struct A {
A () {cout << "A() ";} // default constructor
A (int) {cout << "A(int) ";} // int constructor
A (const A&) {cout << "A(A) ";} // copy constructor
~A () {cout << "~A() ";} // destructor
A& operator = (const A&) {cout << "=(A) "; return *this;} // copy assignment operator
A* clone () {return new A(*this);}};
struct H {
A* _p;
H (A* p) {_p = p;}
H (const H& that) {_p = that._p->clone();}
H& operator = (H rhs) {swap(_p, rhs._p); return *this;}
~H () {delete _p;}};
int main () {
{
vector<H> x(2, new A());
cout << endl;
}
cout << endl;
return 0;}
/*
CS371p: Quiz #33 (5 pts)
*/
/* -----------------------------------------------------------------------
1. Define Shape's constructor.
(4 pts)
*/
struct Shape : Handle<AbstractShape> {
Shape (AbstractShape* p) :
Handle<AbstractShape> (p)
{}
...};
/*
CS371p: Quiz #34 (5 pts)
*/
/* -----------------------------------------------------------------------
1. What is the output of the following program?
(4 pts)
B::f(double)
A::f(int)
*/
#include <iostream> // cout, endl
using namespace std;
struct A {
virtual void f (int) {
cout << "A::f(int)" << endl;}};
struct B : A {
void f (double) {
cout << "B::f(double)" << endl;}};
int main () {
B x;
x.f(2);
A& r = x;
r.f(2);
return 0;}
/*
CS371p: Quiz #35 (5 pts)
*/
/* -----------------------------------------------------------------------
1. As described in Paper #11: Gender Differences in Computer Science
Students, which of the following statements was found to be true?
(2 pts)
a. Gender discrimination is perceived as a significant problem in CS
departments.
b. Female CS majors had less confidence with computers than did male
non-majors.
c. Study participants did not view CS as a field in which career
goals and family life were compatible.
d. A difference in quantitative ability between gender does explain
some of the gender difference in CS enrollment.
b
*/
/* -----------------------------------------------------------------------
2. What company does Sheryl Sandberg work for?
What is her position?
(2 pts)
Facebook
COO
*/
/*
CS371p: Quiz #36 (5 pts)
*/
/* -----------------------------------------------------------------------
1. Define H's copy constructor and destructor.
(4 pts)
*/
struct A {};
class H {
private:
struct RC {
int _c;
A* _p;
RC (A* p) {
_c = 1;
_p = p;}
~RC () {
delete _p;}};
RC* _q;
public:
H (A* p) {
_q = new RC(p);}
H (const H& that) {
_q = that._q;
++_q->_c;}
~H () {
if (!--_q->_c)
delete _q;}};
int main () {
H x = new A;
H y = x;
return 0;}
/*
CS371p: Quiz #37 (5 pts)
*/
/* -----------------------------------------------------------------------
1. What is the output of the following program?
(4 pts)
B() A() C()
~C() ~A() ~B()
*/
#include <iostream> // cout, endl
using namespace std;
struct A {
A () {cout << "A() ";}
~A () {cout << "~A() ";}};
struct B {
B () {cout << "B() ";}
~B () {cout << "~B() ";}};
struct C : B, A {
C () {cout << "C() ";}
~C () {cout << "~C() ";}};
int main () {
{
C x;
cout << endl;
}
cout << endl;
return 0;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment