Programming 1 Final
True or False: Objected-Oriented Programming (OOP) is a programming style in which tasks are solved by collaborating objects. (Note: each object has its own set of data with a set of functions that can act upon the data).
True
True or False: Programs that start from the command line can receive the name of the program and the command line arguments in the main function via arguments/parameters.
True
True or False: Sequential access is when you read a file an item at a time and write to a file an item at a time without skipping forward or backward.
True
True or False: The data type of the point should match the data type of what it is pointing to
True
True or False: We use a 2D (two-dimensional) array to store data in a tabular (i.e. table) format.
True
True or False: When developing a solution to a complex problem, first solve a simpler task
True
True or False: When you assign an array to a pointer, it points to the first element in the array.
True
True or False: You can access any position in a random access file by moving the file pointer prior to a read or write operation.
True
True or False: You have already experienced object-oriented program whenever you used string objects or streams (i.e., cin / cout).
True
True or False: strcpy is a C String Function.
True
True or false: The condition in a for loop is checked before each iteration
True
Many files, in particular those containing images or sounds, do not store information as text but as ________ numbers
binary
The _____ of a function are the lines of code that are executed when the function is ran
body
At the heart of the computer lies the ______________________, which consists of a single chip, or small number of chips. It performs program control and data processing
central processing unit (CPU)
What would be the output of the following code? ostringstream strmOut; strmOut << right << setfill('_') << setw(8); strmOut << 33 << endl; strmOut << 11 << endl; cout << strmOut.str(); a. 33______ 11______ b. 33______ 11 c. ______33 ______11 d. ______33 11
d. ______33 11
Fill in the blank with the correct one-word response: A powerful strategy for problem solving is __________ refinement, where complex tasks are decomposed into simpler ones
stepwise
How do you compute the length of the string variable str?
str.length();
Fill in the correct keyword in this code int str_length_doubled(______ input) { return input.length() * 2; }
string
Programming is
the act of designing and implementing computer programs
The _____ loop is appropriate when the loop body must be executed at least once
do
A member function is invoked using
dot notation
How many times does the following code snippet display "Moo!"? for (int i = 0; i < 5; i + 1) { cout << "Moo!\n"; } a. None - body of loop is never executed b. 3 times c. 4 times d. 5 times e. 6 times f. An infinite number of times
f. An infinite number of times
Fill in the blank with the one-word term: To open a file for reading, you use a(n) ___ variable.
ifstream
A __________ is a construct that can hold data like an array, but can change size in run time
vector
What keyword is used to indicate that a function is NOT returning a value?
void
Fill in the blank with the correct one-word response: The ___ of a variable is the part of the program in which it is visible.
scope
A ___________ program uses the computer to simulate an activity in the real world (or an imaginary one). Common uses include: predicting climate change, analyzing traffic, picking stocks, and video games
simulation
The programs the computer executes are called the
software
Fill in the black with the answer that will allow this code to simulate the roll of a 20 sided die: #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(static_cast<unsigned int>(time(0))); int r = rand() % ___ + 1; cout << r << endl return 0; }
20
True or false: it's good practice to avoid using local variables and making as many of your variable global as possible.
False
____________ is a simulation of code executed in which you step through instructions and track the values of the variables
Hand-tracing
Is the following the correct way of opening a file "File.txt" for reading? in_file.open(File.txt)
No
True or false: Switch statements will not compile without the breaks
False
True or false: This is a valid C++ code. if (10 == "10") { cout << "These values are equal" << endl; }
False
True or false: Vectors and arrays must have & to be passed by reference
False
State the expressions within De Morgan's Law:
!(A && B) is the same as !A || !B !(A || B) is the same as !A && !B
What value is displayed to the user by the following code fragment? int n = 1; int m = -1; if ( n < m) { cout << n; } else { cout << m; }
-1
Given an object of a class (defined in main), what operator do you use to access one of its functions? a. & operator b. | operator c. . operator (dot operator) d. - operator (dash operator)
. operator (dot operator)
This piece of code should extract the substring of length 3 starting at position 0. Complete the code. string city = "Cincinnati"; string partCity = city.substr(___, ___);
0, 3
This loop should run 10 times with the values 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10. Select the correct initial value: for (int i = ___; i < 11; i++) { cout < i << " "; }
1
What output is generated by the code segment below? const int SIZE = 10; int values[SIZE]; for (int k = 0; k < SIZE; k++) { values[k] = k * 2; } cout << values[5] << endl;
10
What does the following evaluate to: (14/6) % 5
2
Complete the C++ program to display: XXXX XXXX XXXX #include <iostream> using namespace std; int main() { for (int row = 0; row < 4; row++) { for (int col = 0; col < ___; col++) { cout << "X"; } cout << endl; } }
4
How many times is i printed in the following loop: for (int i = 0; i <= 12; i = i + 4) { cout << i << endl; }
4
The comparison > is a relational operator. How many relational operators does C++ have?
6
What would this program output to the console? int a = 3; int b = 20; if (b <= 20) { int a = 42; } if (b >= 20) { a *= 2; } cout << a << endl
6
What is a function stub?
A function that returns a simple value that is sufficient for testing another function
__________ are supplied when a function is called
Arguments
What does the % operator do?
Computes the remained or an integer division
2.0 / 3.0 == 0.6666666666666666 Will this evaluate to true or false?
False
True or False: A for loop cannot be nested inside a while loop
False
True or False: A struct contains only function prototypes
False
True or False: Arrays and vectors are passed by reference using the & sign
False
True or False: Functions can have more than one return value
False
True or False: In a switch statement, if a break statement is missing the code will not compile
False
True or False: The following expressions are equivalent in C++ code: Expression 1: if (x && y > 0) Expression 2: if (x > 0 && y > 0)
False
True or False: The single quote delimiter means the same thing as the double quote delimiter in C++
False
True or False: Variable names can start with a digit
False
True or False: You can compare two structures for equality, like you can for two strings, by using the == operator.
False
True or false: "Quit" and "quit" are the exact same string -- letter case does NOT matter
False
True or false: + is a unary operator
False
True or false: Every branch of the switch must be terminated by a stop instruction.
False
What is printed by the following code snippet? int sum = 0; for (int j = 0; j <= 10; j + 2) { sum += j; } cout << sum; a. 0 b. 20 c. 30 d. Nothing, no output is displayed
Nothing, no output is displayed
_______________ is made from electronic circuits that can store data, provided they are supplied with electric power
Primary storage (aka memory)
__________ is an informal description of a sequence of steps for solving a problem
Pseudocode
______________, usually a hard disc or solid-state drive, provides slowed and less expensive storage that persists without electricity
Secondary storage
True or False: A class contains two accessibility section: public and private
True
True or False: A sentinel value denotes the end of a data set, but is not part of the data. In other words, a value that serves as a signal for termination
True
True or False: A structure can have a member that is another structure.
True
True or False: An if statement inside another if statement is called a nested if
True
True or False: An off-by-one error is a common error when programming loops and often occurs because we either do one extra pass of a loop or we are one pass short for a loop
True
True or False: By combining and adapting fundamental algorithms with arrays, you can solve complex programming tasks.
True
True or False: Constructor functions have the same name as the class
True
What sequence is used to denote a literal quote rather than the end of a string?
\"
A computer program is
a sequence of instructions and decisions
A variable is
a storage location in a computer program with a name and value
Insert the missing condition in the following code fragment. The code is intended to compute the sum of a number of integers entered by the user. The loop should stop when the sum exceeds 100. int total = 0; do { cout << "Enter an integer: "; int value; cin >> value; total = total + value; } while (_____); cout << "The sum is: " << total; a. total <= 100 b. total > 100 c. value <= 100 d. value > 100
a. total <= 100
Which represents the best choice for a variable name for the total cost (including tax) for a pizza? a. total_pizza_cost b. int_total pizzaa cost c. tot+tax d. 4pizzaCost
a. total_pizza_cost
Fill in the blank with the correct one-word response: Modifying a ___ parameter has no effect on the caller. a. value b. reference c. global d. floating-point e. scope
a. value
Choose the missing condition for the following code fragment. The code is intended to add 5 to variable shipping_cost if the value of the variable weight is at least 10. if (________) { shipping_cost = shipping_cost + 5; } a. weight >= 10 b. weight => 10 c. weight == 10 d. weight > 10
a. weight >= 10
To add the number "8" to the end of a vector "a" of length 4, you would code __________
a.push_back(8);
Fill in the blank with the correct one-word response: A(n) ___ member function does not change the data of the object on which it operates.
accessor
What operator is used to concatenate strings? a. & b. + c. - d. * e. ^
b. +
Which line of code should replace the placeholder symbols ??? to count correctly the number of integers among the inputs that are less than 100? int count = 0; int input; while (cin >> input) { if (input < 100) { ??? } } a. input++; b. count++; c. input += count; d. count += input;
b. count++;
Fill in the blank with the correct one-word response: Attempting to access an element whose index is not within the valid index range is called a(n) ____ error.
bounds
Fill in the blank with the correct TWO-word response: The text described what is considered the first "worm virus" distributed via the Internet (1988), and the first to gain significant mainstream media attention. The worm carried out a(n) ____ ____ attack, providing an unexpectedly large input to a program on another machine. That program allocated an array of 512 characters, under the assumption that nobody would ever provide such a long input. Unfortunately, that program was written in the C programming language which, like C++, does not check that an array index is less than the length of the array. If you write into an array using an index that is too large, you overwrite memory locations that belong to other variables.
buffer overrun
Which one of the following operators computes the remainder of an integer division? a. ! b. / c. % d. \
c. %
Please complete the following code by selecting the correct answer in the multiple choice below. The line of code is placed where it states //WHAT LINE OF CODE GOES HERE. This function takes input from the user and stores the values in myArray. The loop continues until the array is filled to the CAPACITY. void userInputValues(int myArray[], int CAPACITY) { it current_size = 0; cout << "Please enter values, Q to Quit: "; int input = 0; while(cin >> input) { if(current_size < CAPACITY) { myArray[current_size] = input; } } } a. current_size -= 2; b. current_size += 2; c. current_size++; d. current_size--; e. current_size = 1;
c. current_size++;
Assuming that variable dollars and cost have been declared, which of the following assignment statements is INVALID? a. cost = dollars + 100; b. cost = cost + 50; c. dollars + 100 = cost; d. dollars = cost;
c. dollars + 100 = cost;
You cannot change the value of a variable that is defined as _____
const
Fill in the blank: int a; cout << "Enter an integer: "; cin >> a; if(___________) { cout << "Incorrect input."; return 1; } cout << "Nice number." << endl; return 0;
cin.fail()
Please select the correct condition for the if statement. This program checks if the reading of input fails. Otherwise, it "echos" / displays the input value and asks for another #include <iostream> using namespace std; int main() { bool done = false; double userInput = 0; while(!done) { cout << "Enter prices or Q to quit: " << endl; cin >> userInput; if (__________) { done = true; } else { cout << "You entered: " << userInput << endl; } } }
cin.fail()
Fill in the blank with the correct one-word response: With a partially filled array, one needs to keep a(n) ____ variable for the current size.
companion
A special computer program, a ________, translates code into machine instructions for a particular processor
compiler
string city = "Ada"; string state = "OH"; string zipcode = "45810"; string address = city + ", " + state + " " + zipcode; This (line 5) is an example of using the + operator with strings to do _____________
concatenation
Provide the missing word: The cin and cout objects are used to tell the computer to read input from, and write information to, the _______ window.
console
The body of the function is surrounded by a. Semi colons b. Parentheses c. Square braces d. Curly braces
curly braces
A programmer wishes to insert a comment following an assignment statement. Which of the following will generate an error? a. cost = 25.0; // initial cost of a product b. cost = 25.0; /* initial cost of a product */ c. cost = 25.0; // initial cost of a product */ d. cost = 25.0; */ initial cost of a product //
d. cost = 25.0; */ initial cost of a product //
Which of the following can be used to display the double variable fraction with two decimal digits? a. cout << fraction; b. cout << set2(2) << fraction; c. cout << setprecision(2) << fraction; d. cout << fixed << setprecision(2) << fraction;
d. cout << fixed << setprecision(2) << fraction;
Which statement will generate a random number between -1 and 4 (inclusive), given the following statement which generates a random floating point number between 0 and 1: double r = rand() * 1.0 / RAND_MAX; // Between 0 and 1 a. double x = -1 + 2 * r; b. double x = -1 + 3 * r; c. double x = -1 + 4 * r; d. double x = -1 + 5 * r;
d. double x = -1 + 5 * r;
cin from the library iostream is a __________ variable. a. local b. undefined c. fuzzy d. global
d. global
Which of the following expressions represents a legal way of checking whether a value assigned to the 'num' variable falls between 100 and 200, inclusive? a. if (100 <= num <= 200) b. if (num >= 100 || num <= 200) c. if (num>= 200 && num <= 100) d. if (num >= 100 && num <= 200) e. if (num >= 200 || num <= 100)
d. if (num >= 100 && num <= 200)
Given: int a[] = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 }; What is the value of total after the following code is run? int total = 0; for (int i = 0; i < 10; i++) { total = total + a[i]; } a. 10 b. 26 c. 24 d. 1 e. 25
e. 25
Consider the following code snippet: int ctr = 1; int myarray[3]; for (int i = 0; i < 3; i++) { myarray[i] = ctr++; ctr = ctr + 1; } cout << myarray[2]; What is the output of the code snippet? a. 0 b. 1 c. 2 d. 3 e. 4 f. 5
e. 4
What will be the output of the following code snippet? for (int i = 0; i < 8; i++) { for (int j = 7; j > i; j--) { cout << "+"; } cout << endl; } a. A diamond with eight rows and seven columns of the plus sign b. A rectangle with eight rows and seven columns of the plus sign c. A rectangle with seven rows and eight columns of the plus sign d. A rectangle with seven rows and eight columns of the plus sign e. A right triangle with seven rows and seven columns of the plus sign, with the longest row of plus signs at the top f. A right triangle with seven rows and seven columns of the plus sign. with the longest row of plus signs at the bottom
e. A right triangle with seven rows and seven columns of the plus sign, with the longest row of plus signs at the top
When a function is called, what type of variables are created? a. argument b. informal c. dynamic d. static e. parameter
e. parameter
The values stored in an array are called its
elements
What are the values of x and y after executing the given code snippet? void mystery(int&a, int&b) { a = b; b = a; } int main() { int x = 10; int y = 11; mystery(x, y); return 0; } a. x = 10 and y = 0 b. x = 11 and y = 10 c. x = 10 and y = 11 d. x = 0 and y = 0 e. x = 0 and y = 11 f. x = 11 and y = 11
f. x = 11 and y = 11
What will the variable c evaluate to? bool a = true; bool b = false; bool c = !(a || b)
false
A _________ is used to visually represent a decision statement (if statement, switch statement, etc.). They are made up of tasks (rectangles), inputs/outputs (trapezoids), and decisions (diamonds) with lines connecting to show the flow of the logic.
flowchart
The _____ loop is used when a value runs from a starting point to an ending point with a constant increment or decrement
for
A ________ is a collection of programming instructions that carry out a particular task
function
A __________ is a named sequence of instructions (or a sequence of instructions with a name)
function
Fill in the blank with the correct one-word response: When carrying out the same task multiple times, use a __________.
function
Fill in the blank with the correct one-word response: Variables that are defined outside functions are called ___ variables.
global
The physical computer and peripheral devices are collectively called the
hardware
Enter the correct line of code that will cause this program to output to the screen the following: 5 4 3 2 1 #include <iostream> using namespace std; int main() { int i = 5; while(________) { cout << i << " " << endl; i = i - 1; } return 0; }
i > 0
Fill in the blank with the correct one-word response: The values stored in an array are called its elements; each element has a position number, called a(n) ___.
index
What type is used for numbers that cannot have a fractional part?
int
Enter the correct datatype in the following code: __________ strm; strm.str("2022/11/28"); int year; int month; int day; char slash; strm >> year >> slash >> month >> slash >> day;
istringstream
A program called the ______ takes your machine code and the necessary parts from the C++ library and builds an executable fime
linker
What is the mathematical function for a natural log of x?
log(10)
What is the mathematical function for a decimal log of x?
log10(x)
What is the term for a numeric constant that appears in your code without explanation?
magic number
The _______ ________ of a class define the behaviors of its objects
member functions
Fill in the blank with the correct one-word response: A ___ is a function that modifies the data members of the object.
mutator
Fill in the blank with the one-word term: To open a file for writing, you use a(n) ___ variable.
ofstream
A ________ data member can only be accessed by the member functions of its own class
private
Fill in the blank with the correct one-word response: A class's data members are usually declared to be ___ so that they can only be accessed by member functions of the same class.
private
When a program asks for user input, it should first print a message that tells the user which input is expected, called a
prompt
When a program asks for user input, it should first print a message that tells the user which input is expected. What one-word tern is used to describe such a message?
prompt
Fill in the blank with the correct two-word response: The ___ ___ of a class consists of all member functions that a user of the class may want to apply to its objects.
public interface
When arrays are passed as parameters to functions, they are always passed by __________.
reference
The __________ statement of a function gives the result of the function back to the code that called the function
return
The "output" of a function computes is called the __________--
return value
A _________ error causes a program to take an action that the programmer did not intend
run-time
Loops execute a block of code repeatedly while a condition remains _____.
true
When defining a variable, you also have to specify the _____ of its value
type
Please enter the correct condition for the following do (while) loop that only accepts user input from 1 to 100: #include <iostream> using namespace std; int main() { int userInput = 0; do { cout << "Please enter a number from 1=100:\n"; cin >> userInput; }while(_______________________); cout << "You have entered: " << userInput << endl; return 0; }
userInput >= 0 && userInput <= 100
True or false: Constructor functions must have a unique name
False; Constructors have the same name as their class
True or False: Characters that are grouped together between double quotes (quotation marks) in C++ are called strings
True
True or False: Each and every branch of your program should be covered by a test case
True
True or False: Engineers use the term black box for a device with a given specification but unknown implementation
True
True or False: In order to clear the "stream" of input, we add the following line of code to our programs: cin/clear(); This will allow us to continue reading input after an input failure has occurred
True
True or False: It's good practice to make many, very small functions
True
True or False: Memory leaks are used by allocating memory from the free store area by forgetting to delete / deallocate it.
True
True or False: You can walk-through an array by incrementing a pointer pointing to it.
True
True or False: You can write any while loop using an equivalent for loop
True
True or false: You can write any while loop using a for loop
True
What values does the following generate: rand() % 100 + (2%2) . 0 to 99 b. 1 to 99 c. 1 to 100 d. 0 to 100
a. 0 to 99
If I want to swap two elements in an array, how many extra variables do I need? e.g., I have an array of size 5, I want to swap the second and third element: 3 | 5 | 7 | 9 | 13 I want it to be: 3 | 7 | 5 | 9 | 13 a. 1 variable to temporarily store one value b. 5 variables as we need an extra variable for each value in the array c. 2 variables to temporarily story both values d. 3 variables to temporarily store each of the 2 original values + 1 for a spare
a. 1 variable to temporarily store one value
What is the value of sum displayed by the following code fragment? int i = 0; int sum = 0; for ( i = 0; i <= 5; i++) { sum = sum + 1; } cout << "The value of the sum is " << sum; a. 15 b. 10 c. 6 d. 5 e. 0 f. No value shown due to infinite loop
a. 15
What is the result of evaluating the following expression? (45 / 6) % 5 a. 2 b. 0.6 c. 4 d. 3 e. 0.1
a. 2
The end of a structure definition must be terminated by the ___ character. a. ; b. ) c. = d. ]
a. ;
Which of the following symbols is used to terminate a C++ program statement? a. ; b. ! c. ( d. ?
a. ;
What must a reference parameter refer to in a function call? a. A variable b. A constant c. An expression d. A return statement
a. A variable
Which of the following statements are major reasons for using functions? There may be more than one correct answer a. Code reusability b. Code compartmentalization c. Coding comments d. Program language
a. Code reusability b. Code compartmentalization
Show the output generated by the code segment below. string destination = "Eiffel Tower"; cout << destination.substr(0, 1) + " " + desination/substr(7, 1); a. E T b. ET c. 0 7 d. E o
a. E T
Which of the following is true about do while loops? a. It will execute at least one time b. It could execute 0 time c. Does not exist in C++ d. Exactly the same as a while loop and for loop
a. It will execute at least one time
Which one of the following refers to a number constant that appears in code without explanation? a. Magic Number b. String Literal c. Constant d. Variable
a. Magic Number
One advantage of designing functions as black boxes is that a. Many programmers can work on same project w/out implementation details b. The results returned from black-box functions is always the same data type c. The implementation of the function is open for everyone to see d. There are fewer parameters
a. Many programmers can work on same project w/out implementation details
What is the error in the following piece of code? (The code is supposed to display (x = 0, x = 10, ..., x = 90 each on separate lines for (int x = 0; x < 100; x + 10) { cout << "x = " << x << endl; } a. The loop is infinite because it should be x = x + 10 for the increment counter part b. There is nothing wrong with the code / no error c. You can only use x++ or x-- for the increment counter part (You cannot count by 10s) d. The code only displays 1 cout statement instead of displaying 0, 10, ..., 90
a. The loop is infinite because it should be x = x + 10 for the increment counter part
What output is generated by the statements below? cout << "The total is "; cout << 10 + 25; a. The total is 35 b. The total is 10 + 25 c. The total is 1025 d. "The total is" 10 + 25
a. The total is 35
What is wrong with this code? double input; while (cin >> input) { double total = 0; total = total + input; } a. The total variable should be declared outside the loop b. The total variable should be initialized with -1 c. One should use a do/while loop instead of a while loop d. One should use a for loop instead of a while loop
a. The total variable should be declared outside the loop
What does the following piece of code output to the screen? #include <iostream> using namespace std; int main() { int i = 5; while (i == 4) { cout << i << " "; i = i - 1; } return 0; } a. This displays nothing to the screen (the loop never executes) b. This code will not compile because a while loop does not exist in the C++ programming language c. 5 6 7 8 9 10 11 ... (this continues to infinity) d. 5 4 3 2 1
a. This displays nothing to the screen (the loop never executes)
Writing the program for a computer game with motion, graphics, and sound effects usually requires a. a team of highly skilled programmers writing a large number of simple instructions b. one programmer writing a large number of simple instructions c. a team of highly skilled programmers writing a small number of complex instructions d. one programmer writing a small number of complex instructions
a. a team of highly skilled programmers writing a large number of simple instructions
Which of the following statements represents a logic error? a. cout << "The sum of 5 and 6 is 10" << endl; b. cout << The sum of 5 and 6 is 10 << endl; c. cout << "The sum of 5 and 6 is 11" << endl; d. cout << The sum of 5 and 6 is 11 << endl;
a. cout << "The sum of 5 and 6 is 10" << endl;
Select the statement that will compile and output the value "12" a. cout << 6*2 >> endl; b. cout << "6*2" << endl; c. cout << 6.2 << endl; d. cout << 6x2 << endl; cout << endl;
a. cout << 6*2 << endl;
When writing a C++ program, you type your code into a(n) ________ window. a. editor b. compiler c. linker d. library
a. editor
What single change, if any, do you need to make in the following code snippet to display the output "Let us C" exactly 10 times? int i = 0; while (i <= 10) { cout << "Let us C" << endl; i++; } a. int i = 1; b. while (i < 9) c. while (i < 11) d. for (i = 0; i < 10; i++) e. No change required
a. int i = 1;
Which of the following is a function we have been writing for almost 6 weeks with each program? a. main b. using namespace c. #include <iostream> d. procedure e. void
a. main
Assuming that out_file is a variable used for writing to a file, what is the legal way of writing the string stored in name to a file? a. out_file << name; b. out_file >> name; c. out_file.write(name); d. write_line(out_file, name);
a. out_file << name;
Given the following code: #include <iostream> using namespace std; void print2DArray(char a[][3]); int main(int argc, const char * argv[]) { // insert code here... cout << "Hello, World!\n"; char ticTacToe[3][3] = { {'0', '_', '_'}, {'X', 'X', 'X'), { '_', '0', '_'}, }; print2DArray(ticTacToe); return 0; } If I wanted to change the row of 'X' to 'S' which of the following is the C++ code to do that? a. ticTacToe[1][0] = 'S'; ticTacToe[1][1] = 'S'; ticTacToe[1][2] = 'S'; b. ticTacToe[2][0] = 'S'; ticTacToe[2][1] = 'S'; ticTacToe[2][2] = 'S'; c. ticTacToe[0][0] = 'S'; ticTacToe[0][1] = 'S'; ticTacToe[0][2] = 'S'; d. ticTacToe[0][0] = 'S'; ticTacToe[1][1] = 'S'; ticTacToe[2][2] = 'S';
a. ticTacToe[1][0] = 'S'; ticTacToe[1][1] = 'S'; ticTacToe[1][2] = 'S';
Insert the missing condition in the following code fragment. The code is intended to compute the product of several integers entered by the user. The loop should stop when the user enters something other than an integer. int product = 1; int value; cin >> value; while (________) { product = product * value; cin >> value; } a. value > 0; b. !cin.fail() c. product > 0; d. cin.fail()
b. !cin.fail()
What is the output if a program invoked div(10) using the following code snippet? void div(int n) { if (n > 2) { div(n % 3); } cout << n / 2 << " "; } a. 10 b. 0 3 c. 3 d. 0 10 e. 0
b. 0 3
What output does this for loop generate? for (int j = 0; j < 5; j++) { int value = j * 2; cout << value << ", "; a. 0, 1, 2, 3, 4, b. 0, 2, 4, 6, 8, c. 2, 4, 6, 8, 10, d. 0, 2, 4, 6, 8, 10,
b. 0, 2, 4, 6, 8,
What would be the console output of this code snippet? string throught = "C++ is neat."; cout << thought.length() << endl; a. 13 b. 12 c. 11 d. 10
b. 12
Given the declaration below, what is the value of table.size()? vector<vector<int>> table = { { 4, 3, 5 }, { 0, 4, 3 } }; a. 0 b. 2 c. 3 d. 6
b. 2
What are the first and last values of the loop variable c that are printed by the following code snippet? int n = 4; for (int c = 2; c <= n; c++) { for (int j = 0; j <= c; j++) { cout << c << endl; } } a. 0 and 4 b. 2 and 4 c. 2 and 5 d. 0 and 5
b. 2 and 4
What is the value of the variable result after executing the statements below? int limit = 2; double result = 3.5 + 5 / (6 - limit); a. 2.125 b. 4.5 c. 4.75 d. 8.5
b. 4.5
Which of the following methods is the easiest to process an input stream word-by-word? a. The "get" function b. >> operators c. The "put" function d. The "getline" function
b. >> operators
Given the following code: #include <iostream> using namespace std; void print2DArray(chara[][3])); int main(int argc, const char * argv[]) { // insert code here... cout << "Hello, World!\n"; char ticTacToe[3][3] = { {'0', '_', '_'}, {'X', 'X', 'X'), { '_', '0', '_'}, }; print2DArray(ticTacToe); return 0; } void print2DArray(char a[][3]) { for(int row = 0; row < 3; row++) { for(int column = 0; column < 3; column++) { cout << a[row][column] << " "; } cout << endl; } } In the function print2DArray, why is the parameter char a[][3] and not char a[][]? a. It is not required and optional, but Dr. Stephany just added that to be weird b. A two-dimensional array parameter must have a fixed number of columns c. It should actually be char a[3][3][3] d. A one-dimensional array parameter must have a fixed number of rows
b. A two-dimensional array parameter must have a fixed number of columns
What output is generated by the statements below? cout << "C++"; cout << " is " << endl; cout << "fun"; a. C++ is fun b. C++ is fun c. C++ is fun d. C++ is fun
b. C++ is fun
Which statement best describes the role of the inner loop in the following C++ code? int i; int j; for (i = 0; i <= 3; i++) { for (j = 3; j >= 0; j--) { cout << "#"; } cout << endl; } a. The inner loop will not run b. Controls the number of columns (i.e., the number of # displayed in a given row) c. Controls the number of rows (i.e., the number of # displayed in a given column) d. Displays a wave pattern of *
b. Controls the number of columns (i.e., the number of # displayed in a given row)
What output is generated by the statements below? cout << "Easy as "; cout << "1 + 2 + 3"; a. Easy as 1 2 3 b. Easy as 1 + 2 + 3 c. Easy as 6 d. Easy as 123
b. Easy as 1 + 2 + 3
What is the following code snippet an example of? int cool_function(int input1, int input2); a. Function definition b. Function prototype c. Function utility d. Function string
b. Function prototype
What is the purpose of the following function? void transform(int array[], int length) { int min_position = 0; for (int k = 1; k < length; k++) { if (array[k] < array[min_position]) { min_position = k; } } int temp = array[min_position]); array[min_position] = array[0]; array[0] = temp; a. It finds the smallest array element and overwrites it with a zero b. It finds the smallest array element and swaps its value with the first array element c. It finds the smallest array element and swaps its value with the last array element d. It finds the largest array element and swaps its value with the first array element
b. It finds the smallest array element and swaps its value with the first array element
Which of the following is true about a switch statement in C++? a. A statement that tests all branches against different variables b. Like a sequence of if statements that compares against a single value c. Makes the break statement optional d. Requires compound boolean expressions as alternatives
b. Like a sequence of if statements that compares against a single value
How many times does the following loop print out the value of i? int i = 0; while (i > 5) { cout << i++ << endl; } a. 2 times b. None c. 1 time d. 4 times e. 3 times
b. None
_________ storage, which persists without electricity, is slower but not very expensive a. Primary b. Secondary c. Electronic d. Program
b. Secondary
Which of the following statements is false? a. A C++ programmer writes source code b. The distinction between upper- and lowercase letters is unimportant in C++ c. The hello.cpp program relies on a library for producing output d. Source code and executable programs are stored in files
b. The distinction between upper- and lowercase letters is unimportant in C++
What is the output of: int main() { cout << "The multiplication is << "2 * 6" << endl; return 0; } a. The multiplication is 12 b. The multiplication is 2 * 6 c. The multiplication is 26 d. Invalid C++ code and will not run (so nothing is displayed
b. The multiplication is 2 * 6
How can a programmer printing out a C string know when they've reached the end of input? a. Clairvoyance b. The null character at the end of the C string c. The .size() function d. The .length() function
b. The null character at the end of the C string
Assume: we have a class called Student already written in C++ code. Suppose you want to work with objects of type Student. What is the minimum you need to know? a. Only the names of the member functions b. The public interface of the class c. The private implementation of the class d. Both the public interface and the private implementation
b. The public interface of the class
What does this program print when you run it multiple times? int main() { srand(0); int r; for (int i = 1; i <= 10; i++) { r = 1 + rand(); } cout << r << endl; return 0; } a. Zero each time b. The same integer each time c. A different integer each time d. A random floating-point number between 1 and 2
b. The same integer each time
What is the syntax error in the program below? #include <iostream> using namespace std; int main { cout << "Hello, Goodbye"; return 0; } a. There must be a blank like before main b. There must be parentheses after main c. There must be a << endl after "Hello, Goodbye" d. There should not be a semicolon after return 0
b. There must be parentheses after main
Assuming that str is a string variable, what is the purpose of the code segment below? int count = 0; for (int i = 0; i < str.length(); i++) { string ch = str.substr(i, 1); if (ch != "*") { count++; } } a. To count the number of characters in str that are asterisks b. To count the number of characters in str that are not asterisks c. To count the number of characters in str that are punctuation symbols d. To count the number of characters in str that are not punctuation symbols
b. To count the number of characters in str that are not asterisks
Assuming that weight has type double and value 157.8, and age has type int and value 35, what output is generated by the statement below? cout << "Weight: " << fixed << setw(6) << setprecision(2) << weight << ", Age: " << setw(4) << age << endl; a. Weight: 157.80, Age: 35 b. Weight: 157.80, Age: 35 c. Weight: 157.8, Age: 35 d. Weight: 157.8, Age 35
b. Weight: 157.80, Age: 35
The Date class member function getMonth() is an example of what type of member function? a. destructor b. accessor c. mutator d. constructor e. fetcher
b. accessor
Which of the following represents a correct way to declare and initialize a bool variable valid? a. valid = false; b. bool valid = false; c. bool valid = "false"; d. bool valid = 0;
b. bool valid = false;
Which of the following code segments can be used to print the message Hello Goodbye on one output line? a. cout << Hello; cout << Goodbye; b. cout << "Hello "; cout << "Goodbye"; c. cout << "Hello " << endl; cout << ""Goodbye"; d. cout << "Hello " << endl; cout << ""Goodbye" << endl;
b. cout << "Hello "; cout << "Goodbye";
Which of the following statements would generate a compile-time error? a. cout << "eleven" << endl; b. cout << eleven << endl; c. cout << 4 + 7 << endl; d. cout << "4 + 7" << endl;
b. cout << eleven << endl;
Which of the following options is the correct way of clearing the following dynamically created memory: int * arr; arr = new int[size]; a. delete arr; b. delete[] arr; c. clear arr; d. clear arr[];
b. delete[] arr;
Fill in the blank with the correct one-word response: You use the ___ notation to access members of a structure. a. slash b. dot c. question mark d. caret
b. dot
Suppose variable average is to contain the average (including possible decimal values) of four integer variables: a, b, c, and d. Which of the following statements would perform the correct operation? a. double average = a + b + c + d / 4.0; b. double average = (a + b + c + d) / 4.0; c. double average = a + b + c + d / 4; d. double average = (a + b + c + d) / 4;
b. double average = (a + b + c + d) / 4.0;
Say we want to improve the following code snippet. Each line calculates the distance between two points. int dist1 = sqrt(pow(a1, 2) + pow(b1, 2)); int dist2 = sqrt(pow(a2, 2) + pow(b2, 2)); int dist3 = sqrt(pow(a3, 2) + pow(b3, 2)); int dist4 = sqrt(pow(a4, 2) + pow(b4, 2)); Say you wanted to replace the repeated distance calculations with a reusable function that did the same thing. Select the best function prototype bellow for such a function. a. double distance(void) b. double distance(string a, string b); c. double distance(double a, double b); d. void distance(double a, double b);
b. double distance(double a, double b);
What is the function prototype for calcArea which has 2 int parameters (length and width) and returns the area? a. void calcArea(length, width) b. int calcArea(int length, int width); c. calcArea(length, width); d. void calcArea(int length, int width);
b. int calcArea(int length, int width);
Which of the following represents a correct way to declare and initialize a variable named counter? a. counter = 1; b. int counter = 1; c. int counter = "one"; d. int counter = 1.0;
b. int counter = 1;
Assuming that n is an integer variable with a value of 10, which of the following expressions is true? a. n > 2 && n < 10 b. n > 2 || n < 10 c. n < 2 && n > 10 d. n < 2 || n > 10
b. n > 2 || n < 10
In the statement below, the value stored in variable temp is called _____. double temp = pow(3, 4); a. argument b. return value c. output d. function call
b. return value
An accessor member function a. modifies one or more data members of an object b. returns the value of a data member c. must be declared with the keyword mutator d. is only called by other member functions
b. returns the value of a data member
Select the valid types of streams in C++ from the list. There can be multiple correct answers. a. speach b. string c. file d. terminal/console
b. string c. file d. terminal/console
What will be the output of the following code snippet? bool token1 = true; while (token1) { for (int i = 0; i < 10; i++) { cout << "Hello" << endl; } token1 = false; } a. No output b. No output because of compilation error c. "Hello" will be displayed 10 times d. "Hello" will be displayed infinite times
c. "Hello" will be displayed 10 times
Which of the following headers is required to access the file streams library? a. #include <filestream> b. #include <iostream> c. #include <fstream> d. #include <ofstream> e. #include <ifstream>
c. #include <fstream>
What is the valid range of index values for a vector of size 5? a. 1 to 5 b. 1 to 4 c. 0 to 4 d. 0 to 5
c. 0 to 4
Given: int a[] = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 }; What is the value of total after the following code is run? int total = 0; for (int i = 1; i < 10; i = i + 2) { total = total + a[i]; } a. 1 b. 13 c. 12 d. 25 e. 10
c. 12
What does the following code output? int sum = 22; sum = sum + 1; cout << sum++; cout << sum; a. 2223 b. 2323 c. 2324 d. 2424
c. 2324
Consider the following code snippet: int x; cin >> x; if (cin.fail()) { cout << "Fail"; } else { cout << x; } int y; cin >> y; if (cin.fail()) { cout << " Fail"; } else { cout >> " " << y; } What is printed when the provided input is: 3.14 100 a. 3.14 100 b. 3 14 c. 3 Fail d. Fail Fail
c. 3 Fail
What output does this while loop generate? int number = 1; while (number <= 5) { int value = number * 3; cout << value << ", "; number++; a. 1, 2, 3, 4, 5, b. 3, 3, 3, 3, 3, c. 3, 6, 9, 12, 15, d. 3, 6, 9, 12,
c. 3, 6, 9, 12, 15,
What output is generated by the program below? int main() { int table[][] = { { 4, 3, 5 }, { 0, 6, 3 }. { 1, 2, 2 }, }; for (int k = 0; k < 3; k++) { cout << table[k][k]; } } a. 435 b. 401 c. 462 d. 444
c. 462
What is the value of the variable count after the code segment below is executed? int count = 3; count--; count = count + 2; cout++; a. 3 b. 4 c. 5 d. 6
c. 5
How many times does this program display "Coding is Fun!"? string text = "Hello"; for (int x = 0; x < text.length(); x++) { cout << "Coding is Fun!" << endl; } a. 6 times b. 1 time c. 5 times d. 4 times e. 0 times / Code error
c. 5 times
What is the value of the variable count after the statement below is executed? int count = 3 * 2 + 6 % 5; a. 2 b. 4 c. 7 d. 9
c. 7
How many bits are in a byte? a. 0 b. 4 c. 8 d. 12
c. 8
What will be the range of the random numbers generated by the following code snippet? int value = rand() % 50 + 1; a. Between 0 and 50 b. Between 0 and 49 c. Between 1 and 50 d. Between 0 and 51 e. Between 1 and 49
c. Between 1 and 50
What language is C++ directly based on? a. Algol b. B c. C d. Java
c. C
When designing a recursive solution, it is best to do what? a. Construct a mathematical proof that the solution will terminate b. Pay close attention to the multiple nested candles c. Focus on reducing a problem to a slightly simpler one
c. Focus on reducing a problem to a slightly simpler one
Why should you write functions? a. Functions are not that useful and cause the program to be longer and more difficult to read b. Most programs crash because functions are being used, so they should be avoided at all costs and never used unless absolutely necessary c. Functions allow you to break up your program into logical pieces - especially when you plan to do a set of coding lines multiple times d. Functions do not exist in the C++ programming language e. Functions are a technique to accomplish the exact same thing as a for loop - so when I wanted to repeat a sequence of lines a set number of times
c. Functions allow you to break up your program into logical pieces - especially when you plan to do a set of coding lines multiple times
Which of the following is NOT an advantage of programming in a higher-level language instead of machine code? a. Programmers can program using a syntax that is easier for humans b. A program can be written independent of the hardware it will run on c. Instructions do not need to be encoded before a program runs d. Programming is less tedious and less error-prone
c. Instructions do not need to be encoded before a program runs
What is function tracing? a. Making a map of all of the external libraries you will be using within a program. b. Drawing a picture of a bug in order to de-stress during late-night programming. c. Running through a complex function's execution on a piece of paper to ensure it will work d. Figuring out the chain of function calls from a given function to the main function.
c. Running through a complex function's execution on a piece of paper to ensure it will work
Which of the following symbols is used to terminate a C++ program statement? a. Colon b. Period c. Semicolon d. Single quote
c. Semicolon
The two lines of code below both produce the same output. Why? cout << "Hello" << " Goodbye"; cout << "Hello" << "Goodbye"; a. Since there are no numerical / arithmetic operators, the spaces don't matter b. In fact, the two statements do not produce the same output c. Spacing between symbols in C++ doesn't affect statements d. Because there are no escape characters
c. Spacing between symbols in C++ doesn't affect statements
Which loop does NOT check a condition at the beginning of a loop? a. The for loop b. The while loop c. The do-while loop d. Any loop using a Boolean value
c. The do-while loop
If you assume that weight has value 5.87863, what output is generated by the statement below? cout << "The weight is " << setw(4) << fixed << setprecision(2) << weight << endl; a. The weight is 5.87 b. The weight is 5.8786 c. The weight is 5.88 d. The weight is 5.90
c. The weight is 5.88
Given the following 2D array definition: char ticTacToe[3][3] = { {'_', '_', '_'}, {'_', '_', '_'}, {'_', '_', '_'}, }; How many rows are there? How many columns are there? a. There are 2 rows and 2 columns in the table called data b. There are 4 rows and 4 columns in the table called data c. There are 3 rows and 3 columns in the table called data d. There are 3 rows and 4 columns in the table called data
c. There are 3 rows and 3 columns in the table called data
The "else" part of the following statement has a special name. What is it? In other words, why does this code display just "Good" if the user wants to type in 2 am? string time = ""; int hour = 0; string suffix = ""; cin >> hour; cin >> suffix; if (suffix == "pm") if (hour < 6) time = "afternoon"; else if (hour >= 6) time = "evening"; else time = "morning"; cout << "Good " + time << endl; a. Wizards. b. This is known as hand-tracing and you use it to fix the code using the || and && to fix the code using the code c. This is a dangling else. The last else, regardless of the spacing, is paired with the inside/nested if. You can easily fix this by adding the { and } in the appropriate places. d. There is nothing wrong with this code. If 2 am was entered it would display "Good Morning." (You cannot fool us Dr. Stephany and Dr. Estell). e. This is a triple nested if statement and you need to use the [ and ] to fix the code
c. This is a dangling else. The last else, regardless of the spacing, is paired with the inside/nested if. You can easily fix this by adding the { and } in the appropriate places.
Which of the following is a common pointer error? a. Setting a pointer variable to a new value b. Setting a pointer variable to 'null' c. Using a pointer that has not been initialized d. Dereferencing a pointer
c. Using a pointer that has not been initialized
Which of the following programs are not typically written in C++? a. Database management systems b. Operating systems c. Web applications d. Embedded systems
c. Web applications
Which of the following options does the array/pointer duality law state? a. a[n] is the same as *a + n b. a[n] is the same as a -> n c. a[n] is the same as *(a + n) d. a[n] is the same as (a + *n)
c. a[n] is the same as *(a + n)
What would the console output of this code snippet be? string foo = "abcde"; foo[3] ; 'z'; cout << foo << endl; a. abzde b. abcdz c. abcze d. abcde
c. abcze
The part of the computer that executes instructions is the a. primary storage b. secondary storage c. central processing unit d. hard disk
c. central processing unit
Which of the following code segments is most appropriate for reading an integer value t be entered by the user? a. cout << "Please enter the number of tests"; int tests; cin >> tests; b. cout << "Please enter the number of tests: " << endl; int tests; cin >> tests; c. cout << "Please enter the number of tests: "; int tests; cin >> tests; d. cout <<"Please enter the number of tests: "; double tests; cin >> tests;
c. cout << "Please enter the number of tests: "; int tests; cin >> tests;
A bank account earns interest of p percent per year. How do you computer the interest earned in one year? Assume variables p and balance of type double have already been defined. a. double interest = p % balance; b. double interest = balance % p; c. double interest = balance * p / 100; d. double interest = pow(balance, p / 100);
c. double interest = balance * p / 100;
Which one of these statements is a correct translation of the following equation: 3a2+bxc Assume that the following variables have been declared: double a = 2; double b = 3; double c = 12; a. double result = (3 / a)^2 + b * c; b. double result = (3 \ a) **2 + b * c; c. double result = (3 / a) * (3 / a) + b * c; d. double result = (3 \ a) * (3 \ a) + b * c;
c. double result = (3 / a) * (3 / a) + b * c;
The physical computer and peripheral devices are collectively called the a. software b. programming system c. hardware d. programs
c. hardware
To get command line arguments, we use which of the following? a. int main() b. int main(int argc, char argv) c. intmain(int argc, char* argv[]) d. int main(int argc
c. intmain(int argc, char* argv[])
In the program below, what variable can be legally displayed using the incomplete cout statement? int compute_result(int value) { int result = value * 2; return result; } int main() { int limit = 5; for (int k = 1; k <= limit; k++) { cout << compute_result(k) << end l; } cout << _______; } a. value b. result c. limit d. k
c. limit
A C++ programmer specifies how values should be formatted by using: a. sequencers b. formatters c. manipulators d. operators e. separators
c. manipulators
Choose the missing condition for the following code fragment. The code is intended to validate an integer value entered by the user to represent the current month. Assume that Boolean variable valid has been previously declared. if (_____) { valid = true; } else { valid = false; } a. month < 1 || month > 12 b. month >= 1 || month <= 12 c. month >= 1 && month <= 12 d. month > 1 && month < 12
c. month >= 1 && month <= 12
Select a pseudocode statement to complete the following algorithm, which computes the cost of a shipment. Shipping costs depend on the weight of the item being shipped. The cost is $10 if the item weighs up to five pounds. For heavier items, the cost is $10 plus $2 for each pound in excess of five. cost = $10 if weight is greater than five pounds extra charge = $2 x number of excess pounds add extra charge to cost a. number of excess pounds = weight b. number of excess pounds = weight + 5 c. number of excess pounds = weight - 5 d. number of excess pounds = weight - 10
c. number of excess pounds = weight - 5
How would you format an integer "a" with with white space padding on the left to a stream "out_file?" a. out_file << pad(10) << left << a; b. pad(10) >> left >> a >> out_file; c. out_file << left << setw(10) << a; d. out_file >> left >> setw(10) >> a;
c. out_file << left << setw(10) << a;
Which of the following is a valid declaration of an empty vector of doubles? a. vector foo<double>; b. vector[double] foo; c. vector<double> foo; d. double foo<>;
c. vector<double> foo;
In the following code fragment, the variables 'size' and 'COUNT' serve what roles? const int COUNT = 100; int data[count]; int size = 0; int value; while ((size < COUNT) && (cin >> value)) { data[size] = value; } cout << myarray[2]; a. 'size' is the capacity of the array; 'COUNT' is the number of elements that are equal to zero b. 'size' is the number of elements unused in the array; 'COUNT' is the number of inputs c. 'size' is the capacity of the array; 'COUNT' is the number of elements currently used d. 'size' is the number of elements used in the array; 'COUNT' is the array capacity
d. 'size' is the number of elements used in the array; 'COUNT' is the array capacity
Given the array b is initialized as: int b[] = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 }; And we call both functions: fillArray(b); printArray(b, 10); What will be displayed to the screen? void fillArray(int b[]) { for (int i = 9; i > 0; i--) { b[i] += b[i - 1]; } void printArray(int a[], int size) { for (int i = 0; i < size; i++) { cout << a[i] << " | "; } cout << endl; } a. 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | b. 9 | 7 | 5 | 3 | 1 | 1 | 3 | 5 | 7 | 9 | c. 1 | 1 | 5 | 7 | 9 | 9 | 7 | 5 | 1 | 1 | d. 1 | 3 | 5 | 7 | 9 | 9 | 7 | 5 | 3 | 1 |
d. 1 | 3 | 5 | 7 | 9 | 9 | 7 | 5 | 3 | 1 |
Given: int a[] = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 }; What is the value of total after the following code is run? int total = 0; for (int i = 0; i < 10; i = i + 2) { total = total + a[i]; } a. 12 b. 1 c. 10 d. 13 e. 25
d. 13
Show the output generate by the code segment below. string river = "Niagara"; string attraction = "Falls"; string destination = river + " " + attraction; cout << destination.length(); a. 7 b. 11 c. 12 d. 13
d. 13
What is the result of evaluating the following expression? (14 / 6) % 5 a. 2.5 b. 7 c. 3 d. 2
d. 2
What value is returned by the function call pow(1 + 2, 3)? a. 3.0 b. 6.0 c. 9.0 d. 27.0
d. 27.0
How many times is the value of the variable i printed out by the following code fragment? for (int i = 0; i <= 12; i = i + 4) { cout << i << endl; } a. 1 b. 2 c. 3 d. 4
d. 4
Which of the following statements is true about a compiler? a. A compiler is part of the computer's hardware b. A compiler translates machine instructions into a high-level programming language c. A person who uses a computer for office work often runs a compiler d. A C++ programmer using a compiler doesn't need to know machine instructions
d. A C++ programmer using a compiler doesn't need to know machine instructions
A loop inside another loop is called: a. An infinite loop b. A parallel loop c. A sentinel loop d. A nested loop
d. A nested loop
Which of the following best describes a function stub? a. A function that's only 1 line b. A function has a prototype but no function definition c. A function that is raising compiler errors d. A placeholder function that temporarily returns a sample value while the rest of the program is being developed
d. A placeholder function that temporarily returns a sample value while the rest of the program is being developed
For which of the following applications would a storyboard be most useful? a. A program to compute the sum of the first 1,000 positive integers b. A program to display the amount earned by a $1,000 investment at 3% interest after 1, 2, ..., 20 years c. A program to display a table of Fahrenheit temperatures and their equivalent Celsius temperatures d. A program to compute the maximum and minimum values from a sequence of test scores to be entered by the user
d. A program to compute the maximum and minimum values from a sequence of test scores to be entered by the user
Which of the following problems is NOT a simulation problem? a. Predicting the amount of rainfall tomorrow b. Choosing the basketball team that wins March Madness c. Modeling the growth of a corn plant next summer d. Calculating area of a sphere
d. Calculating area of a sphere
What kind of error is the following code? int size; cin >> size; int * arr; arr = new int[size]; delete[] arr; arr[0] = 22; a. Interoperability error b. Misdirection error c. Recursion error d. Dangling pointer error
d. Dangling pointer error
Which of the following statements are valid with respect to the main function in C++? a. The opening and closing parentheses after the function name main are optional b. A semicolon is required after the declaration int main() c. A semicolon is required after the closing curly braces in the main function d. Every executable C++ program has a main function
d. Every executable C++ program has a main function
The process of hand tracing code is valuable because a. It is usually faster than just running the code b. It is the best way to design an algorithm c. You must already have a working program in order to do it d. It gives valuable insight that you do not get by running the code
d. It gives valuable insight that you do not get by running the code
How could one improve the user experience of a unit conversion program so that users are not frustrated by incompatible units? a. Terminate the program when two units are incompatible b. Return a result of 0 when two units are incompatible: 30 in = 0 oz c. Only provide conversions between compatible units d. List compatible unites in the To unit prompt: e.g., To unit (ft, mi, mm, cm, m, km):
d. List compatible unites in the To unit prompt: e.g., To unit (ft, mi, mm, cm, m, km):
What do you call a decision statement contained inside the branch of another decision statement? a. Arrangement b. Inset c. Bracket d. Nested e. Error / Syntax Mistake
d. Nested
What is the problem with the following algorithm? Repeat a number of times Add sales amount to total sales a. The algorithm has a repeat instruction, but statements in a program cannot be repeated b. The add statement in the algorithm is not executable c. The add statement in the algorithm does not clearly specify what is to be added d. The algorithm is ambiguous because it does not specify how many times to repeat the add statement
d. The algorithm is ambiguous because it does not specify how many times to repeat the add statement
Based on the following code snippet, which of the following statements is correct? #include <iostream> using namespace std; void recursive_func(int count) { recursive_func(count + 2); cout << count << endl; } int main() { recursive_func(1); return 0; } a. The code snippet executes to completion but does not produce any output. b. The code snippet executes and gets into an infinite loop but does not print anything. c. The code snippet executes and displays 1. d. The code snippet executes and gets into an infinite loop that prints a lot of values. e. The code snippet gives a compilation error because the recursive_func function cannot call itself.
d. The code snippet executes and gets into an infinite loop that prints a lot of values.
Which one of the following statements is true about the if statement? a. The if statement can have only one condition that evaluates to an integer value b. The if block is optional c. The if and else blocks should always be included within curly braces d. The else block is optional
d. The else block is optional
What is stepwise refinement? a. The process of unit testing b. The use of a temporary implementation of a function to be improved later c. The design of pseudocode for black-box functions d. The process of breaking complex problems into smaller, manageable pieces
d. The process of breaking complex problems into smaller, manageable pieces
The type double& is read as: a. the value of a double b. a double-precision floating-point c. a passed double parameter d. a reference to a double
d. a reference to a double
Assuming that in_file is an object for reading in text files, which of the following statements can be used to read a name from a data file into an appropriately named string variable? a. in_file.get_line(name); b. in_file << name; c. in_file.get(name); d. in_file >> name;
d. in_file >> name;
In many games, you throw a pair of dice to get a value between 2 and 12. Which of the following accurately simulates this activity? a. int sum = rand() % 12 b. int sum = rand() % 6 + rand() % 6; c. int sum = rand() % 11 + 2; d. int sum = rand() % 6 + rand() 6 + 2;
d. int sum = rand() % 6 + rand() 6 + 2;
Which of the following functions is used to determine whether a character is a capital letter and not any other type of character. a. isalpha b. isalnum c. isdigit d. isupper
d. isupper
Select an expression to complete the code segment below. The nested loops should display: #### ### ## for (int j = 4; j >= 2; j--) { for (int k = 1; _____; k++) { cout << '#'; } cout << endl; } a. k < 3 b. k <= 3 c. k < j d. k <= j
d. k <= j
Which of the following is a valid variable name? a. miles per gallon b. miles/gallon c. 5_miles_per_gallon d. miles_per_gallon
d. miles_per_gallon
What expression will always have the same value as the expression below? !(number_of_dependents > 3 && income <= 50000) a. !(number_of_dependents > 3) && !(income <= 50000) b. number_of_dependents <= 3 && income > 50000 c. number_of_dependents <= 3 || income <= 50000 d. number_dependents <= 3 || income > 50000
d. number_dependents <= 3 || income > 50000
Fill in the blank with the correct one-word response: A structure is defined by using the ___ reserved word. a. structure b. str c. Structure d. struct
d. struct
What would be outputted to the console in this code snippet? string message = "Greetings 1611"; cout << message[4] << endl; a. n b. e c. i d. t
d. t
Assuming that values is an array of LENGTH integer values, insert the missing statement in the following code fragment, which is designed to replace each array element with its square. for (int k = 0; k < LENGTH; k++) { // what is the missing statement that goes here? } a. values[k] = k *= k; b. values[k] = values[k] * 2; c. values[k] = values[k] * k d. values[k] = values[k] * values[k];
d. values[k] = values[k] * values[k];
What does the following code output? char arr[5] = { 'a', 'e', 'i', 'o', 'u' }; char* ptr = arr; ptr = ptr + 3; arr[3] = 'y'; cout << *ptr << endl; a. a b. e c. u d. y
d. y
Use the process of stepwise refinement to
decompose complex tasks into simpler ones
Which of the following expressions will give the result as an integer value? a. 22 / 7 b. 22.0 / 7 c. 22 / 7.0 d. 22.0 / 7.0 e. None of the above
a. 22 / 7
What are the two parts of an if statement? a. A condition and a body b. An increment and a decrement c. A condition and an assignment d. A check and an increment e. An increment and a body
a. A condition and a body
Choose the two components of a variable in C++ a. A duration b. A weight c. A name d. A value
c. A name d. A value
Which of the following is NOT legal in a function definition? a. Parameter variable names b. Parameter variable data types c. One return value d. Multiple return values
d. Multiple return values
The typical ranges for integers may seem strange but are derived from a. Overflows b. Base 10 floating point precision c. Field requirements for typical usage and limits d. Powers of two because of base 2 representation within the computer
d. Powers of two because of base 2 representation within the computer
What kind of operator is the <= operator? a. Ternary b. Arithmetic c. Inequality d. Relational e. Logical
d. Relational
Which one of these statements are valid a. int counter = 1/4; b. int counter = 25; c. int counter = 2.3; d. int counter = -1/4
b. int counter 25;
What is the value of pow(2, 3) ? a. -1 b. 5 c. 6 d. 8 e. 9
d. 8
In the following code snippet, what is the scope of variable b? void func1() { int i = 0; double b = 0; } void func2() { } int main() { func1(); func2(); return 0; } a. It can be used only in func1() b. It can be used in user-defined functions, func1() and func2() c. It can be used anywhere in this program d. It can be used in many programs
a. It can be used only in func1()
A function uses a reference parameter when a. It is designed to update a variable supplied as an argument b. The parameter will take on a number of different values c. The argument supplied to the function is an expression d. The function will not have a return value
a. It is designed to update a variable supplied as an argument
One advantage of designing functions as black boxes is that a. Many programmers can work on the same project without knowing the internal implementation details of functions b. The result that is returned from black-box functions is always the same data type c. The implementation of the function is open for everyone to see d. There are fewer parameters
a. Many programmers can work on the same project without knowing the internal implementation details of functions
The statements that are executed when a function is called are known as: a. The body of the function b. Parameters c. The interface of the function d. The return value of the function
a. The body of the function
Which statement about an 'if' statement is true? a. The condition in an 'if' statement using operators like <= will evaluate to a Boolean result b. The condition in an 'if' statement should make exact comparisons to floating-point numbers c. The condition in an 'if' statement should always evaluate to 'true' d. The condition in an 'if' statement should never include integer variables
a. The condition in an 'if' statement using operators like <= will evaluate to a Boolean result
Consider the following code snippet. What is the potential problem with the if statement? double score = (g1 + g2 + g3 + g4) / 4.0; if (score == 100.0) { cout << "You earned an A in the class!' << endl; } a. Using == to test the double variable 'average' for equality is error-prone b. The conditional will not evaluate to a Boolean value c. The assignment operator should not be used within an if-statement conditional d. Constants should never be used in if-statement conditionals.
a. Using == to test the double variable 'average' for equality is error-prone
A sentinel is a. a value that indicates the end of an input sequence b. a value that creates a bridge between a data set and unrelated input c. a value that is part of the data to be processed by the program d. a value that terminated a program
a. a value that indicates the end of an input sequence
Assuming that the user provides 101 as input, what is the output of the following code snippet? int a; int b; a = 0; cout << "Please enter b: "; cin >> b; if (b > 100) { a = b; } else { a = 0; } cout << "a: " << a << endl; a. a: 101 b. There is no output due to a compilation error c. a: 0 d. a: 100
a. a: 101
Which part of this statement are operands? int a = 5 * b; a. b b. 5 c. int d. a e. =
a. b b. 5
Which of the following is the correct first line for a function definition that takes two arguments of type int and returns true if the first value is greater than the second value? a. bool func(int a, int b) b. boolean func(int a, int b) c. int func(bool a, bool b) d. bool func(int a, b) e. boolean func(int a, b)
a. bool func(int a, int b)
Which line of code should replace the placeholder symbols ??? to count correctly the number of integers among the inputs that are less than 100? int count = 0; int input; while (cin >> input) { if (input < 100) { ??? } } a. count++; b. input++; c. count += input; d. input += input;
a. count++;
What is a valid output statement? a. cout << "Welcome to ONU" << endl; b. "Hello Programming 1!" >> endl >> cout; c. cout("Hello world!"); d. cout << (Hello world!) << endl;
a. cout << "Welcome to ONU" << endl;
Which of the following statements enables the use of input and output operations in a C++ program? a. using namespace std; b. #include <iostream> c. int main(void) d. return 0;
b. #include <iostream>
The value of the expression 20.0 * (9/5) + 32.0 is a. 32.0 b. 52.0 c. 68.0 d. illegal expression, there is no value e. incorrect expression, the / should be %
b. 52.0
Which of the following guidelines will make C++ code more explanatory for others? a. Use more English statements in a code b. Add comments to code c. Avoid usage of complex calculations in code d. Always enclose the statements in curly braces in code
b. Add comments to code
What will be the range of the random numbers generated by the following code snippet? rand() % 100 + 1; a. Between 1 and 99 b. Between 1 and 100 c. Between 0 and 100 d. Between 0 and 99
b. Between 1 and 100
When testing code for correctness, it always makes sense to a. Test all cases b. Identify boundary cases and test them c. Check all cases by hand d. Assume invalid input will never occur
b. Identify boundary cases and test them
The following code snippet contains an error. What is the error? int cost = 0; cin >> cost; if (cost > 100); cost = cost - 10; cout << "Discount cost: " << cost << endl; a. Logical error: use of an uninitialized variable b. Logical error: if statement has do-nothing statement after if condition c. Logical error: assignment statement does not show equality d. Syntax error (won't compile) due to missing curly braces for the body of the if statement
b. Logical error: if statement has do-nothing statement after if condition
An if statement inside another if statement is called a a. Break statement b. Nested if statement c. Switch statement d. Syntax error, since that is not permitted in C++
b. Nested if statement
Consider the following division statements: I. 22 / 7 II. 22.0 / 7 III. 22 / 7.0 IV. 22.0 / 7.0 Which of the following options is correct? a. All the four statements will return an integer value b. Only I will return an integer value c. Only I, II, and II will return an integer value d. Only II and III will return an integer value
b. Only I will return an integer value
Suppose you define a C++ symbol MadCitie and another symbol Madcitie. What can be said about these two symbols? a. Because "citie" is misspelled, the C++ compiler will reject it b. Since C++ is case-sensitive, these are considered to be completely distinct c. Since the C++ symbols both have the same letters, they are considered to be the same d. They are the correct length because all C++ symbols must have exactly eight characters
b. Since C++ is case-sensitive, these are considered to be completely distinct
The Monte Carlo method can find solutions to problems that are difficult to solve precisely. A typical example of such a problem is a. The solution to the quadratic equation (y = ax^2 + bx + c) b. The approximation of the value of pi c. The simulation of multi-sided dice being thrown d. Security algorithms to stop software piracy
b. The approximation of the value of bi
What is the result of the following definition of a vector? vector<int> chkdata; a. The statement causes a compile time error as the size of the vector is not defined b. The statement creates a vector of size 0 c. The statement creates a vector with unlimited size d. The statement creates a vector of size 1
b. The statement creates a vector of size 0
A pointer describes which of the following? a. What a certain value in memory is b. Where a certain value in memory is c. When a certain value in memory is d. The size of a certain value in memory
b. Where a certain value in memory is
Which option represents the best choice for a variable name to represent the average grade of students on an exam? a. AveGd b. average_grade c. $average_grade d. avg_grade
b. average_grade
Many of the C++ library functions expect arguments to be C strings. Which of the following declares a C string? a. char b. char* c. cstring d. cstring* e. string f. *string
b. char*
Which of these expressions will result in the number 1.5? Assume the following declarations have been made: int a = 6; int b = 4; a. double result = a / (double) b; b. double result = (double) a / (double) b; c. double result = (double) (a / b)l d. double result = (int) ((double) a / (double) b);
b. double result = (double) a / (double) b;
Which of the following statements can be used to validate that the user input for the 'floor' variable is between 0 and 20, inclusive? a. if (0 <= floor <= 20) b. if (floor >= 0 && floor <= 20) c. if (floor >= 0 || floor <= 20) d. if (floor <= 0 && floor >= 20) e. if (floor <= 0 || floor >= 20)
b. if (floor >= 0 && floor <= 20)
The 'switch' statement in C++ a. allows a programmer to set a Boolean variable to either 'on' or 'off' b. is like a sequence of 'if' statements that compares a single integer value against several constant alternatives c. is a compound statement that tests all branches against different variables d. allows you to switch the contents of two variables e. requires compound Boolean expressions as alternatives
b. is like a sequence of 'if' statements that compares a single integer value against several constant alternatives
The 'switch' statement in C++ a. is a compound statement that tests all branches against different variables b. is like a sequence of if statements that compares a single integer value against several constant alternatives c. makes the break statement optional d. requires compound Boolean expressions as alternatives
b. is like a sequence of if statements that compares a single integer value against several constant alternatives
How do you compute the length of the string str? a. str.length b. str.length() c. length(str) d. length.str
b. str.length()
Characters that are grouped together between double quotes (quotation marks) in C++ are called a. keywords b. strings c. symbols d. syntax
b. strings
Consider the following line of code for calling a function named 'my_fun': my_fun(dataset, vardata); Which one of the following function signatures is valid for 'my_fun', where dataset is an integer array and 'vardata' is an integer variable? a. void my_fun(dataset, vardata) b. void my_fun(int dataset[], int vardata) c. void my_fun(int vtdata[], vdata) d. void my_fun(int vdata, int vtdata[])
b. void my_fun(int dataset[], int vardata)
How many times is the value of the variable i printed out by the following code fragment? for (int i = 0; i < 12; i = i + 4) cout << i << endl; a. 1 b. 2 c. 3 d. 4 e. Program does not compile because the curly braces for the for loop are missing
c. 3
How many times does the following code fragment display "Hello World"? int i = 5; while ( i >= 0 ) { cout << "Hello World" << endl; i--; } a. 4 times b. 5 times c. 6 times d. An infinite number of times
c. 6 times
In a 'switch' statement, if a 'break' statement is missing a. The statement will not complete b. The break happens at the end of each branch by default c. Execution falls through the next branch until a break statement is reached d. The default case is automatically executed
c. Execution falls through the next branch until a break statement is reached
Which of the following statements is valid with respect to the usage of the endl symbol? I. The endl symbol denotes the end of line marker II. When the end of line marker is sent out to cout, the cursor is moved to the first column in the next screen row III. It is a good practice to end all lines of output with an end of line marker a. I only b. II only c. I and II only d. I and III only
c. I and II only
Which of the following statements about the <code>if</code> statement? a. The <code>if</code> statement can have only one condition that evaluates to an integer value b. The <code>if</code> and <code>else</code> blocks should always be included within curly braces c. The <code>else</code> block is optional d. The <code>if</code> block is optional
c. The <code>else</code> block is optional
What is the problem with the following if statement? double count = 15.0; if (count / 3.0) { cout << "The value of the count is " << count << endl; } a. There should be an "else" condition b. The variable 'count' is not initialized c. The expression within the parentheses does not evaluate to a Boolean value d. It is never possible to use the "/" operator in an if statement
c. The expression within the parentheses does not evaluate to a Boolean value
What value does the following function call return? time( 0 ) a. The current time as a string in the form of hh:mm:ss (hour, minutes, and seconds) b. The current time as a string in the form of hh:mm (hour, minutes), followed by AM or PM c. The number of seconds elapsed since 00:00 GMT 01 January 1970 d. The number of seconds that have elapsed so far today
c. The number of seconds elapsed since 00:00 GMT 01 January 1970
Consider the following code snippet: bool attendance = true; bool failed = false; Which of the following 'if' statements includes a condition that evaluates to true? a. if (attendance = "true") { ... } b. if (attendance == "true") { ... } c. if (attendance) { ... } d. if (failed) { ... } e. if (attendance == failed) { ... }
c. if (attendance) { ... }
Which of the following conditions tests for the failure of the user to enter an integer value for the 'number' variable? int number; cout << "Number: "; cin >> number; a. if (fail(number)) b. if (cin.fail) c. if (cin.fail()) d. if (number < 0) e. if (!cin.isInteger())
c. if (cin.fail())
Which code snippet will output "Yes!" when the contents of the two strings s1 and s2 are equal? a. if (s1 = s2) { cout << "Yes!" << endl; } b. if (s1 < s2) { cout << "Yes!" << endl; } c. if (s1 == s2) { cout << "Yes!" << endl; } d. if (s1 != s2) { cout << "Yes!" << endl; }
c. if (s1 == s2) { cout << "Yes!" << endl; }
What is the output of the following code snippet? cout << "\"cout << endl\" prints out a new line to "; cout << "standard output " << endl; a. No output - compilation error because the cout statement is inside the quotation marks b. cout prints out a new line to standard output c. cout << endl prints out a new line to standard output d. "cout << endl" prints out a new line to standard output
d. "cout << endl" prints out a new line to standard output
Which condition, when supplied in the if statement below in place of (...), will correctly protect against division by zero? if (...) { result = grade / num; } a. (grade == 0) b. ((grade / num) == 0) c. (num == 0) d. (num != 0)
d. (num != 0)
What is the valid range of index value for an array of size 10? a. 0 to 10 b. 1 to 9 c. 1 to 10 d. 0 to 9 e. 0 to 11 f. 1 to 11
d. 0 to 9
What is the output of this code snippet? int sum = 22; sum = sum + 2; cout << sum++; cout << sum; a. 2428 b. 2528 c. 2424 d. 2425
d. 2425
Assuming that a user enters 25 as the value for x, what is the output of the code snippet below? int x = 0; cout << "Enter number: "; cin >> x; if (x < 100) x = x + 5; if (x < 500) x = x - 2; if (x > 10) x++; else x--; cout << x << endl a. 2 b. 27 c. 28 d. 29 e. 30
d. 29
What is the output of the following code snippet? int arr[5] = { 10, 20, 30, 40, 50 }; int* ptr = arr; ptr = ptr + 2; cout << *ptr << endl; a. 10 b. 12 c .20 d. 30 e. 40
d. 30
What is the output of the following program? #include <iostream> using namespace std; int main () { int value = 3; value++; cout << value << endl; return 0; } a. 0 b. 2 c. 3 d. 4 e. No output due to syntax error
d. 4
Suppose you define a C++ variable as SadKitie and another variable as Sadkitie. What can be said about these two variables? a. Since the C++ variables both have the same letters, they are considered to be the same variable. b. They are the correct length because all C++ variables must have no more than eight characters. c. Because "kitty" is misspelled as "kitie", the C++ compiler will reject it. d. Since C++ is case-sensitive, these are considered to be completely distinct variables. e. Neither are legal variables in C++ because they both start with an uppercase letter.
d. Since C++ is case-sensitive, these are considered to be completely distinct variables.
When a compiler finds a syntax error in a program, what happens? a. The compiler produces an executable program but leaves out the statement where there was an error b. The compiler stops immediately c. The compiler requests input from the user before it will continue d. The compiler continues and may report about other errors but does not produce an executable file
d. The compiler continues and may report about other errors but does not produce an executable file
If n is a positive number, what is (n / 10) % 10? a. The first digit of n b. The last digit of n c. The second digit of n d. The second-to-last digit of n
d. The second-to-last digit of n
What is the output from this code snippet? cout << "The sum is " << "8 + 6"; a. The sum is 14 b. The sum is 86 c. The sum is 2 d. The sum is 8 + 6
d. The sum is 8 + 6
Which of the following statements is correct about constants? a. Constants are written using uppercase letters as the compiler ignores those using lowercase letters b. The data stored inside a const variable can be changed using an assignment statement c. You can make a variable constant by using the constant reserved word while declaring the variable d. Variables defined using const make a code snippet more readable and easier to maintain
d. Variables defined using const make a code snippet more readable and easier to maintain
What is the name of the integrated development environment (IDE) used in this course? a. Visual C++ b. Visual Coder c. Visual Programmer d. Visual Studio
d. Visual Studio
Which one of the following observations for the following code snippet is true? int ch = 100; int* ptr = &ch; a. &ptr gives the address of 'ch' b. &ptr gives the value of 'ch' c. ptr stores the value of 'ch' d. ptr stores the address of 'ch'
d. ptr stores the address of 'ch'
What is printed by the following code snippet? int sum = 0; for (int j = 0; j <= 10; j + 2) { sum += j; } cout << sum; a. 0 b. 20 c. 30 d. 50 e. Nothing - no output is displayed
e. Nothing - no output is displayed
What is wrong with this line of code? double canVolume = 0.355; // Liters in a 12-ounce can */ a. The const keyword is missing b. Comments that start with the // delimiter must also end with the // delimiter c. Comments that end with the */ delimiter must always start with the /* delimiter d. Comment is inaccurate: liters measure volume while ounces measure weight e. There is nothing wrong with this line of code
e. There is nothing wrong with this line of code
Fill in the blank: The number of characters in a string is called the __________ of the string. a. size b. dimension c. range d. extent e. length
e. length