Docsity

Solutions for Exam 2 - Structured Programming in C/C++ | CSCE 206, Exams of Computer Science

Toggle sidebar
bg1
CSCE 206 Structured Programming in C/C++ (501-506)
2016 Spring Exam 2
-- A --
Name: ______________________________________________ UIN: ________________________________________________
TA: ________________________________________________ Section: ______________________________________________
TRUE/FALSE. Write 'T' if the statement is true and 'F' if the statement is false.
1)
The following code correctly determines whether x contains a value in the range of 0 through
100.
if (x >= 0 && <= 100)
1)
2)
It is possible for a function to have some parameters with default arguments and some without.
2)
3)
When a function is called, flow of control moves to the function's prototype.
3)
4)
A static variable that is defined within a function is initialized only once, the first time the
function is called.
4)
5)
You may not use the break and continue statements within the same set of nested loops.
5)
6)
If the sub-expression on the left side of an && operator is false, the expression on the right side
will not be checked.
6)
7)
The increment and decrement operators can be used in mathematical expressions; however, they
cannot be used in relational expressions.
7)
8)
The default section is required in a switch statement.
8)
9)
The update expression of a for loop can contain more than one statement, e.g. counter++, total+=
sales.
9)
10)
A local variable and a global variable may not have the same name within the same program.
10)
11)
An expression that has any value other than 0 is considered true by an if statement.
11)
12)
As a rule of style, when writing an if statement you should indent the conditionally-executed
statements.
12)
13)
You must furnish an argument with a function call.
13)
14)
A function's return data type must be the same as the function's parameter(s).
14)
15)
It is not considered good programming practice to declare all of your variables globally.
15)
1
F
T
F
T
F
T
F
F
T
F
T
T
F
F
T
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Solutions for Exam 2 - Structured Programming in C/C++ | CSCE 206 and more Exams Computer Science in PDF only on Docsity!

CSCE 206 Structured Programming in C/C ++ (501 - 506)

2016 Spring Exam 2

-- A --

Name: ______________________________________________ UIN: ________________________________________________ TA: ________________________________________________ Section: ______________________________________________ TRUE/FALSE. Write 'T' if the statement is true and 'F' if the statement is false.

  1. The following code correctly determines whether x contains a value in the range of 0 through

if (x >= 0 && <= 100)

  1. It is possible for a function to have some parameters with default arguments and some without. 2)
  2. When a function is called, flow of control moves to the function's prototype. 3)
  3. A static variable that is defined within a function is initialized only once, the first time the function is called.
  1. You may not use the break and continue statements within the same set of nested loops. 5)
  2. If the sub-expression on the left side of an && operator is false, the expression on the right side will not be checked.
  1. The increment and decrement operators can be used in mathematical expressions; however, they cannot be used in relational expressions.
  1. The default section is required in a switch statement. 8)
  2. The update expression of a for loop can contain more than one statement, e.g. counter++, total+= sales.
  1. A local variable and a global variable may not have the same name within the same program. 10)
  2. An expression that has any value other than 0 is considered true by an if statement. 11)
  3. As a rule of style, when writing an if statement you should indent the conditionally-executed statements.
  1. You must furnish an argument with a function call. 13)
  2. A function's return data type must be the same as the function's parameter(s). 14)
  3. It is not considered good programming practice to declare all of your variables globally. 15) F T F T F T F F T F T T F F T

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

  1. This is a special value that marks the end of a list of values. A) constant B) variable C) sentinel D) loop E) None of these
  1. The while loop has two important parts: an expression that is tested for a true or false value, and: A) a statement or block that is repeated only if the expression is false B) a statement or block that is repeated as long as the expression is true C) one line of code that is repeated once, if the expression is true D) a statement or block that is repeated once, if the expression is true E) None of the above.
  1. This is a variable, usually a boolean or an integer, that signals when a condition exists. A) float B) relational operator C) arithmetic operator D) flag E) None of these
  1. Something within a while loop must eventually cause the condition to become false, or a(n) ________ results. A) compiler error B) null value C) unexpected exit D) infinite loop E) None of these
  1. This is a pre-test loop that is ideal in situations where you want the loop to iterate if the condition is false from the beginning. A) infinite B) while C) for D) do-while E) None of these
  1. This loop is a good choice when you know how many times you want the loop to iterate in advance of entering the loop. A) do-while B) while C) infinite D) for E) None of these

C B D D E D

  1. Given the following function definition void calc (int a, int& b) { int c; c = a + 2; a = a * 3; b = c + a; } What is the output of the following code fragment that invokes calc? (All variables are of type int ) x = 1; y = 2; z = 3; calc(x, y); cout << x << " " << y << " " << z << endl; A) 1 14 9 B) 1 6 3 C) 3 6 3 D) 1 2 3 E) None of these
  1. This statement may be used to stop a loop's current iteration and begin the next one. A) break B) continue C) re-iterate D) terminate E) None of these
  1. Look at the following statement. while (x++ < 10) Which operator is used first? A) < B) ++ C) Neither. The expression is invalid.

D) ++ is not executed, just < E) None of the above.

  1. What will the following code display? int number = 6; cout << number++ << endl; A) 0 B) 5 C) 6 D) 7

E) None of the above. B B A C

  1. What will the following segment of code output? You can assume the user enters a grade of 90 from the keyboard. cout << "Enter a test score: "; cin >> test_score; if (test_score < 60) cout << "You failed the test!" << endl; if (test_score > 60) cout << "You passed the test!" << endl; else cout << "You need to study for the next test!"; A) You failed the test! You did poorly on the test! B) You passed the test! C) You failed the test! D) You failed the test! You passed the test! E) None of these
  1. What is the output of the following code segment? n = 1; for ( ; n <= 5; ) cout << n << ' '; n++; A) 2 3 4 5 6 B) 1 2 3 4 5 C) 1 2 3 4 D) 2 3 4 5 E) 1 1 1 ... and on forever
  1. In a function definition's header, you must furnish: A) data type(s) of the parameters B) names of parameter variables C) data type of the return value D) the name of function E) All of these
  1. The ________ of a variable is limited to the block in which it is declared. A) branching ability B) precedence C) scope D) associativity E) None of these

B E E C

  1. What is the output of the following program?

include

using namespace std; void doSomething(int); int main( ) { int x = 2; cout << x << endl; doSomething(x); cout << x << endl; return 0; } void doSomething(int num) { num = 0; cout << num << endl; } A) 0 0 0

B) 2

C) 2

D) 2

E) None of the above.

  1. What is assigned to the variable "a" given the statement below with the following assumptions: x = 10, y = 7, and z, a, and b are all int variables. a = x >= y; A) 7 B) 1 C) 0 D) 10 E) The string "x >= y"
  1. A loop that is inside another loop is called: A) a pre-test loop B) embedded C) an infinite loop D) a post-test loop E) None of these

D B E

  1. A function can have zero to many parameters, and it can return this many values. A) no B) zero to many C) only one D) a maximum of ten E) None of these
  1. A file must be ________ before data can be written to or read from it. A) initialized B) created C) buffered D) closed E) None of these
  1. These types of arguments are passed to parameters automatically if no argument is provided in the function call. A) global B) local C) relational D) default E) None of these
  1. A file ________ is a small holding section of memory that file-bound information is first written to. A) buffer B) segment C) name D) number E) None of these
  1. If you place a semicolon after the statement if (x < y) A) the code will not compile B) the if statement will always evaluate to false C) the compiler will interpret the semicolon as a null statement D) All of the above E) None of these
  1. This operator increments the value of its operand, then uses the value in context. A) prefix increment B) postfix decrement C) prefix decrement D) postfix increment E) None of these

C A C A D E