#library
#using
#import
#include
namespace uiuc {
class Pair {
double a,b;
};
}
which of the following syntax can be written outside of the namespace declaration to properly create a variable named “p” of type Pair? 1. (uiuc) Pair p; 1. uiuc/Pair p; 1. Pair p; 1. uiuc::Pair p;
&
=
+
<<
Question | Answer |
---|---|
1 | iv |
2 | iii |
3 | iv |
4 | ii |
5 | iv |
6 | iv |
7 | iii |
8 | iv |
9 | i |
10 | iv |
int *p;
p = new int;
*p = 0;
For the code above, which one of the following is NOT true for variable p?
p
int *
&p
int *allocate_an_integer() {
// declare variable i here
*i = 0;
return i;
}
How should variable i be declared?
int *i = new int;
int *i;
int i;
int j; int *i = &j;
int *allocate_an_integer() {
int i = 0;
return &i;
}
int main() {
int *j;
j = allocate_an_integer();
int k = *j;
return 0;
}
What value is variable k assigned and why?
int i;
Which of the following expressions returns the address of the memory location containing the contents of variable i?
*i
i.addr
&i
i->addr
6.
c++
int i = 0;
int *j = &i;
How many memory allocations are made on the stack and on the heap for the above code? For example, declaring an integer would count as one memory allocation.
1. One allocation on the stack and one allocation on the heap.
1. Two allocations on the stack and zero allocations on the heap.
1. Zero allocations on the stack and one allocation on the heap.
1. Zero allocations on the stack and two allocations on the heap.
1. One allocation on the stack and zero allocations on the heap.
7.
c++
int *i = new int;
How many memory allocations are made on the stack and on the heap for the above code? For example, allocating space for one integer would count as one memory allocation.
1. One allocation on the stack and zero allocations on the heap.
1. Zero allocations on the stack and one allocation on the heap.
1. Two allocations on the stack and zero allocations on the heap.
1. Zero allocations on the stack and two allocations on the heap.
1. One allocation on the stack and one allocation on the heap.
8.
c++
int *i = new int;
*i = 0;
int &j = *i;
j++;
What does the last line of the above code segment do?
1. Increments the value of j by one, where the value of j is a local copy stored on the stack of the value of i stored on the heap.
1. Increments the value pointed to by variable i by one.
1. Causes an error.
1. Increments the address pointed to by variable i by one.
int i = 0, j = 1;
int *ptr = &i;
i = 2;
*ptr = 3;
ptr = &j;
j = i;
*ptr = 4;
Enter the number of different values stored in the same address that variable i has during the execution of the code above. (Your answer should be a single integer, which is the total number of different values assigned to that address.)
class Pair {
public: double a,b;
};
int main() {
Pair *p = new Pair;
p->a = 0.0;
return 0;
}
The expression p->a is equivalent to which one of the following?
*(p.a)
(*p).a
p.*a
p.a
Question | Answer |
---|---|
1 | iii |
2 | ii |
3 | i |
4 | iv |
5 | iii |
6 | ii |
7 | v |
8 | ii |
9 | 3 |
10 | ii |
Cube a, b(10);
a = b;
// Function prototype for "contains":
int contains(Cube outer, Cube inner);
// ...
Cube a(10),b(5);
int a_bounds_b = contains(a,b);
Cube b(10);
Cube a = b;
// Function prototype for "intersect":
Cube intersect(Cube &left, Cube &right);
// ...
Cube a(10),b(5);
Cube c;
c = intersect(a,b);
Cube a,b(10);
a = b(10);
Which one of the following statements regarding the declaration of such a custom assignment operator allowing is true?
class Orange {
public:
Orange(double weight);
~Orange();
double getWeight();
private:
double weight_;
};
Select all functions that are present in this class (including any automatic/implicit functions added by the compiler):
class Blue {
public:
double getValue();
void setValue(double value);
private:
double value_;
};
Select all functions that are present in this class (including any automatic/implicit functions added by the compiler):
class Animal {
public:
Animal();
Animal(std::string name);
Animal(std::string name, int age);
Animal(std::string name, int age, double weight);
Animal(const Animal & other);
void setName(std::string name);
std::string getName();
private:
// ...
};
How many explicit (non-automatic) constructors are present in the class?
new
operator to create a class object instance in heap memory, the new
operator makes sure that memory is allocated in the heap for the object, and then it initializes the object instance by automatically calling the class constructor.
After a class object instance has been created in heap memory with new
, when is the destructor usually called?
delete
operator is used with a pointer to the instance of the class.new
operator was used to create the class object instance. double magic(uiuc::Cube cube) {
cube.setLength(1);
return cube.getVolume();
}
int main() {
uiuc::Cube c(10);
magic(c);
return 0;
}
How many times is the uiuc::Cube
’s copy constructor invoked?
*this
. The variable this
is available by default in most class member functions. What is the value of this built-in class variable this
?
int reference_count = 0;
class Track {
public:
Track() { reference_count++; }
~Track() { reference_count--; }
};
Which one of the following procedures (void functions) properly ensures the deallocation of all the memory allocated for objects of type Track so the memory can be re-used for something else after the procedure returns? For the correct answer, the variable reference_count should be zero after all calls to track_stuff() and all of the memory should be deallocated properly. This will dependably occur after only one of the following procedures.
void track_stuff() {
Track t;
// ...
delete t;
return;
}
void track_stuff() {
Track t;
Track *p = new Track;
// ...
delete p;
return;
}
void track_stuff() {
Track *t = new Track;
// ...
t->~Track();
return;
}
void track_stuff() {
Track t;
Track *p = &t;
// ...
delete p;
return;
}
Question | Answer |
---|---|
1 | ii |
2 | i |
3 | iv |
4 | ii, iii, iv, v |
5 | i, iii, iv, v |
6 | iv |
7 | ii |
8 | ii |
9 | i |
10 | ii |
Question 4: option iv returns a cube object when the function returns and this is copied to the main stack Question 6: Include copy constructor in the count as well
A class called Pair has already been declared, but the constructors have not been implemented yet. Pair has two public member variables:
int *pa,*pb;
These two pointers to int are intended to point to heap memory locations that store integers. The remainder of the Pair class expects the following functionality.
Pair(int a, int b)
: This should set up pa
and pb
to point to newly allocated memory locations on the heap. The integers at those memory locations should be assigned values according to the constructor’s integer arguments a
and b
.Pair(const Pair& other)
: This takes as its argument a read-only reference to another Pair. It should set up the newly constructed Pair as a “deep copy,” that is, it should create a new
Pair that is equivalent to the other Pair based on dereferenced values but does not reuse any of the same memory locations. In other words, the copy constructor should set up its own instance’s member variables pa and pb to point to newly allocated memory locations for integers on the heap; those memory locations must be new, not the same locations pointed to by the other Pair, but the integers at these new locations should be assigned values according to the integers that the other Pair is pointing to.~Pair()
that de-allocates all of the the heap memory that had previously been allocated for this Pair’s members.The types of these member functions have already been declared in the declaration of Pair. Now you need to provide the implementation of each of these three member functions.
(Note: The function declarations shown in the code comment below do not include parameter names for the arguments. They show only the types of the arguments. This is allowed for a declaration, but when you define the implementation of those functions, you should give names to the parameters so that you can refer to them.)
/* Class Pair has already been declared
* as shown in the following comments:
*
* class Pair {
* public:
* int *pa,*pb;
* Pair(int, int);
* Pair(const Pair &);
* ~Pair();
* };
*
* Implement its member functions below.
*/
/* Here is a main() function you can use
* to check your implementation of the
* class Pair member functions.
*/
int main() {
Pair p(15,16);
Pair q(p);
Pair *hp = new Pair(23,42);
delete hp;
std::cout << "If this message is printed,"
<< " at least the program hasn't crashed yet!\n"
<< "But you may want to print other diagnostic messages too." << std::endl;
return 0;
}
Pair::Pair(int a, int b){
// assign heap memory and copy values of arguments
pa = new int; *pa = a;
pb = new int; *pb = b;
};
Pair::Pair(const Pair &p){
// assign heap memory and copy values from passed Pair
pa = new int; *pa = *(p.pa);
pb = new int; *pb = *(p.pb);
};
Pair::~Pair(){
// delete the memory allocated on the heap
delete pa; pa = nullptr;
delete pb; pb = nullptr;
};
int main() {
Pair p(15,16);
Pair q(p);
Pair *hp = new Pair(23,42);
delete hp;
std::cout << "If this message is printed,"
<< " at least the program hasn't crashed yet!\n"
<< "But you may want to print other diagnostic messages too." << std::endl;
return 0;
}
int<std::vector> v;
int *v;
std::vector<int> v;
int v[256];
std::vector<char[256]> v;
std::vector v;
std::vector<double> v;
std::vector<std::vector<int>> v;
template <typename Type>
Type max(Type a, Type b) {
return (a > b) ? a : b;
}
Which one of the following exampled is a proper way to call the max function declared above in template form?
max<Type = double>(5.0,10.0)
max(5.0,10.0)
max<double>(5.0,10.0)
<Type = double>max(5.0,10.0)
template <typename Type>
Type max(Type a, Type b) {
return (a > b) ? a : b;
}
class Just_a_double {
public:
double num;
};
int main() {
Just_a_double a,b;
a.num = 5.0;
b.num = 10.0;
// ...
}
Given the above code, which one of the expressions below, if used at line 15, will compile and not generate a compile error?
max(a.num,b.num)
max("five",10.0)
max(a,10.0)
max(a,b)
class Cube(RubikCube) {...};
class RubikCube : public Cube {...};
class RubikCube(Cube) {...};
class Cube : public RubikCube {...};
class Pair {
public:
double a,b;
Pair(double x, double y) { a = x; b = y; }
};
If a class equalPair is derived from the above base class (but specializes it by adding a single boolean “isequal” member variable) then which one of the options below is a proper declaration of a constructor for equalPair?
(As a side note: Although the member variables are of type double, for the sake of this question, we are not concerned about making approximate comparisons of floating-point types, only exact comparisons. Usually, in practical usage, when you compare floating-point values, you should write a function for approximate comparison. That is, you should allow numbers to be considered equal if they have a very small absolute difference, even if they are not exactly the same.)
equalPair(double a, double b) {
this->Pair(a,b);
isequal = (a == b);
}
equalPair(double a, double b) : Pair(a,b) {
isequal = (a == b);
}
equalPair(double a, double b) {
Pair(a,b);
isequal = (a == b);
}
equalPair(double a, double b) {
isequal = (a == b);
}
class Pair {
private:
double a,b;
};
class equalPair : public Pair {
private:
bool isequal;
public:
int status();
}
When the function status() is implemented, which variables will it have access to?
class Just_a_double {
public:
double a;
Just_a_double(double x) : a(x) { }
Just_a_double() : Just_a_double(0) { }
}
Which constructors, if any, compile properly?
Question | Answer |
---|---|
1 | iv |
2 | iii |
3 | ii |
4 | ii |
5 | i |
6 | ii |
7 | ii |
8 | iv |
9 | i |
10 | i |
A base class Pair
contains a single constructor Pair(a,b)
that initializes the pair with the two integer arguments a
and b
. A derived class sumPair
inherits the base class Pair
, and specializes it with a new constructor sumPair(a,b)
and a new variable sum
.
Both of these classes have already been defined.
Implement the new constructor sumPair(a,b)
, which was declared already in class sumPair
. The new constructor sumPair(a,b)
should initialize the inherited class Pair with integer values a,b
and set the member variable sum
to the sum of a
and b
.
/* Class Pair has already been
* declared and defined with the
* following constructor:
*
* Pair(int,int)
*
* that stores its two arguments in
* two private member variables of Pair.
*
* Class sumPair has also already been
* defined as follows:
*
* class sumPair : public Pair {
* public:
* int sum;
* sumPair(int,int);
* };
*
* Implement the constructor
* sumPair(int,int) such that it
* loads the two member variables of
* the base Pair class with its
* arguments, and initializes the
* member variable sum with their sum.
*/
/* Below is a main() function
* you can use to test your
* implementation of the
* sumPair constructor.
*/
int main() {
sumPair sp(15,16);
std::cout << "sp(15,16).sum =" << sp.sum << std::endl;
return 0;
}
The required definition is
sumPair::sumPair(int a, int b) : Pair(a, b){
sum = a + b;
}