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 6

Chapter 6

Structures and Classes

This activity contains 10 questions.

Question 1.
What is the error in the following definition?
struct BankAccount
{
   int ID;
   double amount;
}

 
End of Question 1


Question 2.
Given the following declaration, how do we access the x and y members of the structure?
struct Point
{
  int x;
  int y;
};

 
End of Question 2


Question 3.
Which of the following is a way that structures differ from classes?

 
End of Question 3


Question 4.
If a structure is being accessed as:

Foo x;
x.val1.val2 = 10;

Which definition allows this assignment to be valid?

 
End of Question 4


Question 5.
How would we initialize the following structure to contain 1 and 2 upon declaring the variable of type Foo?

struct Stuff {
  int val2;
};
struct Foo {
  int num;
  Stuff val1;
};

 
End of Question 5


Question 6.
Given the following class definition, what code correctly invokes the method named "printMessage"?

class Foo
{
  public: 
    void printMessage();
    int x;
};

void Foo::printMessage()
{
   cout << "Hello there" << endl;
}

 
End of Question 6


Question 7.
Under the principle of information hiding, which class definition best encapsulated the variable named "x"?

 
End of Question 7


Question 8.
What is the error in the following definition?

class BankAccount
{
  public:
   int ID;
   double amount;
}

 
End of Question 8


Question 9.

Given the following class definition for a Money object:

class Money
{
  private: 
    int dollars;
    int cents;
 public:
    void setValue(int d, int c);
    void addValue(int d, int c);
};

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

How could we implement addValue so that if we add a new dollar and cent amount we update the dollar amount appropriately in case we have over 99 cents? That is, the number of cents after adding should never be more than 99.
 
End of Question 9


Question 10.

Given the following class definition:

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

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

If we change the interface to store a float instead of two integers:

class Money
{
  private: 
    float value;
 public:
    void setValue(int d, int c);
};

How could we write the implementation of the setValue method so that "value" is set appropriately to the dollar and cents but the interface remains the same?
 
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