Computer Science Final Review

Ace your homework & exams now with Quizwiz!

Type the correct value for the integer variable A after the following block of code has run: int A = 2; if (A < 3) A += 3; else A -= 2; if (A == 5) A /= 2; else A *= 2;

2

Select the best choice below for a prototype of a function named myFunc. The function should have a void return type and should have an integer array named myArray as its only parameter, which is passed by reference to the function. A. void myFunc(int myArray[]); B. void myFunc(int[] myArray); C. void myFunc(int &myArray); D. void myFunc(array myArray[]);

A. void myFunc(int myArray[]);

Select every choice below that is a valid parameter data type in C++. A. int B. void C. while D. float

A. int D. float

Identify one logical error from the choices in the main function below: //This program will print all text from someFile.txt on the console int main(){ ifstream myFile; string myString; myFile.open("someFile.txt"); myFile >> myString; cout << myString << endl; return 0; } A. myString will only have the first word of the file stored in it. B. There is no error and this code will run as described. C. The function should return 1. D. myFile should be an ofstream, not an ifstream.

A. myString will only have the first word of the file stored in it.

What C++ keyword is used to create a structure? Select the best choice from below. A. struct B. collect C. vector D. list

A. struct

Select the choice that best completes this statement. Variables are used to store information during run-time in ____ A. the computer's memory B. the code of a program C. the computer's hard drive D. the compiler

A. the computers memory

Only number variables (like int and float) can be passed by reference in C++, not letter variables (like char or string). Is this statement true or false?

False

Select the correct value of a after this block of code has run: bool a = true; for(int i = 0; i < 3; i++){ a = !a; }

False

Select the correct value of the variable errorFlag when the following block of code has executed: bool errorFlag = true; int counter = 2; while(counter < 4){ if(counter % 2 == 0) errorFlag = !errorFlag; counter++; }

False

Select the value of trueOrFalse after the following code block has run: bool someValues[] = {true, true, false, false}; bool trueOrFalse = someValues[1] && someValues[2];

False

int i = 0; for(;i<3;) i++; This block of code will cause a compilation error. Is that statement true or false?

False

Every C++ program must include one or more functions. Is this statement true or false?

False (The program will not compile without a main function)

C++ does not allow two variable to have the same name in the same program. Is this statement true or false?

False (Variables in different scopes can share the same name.)

Every statement written in a C++ program will be executed at run time. Is this true or false and why?

False, because some statements might be "hidden" by false conditions when a program branches.

What is the purpose of a variable in C++?

To store a value in the computer's memory.

What is the purpose of a compiler?

To translate high level programming languages into machine language.

C++ arrays can only contain variables of a single data type (e.g. all ints or all floats). Is this statement true or false?

True

Every C++ program has a main function. Is this statement true or false?

True

Every for loop in C++ can also be written as a while loop. Is this statement true or false?

True

Every switch block can be rewritten as an if..else if...else block in C++. Is this true or false?

True

Every value in an array must have the same data type. Is this statement true of false?

True

If the boolean variable a is true and the boolean variable b is false then (a || b) is:

True

Passing by reference requires extra caution in C++ because a function can alter the value of a passed variable outside of the called function. Is this statement true or false?

True

Select the correct value for the boolean variable A after this block of code has run: bool A = true, B = false; int C = 5, D = 0; A = (B && (C > D)) || (A && (D < C)); A = A || !A;

True

When included in a C++ program the statements 5/3 and 5.0/3.0 give the same answer. Is this statement true or false?

True

int a = true, b = false; b = (a || b); a = (b && a); What will be the value of a after this code has run?

True

What C++ operator returns the sum of two values?

+

Consider this array declaration: int c[100]; What is the first valid index in the integer array named c in C++? Enter your answer as a number.

0

How many times will the body of this loop execute: int end = 3; for(int i = 0; i <= end; i+=2){ //Some other code end +=1; }

0 0 0 4

The following block of code computes a prefix sum on an array. Fill in the value of c[3] after the code has executed. int c[] = {1, 2, 3, 4}; for(int i = 1; i<4; i++) c[i] += c[i-1];

0 10 (Prefix sum of an array = sum of all values in an array. 1+2+3+4=10)

Enter the value of the variable mysteryNumber after the following block of code has executed (i.e. exited the loop): int mysteryNumber = 3; while(mysteryNumber != 53){ mysteryNumber = (17*mysteryNumber)%97; } //What is mysteryNumber here?

0 53 0

Consider this array declaration: int c[100]; What is the last valid index in the integer array named c in C++?

99

What character marks the end of a C++ statement?

;

Of the following choices, which library will let a program read from and write to a file? <iostream> <fstream> <ifstream> <get_file>

<fstream>

Which choice below best describes an algorithm?

A recipe, because it is a list of step-by-step instructions.

Professor Barret has updated her grading program to store her students as an array of Student variables. The definition for this struct is below. Select the choice that will apply a 10 point curve to the seventh assignment for every Student in the allStudents array. The size of the allStudents array is stored in a constant called SIZE. struct Student{ string name; int idNumber; float grades[10]; }; A. for(int i = 0; i < SIZE; i++){ allStudents[i].grades[6] += 10; } B. allStudents.grades[6] += 10; C. for(int i = 0; i < SIZE; i++){ allStudents.grades[i] += 10; } D. allStudents[SIZE].grades[6] += 10;

A. for(int i = 0; i < SIZE; i++){ allStudents[i].grades[6] += 10; }

Select the output of this simple program from the options below. #include <iostream> using namespace std; void printMe(int a){ cout << a << endl; return; } int main(){ int a = 1, b = 2; printMe(b); return 0; } A. 2 B. 1 C. 0 D. printMe

A. 2

Select the output of this simple program from the options below. #include<iostream> using namespace std; float simpleCalculator(char op, float a, float b){ switch(op){ case '+': return a+b; break; case '-': return a-b; break; default: return 0; } } int main(){ cout << simpleCalculator('+', 3.5, 4.0); return 0; } A. 7.5 B. 0 C. 1.5 D. -1.5

A. 7.5

Select the correct output of the code below: for(int i = 0; i < 5; i++){ switch(i){ case 0: cout << "I "; break; case 1: cout << "love "; break; case 2: cout << "C"; break; case 3: case 4: cout << "+"; break; default: cout << "!" << endl; } } A. I love C++ B. I love C+ C. I love !!! D. I love C++!

A. I love C++

Select the correct output for this block of code: ifstream inFile("input.txt"); for(int i = 0; i < 5; i++){ char a; inFile >> a; cout << a << " "; } A. It is impossible to tell without seeing input.txt.. B. 0 1 2 3 4 C. a b c d e D. i n p u t

A. It is impossible to tell without seeing input.txt..

Consider the following block of code: int c[10]; for(int i = 0; i < 10; i += 2) c[i] = 1; Select the best value for c[3] after this code has run. A. It is impossible to tell, because C++ does not guarantee a default value for variables.. B. 3 C. 10 D. 1

A. It is impossible to tell, because C++ does not guarantee a default value for variables..

What issue will the following program encounter at compile time? #include<iostream> int main(){ cout << "Hello, World!" << endl; return 0; } A. No namespace is used. B. The main function should end with a semi-colon. C. cout is only used to read text. D. C++ does not allow the use of libraries.

A. No namespace is used.

How many else if conditionals can follow an if statement in C++? C++ does not allow else if statements. A. There is no limit. B. 4 C. 1 D. C++ does not allow else if statements.

A. There is not limit

There is a logical bug in the code below. Select the option that best describes the bug. int checker = 10; while(checker > 0){ cout << "Current value of checker: " << checker << endl; if(checker % 2 == 0) cout << "Checker is even." << endl; } A. This is an infinite loop. B. The variable checker is out of scope inside the loop. C. The second output should say "Checker is odd." instead of "Checker is even." D. Variable names cannot begin with lowercase letters.

A. This is an infinite loop.

Which option below will return true if integers a and b have the same value? A. a==b B. a<b C. a=b D. a!=b

A. a==b

Which of the C++ keywords below are used for branching? Select all correct answers. A. else B. if C. switch D. cout

A. else B. if C. switch

Orla has been assigned by her project manager to create a function called calculate_power, which will calculate the power consumed by an electrical circuit. The function will prompt the user to input two floating point numbers called volts and amps. The function should return the product of volts and amps as a floating point number. Select the best prototype for this function from the options below. A. float calculate_power(); B. float watts = volts * amps; C. int calculate_power(); D. float calculate_power = volts * amps;

A. float calculate_power();

Select the best prototype from the choices below for a C++ function called getAvg that will be passed a Student stuct by reference as a parameter and return the average of the 10 floats in the grades array. HINT: consider what the correct data type should be for the average of the grades array. struct Student{ string name; int idNumber; float grades[10]; }; A. float getAvg(Student&); B. int getAvg(Student&); C. Student getAvg(struct); D. Student getAvg(struct);

A. float getAvg(Student&);

Select the best choice below to create an array named myArray to hold the values 0.1, 1.2, 3.4, and 5.3. A. float myArray[] = {0.1, 1.2, 3.4, 5.3}; B. int myArray[] = {0.1, 1.2, 3.4, 5.3}; C. myArray[1] = 0.1; myArray[2] = 1.2; myArray[3] = 3.4; myArray[4] = 5.3; D. myArray[0] = 0.1; myArray[1] = 1.2; myArray[2] = 3.4; myArray[3] = 5.3;

A. float myArray[] = {0.1, 1.2, 3.4, 5.3};

Aida has been asked by her project manager to write a function which will take two floating point numbers as parameters and return their product. Select the function definition from the choices below that best completes Aida's task. A. float productOfTwo(float a, float b){ return a*b; } B. float productOfTwo(float a, float b){ return a%b; } C. int productOfTwo(int a, int b){ while (a<b){ return a++; } } D. int productOfTwo(int a, int b){ float c = a*b; return c; }

A. float productOfTwo(float a, float b){ return a*b; }

Adela has written a complicated block of code that she needs to repeat 6 times. Select the correct for loop below for her to use rather than copying and pasting the code 5 times. A. for(int i = 0; i < 6; i++){ //Adela's code } B. for(i < 6; i++; int i =1){ //Adela's code } C. for(int i = 1; i < 6; i++){ //Adela's code } D. if(i<6){ //Adela's code }

A. for(int i = 0; i < 6; i++){ //Adela's code }

Which choices below will correctly create and open an input file stream? Select all correct answers. A. fstream mystream("myfile.txt", fstream::in); B. ifstream.open("myfile.txt"); C. ifstream mystream("myfile.txt"); D. cin << myfile.txt;

A. fstream mystream("myfile.txt", fstream::in); C. ifstream mystream("myfile.txt");

Consider the code snippet below: ifstream myFile; myFile.open("input.txt"); if(!myFile){ //First Block } else { //Second Block } If you were to compile and run this code and find that "First Block" executes, what is one possible explanation? A. input.txt does not exist. B. A filestream cannot be in a condition because it is not an integer. C. <fstream> was not included in the program. D. input.txt is too large for the compiler to open.

A. input.txt does not exist

Select the output of this small program from the options below: #include<iostream> using namespace std; void print_integer(); int main(){ int a =5; print_integer(); return 0; } void print_integer(){ int a = 1; cout << a; return; } A. 5.0 B. 1 C. a D. 5

B. 1

Select the output of this small program from the options below: #include<iostream> using namespace std; void print_integer(); int main(){ print_integer(); return 0; } void print_integer(){ int a[] = {1, 2, 3, 4}; cout << a[1]; return; } A. a B. 2 C. 1 D. A cout statement cannot print an array.

B. 2

Select the output of this simple program from the options below. #include<iostream> using namespace std; int growVariable(int a) { a = a+1; return a; } int shrinkVariable(int &a){ a -= 1; return a; } int main(){ int b = 2; growVariable(b); cout << b; shrinkVariable(b); cout << b; return 0; } A. 32 B. 21 C. 12 D. 31

B. 21

Consider this code snippet: bool a = true, b = false; if (b = a) cout << "Foo"; else cout << "Bar"; What will this code print and why? A. Foo, because two boolean variables are always equal. B. Foo, because the condition uses the assignment operator not the equal to operator. C. Error, because a and b are not integers. D. Bar, because b does not equal a.

B. Foo, because the condition uses the assignment operator not the equal to operator.

The simple program below has one or more bugs in it. Select the line number for each bug. #include<fstream> using namespace std; int main(){ ifstream myFile.open("input.txt"); //Line 3 char fileChar; //Line 4 myFile >> fileChar; //Line 5 if (fileChar += 7) cout << "greater than 7"; //Line 6 return 0; } A. Line 4 B. Line 3 C. Line 6 D. Line 5

B. Line 3 C. Line 6

Which function is run first in every C++ program? A. Void functions always run before functions with a specific return type. B. The main function. C. Whichever function has the first prototype. D. Whichever function is at the top.

B. The main function.

Select the one option below that describes an error in this C++ function prototype: float findDistance(pointA, pointB); A. The function name is too long. B. The parameters do not have a data type. C. A function prototype must be followed immediately by the definition in C++. D. Float is not a legal return type.

B. The parameters do not have a data type.

What value is stored in the variable x after this snippet of code runs: int x = 0, y; x += (2*y); y = 3; A. This code will not run because y is not an int B. We do not know because y was not initialized before x is assigned. C. 2 D. 0

B. We do not know because y was not initialized before x is assigned.

Select the option below that best describes the difference between a while loop and a do...while loop. A. While loops are written into the memory but do...while loops are written to the hard drive. B. While loops check the condition first and then execute the loop body, but do...while loops execute the loop body and then check the condition. C. Do...while loops run for longer than while loops do. D. C++ does not include do...while loops, but it does include while loops.

B. While loops check the condition first and then execute the loop body, but do...while loops execute the loop body and then check the condition.

There is one bug in the code block below. Select the answer that best describes it. int a; cout << "You have three guesses: "; for(int i = 0; i < 3; i++){ int b = 2; cin >> a; if (a != b) cout << "wrong" << endl; } cout << "The right answer is: " << b << endl; A. The condition for the for loop should be i<=3 B. b is not in scope outside the for loop. C. There is not a bug in this code. D. The update to i should be ++i, not i++.

B. b is not in scope outside the for loop.

The short program below may still compile but contains an error. Select the option that best identifies the bug. #include<iostream> using namespace std; void doubleOneIndex(int a[], int index){ a[index] *= 2; return; } int main(){ int b[] = {2, 3, 5, 7}; doubleOneIndex(b, 4); return 0; } A. The parameter a is not passed by reference. B. b[4] is out of bounds. C. The arrays a and b should have the same name. D. The function doubleOneIndex does not have a prototype.

B. b[4] is out of bounds.

Every function in C++ must return a value. Is the statement correct? A. Yes, every function must return an integer value. B. No, C++ only alows subroutines not functions. C. No, void functions do not return a value. D. Yes, a function can return any data type but must return a value.

C. No, void functions do not return a value.

Select the best definition of 'pass by reference' in the context of C++ from the options below. A. Passing a variable by reference is not possible in C++. B. Passing by reference gives only the value of a variable to a function. C. Passing a variable by reference conveys the memory address of the passed variable value to the called function. D. Passing by reference is an optional grade scale in computer science classes.

C. Passing a variable by reference conveys the memory address of the passed variable value to the called function.

Sianna is building a text-based dungeon adventure game. She needs to implement a control system that does different actions based on user input. Select the one code block below that will let the user select an action without causing a compilation error OR a logical error. The action menu in Sianna's game is: 1) List Inventory 2)Check Map 3)Use Healing A. cin >> userChoice; switch (userChoice) { case 1: list_inventory(); case 2: show_map(); case 3: heal_player(); default: cout << "That is not an option." << endl; } B. cin >> userChoice; switch (userChoice) { case 1: list_inventory(); break; case 2: show_map(); break; case 3: heal_player(); break; default: cout << "That is not an option." << endl; } C. cin >> userChoice; if('1'){ list_inventory(); } else if('2'){ show_map(); } else if ('3'){ heal_player(); } else{ cout << "That is not an option." << endl; } D. No answer text provided.

B. cin >> userChoice; switch (userChoice) { case 1: list_inventory(); break; case 2: show_map(); break; case 3: heal_player(); break; default: cout << "That is not an option." << endl; } (Break statements is like an elevator. A break keeps the elevator from falling past the desired floor until prompted by another input.)

Select all of the C++ keywords below that can be used to create a loop: A. main B. for C. if D. select

B. for

Yousef needs to repeat an operation on the variable numClients until it has a value that is bigger than (but not equal to) 100. Which while loop from below best accomplishes this task. A. do{ //Do Yousef's operation to numClients }while(numClients < 100); B. while (numClients <= 100){ //Do Yousef's operation to numClients } C. while (numClints > 100) { //Do Yousef's operation to numClients } D. do{ //Do Yousef's operation to numClients }while(numClients > 100);

B. while (numClients <= 100){ //Do Yousef's operation to numClients }

The Fibonacci sequence is a series of numbers starting with 0 and 1, and where every subsequent number in the list is the sum of the previous two. So 0,1,1,2,3,5,8,13... and so on. Select the code block from below that will create an array named fibNums and load it with the first 10 Fibonacci numbers in consecutive indexes starting with 0 being in fibNums[0]. A. int fibNums[10]; for(int i = 0; i < 10; i++) fibNums[i] = fibNums[i-1] + fibNums[i-2]; B. int fibNums[10]; fibNums[0] = 0; for(int i = 1; i < 10; i++) fibNums[i] = 2*fibNums[i-1]; C. int fibNums[10]; fibNums[0] = 0; fibNums[1] = 1; for(int i = 2; i < 10; i++) fibNums[i] = fibNums[i-1] + fibNums[i-2]; D. int fibNums = {1, 2, 3, 5, 8, 13, 21, 34, 55, 89};

C. int fibNums[10]; fibNums[0] = 0; fibNums[1] = 1; for(int i = 2; i < 10; i++) fibNums[i] = fibNums[i-1] + fibNums[i-2];

Consider a system where integer variables occupy 2 bytes of memory and floating point variables occupy 4 bytes of memory. How much memory will be allocated by the struct definition below? Select the best choice. struct Order{ int productNumber; float unitPrice; int orderQuantity; }; A. 16, because the operating system adds a buffer to both sides. B. 13, because 2 ints and 1 float occupy 8 bytes plus 5 bytes for "Order". C. 0, because no memory is allocated until a struct variable is declared. D. 8, because 2 ints plus 1 float occupy 8 bytes of memory.

C. 0, because no memory is allocated until a struct variable is declared.

Select the output of this small program from the options below: #include<iostream> using namespace std; int divide_two_numbers(){ int a=5, b=2; return a/b; } int main(){ cout << divide_two_numbers(); return 0; } A. This program will not compile. B. 2.5 C. 2 D. 0.4

C. 2

Andre has written the program below but has found that it will not compile. Which libraries does he need to #include in order to make this program work? Select all that apply. using namspace std; int main(){ ifstream myFile("input.txt"); string myWord; myFile >> myWord; cout << myWord << endl; return 0; } A. <C++> B. <cstdio> C. <fstream> D. <iostream> E. "input.txt"

C. <fstream> D. <iostream>

Select all of the choices below that are valid relational operators in C++: A. <> B. += C. >= D. < E. !=

C. >= D. < E. !=

Select the choice below that best describes a C++ array. A. A collection of values stored on disk rather than in memory. B. A list of values whose size can be changed at any time in code. C. A fixed-size collection of same-type variables stored contiguously in memory. D. A variable that can hold any value.

C. A fixed-size collection of same-type variables stored contiguously in memory.

There are one or more errors in this function definition. Check each box below which explains an error in this code. function get_sum(){ int a, b; cin >> a >> b; return = a + b; }; A. The return keyword does not need the assignment operator. B. Function definitions must begin with a data type or void, not the word function C. Function definitions do not need a semicolon after the final closing bracket. D. The variables a and b must be declared as global variables.

C. Function definitions do not need a semicolon after the final closing bracket.

Select the choice below that best explains what the character '&' indicates in the parameter list of a function in C++. A. It indicates that a parameter is volatile. B. It indicates that a parameter is boolean. C. It indicates a parameter that is passed by reference. D. A C++ parameter list cannot have the character '&' in it.

C. It indicates a parameter that is passed by reference.

Every loop in C++ (for or while) can be re-written as a branch (if...else and switch) using what we've learned in class so far. Is this statement true or false?

False

The code below defines a struct called Student. Select the choice which simultaneously declares and assigns a Student variable called student1. struct Student{ string name; int idNumber; float gpa; }; A. Student{"gentry", 12345, 3.2} = student1; B. Student student[] = {"gentry", 12345, 3.2}; C. Student student1 = {"gentry", 12345, 3.5}; D. Student student1 = 12345, "gentry", 3.2;

C. Student student1 = {"gentry", 12345, 3.5};

What is the meaning of scope in C++? Select the best choice from below. A. The type of data that can be stored in a variable. B. The return type of a function. C. The portion of a program where a variable can be referenced. D. A tool used to see distant stars.

C. The portion of a program where a variable can be referenced.

What is the logical error in this for loop: int end = 0; for (int i = 1; i > end; i++){ cout << "Loop " << i << endl; } A. i is not in scope. B. end is not in scope. C. This is an infinite loop. D. The inner body of the loop will never execute

C. This is an infinite loop

A program needs to check if the integer a is holding a value less that 20 and set a to 20 if it is. Select the block of code from below that will accomplish this. A. a = 20 (if a lessThan(20)); B. if (a < b and b == 20) a = 20; C. if (a < 20) a = 20; D. a = 20;

C. if (a < 20) a = 20;

Vikki has asked for your help completing the following program: #include <iostream> using namespace std; int main(){ int a; cin >> a; //TODO Print "Less" if a is less than 5. //TODO Print "More" if a is greater than 5. return 0; } Select the best option for Vikki to complete her TODO list. Hint: consider what will happen when a is exactly 5. A. C++ does not allow this behavior. B. if (a < 5) cout << "Less"; else cout << "More"; C. if (a < 5) cout << "Less"; else if (a > 5) cout << "More"; D. cout << "More" if (a < 5); cout << "Less" if (a not < 5);

C. if (a < 5) cout << "Less"; else if (a > 5) cout << "More";

Check the box for every option below that is a valid return type for a function in C++; A. true B. cout C. void D. int

C. void D. int

Select the choice below that is the best prediction of the output for this nested loop: for(int i = 0; i < 4; i++){ for(int j = 0; j<i; j++){ cout << j << " "; } cout << endl; } A. 0 1 2 3 B. 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 C. 0 0 0 1 1 1 2 2 2 D. 0 0 1 0 1 2

D. 0 0 1 0 1 2

Select the output of this simple program from the options below. #include <iostream> using namespace std; void printMe(int a){ cout << a << endl; return; } int main(){ int a[] = {1, 2, 3, 4}; for(int i = 0; i < 4; i++){ printMe(a[i]); } return 0; } A. 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 B. 1 2 3 4 C. 4 3 2 1 D. 1 2 3 4

D. 1 2 3 4

Professor Barret has updated her grading program again to allow Student variables to be initialized from a file. Select the code snippet that will read the file "derrickL.txt" and store the values in a Student variable called derrickL. The definition of the Student struct is below. struct Student{ string name; int idNumber; float grades[10]; }; A. ifstream studentFile("derrickL.txt"); Student derrickL; derrickL.name = studentFile.name; derrickL.idNumber = studentFile.idNumber; derrickL.grades = studentFile.grades; B. ifstream studentFile; Student derrickL = studentFile.open("derrickL.txt"); C. ifstream studentFile("derrickL.txt"); Student derrickL = studentFile; D. ifstream studentFile("derrickL.txt"); Student derrickL; studentFile >> derrickL.name >> derrickL.idNumber; for(int i = 0; i < 10; i++){ studentFile >> derrickL.grades[i]; }

D. ifstream studentFile("derrickL.txt"); Student derrickL; studentFile >> derrickL.name >> derrickL.idNumber; for(int i = 0; i < 10; i++){ studentFile >> derrickL.grades[i]; }

Yvette has been contracted to write a function that will double every value in an integer array. Her client has provided her this function prototype: void doubleArray(int a[], const int size); Select the function definition from below that will complete Yvette's task. Remember: a 'const' parameter cannot be changed by a function. A. void doubleArray(int a[], const int size){ a[size] *=2; return; } B. void doubleArray(int a[], const int size){ while(size > 0){ size--; a[size] *= 2; } return; } C. void doubleArray(int a[], const int size){ for(int i = 0; i < size; i++) a[i] * 2; return; } D. void doubleArray(int a[], const int size){ for(int i = 0; i < size; i++) a[i] *= 2; return; }

D. void doubleArray(int a[], const int size){ for(int i = 0; i < size; i++) a[i] *= 2; return; }

Select the output of this simple program from the options below. #include<iostream> using namespace std; bool notFive(int a){ return a!=5; } int main(){ int a = 0; while(notFive(a)) { cout << a << " "; a += 1; } return 0; } A. This is an infinite loop. B. 1 2 3 4 5 C. 0 1 2 3 4 5 D. 0 1 2 3 4

D. 0 1 2 3 4

Select the correct output for this block of code: for(int i = 1; i < 16; i*=2){ cout << i << " "; } Hint: i is updated at the "bottom" of the loop. A. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 B. 2 4 6 8 10 12 14 C. 2 4 8 16 D. 1 2 4 8

D. 1 2 4 8

Select the output of this short program from the options below. #include<iostream> using namespace std; void printAndInc(int &a){ cout << a; a += 1; return; } int main(){ int b = 2; printAndInc(b); cout << " " << b; return 0; } A. 3 3 B. 2 2 C. 3 2 D. 2 3

D. 2 3

Select the output of this short program from the options below. #include<iostream> using namespace std; void printAndInc(int &a){ cout << a; a += 1; return; } int main(){ int b = 2; for(int i = 0; i < 3; i++) printAndInc(b); return 0; } A. 012 B. 222 C. 333 D. 234

D. 234

The file short.txt contains only one line of text which reads: 8 19 22 Select the value that will be stored in value1 after this segment of code runs. ifstream myStream; myStream.open("short.txt"); int value1; mystream >> value1; A. 0 B. 22 C. 81922 D. 8

D. 8

Every program has to have a human user. Is this statement true or false?

False

Violet needs to write a program which will allow a user to enter a Contact's name and address through the console and then write that information into a file called "allContacts.dat". Which two libraries should she include in order to start writing this program? A. <cstdlib> and <<iostream> B. <fstream> and <vector> C. <iostream> and <cmath> D. <iostream> and <fstream>

D. <iostream> and <fstream>

Aurora has written the block of code below. It is printing garbage because of an error. Select the choice that best identifies the bug that is keeping Aurora's code from compiling: cont string allStudents[] = {"Bella", "Cody", "Niko"}; cout << "First student on roster: " << allStudents[0] << endl; cout << "Last student on roster: " << allStudents[3] << endl; A. 3 students is too few students to make a roster. B.Strings cannot be declared constant in C++. C. Index 0 is out of bounds. D. Index 3 is out of bounds.

D. Index 3 is out of bounds.

Professor Barret uses the struct below to store her students' grades in an automatic grading program. One of her students has petitioned to have his grade on the second assignment raised from a 78 to an 85. Select to the choice that will update grades[1] in the Student variable called sutdent1 to have the value 85. struct Student{ string name; int idNumber; float grades[10]; }; A. student1.grades[1] = 85; B. struct student1.grades[1] = 85; C. grades[1] += 85; D. Student.grades[1] = 85;

D. Student.grades[1] = 85;

Select the choice below that best identifies the bug in the code below: int i = 0; do(i < 10){ cout << i << " "; }while; A. Integers should be cast to characters before being printed. B. Loops use square brackets [], not curly brackets {}. C. The variable i should be declared inside the loop. D. The condition is in the wrong place.

D. The condition is in the wrong place.

Consider this block of code: if (a > b){ //First Block } else{ //Second Block } When will the code in "Second Block" execute? Hint: consider the case where a and b are equal. A. Whenever b does not equal a. B. When b is greater than a. C. When a and b are integers. D. When a is less than or equal to b.

D. When a is less than or equal to b.

What tool in C++ allows you to group several variables (possibly of different types) as a new data type? Select the best choice from below. A. a vector B. a collection C. a list D. a structure

D. a structure

Sam has been hired as a grader for the Computer Science department. He wants to write a function to automatically grade quizzes for him. The function will ask the user to input a string called key_filename and another string called submission_filename. The function will open two file-streams, compare each line of the key to the student's submission, and return the student's grade as a percentage from 0 to 1. Select the best function prototype for this function from the options below. A. int grade_quiz(); B. ofstream grade_quiz(); C. ifstream grade_quiz(); D. float grade_quiz();

D. float grade_quiz();

Nina has written the block of code below. It compiles and produces correct output but her supervisor wants her to re-write it as a while loop instead of a for loop. Select the option from below that best translates Nina's loop into a while. Nina's Code: for(int i = 0; i <= 9; i += 3){ cout << "i is now " << i << endl; } A. while(int i <= 9){ cout << "i is now " << i << endl; i++; } B. while(i <= 9){ cout << "i is now " << i << endl; i+= 3; } C. No answer text provided. D. int i = 0; while(i <= 9){ cout << "i is now " << i << endl; i+= 3; } E. while(i <= 9){ int i = 0; cout << "i is now " << i << endl; i+= 3; }

D. int i = 0; while(i <= 9){ cout << "i is now " << i << endl; i+= 3; }

Select the choice that best completes this statement. Writing information to a file from a program is useful because ____ A. humans can only read files. B. files do not weigh as much, so your laptop will be lighter C. it is accesible more quickly that way. D. the information remains after a program terminates.

D. the information remains after a program terminates.

A function parameter in C++ must have the same data type as the functions return. For instance this prototype is legal: int myFunc(int); But this prototype is not: int myFunc(float); Is this statement true or false?

False

C++ structures can only contain variables of a single data type (e.g. all ints or all floats). Is this statement true or false?

False

Every case in a switch block needs to be terminated with a break statement for code to compile. Is this true or false?

False

Every if statement in C++ has to be followed by an else statement. Is this true or false?

False

Sarah has written a simple C++ program on a Windows computer. What changes will she have to make in order to compile and run her program on a Macintosh computer?

None, C++ is platform independent.

Trevor wants to store a value input by a user in the variable numStudents. He has written the following inside of his main function: int numStudents; cout << "Please enter a number of students: "; From the options below, which one will take a value from the user and store it in numStudents?

cin >> numStudents;

You need to add a line to a C++ program which will print the message "That action is not allowed" on a user's console. Which option below is a valid solution to this problem?

cout << "That action is not allowed";


Related study sets

Med Term Ch. 11 Gastroenterology ~ Crossword puzzle

View Set

sports marketing finalThe movement of a professional sports team to a new city is an example of which of the following?

View Set

American History II Final Exam - Chapter 15-28

View Set

PFI 3361 Exam 4 (Unit 7 PQ's & Quiz)

View Set

Chapter 1.1-1.5, Chapter 3.1-3.4, 7.1-7.5, Chapter 11.1-11.7 Econ 105 Cypress College

View Set