W11 quiz: cstring, optparams, structs Flashcards
Like variables, the contents of an array could be any values until/unless we specifically store something. The first time we store a value in an array position we are initializing that array element.
_ True?
_ False?
True
The update statement is performed at the bottom of a for loop, just after
each pass through the body.
_ True?
_ False?
True
What output is produced by the segment of code shown below:
for (int i = 1; i <= 10; i = i + 2)
{
cout «_space;i;
}
a: 13579
b: 1357
c: 12345678910
d: 13578
a: 13579
What loos needs a semi-colon after?
a: do
b: while
c: all of the options listed
d: for
a: do
What is the output of the following program:
int main()
{
int myArray[4] = {77, 33, 44, 8};
for(int i=0; i < 4; ++i) { cout << myArray[i]; } } a: 7348 b: None of the options listed c:8443377 d: 7733448
d: 7733448
In nested loops, the outer loop runs to completion first (likely multiple times)
before the inner loop is executed.
_ True?
_ False?
False
The following code will produce an infinite loop:
int main()
{
int i = 10;
while(i<100)
{
cout «_space;i «_space;endl;
}
}
_ True?
_ False?
True
What loops are guaranteed to execute at least once?
a: while
b: none of the options listed
c: do
d: for
c: do
An array is organized into positions, each acting as a storage spot for one item.
_ True?
_ False?
True
What are the 2 expanding ways that we can declare and call functions?
optional parameters and function overloading
Is it possible to provide default values for a parameter?
Yes
What option can the caller have with the parameter?
The caller has the option of calling the function with or without passing that specific parameter
Is it possible to declare multiple different versions of a function with the same name?
Yes
What is the condition for declaring multiple different versions of a function with the same name?
The parameter lists are “different enough” for the compiler to always identify the correct one to call
Can we assign a default for one or more parameters in the declaration?
Yes,
example:
void myfunction(int x, char y = ‘a’, float z = 1.3)
note: must be done in the prototype when following the course code standards
Can the caller omit one or more of the parameters?
Yes
example:
myfunction(10); // uses ‘a’ for y, 1.3 for z
myfunction(10, ‘q’); // uses 1.3 for z
What is the order of parameter assignment?
left-to-right order
example:
void myfunc(int x=1, int y=2, string z=”boo”);
● myfunc(3); // always gets assigned to x
Can you choose to use a default value for a “middle” parameter and pass a value for a later one?
ex:
myfunc(0, “hello); // tries to assign “hello” to y
No, it does not work
Can we declare multiple functions with different names?
No, we cannot. We have to declare multiple functions with the same name
If you declare multiple functions with the same name, what is another condition so that it works?
The number/types of parameters have to be unambiguous. e.g.
void printDate(string date);
void printDate(int yy, int mm, int dd);
void printDate(string weekday, string month, string day);
the compiler decides which to use based on params only, what do these lines mean?
void f1(int x);
int f1(long y);
// later called with int i = f1(3)
// gives error, f1 void
void f1(int x);
int f1(int y, string z=”boo”);
f1(10); // could mean either one
explain what the codes are saying?
when functions include optional parameters then you must also account for any possible combination of passed parameters when considering the ambiguity
include <iostream></iostream>
#include <string>
using namespace std;
void printDate(int mm, int dd=1);
void printDate(string wkday=”Monday”);
int main()
{
printDate(10,31);
printDate(); //1 meaning?
printDate(“Tuesday”);
printDate(7); //2 meaning?
}
void printDate(int mm, int dd)
{
cout << mm << “/” << dd;
}
void printDate(string wkday)
{
cout << wkday;
}</string>
//1: uses “Monday”
//2: uses 7, 1
What is the format of a function definition?
return-value-type function-name (parameters list)
{ declaration and statement }