Content Frame
Note for screen reader users: There is text between the form elements on this page. To be sure that you do not miss any text, use item by item navigation methods, rather than tabbing from form element to form element.
Skip Breadcrumb Navigation
Home  arrow Student Resources  arrow Quizzes  arrow Chapter 7

Chapter 7

Constructors and Other Tools

This activity contains 10 questions.

Question 1.

What will the following code fragment output?

class Foo
{
  private:
    int x;
  public:
    Foo();
    Foo(int val);
    void Print();
};

Foo::Foo()
{
   x=1;
}

Foo::Foo(int val)
{
   x=val;
}

void Foo::Print()
{
  cout << x << endl;
}

int main()
{
   Foo f1;
   Foo f2(3);
   f1.Print();
   f2.Print();
   return 1;
}
 
End of Question 1


Question 2.
What is the bug in the following code fragment?

class Foo
{
  private:
    int x;
  public:
    Foo();
    Foo(int val);
};

Foo::Foo()
{
   x=1;
}

Foo::Foo(int val)
{
   x=val;
}

int main()
{
   Foo f1();
   Foo f2(3);
   return 1;
}

 
End of Question 2


Question 3.

What will the following code fragment output?

class Foo
{
  public:
    Foo();
    void Print();
};

Foo::Foo()
{
   cout << "In default constructor" << endl;
}

void Foo::Print()
{
  cout << "Printing" << endl;
}

int main()
{
   Foo f1, f2;   
   f1.Print();
   f2.Print();
   return 1;
}
 
End of Question 3


Question 4.

Given the following classes, how can we define a BankAccount constructor that initializes the amount member variable as well as the id member variable?

class Money
{
 public:
   Money(int d, int c);
   int dollars;
   int cents;
};

Money::Money(int d, int c)
{
   dollars = d;
   cents = c;
}

class BankAccount
{
 public:
   int id;
   Money amount;
   BankAccount(int id, int dollars, int cents);
};
 
End of Question 4


Question 5.

Which of the following snippets of code will correctly check to see if the two Money objects have the same dollar and cent amount?

class Money
{
 public:
   Money(int d, int c);
   bool equals(Money m);
private:
   int dollars;
   int cents;

};

Money::Money(int d, int c)
{
   dollars = d;
   cents = c;
}

bool Money::equals(Money m)
{
  return ((dollars == m.dollars) && (cents==m.cents));
}
 
End of Question 5


Question 6.
Which of the following is not a valid reason to declare a class parameter as const?

e.g.:

void someFunction(const FooClass &foo)

 
End of Question 6


Question 7.
Why is it recommended that an inline function only be used when the function is short?

 
End of Question 7


Question 8.
Given the class definition below:

class Compute
{
 public:
   static int square(int x);
};

int Compute::square(int x)
{
  return (x*x);
}

What is the shortest way to invoke the square function to get the square of 4?

 
End of Question 8


Question 9.

Give the output of this program:

class Foo
{
 public:
   static int val1;
   int val2;
   Foo(int v);
   void Print();
};

int Foo::val1 = 0;

Foo::Foo(int v)
{
 val1++;
 val2 = v;
}

void Foo::Print()
{
 cout << "val1=" << val1 << " val2=" << val2 << endl;
}

void main()
{
  Foo f1(1),f2(2);
  f1.Print();
  f2.Print();

  Foo f3(3);
  f2.Print();
}
 
End of Question 9


Question 10.

The following code snippet intends to read 5 integers into a vector and then print them out. However, this code has a bug. What is it?

vector<int> v;
 int i, val;

 // Input values
 for (i=0; i<5; i++) {
  cin >> val;
  v.push_back(val);
 }
 // Output values
 for (i=0; i<v.capacity(); i++) {
  cout << v[i] << endl;
 }
 
End of Question 10





Pearson Copyright © 1995 - 2010 Pearson Education . All rights reserved. Pearson Addison Wesley is an imprint of Pearson .
Legal Notice | Privacy Policy | Permissions

Return to the Top of this Page