W11 quiz: cstring, optparams, structs Flashcards

1
Q

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?

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The update statement is performed at the bottom of a for loop, just after
each pass through the body.

_ True?
_ False?

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What output is produced by the segment of code shown below:

for (int i = 1; i <= 10; i = i + 2)
{
cout &laquo_space;i;
}

a: 13579
b: 1357
c: 12345678910
d: 13578

A

a: 13579

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What loos needs a semi-colon after?

a: do
b: while
c: all of the options listed
d: for

A

a: do

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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
A

d: 7733448

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

In nested loops, the outer loop runs to completion first (likely multiple times)
before the inner loop is executed.

_ True?
_ False?

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The following code will produce an infinite loop:

int main()
{
int i = 10;
while(i<100)
{
cout &laquo_space;i &laquo_space;endl;
}
}

_ True?
_ False?

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What loops are guaranteed to execute at least once?

a: while
b: none of the options listed
c: do
d: for

A

c: do

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

An array is organized into positions, each acting as a storage spot for one item.
_ True?
_ False?

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the 2 expanding ways that we can declare and call functions?

A

optional parameters and function overloading

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Is it possible to provide default values for a parameter?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What option can the caller have with the parameter?

A

The caller has the option of calling the function with or without passing that specific parameter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Is it possible to declare multiple different versions of a function with the same name?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the condition for declaring multiple different versions of a function with the same name?

A

The parameter lists are “different enough” for the compiler to always identify the correct one to call

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can we assign a default for one or more parameters in the declaration?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Can the caller omit one or more of the parameters?

A

Yes
example:
myfunction(10); // uses ‘a’ for y, 1.3 for z
myfunction(10, ‘q’); // uses 1.3 for z

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the order of parameter assignment?

A

left-to-right order
example:
void myfunc(int x=1, int y=2, string z=”boo”);
● myfunc(3); // always gets assigned to x

18
Q

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

A

No, it does not work

19
Q

Can we declare multiple functions with different names?

A

No, we cannot. We have to declare multiple functions with the same name

20
Q

If you declare multiple functions with the same name, what is another condition so that it works?

A

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);

21
Q

the compiler decides which to use based on params only, what do these lines mean?
void f1(int x);
int f1(long y);

A

// later called with int i = f1(3)
// gives error, f1 void

22
Q

void f1(int x);
int f1(int y, string z=”boo”);
f1(10); // could mean either one

explain what the codes are saying?

A

when functions include optional parameters then you must also account for any possible combination of passed parameters when considering the ambiguity

23
Q

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>

A

//1: uses “Monday”
//2: uses 7, 1

24
Q

What is the format of a function definition?

A

return-value-type function-name (parameters list)
{ declaration and statement }

25
Q

Can a function be declared inside another function?

A

Under any circumstances, the answer is still no

26
Q

what does the return-value-type Void indicate?

A

It indicates that a function does not return a value

27
Q

Which type of value will be assigned for a function if it does not declare its return-value-type?

A

int

28
Q

How is a function with empty parameter lists expressed?

A

void print(); //does not take in any argument

29
Q

function overloading:

A

must have different parameters

30
Q

What is the technical difference between structs and classes?

A

the only technical difference between a c++ class and a c++ struct is that structs declare/inherit things publicly by default, whereas classes declare/inherit them privately by default

31
Q

What is the convention for using structs and classes?

A

programmers use 2 quite differently: structs being used purely as data containers, and classes being used where we wish to incorporate more advanced functionality

32
Q

What do structs allow us to do?

A

structs allow us to group elements of different data types into a single logical entity

33
Q

When we define structs, are we defining a new data type?

A

Yes

34
Q

What is the syntax of a struct?

A

we specify a new type name, plus types and names for each of the individual fields
Ex:
struct productInfo
{
string name;
float price;
}

35
Q

Can a struct be a parameter value?

A

Yes it can, a function can have structs as parameters

36
Q

How can a function change the content of a struct?

A

The struct must be passed by reference

37
Q

Where can a struct be stored?

A

In arrays

38
Q

An example of struct use

A

productInfor p;
cout &laquo_space;“Enter the product name and price” &laquo_space;endl;
cin&raquo_space; p.name&raquo_space; p.price;
cout &laquo_space;p.name &laquo_space;” $” &laquo_space;p.price &laquo_space;endl;

39
Q

How do you access structure members?

A

Use the dot syntax (.)

40
Q

What is the name for specifying more than one function of the same name in the same scope?

A

function overloading