1.18.  Review Questions

[ fromfile: cppintro-questions.xml id: cppintro-questions ]

  1. What is a stream? What kinds of streams are there?

     

    A stream is an object which you can use for input or output. It accepts other objects via the insertion or extraction operators (<< and >>). A stream can be attached to a file. An input stream can be attached to the keyboard (such as standard input, by default). An output stream can be attached to the program console (as standard output and standard error are, by default). A stream can also be attached to a string.

  2. Give one reason to use an ostrstream.

     

    So that you can format and write to strings in memory, without sending anything to standard output/error.

  3. What is the main difference between getline and the >> operator?

      both operations skip leading whitespace, but the >> operator will stop reading when it encounters the first whitespace, while getline gets an entire line of input.

  4. What is the type of each expression in the following list?

    1. 3.14

        double

    2. 'D'

        char

    3. "d"

        const char*

    4. 6

       int

    5. 6.2f

        float

    6. "something stringy"

        const char*

    7. false

        bool

  5. In Example 1.34, identify the type and value of each numbered item:

    Example 1.34. src/types/types.cpp

    #include <QTextStream>
    
    int main() {
    	QTextStream cout(stdout);
    	int i = 5;
    	int j=6;
    	int* p = &i;                             1
    	int& r=i;      
    	int& rpr=(*p);
    	i = 10;
    	p = &j;                                  2
    	rpr = 7;                                 3
    
    	r = 8;                                   4
    	cout << "i=" << i << " j=" << j << endl; 5
    	return 0;
    }
    

    1

    *p: ______

    2

    *p: ________

    3

    *p: ________

    4

    rpr: ________

    5

    i: ________ j: ________

    <include src="src/types/types.cpp" href="src/types/types.cpp" id="typescpp" mode="cpp"/>


    Example 1.35. src/types/types-soln.cpp

    #include <QTextStream>
    
    int main() {
    	QTextStream cout(stdout);
    	int i = 5;
    	int j=6;
    	int* p = &i;   1
    
    	int& r=i;      
    	int& rpr=(*p);
    	i = 10;
    	p = &j;       2
    	rpr = 7;      3
    
    	r = 8;        4
    	cout << "i=" << i << " j=" << j << endl; 5
    	return 0;
    }
    

    1

    *p: 5

    2

    *p: 6

    3

    *p: 6

    4

    rpr: 8

    5

    i: ________ j: ________

    <include src="src/types/types-soln.cpp" href="src/types/types-soln.cpp" role="solution" mode="cpp" condition="solution"/>


  6. What is the difference between a pointer and a reference?

     

    A pointer is a variable that stores the address of another variable. It must be dereferenced before it can access the value held by the other variable.

    A reference is an alias for another variable. It can be used the same way that the variable can (unless the reference was declared to be const).

  7. What is the keyword "const" used for? Why and how would you use it in a program?

  8. What is the "address-of" operator? Why and how would you use it in a program?

  9. What is the "dereference" operator? Why and how would you use it in a program?

  10. What is a null pointer? Why would you define one in a program?

  11. What is a memory leak? What would cause one in a program?

  12. What might cause a segmentation fault (or, in Windows, a general protection fault)?

  13. What are the possible uses of const when dealing with pointers?

  14. What is a function's signature?

  15. What is meant by the term, "function overloading" ?

  16. Why is it an error to have two functions with the same signature but different return types in one scope?

  17. Why does main(int argc, char* argv[]) sometimes have parameters? What are they used for?

     

    They are the arguments which were passed in from the command-line when running this program. argc is the number of command-line arguments, argv is an array of the actual arguments as char arrays (C-style strings).