Chapter 2 Exercise Set 1: CPE Practice

This and subsequent exercise sets in the chapters ahead labelled “CPE Practice” will contain practice exercises designed to help prepare you for the certification exam that goes by that name.

These exercise sets will also introduce new material not presented in the main text of the book that is required for the certification.

Outputting in Scientific Notation

#include <iostream>
using namespace std;

int main () {
    float f = 4.2;
    cout << scientific << f << '\n';

    return 0;
}

Compile and run the program above. Read about the scientific flag in the Standard C++ Library reference. Using a combination of experiments and web research, develop your understanding of how it works.

Expressing Integer Literals in Octal and Hex

Integer literals in C++ can be written in decimal (the default way), or they can be written in in octal by beginning the numeric value with a leading 0, or in hexadecimal by beginning the integer constant //with 0x.

If you are not familiar with octal and hex from your previous computer science studies, now would be an excellent time to become familiar with them.

Modifying the Basefied

// modify basefield - dec, hex, and oct
#include <iostream>
using namespace std;

int main () {
    int n = 42;
    cout << "Some bits represented in decimal: " << dec << n << '\n';
    cout << "Same bits represented in hexadecimal: " << hex << n << '\n';
    cout << "Same bits represented in octal: " << oct << n << '\n';

    return 0;
}

Compile and run the program above. Read about the setbase function in the Standard C++ Library reference. Using a combination of experiments and web research, develop your understanding of how these functions (setbase, dec, hex, and oct) work.

Exercises

  1. What is the value of i after the code below is run?

    float f = 3.0 / 4.0;
    int i = f;
    
    1. 0

    2. 1

    3. 0.75

  2. What is the value of the following literal?

    028
    
    1. 8

    2. 24

    3. The literal is invalid

    4. 28

  3. Which of the following is a correct integer number in C++?

    1. 4.2

    2. 4E2

    3. 4,2

    4. 42

  4. What is the value of the f variable after the following assignment?

    float f = 4.0 / 8.0 + 8.0 / 16.0 + 8.0
    
    1. 0.010416666666666666

    2. 8.015625

    3. 8.53125

    4. 9.0

  5. What is the value of the following literal?

    -2e-2
    
    1. -0.02

    2. -2.0

    3. -20.0

    4. The literal is invalid

  6. Which of the following is a correct floating-point number in C++?

    1. 4.2

    2. 4E2

    3. 4,2

    4. 42

  7. What is the output of the following snippet?

    int i = 5, j = ++i, k = i++;
    cout << i << j << k << endl;
    
    1. 567

    2. 556

    3. 666

    4. 766

  8. What is the output of the following snippet if 8 is entered at the keyboard?

    int i = 5, j = i++, k = ++i;
    cin >> i;
    cout << j + i << k - i << endl;
    
    1. 100

    2. 13-1

    3. 10-2

    4. 1523

  9. What will be printed by the following program?

    #include <iostream>
    using namespace std;
    
    int main()
    {
        int i = 5, j, k;
        if (i > 0) j = 2 + i * i;
        if (i <= 0) j = 2 * i - 1;
        if (j >= 0) k = j % i + 2;
        if (j < 0) k = i % j + 2;
        if (k < 0) k = k % i % j;
        if (k >= 0) k = j % i % k;
        cout << k << endl;
    
        return 0;
    }
    
    1. 0

    2. 2

    3. 1

    4. 3

  10. What is the output of the following snippet if 4.2 is entered at the keyboard?

float f;

cin > f;
cout << scientific << "f";
  1. f

  2. 4.2

  3. 4.200000e+00

  4. 42E1

  1. What is the value of the following literal?

020
  1. 2

  2. 16

  3. 20

  4. The literal is invalid

  1. What is the output of the following snippet if a digit 9 is entered?

int n;
cin >> n;
cout << n << hex << n + n << oct << n << endl;
  1. 9189

  2. 9x100

  3. 9328

  4. 91211

  1. What is the value of the following literal?

0E1
  1. 0

  2. 0.01

  3. 0.1

  4. The literal is invalid

  1. Which of the following is not a valid variable name?

  1. Bool

  2. BOOL

  3. bool

  4. bOOL

  1. What is the value of the following literal?

0x23
  1. 23

  2. 32

  3. 35

  4. The literal is invalid

  1. What is the value of the following literal?

X10
  1. 2

  2. 10

  3. 16

  4. The literal is invalid

  1. Which of the following is a legal variable name?

  1. 1_this_one_is

  2. this_one_is

  3. this_one is

  4. #this_one_is