Self-test 1:
C++ Basic

  1. Which of the following are correct ways to declare variables?

    1. int length, width;
    2. int length, int width;
    3. int length; width;
    4. int length; int width;
    Solution
    A and D are correct. D is correct although it is not recommended to write two programming statements on the same line.

  2. What is the printout of cout << 'z'- 'a'?

    1. z
    2. 25
    3. a
    4. 26
    Solution
    B is correct. Some of you may think the answer is D. But since 'a'-'a' should be equal to 0, and we have 26 alphabets, you can figure out that:
    • 'a'-'a' = 0
    • 'b'-'a' = 1
    • ....
    • 'z'-'a' = 25


  3. Which one of the following is/are illegal identifier(s)?

    1. abcdefg0123
    2. 1a
    3. char
    4. _John
    Solution

    B and C are illegal identifiers. B is wrong becasue it starts with digit 1. C++ identifer is not allowed to start with a digit (0 - 9). C is wrong because char is a reserved word as it is the name of a basic data type in C++. Note that Char could be a C++ identifier because C++ identifiers are case-sensitive.


  4. Which of the following statement outputs strings "first" and "second" in two separate lines?

    1. cout << "first" << endl << "second";
    2. cout << "first\tsecond";
    3. cout << "first\nsecond";
    4. cout << "first" << "second" << endl;
    Solution

    A and C are correct. C is correct because \n means to print out a new line, so it is equivalent to A. B prints first and second in the same line, but two words are separated by a tab. D prints out firstsecond on the screen.


  5. Which of the following statement prints the following string?
        smith\exam1\test.txt

    1. cout << "smith\\exam1\\test.txt";
    2. cout << "smith\"exam1\"test.txt";
    3. cout << "smith"\exam1"\test.txt";
    4. cout << "smith\exam1\test.txt";
    Solution

    A is correct. B will print out smith"exam1"test.txt. C will encounter compilation errors. D would produce unexpected result.


  6. Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 (exclusive) or the number is negative?

    1. ((x < 100) && (x > 1)) || (x < 0)
    2. (1 > x > 100) || (x < 0)
    3. ((x < 100) && (x > 1)) && (x < 0)
    4. 1 < x < 100 && x < 0
    Solution

    A is correct. B and D are wrong becasue the expression, 1 > x > 100, is invalid in C++. C is wrong because of the wrong use of the last && operator.


  7. Consider the following code fragment:

    int main(void)
    {
       int x = 100;
       int y = 100;
    }
    

    After the definitions of variables x and y, what will be the value of the conditional expression (y < x++)?

    1. true
    2. false
    3. The expression cannot be compiled
    4. Unexpected results
    Solution

    B. According to the semantics of the post-increment operator ++, x++ will use the current value of x for comparison before x is incremented. Thus, x and y have the same value during the comparison which evaluates to false.


  8. Please read the following code, and point out and correct the error(s). If there is/are no error(s), just give the output.

    #include<iostream>
    using namespace std;
    int main(void)
    {
         int a, b;
         a = b = 0;
         b = a++;
         cout << a << " " << b << endl;
         return 0;
    }
    

    Solution

    No error is found. The output is: 1 0. b=a++; can be treated as two programming statements: b = a; a++;

    Notice:
    a = b = 0; is not an error. C++ allows cascade assignment --- assigning a value to multiple variables.


  9. What is the output of the following program segment?

    int main(void)
    {
        int a = 10, b = 13, x = 7; 
        x *= ++a * b++;
        cout << x << endl;
        return 0;
    }
    
    1. 910
    2. 980
    3. 1001
    4. None of the above
    Solution

    C. Since the operator ++ has a higher precedence than * (refer to the notes for C++ basic), you may consider the statement "x *= ++a * b++;" as: x *= (++a) * (b++); In addition, ++a means that we will increment a and use the result for further operation, while b++ will use the current value of b for some operation first before it is incremented. As a result, the statement is equivalent to:

     a = a+1;
     x *= a * b; // x = x * (a * b);
     b = b+1;
    

    Therefore, the resulting value of x is 7*11*13=1001.