Self-test 5:
Basics II and Control II

  1. Which of the following assignment statements is/are legal?

    1. int t = 4.5;
    2. int t = 23;
    3. int t = static_cast<int>(false);
    Solution

    All of them are correct.
    However, what is the value stored in each variable?

    • In A, t will be assigned 4. It is becasue when casting a double to an integer, only the integral part will be kept.
    • In B, t will be assigned 23 as expected.
    • In C, t will be assigned 0 since false is internally represented as 0.


  2. Consider the following code. What will be the output?

    int a = 12, b = 9, c = 6, d = 4;
    int ans = a/static_cast<double>(b)*c-d/b;
    cout << ans << endl;
    
    1. 5
    2. 6
    3. 7.5556
    4. 8
    Solution

    D.
    a/static_cast<double>(b)*c-d/b = 12 / 9.0 * 6 - 4 / 9 = 8.0 - 0 = 8.0 and then the value 8.0 will be coerced to 8.


  3. Consider the following code. What will be the output if the input is 9?

        double test = true;
        int num;
        cin >> num;
    
        if (num > 10)
            test = false;
        else if (num > 9)
            test += 2.2;
        else if (num > 8)
            test += 3.3;
        else if (num > 7)
            test += 4.4;
        else if (num > 6)
            test += 5.5;
    
        if (num > 5)
            test += 6.6;
        else if (num > 4)
            test += 7.7;
        else
            test += 8.8;
    
        cout << static_cast<int>(test) << endl;
    
    1. 4
    2. 10
    3. 11
    4. 38
    Solution

    B.
    Because 9 is greater than 8 and 5, and test is initialized to 1 (since true is internally represented by 1), test += 3.3 and test += 6.6 will be executed. Thus, test will get the value of 10.9. Finally, it is cast back to int 10 before it is printed out.


  4. Which of the following functions CANNOT be compiled?

        void foo1 (int a, int b, int c)
        {
            a = b;
            b = c;
        }
    
        void foo2 (int a, int b, const int& c)
        {
            a = b;
            b = c;
        }
    
        void foo3 (int a, const int b, const int c)
        {
            a = b;
            b = c;
        }
    
    1. foo3 Only
    2. foo1 nor foo3 Only
    3. foo2 nor foo3 Only
    4. All
    Solution

    A.
    Because parameter b in foo3 is defined as const int, it cannot be modified by an assignment statement.
    Function foo1 expects a return value but contains no return statement. It is allowed but is not recommended. As a matter of fact, during compilation, a warning message will be issued. In this course, as far as exam is concerned, it will be considered as an error.
    In foo2, c is declared as const int &. This is allowed. In any case, the object passed to c will be treated as a const int though it is passed by reference.


  5. Which of the following data type(s) cannot be used in the integral expression of switch?

    1. bool
    2. char
    3. double
    4. float
    5. int

    Solution

    C and D.
    We cannot use floating point values for the integral expression in the switch statement.


  6. Consider the following two code segments. Will they give the same result for the identifier count?

    Code segment 1:

        int count = 0;
        for (int i = 0, j = 10; i < 10 && j > 0; i++, j--)
            count = count + 1;
    

    Code segment 2:

        int count = 0;
        for (int i = 0; i < 10; i++);
        for (int j = 10; j > 0; j--)
            count = count + 1;
    
    Solution

    Yes.
    At the end of the program run, the variable count in both program segments will become 10. In Code Segment 1, i increases while j decreases at the same rate. So after executing count = count+1 10 times, i and j becomes 10 and 0 respectively. In Code Segment 2, the first for loop (the i loop) actually has an empty body. The second loop (the j loop) executes 10 times and the identifier count is incremented to 10 too.


  7. What is the output of the following program?

               
        #include <iostream>
        using namespace std;
    
        int main(void)
        {
            int s = 0;
            switch(s)
            {
            case 0:
                cout << s << endl;
                s = s+1;
            case 1:
                cout << s << endl;
                s = s+1;
            case 2:
                cout << s << endl;
                s = s+1;
            case 3:
                cout << s << endl;
                s = s+1;
            default: 
                cout << s <<endl;
            }
    
            return 0;
        }
    
    Solution

    Solution:

    0
    1
    2
    3
    4