CSCI 1170 Final Flashcards
Suppose the file numbers.dat does not exist. If the following snippet was in your main function, what would be the output?
std: :ofstream my_file;
my_file. open (“numbers. dat”) ;
if ( !my_file ){
std: : cout «_space;“Potato” «_space;std::endl;
} else {
std: :cout «_space;“Success” «_space;std::endl; }
Success
In the following function definition (line numbers provided for reference:
1 void B(int a, int b){
2 b + a+3;
3 cout «_space;a «_space;” “ «_space;b «_space;endl;
4 }
Line 1 in the definition above is referred to as the ____?
Function Header
Read the following program.
#include <iostream></iostream>
void ClaculateSum (int &sum) {
sum = 0;
for (int i = 0; i < 5; i++)
sum +=i;
}
int main(){
int sum = 0;
CalculateSum(sum);
std::cout «_space;sum «_space;std::end;
}
What is the printed value at the end of execution?
10
Given the following definition:
int values [5] = {1,2,3};
What will the following statement display?
std::cout «_space;values[3] «_space;std::endl;
0
Given the following assignment statement: double result = 15 / 16;
What is the value stored in the result?
2.0
Which of the following code snippets does not produce the same output as the rest?
int x = 1;
While (x <= 5) (
std:: cout « “I’m here!”
«_space;std: :endl;
}
for(int i = 10; 1> 0; 1 == 4)1
std:: cout <
“I’m here!”
«_space;std:: endl;
}
int x = 0;
while (x<5) (
x++;
std:: cout « “I’m here!”
« std: :endl;
}
for (int i = 1; 1 < 5; i++) (
std:: cout «
“I’m here!”
< std:: endl;
}
for (int i = 1; 1 < 5; i++) (
std:: cout «
“I’m here!”
< std:: endl;
}
Every C++ program MUST have a—–?
main function
Read the following code:
std::string first_name = “Locke”;
char middle_init = ‘L;
std: string last_name = “Lamora”;
std::cout «_space;last_name «_space;middle_init «_space;first_name «< ‘In’;
What is printed out after the above code is executed?
LamoraLLocke
Assume that in_file has been defines as a file object you can read from.
int value;
in_file»_space; value;
The statements above would create a variable called value and repeatedly read from the file until no values were left in it.
False
Consider the following function definition:
___ getGrade (double score)
{ if (score >= 90.0)
return ‘A’;
else if (score >= 80.0)
return ‘B’:
else if (score >= 60)
return ‘D’;
else
return ‘F’;
What should the return type of the function be?
char
Read the function definition:
void myFunction(int value) {
if (value !=0) {
cout «_space;“Hello” «_space;endl;
} else {
return;
}
}
The return statement causes an error when trying to compile. (T or F)
False
Read the following function prototypes:
int DoSomething (int value, int size) ; double DoSomething (double value, int size) ;
float DoSomething (float value, int size) ; void DoSomething (int value, int size) ;
Would all of these functions be allowed in a single program in C++ as overloaded functions?
MacBook Pro
No because the integer returning function and the void function have the same function signature due to the return type being ignored when looking at a function’s signature.
Read the following code snippet:
int = = 0;
int k;
while (i < 10) (
for (k = 4; k <= 6; k++) {
sta:: cout «< “Hello”
«_space;std: :endl;
}
i +=2:
}
How many times total is “Hello” printed?
15
The following array declaration would create an array of size 10 and initialize all values in the array to 0.
int numbers [10];
False
What would be output by the following code?
bool even = false;
if (even = true);
{
std::cout «_space;“It is even!”;
} else {
std::cout «_space;“It is odd!”;
}
It is even!
In C++ the name (identifier) of an array is really the address of the beginning/first element. (T o F)
True