been doing reiew questions to prepare for a c++ test,
and was hoping someone could help me out by looking over the ansewers i have so far. what i have is in red.

thank you very much :)

-- Which of the following operations has the highest precedence?
a. Postincrement.
b. Multiplication.
c. Addition.
d. Assignment.

-- Which of the following does counter-controlled repetition require?
a. An initial value.
b. A condition that tests for the final value.
c. An increment or decrement by which the control variable is modified each time through the loop.

ANS d. All of the above.

--. If a variable is declared in the initialization expression of a for structure, then:
a. It is automatically reinitialized to zero once the loop is finished.
b. The scope of the variable is restricted to that particular for loop.
c. It retains its final value after the loop is finished.
d. It can not be used in any structures that are nested in that for structure.

-- Which of the following is not true?
a. The three expressions in the for structure are optional.
b. The initialization and increment expressions can be comma-separated lists.
c. You must declare the control variable outside of the for loop.
d. A for loop can always be used to replace a while loop, and vice versa.

--. Consider the execution of the following for loop

for (int x = 1; x < 5; increment )
   cout << x + 1 << endl;

If the last value printed is 5, which of the following might have been used for increment?
a. x++.
b. x += 1.
c. ++x.
d. Any of the above.

-- Which of the following for headers is not valid?
a. for ( int i = 0; i < 10; i++ ).
b. int i = 0;
for ( ; i < 10; i++ ).
c. for ( int i = 0; int j = 5; ; i++ ).
d. All of the above.

-- float and double variables should be used:
a. To perform monetary calculations.
b. As counters.
c. To store true/false values.
d. As imprecise representations of decimal numbers.

-- Which of the following is a parameterized stream manipulator used to format output?
a. setw.
b. right.
c. left.
d. fixed.

-- If a do…while structure is used:
a. An infinite loop cannot take place.
b. Counter-controlled repetition is not possible.
c. The body of the loop will execute at least once.
d. An off-by-one error cannot occur.

-- What will the following program segment do?

int counter = 1;

do 
{
   cout << counter << " ";
} while ( ++counter <= 10 );

a. Print the numbers 1 through 11.
b. Print the numbers 1 through 10.
c. Print the numbers 1 through 9.
d. Cause a syntax error.

-- A switch statement should be used:
a. As a single-selection structure.
b. As a double-selection structure.
c. As a multiple-selection structure.
d. To replace all if…else statements.

-- Which of the following is correct when labeling cases in a switch structure?
a. case1.
b. Case1.
c. case 1.
d. Case 1.

--. switch can be used to test:
a. int constants.
b. float constants.
c. string constants.
d. all types of constants.

-- Which of the following data types can be used to represent integers?
a. char.
b. long.
c. hort.
d. All of the above.

Dani AI

Generated

Nice set of review questions. A couple of quick clarifications that build on what and already pointed out.

  • About the invalid for-header: the middle field of a for is the condition. A declaration like int j = 5 cannot live there, which is why that variant does not compile. If you want two loop variables, declare them together in the init field and separate with commas, then give a real condition, e.g.:

    for (int i = 0, j = 5; i < j; ++i) {
        // use i and j
    }
    // i and j are out of scope here
  • Operator precedence: postfix increment has higher precedence than *, which is higher than +, and all of those are higher than assignment. A quick sanity check:

    int a = 2;
    int b = a++ * 3;  // b == 6, a == 3

    Precedence answers which operators bind first, not evaluation order in general. Avoid writing expressions that modify the same variable more than once in a single statement.

  • do...while always runs at least once. In your example, the increment happens before the comparison, so it prints 1 through 10. A fast way to verify on exams: walk two iterations by hand and check where the increment happens.

  • switch details: the labels must be constant integral (or enum) expressions, and the syntax is case 1: with a colon. For formatting output, setw is the parameterized manipulator (it takes a width), while left, right, and fixed are non-parameterized. Also, floats/doubles are binary and cannot exactly represent many decimal fractions, so do not use them for money. Prefer integer cents or a decimal/fixed-point type from your library if available.

    std::cout << std::setw(6) << 42;  // prints with width 6

Recommended Answers

All 6 Replies

I know we're not supposed to give homework help unless the OP shows an effort, but this does look like you've shown an effort.

These are the ones that don't look right to me:

-- float and double variables should be used:
a. To perform monetary calculations.
b. As counters.
c. To store true/false values.
d. As imprecise representations of decimal numbers.

This is why I think it's not right

-- Which of the following for headers is not valid?
a. for ( int i = 0; i < 10; i++ ).
b. int i = 0;
for ( ; i < 10; i++ ).
c. for ( int i = 0; int j = 5; ; i++ ).
d. All of the above.

(try compiling each of them, you'll see which one it is)

-- Which of the following operations has the highest precedence?
a. Postincrement.
b. Multiplication.
c. Addition.
d. Assignment.

seems to contradict your answer.

I hope this helps you! Good luck on the test. :)

I know we're not supposed to give homework help unless the OP shows an effort, but this does look like you've shown an effort.

These are the ones that don't look right to me:

-- float and double variables should be used:
a. To perform monetary calculations.
b. As counters.
c. To store true/false values.
d. As imprecise representations of decimal numbers.

This is why I think it's not right

-- Which of the following for headers is not valid?
a. for ( int i = 0; i < 10; i++ ).
b. int i = 0;
for ( ; i < 10; i++ ).
c. for ( int i = 0; int j = 5; ; i++ ).
d. All of the above.

(try compiling each of them, you'll see which one it is)

-- Which of the following operations has the highest precedence?
a. Postincrement.
b. Multiplication.
c. Addition.
d. Assignment.

seems to contradict your answer.

I hope this helps you! Good luck on the test. :)

helped alot thanks
looked over and these my new
-- float and double variables should be used:
a. To perform monetary calculations.
b. As counters.
c. To store true/false values.
d. As imprecise representations of decimal numbers.

-- Which of the following for headers is not valid?
a. for ( int i = 0; i < 10; i++ ).
b. int i = 0;
for ( ; i < 10; i++ ).
c. for ( int i = 0; int j = 5; ; i++ ).
d. All of the above.

for this this one, is it wrong because there are 2 ; ; after j=5
b/c when i compile it was only one that didn't work
if it isn't mind explaining?

-- Which of the following operations has the highest precedence?
a. Postincrement.
b. Multiplication.
c. Addition.
d. Assignment.

that chart very useful


how do these look?
again thanks

Those are the answers that I would have put. :)

for the second one would you mind explaining why it is not valid?

Because it wouldn't compile. hah. ;)

Um, I guess it's because there are 3 ;'s ? Also, possibly because there's no condition to end the loop.

I wouldn't think it's because there are 2 ;s in a row, because you could do this:

for ( ;; )

//but I wouldn't, because it's an endless loop (unless you have other conditions within the loop to stop it).

It's because a for is structured as for (initialize; condition; increment) So if you have for ( int i = 0; int j = 5; ; i++ ) which is the initialize, condition, and increment.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.