Part 1: Multiple-choice questions

1. This is a set of rules that must be followed when constructing a program.

a. Punctuation
b. Portability
c. Operators
d. Syntax
e. Key words

2. What statement best describes a variable and its primary purpose?

a. A variable is a collection of eight bits.
b. A variable is a word that has a special meaning to the compiler.
c. A variable is a "line" of code in the body of a program that may change.
d. A variable is a structured, general-purpose language designed primarily for teaching programming.
e. A variable is a named storage location in the computer's memory used for holding a piece of information.

3. Which of the following is NOT a valid C++ identifier?

a. new Word
b. m_x
c. Abcd
d. $_AAA
e. Width8

4. This is a complete instruction that causes the computer to perform some action.

a. Main
b. Statement
c. Line
d. Variable
e. Key Word

5. Which of the following CANNOT cause a syntax error to be reported by the C++ compiler?

a. Missing ;
b. Mismatched {}
c. An extra blank line.
d. An extra main function
e. Missing */ in a comment that begins with /*

6. You want the user to enter the length, width, and height from the keyboard. Which cin statement is correctly written?

a. cin.get(length, width, height);
b. cin << length, width, height;
c. cin >> length >> width >> height;
d. cin >> length, width, height;

7. Which of the following is NOT a control structure?

a. Selection structure
b. Sequence structure
c. Repetition structure
d. Declaration structure
e. All of the above

8. Which of the following is a double-selection control statement?

a. switch
b. do ... while
c. if
d. else if
e. if ... else

9. Which loop will iterate at least once no matter what the value of its test expression is?

a. while loop
b. for loop
c. do-while loop
d. Both while loop and for loop

10. A loop that does not have a way of stopping is

a. an infinite loop
b. a pre-test loop
c. a post-test loop
d. a good loop
e. None of the above

Part 2: Short Answer questions

1. What will the following code display?

cout << "This " << "is \n";
cout << "the " << "first/n";
cout << "test." << endl;

2. What is the output of the following statement?

cout << 5 + (26 - 9) % 2 << endl;

3. What is the value stored at x, given the following statements?

int x;
double y=13.0
x = y / static_cast< int >(3.4 + 9.3);

4. What are the result values of c and d at the end of the following code segment?

int c=9, d=13;
c++;
++c;
(++d)++;
c %= 5;
d /= 6;

5. What will be the output of the following code segment after the user enters 0 at the keyboard?

int x=-1;
cout << "Enter a 0 or a 1 from the keyboard: ";
cin >> x;
if (x==1)
cout << "It is for true!" << endl;
else
cout << "It is for false!" << endl;

6. Look at the following code segment, what will the do-while loop display on the screen?

int x=0;
do
{
cout << x << '\n';
x++;
} while (x<6);

7. Which line in the following program will cause a compiler error? How to fix?

1 #include < iostream >
2 using namespace std;
3
4 int main()
5 {
6 int number=5;
7 do {
8 cout << "The number is " << number << endl;
9 number--;
10 } while (number>0)
11
12 return 0;
13 }

Part 3: Error-correction questions

Find out and correct the errors in the following program as many as you can:

1 /*
2 This is an example for Case Study of Control Statements
3 It will find out the largest number from the 10 user-entered numbers
4 October 9, 2019
5 *
6
7 #include iostream //preprocessor directive
8 #using namespace std //namespace definition
9
10 main[ ]
11 {
12 //declare variables
13 int number,
14 int counter=1,
15 int largest=-9999;
16
17 //use while loop to request input & process
18 while (counter <= 10);
19 {
20 cout << "Please enter integer #" << counter << ": ";
21 cin << number;
22
23 + counter;
24
25 if (number > largest)
26 }
27 largest == number;
28 cout << "Find a new largest number!\n";
29 }
30 otherwise
31 cout << "Not a new largest number!\n";
32 }
33
34 //output result
35 cout >> "\nThe largest number is " << largest << ".\n\n";
36
37 Return 0;
38 } //end of main

Part 4: Programming questions

The following code segment use control structure switch-case statement to make decision. num and total are int type variable defined previously. Answer the following two questions:

cout << "Enter a number from 1 to 10: ";
cin >> num;
switch (num)
{
case 1: total = 1; break;
case 2: total = 5; break;
case 3: total = 10; break;
case 4: total = 20; break;
default: total = 40;
}
cout << total << endl;

i. When user enters 6, what is the output of total? Briefly explain why.

ii. What user should enter if he likes to get the total of 20? Briefly explain why.

Matching

Match the phases in the left column to the description in the right column:

1. body of a function a. It refers to a range where an identifier is accessible (visible) in a program.
2. a formal parameter b. the syntax of a value-returning function header
3. a local declaration c. an identifier delcared outside of every function definition
4. function prototype d. A calling function (caller) gives a called function (callee) the ability to access and potentially modify the caller's data directly
5. returnType functionName (formal parameter list) e. It is the code required to accomplish a function's task and usually enclosed within a pair of curly braces.
6. functionName (actual parameter list); f. the syntax of a void function call
7. a reference parameter g. a function heading without the body of the function, ending with semi colon
8. global identifier h. multiple function declarations with same name but different formal parameter set
9. the scope of an identifier i. A copy of the argument's value is made and passed to the called function
10. function overloading j. a declaration of a variable inside a function for use only within that function
11. pass by value k. a formal parameter that receives the location (or memory address) of the corresponding actual parameter.
12. pass by reference l. a variable declared in the function heading

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.