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
. They can also contain optional '
characters used for
visual separation which are ignored by the compiler.
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¶
What is the value of
i
after the code below is run?float f = 3.0 / 4.0; int i = f;
What is the value of the following literal?
028
8
24
The literal is invalid
28
Which of the following is a correct integer number in C++?
4.2
4E2
4,2
42
What is the value of the
f
variable after the following assignment?float f = 4.0 / 8.0 + 8.0 / 16.0 + 8.0
0.010416666666666666
8.015625
8.53125
9.0
What is the value of the following literal?
-2e-2
-0.02
-2.0
-20.0
The literal is invalid
Which of the following is a correct floating-point number in C++?
4.2
4E2
4,2
42
What is the output of the following snippet?
int i = 5, j = ++i, k = i++; cout << i << j << k << endl;
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;
100
14-2
10-2
1523
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; }
What is the output of the following snippet if 4.2 is entered at the keyboard?
float f; cin >> f; cout << scientific << "f";
f
4.2
4.200000e+00
42E1
What is the value of the following literal?
020
2
16
20
The literal is invalid
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;
9189
9x100
9328
91211
What is the value of the following literal?
0E1
0
0.01
0.1
The literal is invalid
Which of the following is not a valid variable name?
Bool
BOOL
bool
bOOL
What is the value of the following literal?
0x23
23
32
35
The literal is invalid
What is the value of the following literal?
X10
2
10
16
The literal is invalid
Which of the following is a legal variable name?
1_this_one_is
this_one_is
this_one is
#this_one_is
What is the output of the following program if a digit 2 followed by
Enter
is typed at the keyboard?
#include <iostream> using namespace std; int main() { int x = 3, y = x++, z = ++x; cin >> x; cout << z - x << y - x << endl; return 0; }
Which of the following are legal integer literals in C++?
x13AE
643'321'794
-111222333
b'11001101'
0xABDC
0734
What is the output of the following program if a digit 2 followed by Enter is typed at the keyboard?
#include <iostream> using namespace std; int main() { int x = 3, y = x++, z = ++x; cin >> x; cout << z - x << y - x << endl; return 0; }
What the output of the program below?
#include <iostream> using namespace std; int main() { int n = 2; cout << (n == "2") << endl; return 0; }
1
The program causes a runtime error
0
Compilation fails