1. What is the output of the following code fragment? (All
variables are of type int.)
limit = 8;
cout << 'H';
for (loopCount = 10; loopCount <= limit; loopCount++)
cout << 'E';
cout << "LP");
2. What is the output of the following code fragment if the
input value is 4? (Be careful here.)
int num;
int alpha = 10;
cin >> num;
switch (num)
{
case 3 : alpha++;
case 4 : alpha = alpha + 2;
case 8 : alpha = alpha + 3;
default : alpha = alpha + 4;
}
cout << alpha << endl;
3. What is the output of the following code fragment? (All
variables are of type int.)
n = 2;
for (loopCount = 1; loopCount <= 3; loopCount++)
while (n <= 4) |
n = 2 * n; |
cout << n << endl;
4. Which of the following is not an assertion?
a. a function postcondition
b. both a and b
c. a switch label
d. a function precondition
5. What is the output of the following code fragment? (loopCount
is of type int.)
for (loopCount = 1; loopCount > 3; loopCount++)
cout << loopCount << ' ';
cout << "Done" << endl;
6. True or False? (), ++, and – represent three operators that have the highest grouped precedence and ==, +=, -=, ∗=, and / represent three operators that have the lowest grouped precedence?
7. True or False? The code segment
cout << "Do you wish to continue? ";
cin >> response;
while (response != 'Y' && response != 'N')
{
cout << "Do you wish to continue? ";
cin >> response;
}
is equivalent to the following code segment.
do
{
cout << "Do you wish to continue? ";
cin >> response;
} while (response != 'Y' && response != 'N');
8. True or False? The statement
switch (n)
{
case 8 : alpha++; break;
case 3 : beta++; break;
default: gamma++; break;
}
is equivalent to the following statement.
if (n == 8)
alpha++;
else if (n == 3)
beta++;
else
gamma++;
9. What is the value of sum after execution of the following For
loop?
sum = 0;
for (count = 1; count <= 21; count++)
sum = sum + count;
10. True or False? The increment and decrement operators (++ and --) operate only on variables, not on constants or arbitrary expressions
1)Output: HLP
Explanation:
If for loop has no curly braces({ }), then the output will be HLP
2)Output: 19
Explanation:
if input (num) is 4, then switch with case 4 will be executed and alpha value will be 10(initial alpha value)+2=12.
Then since case 4 has no break point,control goes to next statement(case 8)
Now alpha = alpha + 3 = 12 + 3 = 15; Since case 8 has also now break point, default statement will also be executed.
Now alpha = alpha + 4= 15 + 4 = 19.
3)Output:
4
8
4)Answer: c)a switch label
5)Done
Explanation:
Because loopCount = 1 is not greater than 3. So loop will never executed.
6)True
7)True
8)True.
Explanation;
Both switch case and if-else statements are logically correct.
9)Answer: 231
Value of sum after execution of the For loop is 231.
10) True
Explanation:
int speed = 25;
speed++; // speed will be incremented by 1
but it does not work for constants. Example 25++ is compile time error.
Get Answers For Free
Most questions answered within 1 hours.