337 Midterm 2 Practice Flashcards

1
Q
What is the output of the following?
const char* s[] = {"NBY", "SDT"};
const char** z = s;
cout << z[1][1];
cout << *(z[1] + 2);
cout << (*z + 1);
A

DTBY

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
What is the output of the following program?
void fun(int n){
if (n <= 0) return;
cout << n + 1;
fun (n-3);
cout << n-1;
}

int main()
{
fun (5);
return 0;
}
A

6314

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
Consider the statement: char x[] = {‘A’,’B’, ‘C’, ‘\0’, ‘D’, ‘\0’}, y[] = "ABC";
Which of the following is true in C/C++? Assume: strcmp returns a positive number if x is greater than y,
and a negative number if x is less than y, otherwise it returns zero.

a. x != y
b. strcmp(x, y) == 0
c. sizeof(y) < sizeof(x)
d. strlen(y+1) < strlen(x)
e. All of the above is true
f. None of the above is true
A

a. x != y
The functions, strcmp, sixeof, strlen read until they read a \n character.

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

Which one of the following statements is true:
An assignment operator in a C++ class must:

a. return a reference to *this and must avoid self-copying
b. return a reference to this and must avoid self-copying
c. return a reference to this and must do self-copying
d. return a reference to *this and must do self-copying
e. None of the above are correct

A

a. return a reference to *this and must avoid self-copying

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

Consider the following code segment and select the best answer:
string s1 = “863”;
s1 += “79”;
int y = s1.at(4) - s1.at(2);
a. This code gives a compilation error on the second line.
b. The value of y after this code segment will be 6
c. The value of y after this code segment will be -6
d. The value of y after this code segment will be 1
e. None of the above

A

b. The value of y after this code segment will be 6

Note that .at follows index notation (meaning it starts at 0). Therefore at index 4 it is 9. At index 2, it will be 3.

By the nature of subtracting characters of numbers, it would the same as if we were subtracting integers since the ASCII value difference between the 2 would be the same.

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

Use the following information to answer the question.

char a[10] = “March”;
const char* s = a;
char *const p = a;
*(s-1) = ‘U’;
*p = ‘M’;
p = s;

Which one of the following statements is true for line 3:
a. This line gives compilation error.
b. This line gives runtime error.
c. None of the above is true.

A

c. None of the above is true.

The line declares a constant pointer p pointing to the array a. It’s not an attempt to reassign the pointer.

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

Use the following information to answer the question.

char a[10] = “March”;
const char* s = a;
char *const p = a;
*(s-1) = ‘U’;
*p = ‘M’;
p = s;

Which one of the following statements is true for line 4:
a. This line gives compilation error.
b. This line gives runtime error.
c. None of the above is true.

A

a. This line gives compilation error.

This line attempts to modify the character before the start of the string (*(s - 1)), resulting in undefined behavior.

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

Use the following information to answer the question.

char a[10] = “March”;
const char* s = a;
char *const p = a;
*(s-1) = ‘U’;
*p = ‘M’;
p = s;

Which one of the following statements is true for line 5:
a. This line gives compilation error.
b. This line gives runtime error.
c. None of the above is true.

A

c. None of the above is true.

This line successfully assigns the value ‘M’ to the first character of the array a (changing “March” to “March”).

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

Use the following information to answer the question.

char a[10] = “March”;
const char* s = a;
char *const p = a;
*(s-1) = ‘U’;
*p = ‘M’;
p = s;

Which one of the following statements is true for line 6:
a. This line gives compilation error.
b. This line gives runtime error.
c. None of the above is true.

A

a. This line gives compilation error.

This line attempts to reassign the constant pointer p, which is not allowed, and it will result in a compilation error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
Consider the following statement: 

const char *s = "ON TIME";
const char** m = &s;

Which one of the following C++ code, output character ‘N’ on the screen in a cout statement:
a. (*(*m+1)+1)
b. *(*m+1)
c. **m + 1
d. m[0][1]
e. b and d
f. c and d
g. None of the above
A

The correct answer is b and d. Both formats are fine. (Note that in solution only lists m[0][1] as a solution, but b also works.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
What will be the output of this program?

void fun(int n) {
if (n <= 0) return;
cout << "1";
fun (n-2);
cout << "2";
}
int main()
{
fun (3);
return 0;
}
A

1122

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
Consider the following code and select the best answer:
void main(void){
    double a = 11, b = 22, c = 33, d =44;
    double *x [4] = {&a, &b, &c, &d};
    double** y = x +1;
    cout << y[1][0] << endl;
    cout << **(y+2) << endl;
}

Which one of the following statements is correct?
a. output in line 5 is: 22
b. Program output in line 5 is: 33
c. Program output in line 5 is: 11
d. None of the above statements is correct.
A

b. Program output in line 5 is: 33

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
Consider the following code and select the best answer:
void main(void){
    double a = 11, b = 22, c = 33, d =44;
    double *x [4] = {&a, &b, &c, &d};
    double** y = x +1;
    cout << y[1][0] << endl;
    cout << **(y+2) << endl;
}

Which one of the following statements is correct?
a. Program output at line 6 is: 44
b. Program output at line 6 is: garbage
c. Program output at line 6 is: 11
d. None of the above statements is correct
A

a. Program output at line 6 is: 44

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
class Point {
public:
    Point(double x = -99, double y = -99) { this -> xM = x; this -> yM = y;}
    double getx() {return xM;}
    double gety() {return yM;}
    void setx(double x) {this -> xM = x;}
    void sety(double y) {this -> yM = y;}
private:
    double xM, yM;
};

What is the output of the following?
Point p1(100);
cout << p1.getx() << “ “ << p1.gety();

a. The output of the program is: -99 -99
b. The output of the program is: 100 100
c. The output of the program is: -99 100
d. The output of the program is: 100 -99
e. None of the above. It doesn’t compile because constructor of Point needs two arguments.
A

d. The output of the program is: 100 -99

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
class Point {
public:
    Point(double x = -99, double y = -99) { this -> xM = x; this -> yM = y;}
    double getx() {return xM;}
    double gety() {return yM;}
    void setx(double x) {this -> xM = x;}
    void sety(double y) {this -> yM = y;}
private:
    double xM, yM;
};

How many times does the constructor of class Point get called in the following code snippet:
Point a(100, 200);
Point b[6];
Point *c = new Point;
Point *d = new Point(300, 400);
A

Nine times

Point a(100, 200);: This line creates an object a of class Point using the constructor with arguments. So, the constructor is called once.

Point b[6];: This line creates an array b of 6 objects of class Point using the default constructor (no arguments). So, the constructor is called 6 times.

Point *c = new Point;: This line dynamically allocates memory for a single object of class Point using the default constructor. So, the constructor is called once.

Point *d = new Point(300, 400);: This line dynamically allocates memory for a single object of class Point using the constructor with arguments. So, the constructor is called once.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
What is the output of the following code segment?

typedef vector<int> ROW;
vector<ROW> x(3, ROW(5)); // creating a vector of 3 ROW with 5 columns
// populating the matrix with integer numbers
for(int i = 1; i <= 3; i++)
    for (int j = 1; j <= 5; j++)
        x.at(i-1).at(j-1) = i+j;
        
for(int i = 0; i < 3; i++)
    for (int j = 0; j < 5; j++)
        if(i == j)
        cout << x[i].at(j);
A

246
The full matrix looks like this.
| 2 3 4 5 6 |
| 3 4 5 6 7 |
| 4 5 6 7 8 |