CSCI 211 Midterm Exam 2 Study Guide

0.0(0)
studied byStudied by 3 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/21

flashcard set

Earn XP

Description and Tags

Tyson Henry, Chico State

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

22 Terms

1
New cards

(6 points) What two major tasks does the C++ delete operator perform? Include the order that these two tasks are performed.

If the object being deleted has a destructor, the delete operator first calls the destructor for the object.

Then it deallocates the memory used by the object (it marks the memory as unused so it can be allocated during a future call to the new operator).

2
New cards
<p>Rewrite both bold/underlined code segments in the following function with code that does the exact same task but does not use [ and ]. Briefly explain your answer.</p>

Rewrite both bold/underlined code segments in the following function with code that does the exact same task but does not use [ and ]. Briefly explain your answer.

int *array
total +- *(array+i);

An array is simply a pointer to the first element. int array[] is the same as int *array.

The [] operator takes its second argument (the number inside the []) and adds it to its first argument (the pointer to the array) and then dereferences the resulting pointer. The above code does the same thing. The () are necessary because * has a higher precedence than + (it happens first).

3
New cards

(6 points) What is a memory leak? Keep your answer short.

A memory leak is when dynamic memory (memory allocated using new) is no longer needed but is not deleted.

4
New cards
<p>(6 points) Write a main function that accepts an integer as the command line argument and prints out twice the value of the given integer. You can assume there is only one argument and that the argument is an integer (you don't have to do any error checking). Don't write the #include files, just the main(). The program should work like this:</p>

(6 points) Write a main function that accepts an integer as the command line argument and prints out twice the value of the given integer. You can assume there is only one argument and that the argument is an integer (you don't have to do any error checking). Don't write the #include files, just the main(). The program should work like this:

<p></p>
5
New cards
<p>(10 points) What does the following program print. You will not get any points if you don't show the steps of the calculation. Be very methodical, it is easy to make a mistake.</p>

(10 points) What does the following program print. You will not get any points if you don't show the steps of the calculation. Be very methodical, it is easy to make a mistake.

The program prints “no”

<p>The program prints&nbsp;“no”</p>
6
New cards

(6 points) What two major tasks does the C++ new operator perform? Specify the order of these two tasks.

First it allocates (reserves) some memory for the object being created. Then it calls the constructor for the object. The newly allocated memory becomes the memory for the new object (it becomes the this pointer for the object).

7
New cards
<p>(6 points) What does the following function print? Explain why it prints this value or you won't get any points. Rewrite the bold code (and only the bold code) so this function print 0.5</p>

(6 points) What does the following function print? Explain why it prints this value or you won't get any points. Rewrite the bold code (and only the bold code) so this function print 0.5

It prints “0”

Since a and b are both integers, a/b is evaluated as integer division. With integer division, the fractional component of the result is ignored.

<p>It prints “0”<br><br>Since a and b are both integers, a/b is evaluated as integer division. With integer division, the fractional component of the result is ignored.</p>
8
New cards
<p>(10 points) What does the following program print. You will not get any points if you don't show the steps of the calculation. You do not have to explain what the function does. Be very methodical, it is easy to make a mistake.</p>

(10 points) What does the following program print. You will not get any points if you don't show the steps of the calculation. You do not have to explain what the function does. Be very methodical, it is easy to make a mistake.

It prints 62

<p>It prints 62</p>
9
New cards
<p>Why did my simulation program have a segmentation fault? Look closely at #0 in the gdb output. Dereferenced a NULL pointer is NOT a complete enough answer. Explain the exact problem.</p>

Why did my simulation program have a segmentation fault? Look closely at #0 in the gdb output. Dereferenced a NULL pointer is NOT a complete enough answer. Explain the exact problem.

The this argument to Cust::set_done_checkout_time() is 0x0 (which is NULL). That means that Cust::set_done_checkout_time() was called on a NULL object. When that NULL object is dereferenced on line 29 of cust.cpp, there is a segmentation fault. Since Cust::set_done_checkout_time() is called on line 35 of sim.cpp current_customer must be NULL.

10
New cards
<p>(6 points) Why does the following program print 2.07508e­322 ?</p>

(6 points) Why does the following program print 2.07508e­322 ?

Because ptr points to an integer in memory and that integer is being interpreted as a double so the value printed is garbage.

11
New cards
<p>(10 points) What does the following program print. You will not get any points if you don't show the steps of the calculation. Be very methodical, it is easy to make a mistake (such as using v where you should use d).</p>

(10 points) What does the following program print. You will not get any points if you don't show the steps of the calculation. Be very methodical, it is easy to make a mistake (such as using v where you should use d).

It prints 3

<p>It prints 3</p>
12
New cards

(6 points) The following program compiles and runs without error (it prints 17). Circle the line of code that is a terrible idea. Why is this line a terrible idea?

int main() {
// some other code
int i;
int *p = &i;
p[42] = 17;

cout « p[42] « endl;
// some other code
return 0;
}

The compiler will allocate room for ONE integer &i is the address of that ONE integer. This line uses memory after memory allocated for i, a terrible idea because that memory could be used for another variable.

<p>The compiler will allocate room for ONE integer &amp;i is the address of that ONE integer. This line uses memory after memory allocated for i, a terrible idea because that memory could be used for another variable.</p>
13
New cards
<p>(6 points) The following program does not have any error checking but it compiles without error. If given good input, it will run without any problems. What does this program do? Assuming the executable is named a.out, give an example of how the program is run. Keep your answer short—don't explain all the details.</p>

(6 points) The following program does not have any error checking but it compiles without error. If given good input, it will run without any problems. What does this program do? Assuming the executable is named a.out, give an example of how the program is run. Keep your answer short—don't explain all the details.

$ a.out a b
copies file a to file b (same as $ cp a b)

14
New cards
<p>(6 points) If the first cout statement in the following code prints 0x194a010 , what does the second one print? Very briefly explain your answer or you won't get any points.</p>

(6 points) If the first cout statement in the following code prints 0x194a010 , what does the second one print? Very briefly explain your answer or you won't get any points.

0×194a014

An integer is 4 bytes long. The “+ 1” means to add “1 integer worth of bytes”

to ptr's value. Thus it adds 4 (an integer is 4 bytes long).

15
New cards
<p>(6 points) Replace the following bold pseudo­code with real C++ code. Assume the input contains only legal integers, legal strings, and whitespace. For example: 42 three 44 45 six. You may call the isdigit(char ch) and isspace(char ch) functions. The output from the above string should be:<br><br>42<br>three<br>44<br>45<br>six</p>

(6 points) Replace the following bold pseudo­code with real C++ code. Assume the input contains only legal integers, legal strings, and whitespace. For example: 42 three 44 45 six. You may call the isdigit(char ch) and isspace(char ch) functions. The output from the above string should be:

42
three
44
45
six

if (isdigit(cin.peek())
cin » 1;

else if (isspace(cin.peek())
cin.ignore();


cin » str;

16
New cards
<p>(10 points) What does the following program print. You will not get any points if you don't show the steps of the calculation. Be very methodical, it is easy to make a mistake.</p>

(10 points) What does the following program print. You will not get any points if you don't show the steps of the calculation. Be very methodical, it is easy to make a mistake.

The program prints “28”

<p>The program prints&nbsp;“28”</p>
17
New cards

(6 points) What is the difference between a member variable declared as private and one declared as public?

Private member variables can only be accessed from member functions. Public member variables can be accessed by member functions AND from outside the class. For example:

List list;
list.m_head = NULL; // if m_head is private it cannot be accessed here, if public it can be

18
New cards
<p>(6 points) Consider the following output from gdb. I've tested the Video::print() function and it usually works. Why did this program have a segmentation fault in the Video::print() function? You may either explain the situation or provide sample code. What did you see in the gdb output that led to this conclusion?</p>

(6 points) Consider the following output from gdb. I've tested the Video::print() function and it usually works. Why did this program have a segmentation fault in the Video::print() function? You may either explain the situation or provide sample code. What did you see in the gdb output that led to this conclusion?

Video::print() was called with a NULL pointer.

Video *video = NULL;
video→print();

The “this” pointer argument to Video::print() is shown as 0×0 (NULL)

19
New cards

(6 points) Define stack overflow. What can cause stack overflow?

Each time a function is called, a block of memory is allocated and pushed onto the runtime stack. If too many functions are called, the runtime stack uses up all the memory allocated for the stack. Stack overflow is when the runtime stack uses up all of its memory.

20
New cards
<p>(6 points) What does the following code segment print? Explain your answer or you won't get any credit.</p>

(6 points) What does the following code segment print? Explain your answer or you won't get any credit.

yes 2
12 11

Because the expression clause after the && in the first is not executed, so j is only incremented once while i is incremented twice.

21
New cards
<p>(6 points) The following code contains a mistake that is clear from the given code. It compiles but does not run. What is the problem?</p>

(6 points) The following code contains a mistake that is clear from the given code. It compiles but does not run. What is the problem?

The variable list is a local variable, it was NOT dynamically allocated (i.e. using new). Calling delete on a local variable usually causes problems. When it goes out of scope (when control reaches the end of function f()) it will be automatically deleted and if you already deleted it, the second delete causes problems.

22
New cards
<p>(10 points) What does the following program print. You will not get any points if you don't show the steps of the calculation. Be very methodical, it is easy to make a mistake (such as using p where you should use q).</p>

(10 points) What does the following program print. You will not get any points if you don't show the steps of the calculation. Be very methodical, it is easy to make a mistake (such as using p where you should use q).

It prints 8

<p>It prints 8</p>