CSCE 206: Structured Programming in C++ 2017 Spring Exam 2 Monday, March 20, 2017 Total - 100 Points B Instructions: Total of 13 pages, including this cover and the last page. Before starting the exam, count the pages to make sure you have all 13. Read the questions and instructions carefully to see what kind of answer is expected. Some code snippets may not have the header and std namespace inclusion, but assume that there are included. Put the Exam Code on your scantron in the lower right corner There are 50 questions worth 2 points each. On my honor, as an Aggie, I have neither given nor received unauthorized aid on this academic work Signature: Name (Print): UIN: Section: 1
TRUE/FALSE: Select A if the statement is true and B if the statement is false. 1) If the specified input file doesn t exist, the program will create one when it tries to open it. 2) The >> operator not only reads data from a file, but also returns a true or false value indicating whether the data was successfully read or not. 3) When evaluating a conditional expression, any value except 0 and negative values is considered true. 4) The following expression will evaluate to true given x = 115, y = 27, z = 3. (x <= z) (y!= z) 5) Following code snippet generates compilation error. int a; char x = '&'; a = x > 100? 0 : 1; 6) The output of the following code snippet is 01234 for (int i = 0; i < 5; i++) cout << i; i++; 7) A function prototype eliminates the need to place a function definition before all calls to the function. 8) The result of expression 1 < 2 is same as the expression 1 < 2. 9) If not all the parameters of a function have default values, the ones that don t are declared first in the parameter list. 10) When passing by value, changes to the parameter in the function do not affect the value of the argument. 11) Local (auto) variables, static variables, and global variables are all created on the stack. 12) The expression of a switch statement must be an integer variable or an expression that evaluates to an integer value. 2
13) The following if statement will test whether x is between 0 and 4: if (0 <= x <= 4) 14) In a nested loop, the inner loop goes through all repetitions for each repetition of the outer loop. 15) The value of c after the execution of the following program is 31. int a = 5; int b = 6; int c = ++(a*b); MULTIPLE CHOICE: Choose the the single option that best completes the statement or answers the question. 16) Which type of loop is best when the number of iterations is known? A) for B) while C) do-while D) Both B and C E) Both A and C 17) What is the value of n after the following code is executed? int n = 8; (n % 2 == 0)? n = n/2 : n = (3*n)+1; A) 8 B) 25 C) 4 E) Either A or B, depending on the computer. 3
18) What is the output of the following code? for (int i = 0; i > 4; i++) cout << i*i << ; A) 0 1 4 9 B) 0 1 4 9 16 C) No output E) Garbage value 19) If a switch statement without a default does not have a matching case for the value being evaluated, the program: A) Ends immediately B) Moves to the first line of code after the switch statement C) Has a runtime error D) Has a compilation error E) Prints garbage values 20) A function s local variables exist. A) As long as the program is executing B) From the first function call in the program to the end of the program C) From the first function call in the program to the last function call in the program D) As long as the function is executing 21) A function definition includes: A) Return type B) Function name C) Parameter List D) Function body E) All of these 22) Which type of loop is best when the loop must execute at least once? A) for B) while C) do-while D) Any of these 4
23) What is the output of the following code? int count = 0; while (count < 5); cout << Hello << endl; count++; A) An infinite loop B) Hello will print four times C) Hello will print five times 24) Which of the following expressions will return true if x is between 27 and 38? A) 27 < x < 38 B) 27 <= x <= 38 C) 27 < x x < 38 D) 27 <= x x <= 38 E) 27 < x && x < 38 25) When you print a variable within an inner block that has the same name as the variable in an outer block, the result is: A) The value of the outer variable is printed B) The value of the inner variable is printed C) A runtime error E) Garbage value is printed 26) If multiple expressions in an if/else if block are true, the program will. A) Throw an compilation error B) Execute all blocks with a true expression C) Execute one of the blocks with a true expression at random D) Execute the first block with a true expression E) Halt abruptly 5
27) What is the output of the following code? int count = 0; do cout << Goodbye << endl; count++; while (count < 5) A) An infinite loop B) Goodbye will print 4 times C) Goodbye will print 5 times 28) Which of the following is a valid function prototype for a function that takes a single integer argument and doesn t return a value? A) void displayprice(int); B) void displayprice(int) C) displayprice(int price); D) displayprice(int); E) Both B and D 29) What is the output of the following code? int grade = 83; if (grade >= 90) cout << Congratulations, ; cout << you got an A! ; A) Congratulations, B) Congratulations, you got an A! C) Congratulations, you got an A! 6
30) What is the output of the following code? int x = 12; int p = 10 * --x * x++; cout << p++; A) 1320 B) 1210 C) 1321 D) 1211 31) Inspecting user input to determine whether it is acceptable or not is called. A) Menu selection B) Compiling C) Input grounding D) Range checking 32) What is the output of the following code? int x = 23; if (x < 20); cout << x is less than 20 ; A) x is less than 20 B) There is no output C) A runtime error 7
33) What is the output of the following code? int sum(int, int = 5, int); int main() cout << sum(12, 15); return 0; int sum(int val1, int val2, int val3) return val1 + val2 + val3; A) 17 B) 27 C) 32 D) A runtime error E) Compilation error 34) What is the output of the following code? bool rainy = true, cold = true; if (rainy) cout << It is raining today ; else if (cold) cout << It is cold today ; else cout << The weather is nice today ; A) It is raining today B) It is cold today C) It is raining today It is cold today D) The weather is nice today E) The program will crash 8
35) What is the output of the following program. int a = 3 * 4; int b = 4; if(a%b == 0) cout << "Variable" << a << "is even"; break; A) Variable 4 is even B) Variable a is even C) Compilation error D) Run time error E) No output 36) Values passed to a function are called, variables in a function that hold the values passed to it are called. A) Arguments, specifications B) Arguments, parameters C) Parameters, arguments D) Parameters, specifications E) Formal argument, Actual argument 37) The correct order of operations for logical operators based on their precedence is: A), &&,! B) &&,,! C) &&,!, D)!, &&, E)!,, && 38) To read/write to files, the header must be included. A) iostream B) filestream C) fstream D) fileio 9
39) What is the output of the following code? int n = 1; switch (n) case 0: cout << Four score << endl; break; case 1: cout << and seven << endl; break; default: cout << years ago << endl; A) Four score B) and seven C) years ago D) and seven years ago E) Compilation error 40) Which of the following is a valid function call for a function that takes two integer arguments and returns an integer value? A) sum(int, int); B) sum(4, 5); C) sum(int num1, int num2); D) int sum(4, 5); E) Both B and D 41) Functions with the same name, but different parameter lists are called functions. A) Overwritten B) Overpowered C) Overloaded D) Overflow E) Shortcut 10
42) Which of the following statements will increase x by 1? A) n += 1; B) n++; C) ++n; D) n = n+1; E) All of these 43) What is the value of n after the following code is executed? int addstatic(int); int main() int n = 0; n = addstatic(n); n = addstatic(n); return 0; int addstatic(int n) static int x = 0; return n + ++x; A) 0 B) 1 C) 2 D) 3 E) Compilation error 11
44) What is the output of the following code snippet. int a = 8, b = 7, c = 3; c *= a + b++; b = c++ + ++b; cout << a << " " << b << " " << c; A) 8 54 46 B) 8 40 32 C) 8 65 57 D) Depends on the computer E) Compilation error 45) Which keyword can be used to terminate the execution of a loop? A) stop B) halt C) exit D) break 46) A compiler must know the following about a function before it is called, except: A) Its name B) Its return type C) The number of the parameters D) The data type of each parameter E) It s local variables 47) What is the output of the following code if word.txt exists? ofstream outfile; outfile.open( word.txt ); outfile << balloon ; outfile.close(); A) A runtime error B) balloon is written at the end of the file C) The file is overwritten with the word balloon E) The program ask create a new file word(1).txt and the word balloon is written into it 12
48) A(n) argument is an argument that is passed automatically to a parameter if the argument is missing on the function call. A) Automatic B) Constant C) Static D) Default 49) A variable that signals a condition is called a(n): A) Flag B) Boolean C) Warning D) Constant E) Sentinel 50) retain their contents between function calls. A) Local variables B) Parameters C) Static local variables D) Auto variables E) Register variables 13