CIS 250 Midterm Review

Please note that this study guide was not meant to be exhaustive. It certainly does NOT have everything that is on the midterm, and probably contains a number of items not found on the midterm. Please advise the instructor of any errors found so they can be corrected as soon as possible.

Show all answers     Hide all answers

    C++ libraries

  1. What objects are defined in iostream? Answer cin, cout, cerr, clog
  2. What parametric manipulators for I/O are defined in iomanip? Answer setbase, setfill, setprecision, setw (and setiosflags, resetiosflags)
  3. What non-parametric manipulators for I/O are defined in ios? Answer boolalpha, showbase, showpoint, uppercase, noboolalpha, noshowbase, noshowpoint, nouppercase, dec, hex, oct, fixed, scientific, left, right, endl (and others)
  4. What do the manipulators boolalpha/noboolalpha control? Answer They control whether true and false values display as the text "true" and "false".
  5. What do the manipulators showbase/noshowbase control? Answer They control whether the number base is displayed (eg. 0x for hex)
  6. What do the manipulators showpoint/noshowpoint control? Answer They control whether the a decimal point is required to be displayed.
  7. What do the manipulators uppercase/nouppercase control? Answer They control whether letters in numbers that are in bases that require letters are displayed in upper or lower case.
  8. What do the manipulators left/right control? Answer They control text alignment in a field.
  9. What do the manipulators oct/dec/hex control? Answer They specify the number base being used.
  10. What does the manipulator setbase control? Answer It sets what number base is being used. The base should be 8, 10, or 16.
  11. What does the manipulator setfill control? Answer It sets the character used to pad output on the left if the output is shorter than the field width.
  12. What does the manipulator setprecision control? Answer It controls how many digits are printed after the decimal point.
  13. What does the manipulator setw control? Answer It controls how many characters wide the output field will be. It must be reset after every field that is output.
  14. What is the difference between using endl and '\n'? Answer Both output a newline, but endl also flushes the output buffer.
  15. What objects does the fstream library define? Answer fstream, ifstream, ofstream
  16. What is the fstream library used for? Answer file access
  17. What is the iostream library used for? Answer input and output to standard streams
  18. What is the cstdio library used for? Answer It is the old C library used for input and output to standard streams. It has the printf and other associated functions (such as sprintf).
  19. What library should you include if you want to use C++ string objects? Answer string
  20. What library should you include if you want to use C style string functions? Answer cstring (or string.h)
  21. Operators

  22. Name the following operator: << Answer It is the insertion operator.
  23. Name the following operator: >> Answer It is the extraction operator.
  24. What can you tell about a if you see the expression: a->b? Answer You know that a is a pointer to an object.
  25. Name the following operator: :: Answer It is the scope resolution operator.
  26. Rewrite the following using a conditional operator:
    if (x >= 0) n = x; else n = -x; Answer n = (x >= 0) ? x : -x;
  27. Rewrite the following using an if/else statement:
    n = x >= 0 ? x : -x; Answer if (x >= 0) {
       n = x;
    } else {
       n = -x;
    }
  28. File access

  29. Write a statement that will open a file named file1 for text input and store the reference in a new variable named inputFile. Answer ifstream inputFile("file1");
  30. Write a statement that will open a file named file2 for text output and store the reference in a new variable named outputFile. Answer ofstream outputFile("file2");
  31. Write a statement that will open a file named file3 for text output where you want to append to the end of the file and store the reference in a new variable named appendFile. Answer ofstream appendFile("file3", ios::app);
  32. Write a statement that will open a file named file4 for binary input and store the reference in a new variable named binaryFile. Answer ifstream binaryFile("file4", ios::binary);
  33. Write a statement that will open a binary file named file5 for both reading and writing and store the reference in a new variable named busyFile. Answer fstream busyFile("file5", ios::in | ios::out | ios::binary);
  34. Write a statement the will set the read pointer for busyFile to the end of the file. Answer busyFile.seekg(0L, ios::end);
  35. Write a statement the will set the write pointer for busyFile to the beginning of the file. Answer busyFile.seekp(0L, ios::beg);
  36. Write a statement that will store the current write pointer for busyFile in a new variable named pos. Answer long pos = busyFile.tellp();
  37. Write the code needed to find out how many records are in the binary data file named data. Store the answer in a new long int variable named numRecords. Assume each record in the file is the same size as the struct named record. Answer ifstream ifile("data", ios::binary);
    ifile.seekg(0L, ios::end);
    long numRecords = ifile.tellg() / sizeof(record);
    ifile.seekg(0L, ios::beg);
  38. Arrays

  39. Write the statement needed to declare an array of five floats named arr. Answer float arr[5];
  40. Write the statement needed to declare an array of five ints named vect and immediately set them to 3, -2, 71, 5, 1. Answer int vect[] = {3, -2, 71, 5, 1 };
  41. Write the statement that will set the third element of an array named arr to 3.14159. Answer arr[2] = 3.14159;
  42. Write the statement that will set the third element of an array named arr to 3.14159 without using array subscript notation (no []). Answer *(arr+2) = 3.14159;
  43. Write the statement needed to send an array named arr to a function named display. The function has a void return data type. Answer display(arr);
  44. Write the statement needed to declare a 2D array (2 by 3) of ints named grid. Answer int grid[2][3];
  45. Write the statement needed to declare a 2D array (2 by 3) of ints named grid and initialize the first row to 1, 2, 3, and the second row to 4, 6, 8. Answer int grid[][] = {{1, 2, 3}, {4, 6, 8}};
  46. Can different elements of a single dimension array in C++ be different data types? Answer No. All of the elements must be the same data type.
  47. Can different elements of a multi-dimensional array in C++ be different data types? Answer No. All of the elements must be the same data type.
  48. When listed in the parameter list of a function, all of the dimensions of an array must be specified except for which one? Answer The first dimension does not have to be specified.
  49. Structs, classes, objects

  50. What is the primary difference between a struct and a class? Answer A struct has default public access to its members. A class has default private access to its members.
  51. How are a class and an object of that class related? Answer A class is like a blueprint for an object. An object is an instance of its class.
  52. What is the difference between static and instance data members? Answer A unique copy of every instance data member is created for each object created from the class. There is only one copy of each static data member which all objects share, and which exists before any objects of the class are created.
  53. Can instance methods/functions access static data members directly? Answer Yes
  54. Can static methods/functions access instance data members directly? Answer No
  55. What does public access mean? Answer Public means that all code has access.
  56. What does private access mean? Answer Private means that only members of the same class have access.
  57. What does protected access mean? Answer Protected means that only members of the same class and subclasses have access.
  58. What is the this pointer? Answer The this pointer always refers to the object it is a part of.
  59. Write a prototype for the default constructor of a class named ClassA. Answer ClassA();
  60. Write a prototype for the copy constructor of a class named ClassA. Answer ClassA(const ClassA& obj);
  61. What are accessor methods? Answer Accessor methods are used to retrieve the values of private data members.
  62. What are mutator methods? Answer Mutator methods are used to set the values of private data members.
  63. What are predicate methods? Answer Predicate methods return a true or false value.
  64. Name four methods automatically generated for a class if you do not write them. Answer default constructor, copy constructor, assignment operator overload, destructor
  65. Describe when you usually have to write your own copy constructor, assignment operator overload, and destructor. Answer You will probably have to write your own versions of those methods when the class uses dynamic memory allocation. You may also have to override the default constructor if you want a default constructor available.
  66. What does the const keyword mean when placed in front of a variable declaration? Answer It means the variable is a constant and the value can not be changed after it is initialized.
  67. What does the const keyword mean when placed in front of a parameter to a function/method? Answer It means the value of that parameter/argument will not be changed within that function/method.
  68. What does the const keyword mean when placed in front of a function/method definition? Answer It means that the returned value may not be changed.
  69. What does the const keyword mean when it is placed after the parameter list of a function/method? Answer It means that the data members of the object may not be modified within this function/method.
  70. Strings

  71. Write a statement that will declare a C-style string named str that can contain 20 characters. Answer char str[21];
  72. Write a statement that will declare a C++ string object named str. Answer string str;
  73. Write a statement which will store the length of a C-style string named str in a variable named len. Answer len = strlen(str);
  74. Write a statement that will copy a C-style string from a variable named str1 to a variable named str2. Answer strcpy(str2, str1);
  75. Write a statement that will copy a C-style string from a variable named str1 to a variable named str2, but limit the number of characters copied to 12. Answer strncpy(str2, str1, 12); str2[12] = '\0';
  76. Write a statement that will dynamically allocate space for a 20 character C-style string and store the pointer in a char* variable named str. Answer str = new char[21];
  77. Write a statement that will free the memory used by a C-style string pointed to by a char* variable named str. Answer delete[] str;
  78. Write a statement that will concatenate two C++ strings named first and second and store the result in a variable named third. Answer third = first + second;
  79. How can you compare C-style strings? Answer You can not use relational operators. You can use a function such as strcmp(const char*, const char*)
  80. Practical object programming

  81. Write a function prototype for a function named mult which takes two int arguments and returns their product. The second argument should have a default value of 10.. Answer int mult(int a, int b = 10);
  82. Write a class with the following requirements:
    1. The class should be named MyClass.
    2. It should contain a static private int named count.
    3. It should contain an instance private string object named id.
    4. It should have a private method named init that all constructors use to initialize data members.
    5. It should have a default constructor that sets id to a zero-length string and increments count.
    6. It should have a copy constructor that does what is appropriate.
    7. It should have a constructor that takes a string object as an argument and uses it to set id. It should also increment count.
    8. It should have an accessor method named getCount that returns the appropriate value.
    9. It should have an accessor method named getId that returns the appropriate value. The returned value must not allow any changes to be made to the returned data.
    10. It should have a mutator method named setId that sets the id with the string object sent to it.
    11. It should have a destructor which decrements count.
    12. It should have a friend function that overloads the insertion operator and displays the object's id.
    Answer
    // test code was not required, but is included
    #include <string>
    #include <iostream>
    using std::cout;
    using std::ostream;
    using std::string;
    
    class MyClass {
    private:
        static int count;
        string id;
        void init(const string str) { setId(str); count++; }
    public:
        MyClass() { init(""); }
        MyClass(string str) { init(str); }
        MyClass(const MyClass& obj) { init(obj.id); }
        ~MyClass() { count--; }
        static int getCount() { return count; }
        string getId() const { return string(id); }
        void setId(const string str) { id = str; }
        friend ostream& operator<<(ostream& strm, const MyClass& obj);
    };
    
    int MyClass::count = 0;
    
    ostream& operator<<(ostream& strm, const MyClass& obj) {
        strm << obj.id;
        return strm;
    }
    
    int main(int argc, char* argv[]) {
        MyClass m1;
        MyClass m2(string("12345678901234567890"));
        MyClass m3(m2);
        cout << "m1 = " << m1 << '\n';
        cout << "m2 = " << m2 << '\n';
        cout << "m3 = " << m3 << '\n';
        cout << "count = " << MyClass::getCount() << '\n';
        {
            MyClass m4(string("Temporary item"));
            cout << "m4 = " << m4 << '\n';
            cout << "count = " << MyClass::getCount() << '\n';
        }
        m3.setId(string("New id"));
        cout << "m3 = " << m3 << '\n';
        cout << "count = " << MyClass::getCount() << '\n';
        return 0;
    }
    
    /*
        Sample output:
        
        m1 =
        m2 = 12345678901234567890
        m3 = 12345678901234567890
        count = 3
        m4 = Temporary item
        count = 4
        m3 = New id
        count = 3
    */