final review 1

The flashcards below were created by user dimeng on FreezingBlue Flashcards.

  1. What is the correct way to declare and initialize a variable of type Item?
    struct Item{
    int length;
    string label;
    double percent;
    };
    Item it = { 34, "hey", 9.33};
  2. Assuming that Item it was declared and initialized, how would you change its length to 73?
    struct Item{
    int length;
    string label;
    double percent;
    };
    it.length = 73;
  3. (T/F) Because Item is a struct, someone declaring an Item variable could set its length to any integer value, even if this ruined the Item's data set.
    struct Item{
    int length;
    string label;
    double percent;
    };
    true
  4. If you const modify a member function in a class, what is the result?
    a) You cannot call the function
    b) The function can access and change an object's data
    c) The function can access an object's data, but cannot change it.
    d) The function cannot access an object's data.
    c
  5. Class definitions always end with:
    a) }
    b) };
    c) );
    d) }}
    b
  6. If you use the static modifier on a local variable in a function, what is the result?
    a) The function cannot modify the variable.
    b) The variable stays in memory after the function ends.
    c) Once you set the variable's value, you cannot change it.
    d) All functions in the program can change the variable.
    b
  7. Select the default constructor:
    a) Zip (const Zip &zip);
    b) Hype h;
    c) Bowler (string name, int score);
    d) Crayon();
    d
  8. Select the initializing constructor:
    a) Bowler(string name, int score);
    b) Zip(const Zip &z);
    c) Hype h;
    d) Crayon();
    a
  9. Select the copy constructor:
    a) Zip(const Zip &z);
    b) Hype h;
    c) Bowler(string name, int score);
    d) Crayon();
    a
  10. One of these sentences is false, select it.
    a) By default, all functions in C++ are const.
    b) By default, all functions in C++ are global.
    c) Member functions in a class should be in the public: section of the class definition.
    d) The scope resolution operator(::) marks a function as belonging to the class.
    b
  11. The #pragma once and #ifndef - #define - #endif combo serve the same purpose. What is it?
    They both serve to prevent redefining the class in memory.
  12. Public inheritance is which type of class relationship?
    a) has-a
    b) Was-a
    c) Is-a
    d) As-a
    c
  13. (T/F) In C++, you can derive a class directly from more than one superclass.
    true
  14. (T/F) Composition is the same as protected inheritance.
    false
  15. In C++, what notation is used to indicate that a function is a pure virtual function?
    a) *p
    b) &x
    c) =0
    d) <>
    c
  16. (T/F) Any pointer can be used to create a dynamic array.
    true
  17. List the big three functions that you need to make when a class uses pointers to create dynamic memory.
    • overloaded assignment operator
    • copy constructor
    • destructor
  18. When does a class's copy constructor execute?
    a) When a new object is being created as a call-by-reference parameter.
    b) When a new object is being created using an initializing constructor.
    c) When a new object is being created as a call-by-value parameter.
    d) When the object is going out of scope.
    c
  19. What are friend functions?
    a) Non-member functions that have access to member data.
    b) Non-member functions that have access to non-member data.
    c) Member functions that have access to member data.
    d) Member functions that have access to non-member data.
    a
  20. The two overloaded operators shown below can be called using (obj + obj2). Indicate which one is a member function of class Goober and which one is not.
    bool operator < (Goober g1, Goober g2);
    bool operator < (Goober g1);
    The first is a non-member; you can tell because it has two arguments. The second is a member because it has one argument.
  21. To prevent a pointer from 'dangling', set its value to ________.
    NULL
  22. <<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>
    • What is a struct?
    • a collection of a fixed number of components.
  23. What is the syntax to define a struct?
    • struct structName {
    • datatype1 name1;
    • datatype2 name2;
    • ...
    • datatypen namen;};

    • Components of a struct are called ________ of the struct.
    • members
  24. How are members of a struct accessed?
    The dot operator - or member access operator - is used. e.g. structname.membername
  25. What are the only built-in operations on a struct?
    assignment and member access (dot operator)
  26. (T/F) Relational operations can be used on structs.
    False. (==, >, < would need to be defined to work on a given struct.)
  27. A struct can be passed to a function ________.
    either by value or by reference
  28. (T/F) A function can return a value of type struct.
    True
  29. (T/F) A struct can be a member of another struct.
    True
  30. (T/F) Two structs can be evaluated for equality, as in struct1 == struct2;
    False, structs must be compared memberwise to see if they are equal.
  31. What is a class?
    A class is a collection of a fixed number of components.
  32. Components of a class are called ________ of the class.
    members
  33. Members of a class are assigned one of three visibility modifiers. What are they.
    • public
    • private
    • protected
  34. Where are private members of a class accessible?
    Only within the class.
  35. Where are public members of a class accessible?
    From inside and outside the class.
  36. A member of a class can be ________.
    a function or variable
  37. How are functions declared as members of a class?
    Usually functions are declared as a member of a class using the function's prototype. It is declared in the appropriate section (public, private, protected).
  38. How is a variable declared as the member of a class.
    It is declared like any other variable, and it is declared in the appropriate section (public, private, protected).
  39. How is a protected member of a class designated in a UML diagram?
    #
  40. When is the memory for a class allocated?
    When it is declared. (NOT in the definition)
  41. How is a member of a class accessed?
    Using the member (dot) operator. e.g. className.memberName
  42. What are the built-in operations on classes?
    assignment and member access
  43. Classes can be passed to a function ________.
    by value or by reference
  44. (T/F) A function can return a value of type class.
    true
  45. What is an accessor function?
    A member function of a class that accesses but does not modify the values of member variables of the class.
  46. What is a mutator function?
    A member function of a class that modifies the value(s) of the member variables.
  47. (T/F) A const (or constant) can modify the values of member variables.
    False
  48. A constant (const) member function can only call ________.
    Other constant member functions of the class
  49. What is the purpose of a constructor?
    The constructor guarantees that the member variables are initialized when an object is declared.
  50. The name of a constructor is ________.
    The same as the name of the class.
  51. (T/F) A class can have more than one constructor.
    true
  52. What is a default constructor?
    A constructor that initializes the member variables to default values.
  53. Constructors automatically execute when ________.
    a class object enters its scope
  54. Destructors automatically execute when ________.
    a class object goes out of scope.
  55. (T/F) A class can have more than one destructor.
    False
  56. How is a destructor named?
    ~className();
  57. What is an abstract data type?
    It is a data type that separates the logical properties from the implementation details.
  58. (T/F) To implement an ADT, you must represent the data and write related algorithms to implement the operations.
    true
  59. For each ________ variable of a class, C++ only allocates one memort space. All members of the class refer to the same memory space.
    static
  60. A public static member of a class can be accessed using the class name and the ________.
    scope resolution operator (::)
  61. (T/F) Static members of a class exist even when no object of that class exists.
    true
  62. Non-static member variables of a class are called ________.
    instance variables
  63. ________ and ________ are meaningful ways to relate two or more classes.
    inheritance and composition
  64. Inheritance is a/an ___-a relation.
    is
  65. Composition (aka aggregation) is a ___-a relation.
    has
  66. In ________, the derived class is derived from only one existing class called the base class.
    single inheritance
  67. In ________, the derived class is derived more than one base class.
    multiple inheritance
  68. The public members of a base class can be inherited as ________ or ________ by the derived class.
    public, private
  69. A derived class can ________ the member functions of a base class. This only applies to members of the derived class.
    redefine
  70. How is the construction of the derived object's corresponding base object handled?
    A call to the base class's constructor (with parameters) is specified in the heading of the definition of the derived class's constructor.
  71. What if a derived class's constructor does not specify a call to the base class's constructor?
    The default constructor for the base class is executed during the derived class's object declaration.
  72. When initializing an object of a derived class, is the base class object's constructor executed first of is the derived class object's constructor executed first?
    The base class's constructor is executed first.
  73. In ________ or ________, a member of a class is an object of another class.
    composition, aggregation
  74. What are the three basic principles of object oriented design?
    • encapsulation
    • inheritance
    • polymorphism
  75. What is encapsulation?
    The ability to combine data and operations on that data in a single unit.
  76. What is inheritance?
    The ability to create new object (classes) from existing objects (classes).
  77. What is polymorphism?
    The ability to use the same expression to denote different operations.
  78. Describe an easy way to identify classes, objects and operations?
    Describe the problem in English. Nouns suggest objects, verbs suggest operations.
  79. Pointer variable contain ________ as their values.
    the addresses of other variables
  80. (T/F) In C++, a name is associated with the pointer data type.
    false
  81. A pointer variable is declared by using ________ between the data type and the variable name.
    • *
    • e.g., int *p;
  82. What does *p point to?
    The value stored in the memory address stored in p.
  83. What does p point to?
    A memory address of the type it was declared with. E.g., int *p => p would point to a memory location that will store an integer.
  84. What is & called?
    the address of operator
  85. The address of operator returns ________.
    the address of its operand.
  86. When used as a unary operator, * is called ________.
    the dereferencing operator
  87. How is the memory location indicated by the value of a pointer variable accessed?
    By using the dereferencing operator. If int *p is initialized to 25, *p = 25 and p is the memory address where the value of p is stored (which is currently 25).
  88. What is the member access operator used with pointers?
    The arrow ->. It is used to access members of objects pointed to by the pointers.
  89. What can pointer variables be initialized with?
    0, NULL, or the address of a variable of the same type (often a reference - &).
  90. What arithmetic operations are allowed on pointer variables?
    ++, --, adding or subtracting an integer from a pointer variable, and subtracting a pointer from another pointer.
  91. (T/F) Pointer variables cannot be compared using relational operators.
    false. It makes sense to compare pointers of the same type.
  92. (T/F) A dynamic variable is accessed by its name.
    False. Dynamic variables don't have names, they are only pointed to by pointer variables.
  93. A variable created during program execution is called ________.
    a dynamic variable
  94. What operator is used to create a dynamic variable?
    new
  95. What operator is used to deallocate the memory occupied by a dynamic variable?
    delete
  96. (T/F) The new and delete operators can be used with either single dynamic variables or arrays of dynamic variables.
    true
  97. If p is a dynamic array, what statement would be used to deallocate the memory occupied by p?
    delete [] p;
  98. Describe the variable **p.
    In this example, p is a pointer to a pointer.
  99. Describe the difference between a shallow copy and a deep copy.
    In a shallow copy, two or more pointers point to the same memory space. In a deep copy, two or more pointers of the same type have their own copies of the data.
  100. When is a destructor called?
    When a class object goes out of scope.
  101. If a class has pointer member variables, the built-in assignment operators provide a ________ copy of the data.
    shallow
  102. When does a copy constructor execute?
    When an object is declared and initialized by using the value of another object and when an object is passed by value as a parameter.
  103. (T/F) C++ allows a user to pass an object of a derived class to a formal parameter of the base class type.
    true. It makes sense because the derived class contains an instance of the base class.
  104. What does a virtual function do?
    It allows the binding of the function to occur at runtime (not compile time). This is called dynamic binding.
  105. How are virtual functions declared?
    By using the reserved word virtual.
  106. A class is called ________ if it contains one or more pure virtual functions.
    an abstract class
  107. Why can't you create objects of an abstract class?
    An abstract class contains pure virtual functions - functions that are not defined in the abstract class. For that reason, you cannot create such objects.
  108. Why would you want to declare a pure virtual function?
    If you are creating an abstract class to work with geometric shapes, you might want to include a function to calculate area. Because area is calculated differently for different shapes, declaring a pure virtual function in your class forces classes derived from shape to implement the area function that is appropriate for their class.
  109. Can an abstract contain functions that are not pure virtual?
    Yes; they must be defined in the class. Also, abstract classes can contain instance variables and constructors.
  110. (T/F) The address of operator can be used to return the address of a private member variable of the class.
    true.
  111. If you need to return the location of a variable in a class, but don't want to allow changes to the value of the variable, what can you do?
    declare the function as const (i.e. const int& addressOfX();
Author:
dimeng
ID:
313248
Card Set:
final review 1
Updated:
2015-12-15 01:43:21
Tags:
diane cs240
Folders:
diane,cs240
Description:
cs240 final review part 1
Show Answers: