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.
Q-1: 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.
Fix this program so that it compiles.
Q-4: 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.
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.
- int a;
- This is a declaration
- a = b;
- Correct.
- size_t sz = 10;
- This is a declaration with initialization
- cin >> a;
- This may not look like an assignment, but it is.
- int if = a;
- Does not compile. The word 'if' is a reserved word and can't be used.
Q-7: 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.
- int inner_product_of_a_and_b;
- A ridiculously long, but valid name.
- double* p2;
- Correct.
- char 1st_letter;
- A variable may not start with a number
- long large num;
- A variable can't contain spaces or most special characters
- long double _d;
- Correct. 'long double' is a single type.
Q-9: Which of the following are legal variable names? Check all that apply.
- x = -1, y = 1, z = 4
- This code subtracts one from x, adds one to y, and then sets z to to the value in z plus the current value of y.
- x = -1, y = 2, z = 3
- This code subtracts one from x, adds one to y, and then sets z to to the value in z plus the current value of y.
- x = -1, y = 2, z = 2
- This code subtracts one from x, adds one to y, and then sets z to to the value in z plus the current value of y.
- x = -1, y = 2, z = 2
- This code subtracts one from x, adds one to y, and then sets z to to the value in z plus the current value of y.
- x = -1, y = 2, z = 4
- This code subtracts one from x, adds one to y, and then sets z to to the value in z plus the current value of y.
Q-10: 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;
- 15
- This would be the result of 158 divided by 10. Modulus gives you the remainder.
- 16
- Modulus gives you the remainder after the division.
- 8
- When you divide 158 by 10 you get a remainder of 8.
Q-11: What is the result of 158 % 10?
- 3
- 8 goes into 3 no times so the remainder is 3. The remainder of a smaller number divided by a larger number is always the smaller number!
- 2
- This would be the remainder if the question was 8 % 3 but here we are asking for the reminder after we divide 3 by 8.
- 8
- What is the remainder after you divide 3 by 8?
Q-12: What is the result of 3 % 8?
- x = 6, y = 2.5, z = 2
- This code sets x to z * 2 (4), y to y divided by 2 (5 / 2 = 2) and z = to z + 1 (2 + 1 = 3).
- x = 4, y = 2.5, z = 2
- Variable y must be an int, z is incremented
- x = 4, y = 2, z = 3
- Correct
- x = 4, y = 2.5, z = 3
- This code sets x to z * 2 (4), y must be an int
- x = 6, y = 2, z = 3
- This code sets x to z * 2 (4)
Q-13: 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;
Q-14: Given the following statement:
double x = 2 + 2^3
What is value stored in x
?