C++代写:CPSC221 C++ Problems

代写C++基础作业,回答基础问题。

Requirement

Each individual or pair will be submit one completed copy of this homework onto gradescope. You may either print it and write your solutions in the space provided, or complete the assignment in latex. Show your work where necessary for full credit. If you require additional space, please indicate in the question space that you are writing on the last blank page, and also indicate on the blank page which question the work solves.

You must upload the completed document, including this page and the last page, to GradeScope, using your 4- or 5-character CS ID.

Problem 1

In each of the multiple choice problems below, bubble the best answer. In a few cases, more than one response is correct. Mark them all! You are welcome to explore some of the coding problems using a compiler, but be careful-sometimes the system gives you an unreasonable, reasonable answer. A good way to solve these problems is to speculate on a response without using the compiler, and then check to see if the code behaves as you predicted.

1
2
3
4
5
6
7
8
int main(void) {
int *array = new int[4];
for (int i = 0; i < 4; i++) {
array[i] = rand() % 10;
}
delete array;
return 0;
}

What is the problem with this code?

  • array should be declared as int[].
  • We should declare array outside of main.
  • array is missing a constructor and a destructor.
  • A line of the code is leading to undefined behavior.
  • This code has a compiler error on line 6.
  • This code will seg-fault on line 4.
  • None of the other answers is true.

Problem 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Coffee{
public:
bool awesome;
void setName(int);
char *getName() { return name; }
Coffee() : awesome(true), name(new char[100]) {}
~Coffee() { delete[] name; }
private:
char *name;
};

void Coffee::setName(int type) {
// given type, set name to something meaningful
}

int main(void) {
Coffee latte;
latte.setName(1);
if (latte.awesome) {
Coffee caffelatte(latte);
cout << caffelatte.getName() << endl;
caffelatte.setName(2);
}
cout << latte.getName() << endl;
return 0;
}

Identify all the problems in this code.

  • When setName is declared the parameter name is omitted.
  • The name member in caffelatte is incorrectly initialized.
  • The getName function returns a dangling pointer.
  • The instance latte is not initialized.
  • None of the above are correct.

Problem 3

What error(s) might be caused by the code above?

  • Double free error.
  • Memory leak.
  • C++ compilation errors.
  • Segmentation fault.
  • None of the other answers is true.

Problem 4

How would you fix the error(s)?

  • Explicitly implement a copy constructor.
  • Declare caffelatte outside the if statement.
  • Declare name as a public member in Coffee.
  • Define setName inside Coffee’s class member list (with the parameter name!) None of the other answers is true.

Problem 5

1
2
3
4
5
6
7
8
9
10
11
12
class LegoMov {
public:
bool *evIsAwes;
void setEvIsAwes(bool b) { *evIsAwes = b;}
LegoMov() { evIsAwes = new bool(false); }
};

int main(void) {
LegoMov movie;
movie.setEvIsAwes(true);
return 0;
}

Are there any errors in the code?

  • Yes, movie is not initialized so we cannot call setEvIsAwes.
  • Yes, the value that evIsAwes points to is not modified after we call setEvIsAwes.
  • Yes, the code won’t compile because of other unstated reasons.
  • Yes, there might be a memory leak.
  • No, it looks good to me.

Problem 6

Consider this simple example

1
2
3
4
5
6
7
8
9
10
11
12
13
class Bear {
public:
Bear() { cout << "Growl "; }
void roar() { cout << "Roar "; }
~Bear() { cout << "Stomp stomp stomp "; }
};

int main(void) {
Bear beary;
beary.roar();
cout << "Run! ";
return 0;
}

What is the result of compiling and executing this code?

  • Roar Run!
  • Run!
  • Growl Roar Run! Stomp stomp stomp
  • Growl Roar Run!
  • This code does not compile.

Problem 7

1
2
3
4
5
6
7
8
9
10
11
12
13
class MessageBlock {
public:
MessageBlock(const string & str) : msg(str) {}
/* PART A */ (int position)
{ return msg[/* PART B */]; }
private:
string msg;
};

MessageBlock privet("3apabctbynte");
for(int i=0;i < 12; i+=1)
cout << privet(i);
cout << endl;

Which of the following statements complete the code above so that the output is etnybtcbapa3?

  • char & operator() and msg.length()-position
  • char & operator[] and position
  • char & operator() and position
  • char & operator[] and msg.length()-position-1
  • char & operator() and msg.length()-position-1

Problem 8

Consider this simple code that protects your backyard. How many times is plant’s copy constructor called? Note that similar to the case of the default constructor, compilers may explicitly generate plant’s copy constructor and assignment operator.

1
2
3
4
5
6
7
8
9
10
11
plant * imitater(plant & orig) {
plant * tater = new plant(orig);
return tater;
}

int main(void) {
plant peashooter;
plant *repeater;
repeater = imitater(peashooter);
return 0;
}

  • Never, but the code executes with no errors.
  • Never, because this code has a compiler error.
  • One time.
  • Twice.
  • Three times.

Problem 9

How many times are all the plant constructors called in the example above?

  • Never, but the code executes with no errors.
  • Never, because this code has a compiler error.
  • One time.
  • Twice.
  • Three times.

Problem 10

The code in MISC8 gives us heebie jeebies because it.

Problem 11

1
2
3
void foo(int &bar, int *baz) {
// FILL IN THIS LINE
}

What is the statement for assigning the sum of the value referred by bar and the value pointed to by baz to bar?

  • bar = bar + baz;
  • bar += *baz;
  • &bar = &bar + *baz;
  • *bar += baz;
  • *bar = bar + *baz;
  • None of the other options.

Problem 12

What about assigning the sum to baz?

  • *baz += &bar;
  • *baz = bar + *baz;
  • *baz = &bar + *baz;
  • &baz += *bar;
  • baz = *bar + &baz;
  • None of the other options.

Problem 13

1
2
3
4
5
int shrub = 10;
int *bush = &shrub;
cout << *bush << " ";
foo(shrub, bush);
cout << shrub << " " << *bush << endl;

Following MISC12, if we assign the sum to baz after calling foo(int &, int *), which of the following options below would be printed out by the program?

  • 10 20 10
  • 10 20 20
  • 10 10 10
  • 10 10 20
  • I can’t find the answer because my code won’t compile.

Problem 14

Which of the following vector ADT implementations gives us an O(n) time for push back, i.e inserting an element at the end of the list, assuming we do not copy any data?

  • A singly circular-linked list with only a head pointer.
  • A doubly circular-linked list with only a head pointer.
  • A doubly-linked list with only a head pointer.
  • A doubly-linked list with head and tail pointers.
  • None of the other options is correct.

Problem 15

In a sorted singly linked list, what will be the time required to insert at the middle position of the list?

  • O(1)
  • O(log log n)
  • O(log n)
  • O(n)
  • O(n log n)

Problem 16

Suppose you are given a pointer to some node in a singly linked list, but NOT the head pointer. Is it possible to delete the node from the list?

  • Yes, we can find a way to delete any of the nodes.
  • Yes, but we can only delete a node if it’s not the last node.
  • No, because we need to change the next field in the previous node.
  • No, because we cannot delete the pointer to the node.
  • None of the above is correct.

Problem 17

Suppose we have implemented the Queue ADT as a singly linked list with head pointer and no sentinels, nor tail pointer. Which of the following best describe the tightest running times for the functions enqueue and dequeue, assuming there are O(n) items in the list, and that the front of the queue is at the head of the list?

  • O(1) for both.
  • O(n) for both.
  • O(1) for enqueue and O(n) for dequeue.
  • O(n) for enqueue and O(1) for dequeue.
  • None of the options is correct