Self Check¶
The questions in this section provide a chance to demonstrate your understanding of the concepts discussed so far.
After reading the material in this chapter, you should be able to answer these questions.
Given the following plain english statements:
Create a variable x with value 8
Create a variable y with value 5
Add x and y, storing the result in y
If they were implemented as a program,
then what value is y when finished?
Place these statements in their proper order so that the program prompts for input, computes the area, and displays the results.
-
double area = h*w; -
double h = 0; double w = 0; -
std::cin >> h; -
std::cin >> w; -
std::cout << "Enter height:\t"; -
std::cout << "Enter width:\t"; -
std::cout << area << '\n';
Fix this program so that it compiles.
1int main() {
2 21 = value;
3 double value;
4
5 std::cout << value << '\n';
6}
Given the following program:
1int main() {
2 int a = 7;
3 int b = 4;
4
5 if (a<=b) {
6 a = 99;
7 } else {
8 int t = a;
9 a = b;
10 b = t;
11 }
12 return a;
13}
What value is returned?
Write a program that accumulates the sum of the numbers 1 - 10 and prints the result.
1int main() {
2
3}
When assembled in its proper order, the following program segment prints 'Odd numbers:' followed by all the odd numbers from 1 - 100, one per line.
-
} } -
for(int num=1; num<=100; ++num) { -
if(num % 2 != 0) { -
if(num * 2 == 0) { #distractor -
int main () { -
std::cout << "Odd numbers:\n"; -
std::cout << '\t' << num << '\n'; -
}
Which of the following statements represent assignment to a variable? Check all that apply.
Write a program that stores your name in a local variable and then prints it.
1#include <iostream>
2
3int main() {
4
5}
Which of the following are legal variable names? Check all that apply.
What are the values of x, y, and z after the following code executes?
int x = 0;
int y = 1;
int z = 2;
--x;
++y;
z+=y;
What is the result of 158 % 10?
What is the result of 3 % 8?
What are the values of x, y, and z after the following code executes?
int x = 3;
int y = 5;
int z = 2;
x = z * 2;
y /= 2;
++z;
Given the following statement:
double x = 2 + 2^3
What is value stored in x?