In: Computer Science
5C++ Questions
1. What will the following code print?
num = 8;
cout << --num << " ";
cout << num++ << " ";
cout << num;
2.Since the while loop evaluates the condition before executing the statement(s), it is known as a(n)____ loop, whereas the do-while loop evaluates the condition after the statements have been executed, it is known as a(n)____ loop.
3.T/F While loops can only be used when a counter is decremented to zero. If the counter is incremented you must use the do-while loop.
4.How many times will the following program print Hello?
counter = 3; while (counter > 0) { loop = 5; while (loop > 0) { cout << "Hello" << endl; loop--; } counter--; }
5.If nothing within a while loop causes the condition to become false, a(n) ________ may occur.
6.The -- operator ... (check all that apply)
subtracts one from the value of its operand. |
||
must have an lvalue, such as a variable, as its operand. |
||
can be used in either prefix or postfix mode. |
||
is a unary operator. |
7.Which of the following statements will result in an infinite loop.
x = 10; while (x >= 0) { cout << x; ++x; } |
||
x = 10; while (x >= 0) { cout << x; } |
||
x = 0; while (x <= 10) { cout << x; x = 10; } |
||
x = 10; while (x >= 0); { cout << x; x--; } |
8.T/F In C++, a file must be opened before the contents can be read.
9. T/F In C++, a file must exists before it can be written to.
10.Which of the following statements show the proper way to use the increment (++) operator.
x++ = 1023; |
||
x = 23++; |
||
y = ++x; |
||
counter--; |
Question 1: Output of the code 7 7 8 Answer: 7 7 8 Question 2: Since the while loop evaluates the condition before executing the statement(s), it is known as a(n) pre condition loop, whereas the do-while loop evaluates the condition after the statements have been executed, it is known as a(n) pre condition loop. Answer: pre condition post condition Question 3: While loops can only be used when a counter is decremented to zero. If the counter is incremented you must use the do-while loop. Answer: False Question 4: Number of times will the following program print Hello is 15 Answer: 15 Question 5: If nothing within a while loop causes the condition to become false, a(n) infinite loop may occur. Answer: infinite loop Question 6: The -- operator can be used in either prefix or postfix mode. Answer: can be used in either prefix or postfix mode. Question 7: x = 10; while (x >= 0) { cout << x; ++x; } x = 10; while (x >= 0) { cout << x; } x = 0; while (x <= 10) { cout << x; x = 10; } will result in an infinite loop Answer: Options 1,2,3 Question 8: In C++, a file must be opened before the contents can be read. Answer: True Question 9: In C++, a file must exists before it can be written to. Answer: False Question 10: statements show the proper way to use the increment (++) operator is y = ++x; Answer: y = ++x;