Self-test 1:
C++ Basic
-
Which of the following are correct ways to declare variables?
- int length, width;
- int length, int width;
- int length; width;
- int length; int width;
SolutionA and D are correct. D is correct although it is not recommended to write two programming statements on the same line.
-
What is the printout of cout << 'z'- 'a'?
- z
- 25
- a
- 26
SolutionB is correct. Some of you may think the answer is D. But since 'a'-'a' should be equal to 0, and we have 26 alphabets, you can figure out that:- 'a'-'a' = 0
- 'b'-'a' = 1
- ....
- 'z'-'a' = 25
-
Which one of the following is/are illegal identifier(s)?
- abcdefg0123
- 1a
- char
- _John
SolutionB and C are illegal identifiers. B is wrong becasue it starts with digit 1. C++ identifer is not allowed to start with a digit (0 - 9). C is wrong because char is a reserved word as it is the name of a basic data type in C++. Note that Char could be a C++ identifier because C++ identifiers are case-sensitive.
-
Which of the following statement outputs strings "first" and "second" in two separate lines?
- cout << "first" << endl << "second";
- cout << "first\tsecond";
- cout << "first\nsecond";
- cout << "first" << "second" << endl;
SolutionA and C are correct. C is correct because \n means to print out a new line, so it is equivalent to A. B prints first and second in the same line, but two words are separated by a tab. D prints out firstsecond on the screen.
-
Which of the following statement prints the following string?
smith\exam1\test.txt- cout << "smith\\exam1\\test.txt";
- cout << "smith\"exam1\"test.txt";
- cout << "smith"\exam1"\test.txt";
- cout << "smith\exam1\test.txt";
SolutionA is correct. B will print out smith"exam1"test.txt. C will encounter compilation errors. D would produce unexpected result.
-
Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 (exclusive) or the number is negative?
- ((x < 100) && (x > 1)) || (x < 0)
- (1 > x > 100) || (x < 0)
- ((x < 100) && (x > 1)) && (x < 0)
- 1 < x < 100 && x < 0
SolutionA is correct. B and D are wrong becasue the expression, 1 > x > 100, is invalid in C++. C is wrong because of the wrong use of the last && operator.
-
Consider the following code fragment:
int main(void) { int x = 100; int y = 100; }After the definitions of variables x and y, what will be the value of the conditional expression (y < x++)?
- true
- false
- The expression cannot be compiled
- Unexpected results
SolutionB. According to the semantics of the post-increment operator ++, x++ will use the current value of x for comparison before x is incremented. Thus, x and y have the same value during the comparison which evaluates to false.
-
Please read the following code, and point out and correct the error(s). If there is/are no error(s), just give the output.
#include<iostream> using namespace std; int main(void) { int a, b; a = b = 0; b = a++; cout << a << " " << b << endl; return 0; }SolutionNo error is found. The output is: 1 0. b=a++; can be treated as two programming statements: b = a; a++;
Notice:
a = b = 0; is not an error. C++ allows cascade assignment --- assigning a value to multiple variables.
-
What is the output of the following program segment?
int main(void) { int a = 10, b = 13, x = 7; x *= ++a * b++; cout << x << endl; return 0; }- 910
- 980
- 1001
- None of the above
SolutionC. Since the operator ++ has a higher precedence than * (refer to the notes for C++ basic), you may consider the statement "x *= ++a * b++;" as: x *= (++a) * (b++); In addition, ++a means that we will increment a and use the result for further operation, while b++ will use the current value of b for some operation first before it is incremented. As a result, the statement is equivalent to:
a = a+1; x *= a * b; // x = x * (a * b); b = b+1;
Therefore, the resulting value of x is 7*11*13=1001.