Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

EECS 280 Midterm Exam

Fall 2019

Multiple Choice and Reference Packet

This is a closed-book exam. You may use one note sheet, 8.5"x11", double--sided, with your name on it. This booklet contains multiple-choice questions, reference code for the written    portion, and space for scratch work.

Read the entire exam through before you begin working. Work on those problems you find        easiest first. Read each question carefully, and note all that is required of you. Assume all code is in standard C++11, and use only standard C++11 in your solutions.

Instructions:

Record your choices for all multiple-choice questions in the written-portion booklet. Fill in the circle completely. Erase completely if you change your answer

●   Throughout the exam, assume all necessary #include headers and the using namespace std; directive are present unless otherwise directed.

●   You do not need to verify REQUIRES clauses with assert unless instructed to do so.

●   The multiple choice booklet WILL be turned in. However, NOTHING written inside of this booklet will be graded. You must record your answers in the written-portion booklet.

●   Turn in both this booklet and the written-portion exam book. One will not be accepted without the other. Keep your note sheet.

Problem 0: Short Answers (30 Points)

Record your answers in Question 0 of the written booklet

0a) True/False (10 points)

1.   Public class members marked as static can be accessed without referencing any particular instance of the class.

2.   Consider the following two lines of code: int a = 2;

____ x = * (&a);

In order to compile, the type of x should be (int *).

3.   Let ptr_y be a pointer to a double. Then ptr_y += 1; will increment the value of ptr_y by the size of a double instead of 1.

4. Consider a program with two variables: double b = 3.14;

double &c = b;

These variables correspond to two objects in memory with distinct addresses: one whose value is 3.14, and the other whose value is the address of b.

5. A derived class inherits all public member functions of its base class, including the base class constructor.

6. A derived class has direct access to private member variables of its base class.

7. Although not all abstract data types (ADTs) are implemented as structs, all structs are examples of ADTs.

8.   If p is a pointer type, the statement cout << p; is guaranteed to print an address.

9. Procedural abstraction involves logically separating what a procedure (or function) does from how it is implemented.

10. The following two declarations:

*int const ptr;

int *const ptr;

are functionally identical.

0b) Multiple Choice (20 points)

For questions 1-4, consider the following program which has been compiled into an executable named myprog.exe:

int main(int argc, char **argv){

cout << *(argv + 1) << endl; //first cout

cout << argv[2][4] << endl; //second cout

}

Assume the program is then run with the following command:

./myprog.exe file1.txt file2.txt 4 1000

1.  What is the output of the first cout statement?

a.   ./myprog.exe file1.txt file2.txt 4 1000

b.  file1.txt

c.   myprog.exe

d.  file1.txt file2.txt 4 1000

e.   /

2.  What is the output of the second cout statement?

a.   0

b.   1

c.   2

d.   2.txt

e.   00

3.  What is the value of argc?

a.   1

b.   2

c.   4

d.   5

e.   1000

4.  Which of the following is a valid alternate declaration of argv?

a.   char *argv[]

b.   *char []argv

c.   char *argv

d.   string *argv

e.   argv definition is implicit if omitted

For questions 5 to 10, consider the following program:

void world(char x[], char y[], std::string z){

if(x == y)

std::cout << z;

}

int main(){

char x[] = "hello";

char y[] = {'h','e','l','l','o'};

std::string z = "hello";

// determine behavior at this point

}

The program compiles successfully. Determine what would be printed by each of the following statements, assuming they were placed at the end of main. If the statement would cause undefined behavior, select [Undefined]. If nothing is printed, select        [Nothing]

5.  world(x,y,z);

a.   z

b.   h

c.   hello

d.   [Nothing]

e.   [Undefined]

6.  world(y,y,z);

a.   z

b.   h

c.   hello

d.   [Nothing]

e.   [Undefined]

7.  world(nullptr, x, z);

a.   z

b.   h

c.   hello

d.   [Nothing]

e.   [Undefined]

8.   cout << y;

a.   h

b.   hello

c.   hellohello

d.   [Nothing]

e.   [Undefined]

9.   cout << *y;

a. h

b. hello

c. hellohello

d. [Nothing]

e. [Undefined]

10. for(int i = 0; i < strlen(x); i++) cout << x[i];

a. h

b. hello

c. hello\0

d. [Nothing]

e. [Undefined]

Problem 1: Strings and I/O (17 Points)

1a) (9 points)

Implement the split_string() function according to the RME, along with any other helper functions used

● Your implementation MUST use traversal by pointer and NOT traversal by index. ● You MAY NOT use any classes or functions from the standard library (e.g.

strlen()), but you may implement and call helper functions (do not worry about the relative order that you write the functions, i.e. you may assume all functions are declared before their use)

// REQUIRES: src points to a valid C-style string

//

dst points to allocated memory at least as large

//

as allocated for src

//

pos is the position at which to split src and

//

must be less than the length of str

// MODIFIES:

*dst

// EFFECTS:

Writes a C-style string (to address specified by

//

dst) which is the concatenation of the substring

//

following and including src[pos], followed by the

//

substring preceding src[pos]

// EXAMPLES:

//

char dst[10];

//

cout << split_string(dst, hello”, 2);

// dst llohe

//

cout << split_string(dst, racecar”, 3);

// dst ecarrac

//

cout << split_string(dst, penny”, 0);

// dst penny

//

cout << split_string(dst, goodbye”, 6);

// dst egoodby

void split_string (char *dst, const char *src, int pos);

WRITE YOUR IMPLEMENTATION IN QUESTION 1A OF THE ANSWER BOOKLET

1b) (8 points) For this problem, you are to implement a program which is called from the          command line with an integer N larger than 0, followed by an optional string specifying an input file. If the input file is not specified, input should be read in from cin. Starting from the beginning of the file, the program should read in and print every Nth word (e.g. if N is 3, the 1st word is      printed, then the 4th , 7th , etc), to standard output (cout).  Each printed word should be followed  by a comma. The remaining words are ignored and are not printed.

Example: If the program is compiled to main.exe and run as:

$./main.exe 2 input.txt

and input.txt” consists of the following:

She went to the store

the following should be printed to standard output:

She,to,store,