DUE Thursday, February 9

plumponionchipsSoftware and s/w Development

Nov 18, 2013 (3 years and 8 months ago)

75 views


OOP Spring 2012

HW

#1


DUE
Thursday, February 9


(1) What Danish computer scientist invented C++?


(2) What’s wrong with the following code?


template <class Container>

void fun(const Container& x)

{


Container::iterator i;



for(i = x.begin(); i != x.en
d(); i++)



cout << *i;

}


(3) W
hich of the following C++ mechanisms

is used to create polymorphism?


Virtual functions ______________ Templates ________________


(4)
Which of the following conversions is generally not legal?

(a) Converting a ba
se object to a derived object?

(b) Converting a derived object to a base object?


(5) What are the four cast operators in C++?


(6) (a) What is a nothrow function and how do you declare it?

(b) Why would you want such a function?


(7) True or false: it is
illegal for a destructor to throw an exception.


(8) True or false: C++ was designed to be backwards compatible with C.


(9) True or false: Only object
-
oriented programming languages can exhibit
polymorphism.


(10) What was the first object
-
oriented progra
mming language?


(11) How do you declare and allocate memory for a two dimensional array A with
n

rows
and
m

columns (where
n
,
m

are integer variables determined at run
-
time)?


(12) For each of the following state whether the code is legal or illegal and sa
y why.

(a) int f1(int x);


main()

{


cout << f1(5)++;

}

(b) int &f2(int x);


main()

{


cout << f2(5)++;

}

(c) const_int &f3(int x);


main()

{


cout << f3(5)++;

}


(13) What does STL stand for and who invented it?


(14) You have a string s. Write one line o
f C++ code that prints (to standard output) the
string in reverse order.


(15) What is the difference between function overloading and function overriding?


(16) Why is the following code illegal?


s
tring operator.(const string &s, const string &t)

{


stri
ng temp(s);



temp.append(t);

return t
emp
;

}


(17) True or false: the following code is legal.


#include <iostream.h>

#include <iostream>


main()

{


cout << “Hello World”;



return 0;

}


(18) What is wrong with the following code?


vector<vector<int>>


(19
) What is a predicate in C++?


(20) What is the primary difference between a struct and a class in C++?


(21) What is the most significant difference between malloc() (the C dynamic memory
allocation function) and new() (the C++ dynamic memory allocation o
perator)?


(22) What is a function object?


(23) How does a function object differ from a function?


(24)

(a)

What is wrong with the following declaration
?


class X {

public:


int x;


X(int a) x(a) {}


void f(int a) const;

};


main()

{


X my_x(5);


int *p
= &my_x::x;


*p = 8;



return 0;

}


(b) Define a pointer p to the function f in class X.


(25) What is the meaning of the key word
mutable
?


(26) What are the member functions that the compiler will write for you?


(27) (a) Is the following code legal?

cla
ss X {};


class Y: public X{};


main()

{


X *p = new Y;

}

(b) Is the following code legal?

class X {};


class Y: private X{};


main()

{


X *p = new Y;

}


(28) In the following code: inside function where(), which variable
who

is
used
? Is there
a way to gua
rantee that the global
who

will be used?


int who;


class what {


int who;

public:


int where(int when) { return when*who; }

};


(29) What is a smart pointer?


(30) What is wr
ong with the following function?


int foo(const vector<int> &v)

{


n = v.size();


if (n = 0) { //Check if vector is empty



cout << “Error: Empty vector
\
n”;



return
-
1;


}


return v[0];

}


(31) Explain the difference between overloading and polymorphism.


(32) Why are namespaces used in C++?



(33) Is the following code legal or illeg
al? Explain.


namespace {


int BOB;

}


main() {


BOB = 4;

}