Name
CS 012 - Practice
Login
Summer - 2019
1. Suppose that p1 and p2 are both pointers to the same dynamic integer variable; y is a local integer
variable. After the execution of which of the statements below is the pointer variable p1 considered a
dangling pointer (not pointing to a valid memory address)?
a. p1 = p2;
b. p1 = 0;
c. p2 = 0;
d. p1 = &y;
e. delete p2;
f. p2 = &y;
2. Which of the following values is the closest approximation of the most number of comparisons
needed to search for a value within an unsorted data set of size 100? (i.e. uses linear search)
a. 1
b. 100
c. 200
d. 10,000
e. 7
f. 700
3. Which of the following values is the closest approximation of the most number of comparisons
needed to search for a value within a sorted vector of size 100 using the binary search algorithm?
a. 1
b. 100
c. 200
d. 10,000
e. 7
f. 700
4. Which version of the print function would be invoked by the code:
Person* x = new Student;
x->print();
class Person
{
public:
void print() const;
};
class Student : public Person
{
public:
void print() const;
};
a. Person::print()
b. Student::print()
c. void print();
d. No function gets called, this causes a
compile-time error
5. Which version of the print function would be called by the same code, if its declaration in both
classes is changed to:
virtual void print() const;
Use the same set of responses as in the previous question
6. What is returned by the function call: f("tweedleddeee") given the following definition of the
function f?
int f(const string &s)
{
if (s.size() < 2) return 0;
if (s.at(0) == s.at(1)) return 1 + f(s.substr(1));
return f(s.substr(1));
}
a. 1
b. 2
c. 4
d. 'e'
e. 3
f. 6
7. Which of the following pairs of classes would it make sense for them to have an inheritance
relationship?
a. Zoo, Animal
b. Drink, Coffee
c. Car, Road
d. Car, Tire
e. All of the above
f. None of the above
Page 1 of 2