CS Final
Which of the following lines of code are necessary to include in the PhoneNumber.cpp file (below) to implement the constructor from the PhoneNumber.h file above? See the implementation below: [Choose all that apply] PhoneNumber(int areaCode, int centralOfficeCode, int lineNumber){ m_areaCode = areaCode; m_centralOfficeCode = centralOfficeCode; m_lineNumber = lineNumber;}
#include "PhoneNumber.h" PhoneNumber::
Which of the following lines does NOT consist of (a) valid comment(s)?
/* This is a //*// First Rate Program //*//
How many spaces are printed out by this statement: cout << "how" << "now" << "brown" << "cow" << "?";
0
In C++ when a relational expression is false, it has the value:
0
What output is produced by the following code? int count = 0;do {cout << count << endl;count++;} while (count >= 4);cout << "count after loop = " << cout << endl;
0 count after loop = 1
In object-oriented programming with C++, what is the purpose of a class?
A class is a user defined data type that encapsulates both data and functionality and has access modifiers to control access to its data.
What are the differences between a for loop and a do-while loop?
A for loop is a counter-controlled loop. A do-while loop is sentinel controlled and executes its loop body at least once. For loops are pre-test, but do-while loops are post-test.
What is the purpose of a namespace in C++?
A named scope that prevents them from being mistaken for identically-named symbols in other scopes.
In object-oriented programming with C++, what is the purpose of an object?
A object is an instance of a class. It has memory allocated to it and is given values for its data.
What are the differences between structures (struct) and classes in C++? [Select all that apply, there may be more than one correct answer]
A structure is a composite data type with data only. A class is a composite data type with both member variables and member functions. A structure has entirely public data. A class can have both public and private data.
Which of the following most closely represents the purpose of a header file?
Contain definitions of variables and function prototypes which can be imported into any C++ program.
Which of the following most closely represents the purpose of an implementation file?
Contain the implementation code for the member functions of a class.
What output is produced by the following code? int i = 0;while (i == 0) {cout << "Don't you just love loops? :)" << endl;i++;}if (i == 0) {cout << "After the loop." << endl;}
Don't you just love loops? :)
What output is produced by the following code? int time = 2, tide = 3; if (time + tide > 6)cout << "Time and tide wait for no one.";else if (time + tide > 5)cout << "Time and tide wait for someone.";else if (time + tide > 4)cout << "Time and tide wait for everyone.";elsecout << "Time and tide wait for me!";
Time and tide wait for everyone
Which of the following best describes the purpose of a default constructor?
To instantiate a new object by initializing its instance variables to null or default values.
Which of the following best describes the purpose of a copy constructor?
To instantiate a new object by initializing its instance variables to the values of another object (of the same type)
To which of the following does the protected keyword grant access? [Choose all that apply - there may be more than one correct answer]
the class itself any derived classes
The ________ is used to display information on the computer's screen (console).
cout object
When a programmer is finished with a chunk of dynamically allocated memory, s/he should free it with the __________ operator.
delete
Which of the following initializes each score in the array examScores to -1.0? const int NUM_STUDENTS = 30; double examScores[NUM_STUDENTS]; [Select all that apply]
for (int i = 0; i < NUM_STUDENTS; i++) examScores[i] = -1.0;
If a class does not have sub classes of its own, then its member variables should be labeled with the _______ access modifier for security.
private
Which of the following is inherited by a derived class?
protected and public member variables and methods
What do the following lines of code output? string word = "computer";cout << word.substr(3, 3);
put
Which of the following C++ keywords is used when a variable belongs to the class, not each object? [Choose all that apply]
static
Given the excerpt of the ASCII table below, what would the following code output; char c = 'H';char d = 'i';cout << (c - d);
-33
What do the following lines of code output? string name = "Programmer"; cout << name.size() << endl;
10
What do the following lines of code output? string name = "Programmer";cout << name.size() << endl;
10
What is the output of the following code? int i = 1; int numbers[5] = {1, 2, 3, 4, 5}; cout << numbers[i++];
2
What is the output of the following code? int numbers[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; for (int i = 1; i <= 5; i++)cout << numbers[++i];
357
How many iterations will the following loop make? for (int i = 1; i < 8; i += 2.5) {cout << i << endl;}
4
What is the output of the following code: int x = 5, y = 6; cout << ((x != 5) ? x : y) << endl;
6
What is the purpose of an array in C++?
Arrays are a data structure of related data under a single data type and occupy contiguously allocated memory.
Which of the following are properties of a text file? (Select all that apply - there may be more than one correct answer)
Human understandable (can be read and modified) Contains a stream of ASCII text A typical file extension is .txt
Given the following array definition: int numbers[ ] = {2, 4, 6, 8, 10}; What will the following statement display? cout << numbers[5] << endl;
Indeterminate
With respect to object-oriented programming in C++, what is the definition of a constructor?
It is used primarily to create new objects.
What is the name and purpose of the * operator in C++?
It's named the "indirection (pointer) operator" and returns the value stored at the memory location.
What is the difference between a member variable and a class variable?
Member variables belong to each object, class variables are shared by all objects.
Every C++ program must contain a _____ method.
main
Every C++ program must contain a _____ method. (Choose all that apply, there may be more than one correct answer)
main
(True or False) A base class in C++ can have more than one constructor.
true
(True or False) In C++, a class can directly inherit from two or more base classes.
true
(True or False) In C++, a class can have two or more derived classes.
true
Which of the following loops (repetition statements) would be most appropriate to read through a text file with an unknown number of lines of text in it?
while loop
Which of the following lines of code should be placed around the class to prevent the PhoneNumber.h file from being re-compiled in the same .cpp file more than once? [Choose all that apply] class PhoneNumber{ private:int m_areaCode;int m_centralOfficeCode;int m_lineNumber; public:PhoneNumber(int, int, int); friend ostream& operator<< (ostream& os, const PhoneNumber& p); };
#endif #ifndef PHONENUMBER_H #define PHONENUMBER_H
Which of the following include directives is needed to allow use of the various I/O operators such as cout and cin? (Select all that apply, there may be more than one correct answer)
#include <iostream>
A comment starts with what characters?
// (two forward slashes)
What does the following loop output? int i = 0;while ( i < 25) {cout << i << " ";i+= 5;}
0 5 10 15 20
How many iterations will the following loop make? for (int i = 1l i < 9; i +=2) cout << "i";
4
How many lines are printed by this statement: cout << "abc\ndef\tghi\njkl" << "\n" << "\n" << "mno\npqr\n"; (count all lines, including blank ones, with the exception of the line where the cursor ends up)
6
How many lines are printed out by this statement: cout << "abc\ndef\tghi\njkl" << endl << endl << "mno\npqr\n"; (write your answer as a number, e.g. 2) Count all lines, except the last one where the cursor ends up.
6
What is the value of the variable sum after the following loop executes? int sum = 0; for (int i = 0; i < 20; i +=3) sum += i;
63
Assume the variables: u = 2, v = 3, w = 4, x = 5, y = 6; have been declared and given values. What is the value of the following expression? u + (y % v) * w + x
7
How many iterations (repetitions) does the following loop encounter? int sum = 0; for (int i = 0; i < 20; i +=3) sum += i
7
What would the following code output? double scores[] = {90.0, 80.0, 75.0, 70.0, 60.0}; cout << scores[2] << endl;
75.0
Given the following array definition: int numbers[ ] = {2, 4, 6, 8, 10}; What will the following statement display? cout << numbers[3] << endl;
8
Which standard library header file is used for functions prototype for standard input and output functions?
<iostream>
What is the dot operator (.)?
Allows programmer to access a specific member of a class
C++ defines a series of access specifier for members (variables and functions) of a class. Which of the following best describes the friend access specifier?
Although the member function is not strictly part of the class, it still has access to the private members of the class.
With respect to object-oriented programming in C++, what is the definition of a member variable?
An attribute (data) that is stored about the object
When referring to arrays, what is an element?
An element refers to the value at a certain position in the array.
What is a syntax error?
An error that occurs when there is an issue in the writing of the code itself (e.g. programmer forgot a semi-colon at the end of a statement)
What are the differences between a declaration and an expression in C++? (Select all that apply, there could be more than one correct answer)
An expression is a statement that has both a value and a type, while a declaration introduces new names into a program.
When referring to arrays, what is an index?
An index refers to a certain position in the array.
With respect to object-oriented programming in C++, what is the definition of a member function?
An operation that can be performed by the object.
Consider the following fragment of code (assume the integer variable x has already been declared and initialized with a value): if (x > 5)cout << "A";else if (x < 10)cout << "B";elsecout << "C"; What is displayed if x has the value 5?
B
What is the purpose of an inline function?
C++ enhancement feature that allows the compiler to place a copy of the code of that function at each point where the function is called.
What output is produced by the following code? for(int i = 100; i > 2;i -= 2) {cout << i << endl;}
Even numbers counting down from 100 to 4
C++ defines a series of access specifiers for members (variables and functions) of a class. Which of the following best describes the public access specifier?
The member is accessible to all classes.
C++ defines a series of access specifiers for members (variables and functions) of a class. Which of the following best describes the private access specifier?
The member is accessible to only the class itself.
Which of the following best describes the purpose of a parameterized constructor?
To instantiate a new object by initializing its instance variables with the arguments coming into the constructor.
What are the differences between compile time arrays and dynamically allocated arrays?
With compile time arrays, the size is predetermined. C++ automatically deletes array. With dynamically allocated arrays, the size is generated when the code is executed. Size is typically specified by the user. Programmer must manually delete the array.
What is the output of the following code? for (int i = 1; i < 4; i++) {switch (i) {case 1:cout << "You have chosen UC San Diego" << endl;break;case 2:cout << "You have chosen CSU San Marcos" << endl;break;case 3:cout << "You have chosen UC Berkeley" << endl;break;case 4:cout << "You have chosen UC Irvine" << endl;break;default:cout << "You have chosen SDSU" << endl;break;}}
You have chosen UC San DiegoYou have chosen CSU San MarcosYou have chosen UC Berkeley
In programming terms, a group of characters inside a set of double quotation marks (" ") is called
a string literal
Given the following definition of an array: const int NUM_STUDENTS = 30;int attendance[NUM_STUDENTS]; Which of the following expression refers to the first element in the attendance array?
attendance[0]
Given the following definition of an array: const int NUM_STUDENTS = 30;int attendance[NUM_STUDENTS]; Which of the following expressions refers to the last element in the attendance array? (Select all that apply - there may be more than one correct answer)
attendance[NUM_STUDENTS-1] attendance[29]
The text of a comment:
can be anything the programmer wants to write.
Which of the following would be examples of classes in C++? [select all that apply]
car sports car football team programming language
Which of the following correctly declares a constant to keep track of the baseline temperature of a human? Currently, the baseline temperature for an adult is 98.2 degrees Fahrenheit. (Select all that apply, there could be more than one correct answer)
const double BASELINE_TEMP = 98.2;
Assume two variables billAmount (double) and tipPercentage (double) and tipAmount (double) have already been declared. Which of the following would correctly prompt the user to enter the bill amount and tip percentage (as a percent %), then calculate the correct tip amount? (Select all that apply, there could be more than one correct answer)
cout << "Enter bill amount $";cin >> billAmount; cout << "Enter tip percentage %";cin >> tipPercentage; tipAmount = (billAmount * tipPercentage) / 100.0; cout << "Enter bill amount $";cin >> billAmount; cout << "Enter tip percentage %";cin >> tipPercentage; tipAmount = billAmount * (tipPercentage / 100.0);
Which of the following would declare and initialize a double array named dailyLowTemps with 31 elements, all initialized to 0.0?
double dailyLowTemps[31] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; double dailyLowTemps[31];for (int i = 0; i < 31; i++) dailyLowTemps[i] = 0.0;
Which of the following nested loops produces the following output? 1111 2222 3333 4444 5555
for (int row = 1; row <= 5; row++){ for (int col = 1; col <= 4; col++) { cout << row; } cout << endl;}
Assume the user is prompted to enter a grade between 0 and 100 (inclusive). Which of the following would perform input validation and check to see if the grade was outside the range (e.g. less than 0, greater than 100)? The user input has already been stored in a variable named grade. Select all that apply - there may be more than one correct answer
if (grade < 0 || grade > 100) if (grade > 100 || grade < 0)
Which of the following fragments of code tests whether an integer variable named score is in the range 0 to 100 (including 0 and 100). (select all that apply - there could be more than one correct answer)
if (score >= 0 && score < = 100) if (score <= 0 && score > = 0)
The ________ statement executes one block of statements if a test condition is true, and another block if the condition is false.
if/else
Which of the following data types are necessary to read and write text to/from a file? (Choose all that apply - there may be more than one correct answer)
ifstream ofstream
Assume you are working with an already defined text file named Input.txt, which is saved in the same folder as your C++ source code. Input.txt is a file containing data (of an unknown type). Which one of the following excerpts of C++ code will open the text file for reading, loop through each line in the file (assume text data) and keep a count of how many lines are in the entire file? After the loop completes, the code should be sure to close the file, and display the count of lines in the file in a message like "The file: Input.txt contains 18 lines." where the value (18) depends on how many lines are in the file.
ifstream inFile; inFile.open("Input.txt"); int count = 0;string line; while (!inFile.eof()){ inFile >> line;count++; }cout << "The file: Input.txt contains " + count + " lines." << endl;inFile.close();
What is the output of the following loop? for (int i = 1; i < 9; i += 2) {cout << "i";}
iiii
Which of the following keywords are the names of C++ data types? (Choose all that apply, there may be more than one correct answer)
int string bool char double
Which of the following loops would count up through the even numbers from 2 to 1000? (select all that apply - there may be more than one correct answer)
int i=2; while(i <= 1000){i+=2} int i=2; while(i<=1000) {i=i+2}
Which of the following loops would count down through the all numbers from 999 to 1? (select all that apply - there may be more than one correct answer)
int i=999; while(i >= 1) {i--} int i=999; while(i >= 1) {i-=1}
Which of the following excerpts of C++ code accomplishes the following: Defines an integer variable named oddSum and initializes its value to 0. Adds the odd numbers between 0 and 100 and stores the answer in oddSum Only iterates over odd numbers (skips the even ones)
int oddSum = 0; for (int number = 1; number <= 100; number+=2){ oddSum += number;}
Which of the following declares a two-dimensional int array named sudokuBoard with 9 rows and 9 columns?
int sudokuBoard[9][9];
Which of the following keywords are the names of C++ data types? (Choose all that apply, there may be more than one correct answer)
int, short, float, double
Which of the following is NOT a C++ keyword (reserved word)?
main
Which of the following is NOT a C++ keyword (reserved word)? (Select all that apply - there may be more than one correct answer)
main iostream
Which of the following would be examples of objects in C++? [select all that apply]
my sports car san diego chargers c++
A trailing else placed at the end of an if/else if statement provides a default action when ________ of the if conditions is/are true.
none
What output is produced by the following statements? int number = 7; if (number > 0)cout << "Not positive" << endl;elsecout << "Positive" << endl;cout << number;
not positive 7
A pointer that contains the address 0 is called a(n) __________ pointer.
null
What does the following code output? string password = "wordpass";char c = password[1];cout << c << endl;
o
What does the following code output? string password = "wordpass";int size = password.size();int i = 4;for ( i < length) {cout << password[i];i++;}
pass
Which of the following would indicate a failure during execution of a program in C++? Select all that apply - there may be more than one correct answer.
return -1;
Assume you have a double variable named sales, for example: double sales = 1000.0; Which of the following will increase sales by 25% and update the value of the variable sales to store the increased amount? Specifically, after the code executes the value of sales should be 1250.0 (select all that apply - there could be more than one correct answer)
sales = sales*1.25; sales *= 1.25;
Which of the following prints each character of the string word (one character per line) using a for loop with a loop counter variable?
string word = "cs150rocks";int length = word.length(); for (int i = 0; i < length; i++) cout << word.at(i) << endl;