C++ Program - Quiz 2 | Data Structures | CS 225, Quizzes of Data Structures and Algorithms

Material Type: Quiz; Class: Data Structures; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Unknown 1989;

Typology: Quizzes

Pre 2010

Uploaded on 03/10/2009

koofers-user-i45-1
koofers-user-i45-1 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS225 Headbangers
, #2
NetId:
1. What is true about the following C++ program:
1: int main () {
2: int *x = 0;
3: int &a = *x;
4: return 0;
5: }
(a) This generates compile error in line 2.
(b) This generates compile error in line 3.
(c) This compiles fine, but will generate segmenation fault when executed (runtime error).
(d) This compiles and executes without any system error.
2. Consider the following C++ class definition:
#include <iostream>
using namespace std;
class Test {
public:
Test(int a) {
p = new int;
*p = a;
}
~Test() {
cout << *p << "\n";
delete p;
p = NULL;
}
private:
int *p;
};
What is the output of the following program:
int main() {
Test t(10);
t.~Test();
return 0;
}
(a) No output. Generates segmentation fault.
(b) Outputs 10 and then generates segmentation fault.
(c) Outputs 10 in the first line and 0 in the second line.
(d) Outputs 10 in the first line and 10 in the second line.
3. What is the output of the following program:
1
pf2

Partial preview of the text

Download C++ Program - Quiz 2 | Data Structures | CS 225 and more Quizzes Data Structures and Algorithms in PDF only on Docsity!

CS225 Headbangers ,

NetId:

  1. What is true about the following C++ program:

1: int main () { 2: int *x = 0; 3: int &a = *x; 4: return 0; 5: }

(a) This generates compile error in line 2. (b) This generates compile error in line 3. (c) This compiles fine, but will generate segmenation fault when executed (runtime error). (d) This compiles and executes without any system error.

  1. Consider the following C++ class definition:

#include using namespace std;

class Test { public: Test(int a) { p = new int; *p = a; }

~Test() { cout << *p << "\n"; delete p; p = NULL; }

private: int *p; };

What is the output of the following program:

int main() { Test t(10); t.~Test();

return 0; }

(a) No output. Generates segmentation fault. (b) Outputs 10 and then generates segmentation fault. (c) Outputs 10 in the first line and 0 in the second line. (d) Outputs 10 in the first line and 10 in the second line.

  1. What is the output of the following program:

int main() { Test t(10); t.Test(20);

return 0; }

(a) This will not be compiled. (b) No output and no memory leak. (c) No output and we have memory leak. (d) Outputs 10. (e) Outputs 20 and we have memory leak.

  1. What is the output of the following program:

int main() { Test *t, *tt; t = new Test(10); *tt = *t;

return 0; }

(a) This will not be compiled. (b) Generates segmentation fault with no memory leak. (c) Generates segmentation fault and memory leak. (d) Outputs 0.