B. Mouse
C. Scanner
D. Microphone
b. _employee_number
c. ___department
d. myExtraLongVariableName
cout << "Monday"; cout << "Tuesday"; cout << "Wednesday";
int number = 7;
cout << "The number is " << "number" << endl;
int x = 0, y = 1, z = 2;
cout << x << y << z << endl;
score
and
seven
years ago
cout << "Fourn" << "scoren"; cout << "and" << "nseven"; cout << "nyears" << " ago" << endl;
cout << "Four " << "score "; cout << "and " << "seven/n"; cout << "years" << "ago" << endl;
andseven
yearsago
cout << "Four" << "score" << endl; cout << "and" << "seven" << endl; cout << "years" << "ago" << endl;
“William”
(Hint: Answer this question carefully. Refer to slide 40. Each character requires one byte. What are the essential characters that must be stored in the memory for a typical string?.
int x;
x = 3 + 10 * (16%7) + 2 / 4 ;
double x;
x = 3.0 / 6 + 18 / (15 % 4 + 2);
int x;
x = 24/(1 + 2%3 + 4/5 + 6 + 31%8);
char letter;
What statement correctly assigns the character Z to the variable?
string name;
What statement correctly assigns a string literal to the string object?
int x;
x = 3 / static_cast
result = 6 – 3 * 2 + 7 – 10 / 2;
c = (a^3)/((b^2)(k^4))
x = (19 − 3) * (2 + 3) / 4
double average;
average = 1.0 + 2.0 + 3.0 / 3.0;
int number = 10;
number += 5;
number -= 2;
number *= 3;
1 #include
2 using namespace std;
3
4 int main()
5 {
6 const int MY_VAL;
7 MY_VAL = 77;
8 cout << MY_VAL << endl; 9 return 0; 10 }
1 #include
2 using namespace std;
3
4 int main()
5 {
6 const int MY_VAL = 77;
7 MY_VAL = 99;
8 cout << MY_VAL << endl; 9 return 0; 10 }
cin >> input_value;
if (input_value > 5)
input_value = input_value + 5;
else if (input_value > 2)
input_value = input_value + 10;
else
input_value = input_value + 15;
int x = -1;
cout << "Enter a 0 or a 1 from the keyboard: "; cin >> x;
if (x)
cout << "true" << endl; else cout << "false" << endl;
This is all folks!
int x = 5;
if (x = 2)
cout << "This is true!" << endl; else cout << "This is false!" << endl; cout << "This is all folks!" << endl; Hint: answer this question carefully. The way a programmer enters indentation might be wrong. So indentations do not show the exact way a program executes. Remember that if your "if" or "else" condition contains more than one statement, you should enclose them in brackets to make a block. Otherwise not all the statements execute. Refer to slide ch4-17.
You passed the test!
Assume the user enters a grade of 90 from the keyboard.
cout << "Enter a test score: "; cin >> test_score;
if (test_score < 60); cout << "You failed the test!" << endl; if (test_score > 60)
cout << "You passed the test!" << endl; else cout << "You need to study for the next test!"; Hint1: answer this question carefully. What happens when there is a ";" right at the end of an "if" condition? Hint2: The way a programmer enters indentation might be wrong. So indentations do not show the exact way a program executes.
This is a test question!
score = 40;
if (score > 95)
cout << "Congratulations!n"; cout << "That's a high score!n"; cout << "This is a test question!" << endl; Hint: answer this question carefully. The way a programmer enters indentation might be wrong. So indentations do not show the exact way a program executes. Remember that if your "if" condition contains more than one statement, you should enclose them in brackets to make a block. Otherwise not all the statements execute. Refer to slide ch4-17.
1. x == 5;
2. 7 <= (x + 2); 3. z < = 4; 4. (1 + x) != y; 5. z >= 8;
6. x >= 0;
7. x <= (y * 2)
int number;
cin >> number;
if (number > 0)
cout << "C++"; else cout << "Soccer"; cout << " is "; cout << "fun" << endl;
int funny = 7, serious = 15;
funny = serious % 2;
if (funny != 1)
{
funny = 0;
serious = 0;
}
else if (funny == 2)
{
funny = 10;
serious = 10;
}
else
{
funny = 1;
serious = 1;
}
cout << funny << " " << serious << endl;
#include
using namespace std;
int main()
{
int a = 0, b = 2, x = 4, y = 0;
cout << (a == b) << " "; cout << (a != b) << " "; cout << (b <=x) << " "; cout << (y > a) << endl; return 0; }
int x = 1, y = 1, z = 1;
y = y + z;
x = x + y;
cout << "result = " << (x < y ? y : x) << endl;
cout << "This is a checkn";
int donuts = 10;
if (donuts != 10)
donuts = 0;
else
donuts += 2;
int w = 98;
int x = 99;
int y = 0;
int z = 1;
if (x >= 99)
{
if (x < 99) cout << y << endl; else cout << z << endl; } else { if (x == 99) cout << x << endl; else cout << w << endl; }
“That number is acceptable.”
int number;
cin >> number;
if (number > 10 && number < 100) cout << "That number is acceptable.n"; else cout << "That number is not acceptable.n";
1 #include
2 using namespace std;
3
4 int main()
5 {
6 int number = 5;
7
8 if (number >= 0 && <= 100) 9 cout << "passed.n"; 10 else 11 cout << "failed.n"; 12 return 0; 13 }
while (x++ < 10) Which operator is used first?
n = 1;
while (n <= 5) cout << n << ' '; n++;
1
2
3
4
int x = 0;
while (x < 5) { cout << x << endl; x++; }
int number = 6;
cout << number++ << endl;
int number = 6;
cout << ++number << endl;
int number = 6;
int x = 0;
x = number–;
cout << x << endl;
int x = 0;
for (int count = 0; count < 3; count++) x += count; cout << x << endl;
for (int i = 1; i < 20; i++) cout << "Hello!" << endl;
for (int i = 0; i <= 20; i++) cout << "Hello!" << endl;
for (int i = 20; i > 0; i–)
cout << "Hello!" << endl;
int number = 6;
number++;
cout << number << endl;
int number = 6;
++number;
cout << number << endl;
int number = 6
int x = 0;
x = –number;
cout << x << endl;
2
#include
using namespace std;
void showDub(int);
int main()
{
int x = 2;
showDub(x);
cout << x << endl; return 0; } void showDub(int num) { cout << (num * 2) << endl; }
0
2
#include
using namespace std;
void doSomething(int);
int main()
{
int x = 2;
cout << x << endl; doSomething(x); cout << x << endl; return 0; } void doSomething(int num) { num = 0; cout << num << endl; }
0
0
#include
using namespace std;
void doSomething(int&);
int main()
{
int x = 2;
cout << x << endl; doSomething(x); cout << x << endl; return 0; } void doSomething(int& num) { num = 0; cout << num << endl; }
1 #include
2 using namespace std;
3
4 void showDub(int);
5
6 int main()
7 {
8 int x = 2;
9
10 showDub(x);
11 cout << x << endl; 12 return 0; 13 } 14 15 void showDub(int num) 16 { 17 cout << (num * 2) << endl; 18 }
1 #include
2 using namespace std;
3
4 void showDub(int);
5
6 int main()
7 {
8 int x = 2;
9
10 showDub(x);
11 cout << x << endl; 12 return 0; 13 } 14 15 void showDub(int num) 16 { 17 cout << (num * 2) << endl; 18 }
1 #include
2 using namespace std;
3
4 void showDub(int);
5
6 int main()
7 {
8 int x = 2;
9
10 showDub(x);
11 cout << x << endl; 12 return 0; 13 } 14 15 void showDub(int num) 16 { 17 cout << (num * 2) << endl; 18 }
int myFunction(double, double, double);
How many parameter variables does this function have?
#include
using namespace std;
int getValue(int);
int main()
{
int x = 2;
cout << getValue(x) << endl; return 0; } int getValue(int num) { return num + 5; }
void computeValue(int value)
Which of the following is a valid call to the function?
void calc (int a, int& b)
{
int c;
c = a + 2;
a = a * 3;
b = c + a;
}
What is the output of the following code fragment that invokes calc?
int x = 1;
int y = 2;
int z = 3;
calc(x, y);
cout << x << " " << y << " " << z << endl;
b. strings of different lengths
c. uninitialized elements
int values[5];
int bugs[1000];
int numbers[] = {99, 87, 66, 55, 101 };
cout << numbers[3] << endl;
66
55
int numbers[] = { 99, 87, 66, 55, 101 };
for (int i = 1; i < 4; i++) cout << numbers[i] << endl;
int numbers[4] = { 99, 87 };
cout << numbers[3] << endl;
const int SIZE = 5;
double x[SIZE];
for(int i = 2; i <= SIZE; i++) { x[i] = 0.0; }
vector
5
vector
for (int val : numbers)
cout << val << endl;
vector
bookList[2].publisher[3] = ‘t’;
This statement ________.
Circle doSomething(Circle c2)
can be the header line for a function that ________.
enum Tree { OAK, MAPLE, PINE };
In memory, what value will the MAPLE enumerator be stored as?
struct Circle
{
double centerX;
double centerY;
double radius;
};
Assume that circle1 and circle2 are variables of the Circle type, and their members have been initialized.
True/False: The following if statement correctly determines whether the two variables’ members contain the same data:
if (circle1 == circle2)