Be aware that some of the following questions are properly characterized as "trick questions." Nevertheless, I include them here because they represent common programming errors, and you need to begin training yourself to see them. Please study Null Statements and The Dangling Else Problem carefully.
- Choose a C++ expression that uses a relational operator to evaluate to true if the variable dilbert is not equal to alice.
- dilbert == alice
- dilbert != alice
- dilbert <> alice
- dilbert =! alice
- Is -1 considered to be true or false?
- true
- false
- Is 0 considered to be true or false?
- true
- false
- In a for loop with a multi-statement loop body, semicolons must appear following
- the for statement itself.
- the closing brace in a multi-statement loop body.
- each statement within the loop body.
- True or false: The update expression in a for loop can decrement the loop variable.
- true
- false
- Choose a for loop that displays the numbers from 100 to 110.
- for (int i = 100; i <= 110; i++) cout << i << endl;
- for (int i = 100; i < 111; i++) cout << i << endl;
- for (int i = 100; i >= 110; i++) cout << i << endl;
- for (int i = 100; i > 111; i++) cout << i << endl;
- A block of code is delimited by
- < and >
- ( and )
- [ and ]
- { and }
- A variable defined within a block is visible
- from the point of definition to the end of the program.
- throughout the program.
- from the point of definition to the end of the block.
- throughout the block.
- Write a while loop and any additional statements needed to display the numbers from 100 to 110.
- aint i = 100; while (i <= 110) cout << i++ << endl;
- int i = 99; while (i <= 109) cout << ++i << endl;
- int i = 100; while (i < 111) cout << i++ << endl;
- int i = 99; while (i < 110) cout << ++i << endl;
- True or false: Relational operators have a higher precedence than arithmetic operators.
- true
- false
- How many times is the loop body executed in a do-while loop?
- only once
- depends entirely on the controlling expression
- at least once but maybe more depending on the controlling expression
- Choose a do-while loop that displays the numbers from 100 to 110.
int i = 99;
do
cout << ++i << endl;
while (i < 110);
int i = 99;
do
{
cout << ++i << endl;
} while (i < 110);
int i = 100;
do
cout << i++ << endl;
while (i <= 110);
int i = 100;
do
{
cout << i++ << endl;
} while (i <= 110);
- Choose an if statement that prints Yes if a variable age is greater than 21.
- if (age > 21) cout << "Yes" << endl;
- if (21 <= age) cout << "Yes" << endl;
- if (age < 21) cout << "Yes" << endl;
- if (21 >= age) cout << "Yes" << endl;
- The library function exit() causes an exit from
- the loop in which it occurs.
- the block in which it occurs.
- the function in which it occurs.
- the program in which it occurs.
- The break statement causes an exit from
- the loop in which it occurs.
- the block in which it occurs.
- the function in which it occurs.
- the program in which it occurs.
- Choose an if-else statement that displays Yes if a variable age is greater than 21, and displays No otherwise.
if (age > 21)
cout << "Yes" << endl;
else
cout << "No" << endl;
if (21 <= age)
cout << "Yes" << endl;
else
cout << "No" << endl;
if (age < 21)
cout << "Yes" << endl;
else
cout << "No" << endl;
if (21 >= age)
cout << "Yes" << endl;
else
cout << "No" << endl;
- The _getche() library function
- returns and displays a character when a key is pressed on the keyboard.
- displays but does not return a character on the screen.
- returns but does not display a character on the screen.
- returns and displays a character when the Enter key is pressed.
- Choose a switch statement that prints Yes if a variable ch is 'y', prints No if ch is 'n', and prints Unknown response otherwise.
switch (ch)
{
default:
cout << "Unknown response" << endl;
break;
case 'y' :
cout << "Yes" << endl;
break;
case 'n' :
cout << "No" << endl;
break;
}
switch (ch)
{
case 'y' :
cout << "Yes" << endl;
break;
case 'n' :
cout << "No" << endl;
break;
default:
cout << "Unknown response" << endl;
break;
}
switch (ch)
{
case 'y' :
cout << "Yes" << endl;
break;
case 'n':
cout << "No" << endl;
break;
default:
cout << "Unknown response" << endl;
}
switch (ch)
{
default:
cout << "Unknown response" << endl;
break;
case 'y' :
cout << "Yes" << endl;
break;
case 'n':
cout << "No" << endl;
}
- Choose a statement that uses a conditional operator to set ticket to 1 if speed is greater than 55 and to 0 otherwise.
- ticket = (speed > 55) ? 1 : 0;
- ticket = speed > 55 ? 1 : 0;
- ticket = (speed <= 55) ? 0 : 1;
- ticket = speed <= 55 ? 0 : 1;
- Choose an expression involving a logical operator that is true if limit is 55 and speed is greater than 55.
- limit == 55 && speed > 55
- speed > 55 && limit == 55
- limit > 55 && speed = 55
- limit = 55 && speed > 55
- The break statement causes an exit
- only from the innermost loop.
- only from the innermost switch.
- from all loops and switches.
- from the innermost loop or switch.
- Executing the continue operator from within a loop causes control to go to
- pot
- the next iteration of the loop
- the next statement following the loop
- What does the following code print?
int x = 10;
{
int x = 5;
}
cout << x << endl;
- 5
- 10
- It will not compile because it defines two variables with the name.
- It will compile but crash during execution because there are two variables with the same name.
- What does the following code print?
int x = 10;
{
int x = 5;
cout << x << endl;
}
- 5
- 10
- It will not compile because it defines two variables with the name.
- It will compile but crash during execution because there are two variables with the same name.
- How many lines of output will the following code fragment print?
for (int i = 0; i < 10; i++)
cout << "Hello world" << endl;
- 0
- 9
- 10
- 11
- Creates an infinite loop
- How many lines of output will the following code fragment print?
for (int i = 0; i <= 10; i++)
cout << "Hello world" << endl;
- 0
- 9
- 10
- 11
- Creates an infinite loop
- How many lines of output will the following code fragment print?
int counter = 0;
while (counter < 100)
{
cout << "counter = " << counter << endl;
counter++;
}
- 0
- 1
- 100
- 101
- Creates an infinite loop
- How many lines of output will the following code fragment print?
int counter = 100;
while (counter > 0)
{
cout << "counter = " << counter << endl;
counter--;
}
- 0
- 1
- 100
- 101
- Creates an infinite loop
- How many lines of output does the following code fragment print?
int a = 10;
int b = 20;
if (a > b)
cout << "It is bigger\n";
- 0
- 1
- Will not compile: missing else part of statement
- What output does the following code fragment produce?
int flag = 0;
if (flag)
cout << "It is true\n";
else
cout << "It is false\n";
- It is true
- It is false
- The code will not compile because there is no relational operator in the if-statement test
- The code will compile but will not run because there is no relational operator in the if-statement test
- What value does the following statement store in variable c if a = 5 and b = 10?
c = (a < b) ? (b - a) : (b + a);
- 0
- 5
- 10
- 15
- It is a malformed statement and will not compile
- What output does the following code fragment produce?
double d = 3.14159;
int i = 0;
if (d > i)
cout << "It is greater\n";
- It is greater
- No output is produced
- The code will not compile because you can’t compare a double with an int
- How many lines of output will the following code fragment print?
for (int i = 0; i < 10; i++)
cout << "Looping\n";
cout << "Hello world" << endl;
- 9
- 10
- 11
- 19
- 20
- 21
- What does the following code print?
int a = 0;
int b = 0;
if (a < 10)
if (b > 100)
cout << "Hello\n";
else
cout << "World\n";
- Hello
- World
- It does not print anything
- It is a malformed if-statement and will not compile
- How many lines of output will the following code fragment print?
for (int i = 0; i < 10; i--)
cout << "Hello world" << endl;
- 0
- 9
- 10
- 11
- Creates an infinite loop
- How many lines of output will the following code fragment print?
int i = 0;
for (; i < 10; i++)
cout << "Hello world" << endl;
- 9
- 10
- 11
- The code will not compile because the for-statement is missing the first expression.
- Creates an infinite loop
- How many lines of output will the following code fragment print?
int counter = 0;
do
{
cout << "counter = " << counter << endl;
counter++;
} while (counter < 10);
- 0
- 1
- 10
- 11
- Creates an infinite loop
- How many lines of output will the following code fragment print?
int counter = 100;
while (counter < 100)
{
cout << "counter = " << counter << endl;
counter++;
}
- 0
- 1
- 100
- 101
- Creates an infinite loop
- How many lines of output will the following code fragment print?
for (int i = 10; i < 10; i--)
cout << "Hello world" << endl;
- 0
- 1
- 9
- 10
- 11
- Creates an infinite loop
- How many lines of output will the following code fragment print?
int counter = 100;
do
{
cout << "counter = " << counter << endl;
counter--;
} while (counter < 10);
- 0
- 1
- 10
- 11
- Creates an infinite loop
- What output does the following code fragment produce?
int i = 5;
switch(i)
{
default :
cout << "sixth\n";
break;
case 0 :
cout << "first\n";
break;
case 1 :
cout << "second\n";
case 10 :
cout << "third\n";
case 100 :
cout << "fourth\n";
break;
case 1000 :
cout << "fifth\n";
break;
}
- first
- second
- third
- fourth
- third and fourth
- sixth
- Does not compile: the default statement must come at the end
- What output is produced by the following code fragment?
int i = 100;
switch(i)
{
case 0 :
cout << "first\n";
break;
case 1 :
cout << "second\n";
break;
case 10 :
cout << "third\n";
case 100 :
cout << "fourth\n";
case 1000 :
cout << "fifth\n";
break;
default :
cout << "sixth\n";
break;
}
- first
- second
- third
- fourth
- fourth and fifth, on separate lines
- fourth, fifth, and sixth, on separate lines
- How many lines of output are produced by the following code?
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
cout << i * j << endl;
- 0
- 1
- 10
- 100
- 1000
- Will not compile or run
- How many lines of output are produced by the following code?
for (int i = 0; i < 10; i++)
{
if (i == 0)
break;
for (int j = 0; j < 10; j++)
cout << i * j << endl;
}
- How many lines of output are produced by the following code?
for (int i = 0; i < 10; i++)
{
if (i == 0)
continue;
for (int j = 0; j < 10; j++)
cout << i * j << endl;
}
- How many lines of output are produced by the following code?
int counter = 0;
while (counter++ < 10);
cout << "Hello world" << endl;
- How many lines of output will the following code fragment print?
for (int i = 10; i < 10; i--);
cout << "Hello world" << endl;
- 0
- 1
- 9
- 10
- 11
- Creates an infinite loop
- How many lines of output will the following code fragment print?
int counter = 100;
while (counter < 100);
{
cout << "counter = " << counter << endl;
counter++;
}
- 0
- 1
- 100
- 101
- Creates an infinite loop
- How many lines of output will the following code fragment print?
do
{
int i = 0;
cout << i++ << endl;
} while (i < 10);
- 0
- 1
- 9
- 10
- 11
- The code will not compile.
- Write a for loop that counts from 0 to 10 by twos: 0, 2, 4, ..., 10. You may not use if-statements, or the comma or conditional operators.
- Write a for loop that counts backwards from 10 to 0 by twos: 10, 8, 6, ..., 0. You may not use if-statements, or the comma or conditional operators.
- Write a complete program that:
- prompts the user to enter a sequence of non-negative integers (i.e., all input integers are ≥ 0)
- calculates the total of the the integers
- the user will enter a -1 to end input (but the -1 is not included in the total)
- after the user enters a -1, the program prints the total and ends
- Avoid convoluted logic (the code must be simple and straightforward); do not include unneeded variables or statements