Title: OOP Class Lawrence D
1OOP ClassLawrence DAntonio
2Quiz 1
- (1) What Danish computer scientist invented C?
- Bjarne Stroustrup
- (2) What was the first object-oriented
programming language? - Simula
3- (3) What does STL stand for and who invented it?
- Standard Template Library, Alex Stepanov
- (4) (a) Which language is older C or Java?
- C (sort of)
- (b) Which language is older C or Smalltalk?
- Smalltalk
4- (5) True or false C was designed to be
backwards compatible with C. - True (a least in theory, in practice is another
story) - (6) What is the difference between overloading
and polymorphism? - Overloading creates many functions of the same
name that work on different types. Polymorphism
creates one function that work on different
types.
5- (7) True or false Only object-oriented
programming languages can exhibit polymorphism. - False, many languages exhibit polymorphism. In
Lisp, car and cons are examples of polymorphic
functions.
6- (8) What is the difference between function
overloading and function overriding? - Overloading involves functions of the same name
having different signatures. Overriding occurs in
when a function in a derived class replaces a
function of the same name in the base class.
7- (9) What is a function object?
- A function object is an object that may be called
like a function. In C this is achieved by
overloading the () operator. - (10) How does a function object differ from a
function? - A function object has state. It can be
initialized through a constructor and retains its
state between invocations.
8- (11) Which of the following C mechanisms is
used to create polymorphism? - Virtual functions X Templates X
- Virtual functions exemplify inclusion
polymorphism. A reference or pointer to a parent
object may be converted to an object of any
inherited type. This means that determining which
method is being called is a run-time decision. - Templates implement the concept of parametric
polymorphism. Code works correctly for a variety
of datatypes. Functional languages such as ML,
Haskell, Lisp typically use parametric
polymorphism.
9- (12) What is a predicate in C?
- A function or function object that returns a
boolean. - (13) What is the primary difference between a
struct and a class in C? - In a struct, access is public by default whereas
in a class it is private by default.
10- (14) What is the meaning of the key word mutable?
- A data member of a class is declared to be
mutable then it may be modified inside a const
member function.
11Mutable example
Ex class Image private mutable bool
is_loaded public void Redraw() const if
(is_loaded false) //load image from
disk is_loaded true //..paint image on
screen
12- (15) What are the member functions that the
compiler will write for you? - Default constructor, copy constructor, assignment
operator, destructor - (16) True or false a default constructor cannot
take any arguments - False. A default constructor must either have no
arguments or all arguments must have default
values
13- (17) What are the four cast operators in C?
- static_cast, dynamic_cast, reinterpret_cast,
const_cast
14- (18) Which of the following conversions is
generally not legal? - (a) Converting a base object to a derived object?
- Not legal (the base object is not a derived
object). - (b) Converting a derived object to a base object?
- Legal (the derived object is a base object).
15- (19) What is an exception specification?
- A specification of what types of exceptions a
function may throw (or none at all).
void f() throw(int) //throws only an int void
g() throw() //throws no exceptions
16- (20) What is a nothrow function and how do you
declare it? - A nothrow function guarantees that it wont
throw an exception. The only example currently
allowed is the nothrow new operator.
17nothrow code
Class X // X p new (nothrow) X Note
even though this code wont throw an exception,
one can still check if the allocation was
successful by using the code if (!p) //
18- (21) True or false it is illegal for a
destructor to throw an exception. - False, but it is generally a bad idea.
19- (22) 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)?
int A A new intn for(int i 0 i lt n
i) Ai new intm
20- (23) What is the most significant difference
between malloc() (the C dynamic memory allocation
function) and new() (the C dynamic memory
allocation operator)? - malloc() doesnt call a constructor for allocated
objects, while new() does.
21- (24) (a) Is the following code legal?
class X class Y public X main() X p
new Y Legal, since a Y is an X.
22- (b) Is the following code legal?
class X class Y private X main() X
p new Y Illegal, since private inheritance
is used, Y is not an X.
23- (25) What is a smart pointer?
- A smart pointer is an object that acts like a
pointer. A smart pointer can have state, so it
can be responsible for owning what it points to.
auto_ptr is an example of a smart pointer in the
standard library.
24- (26) What is wrong with this code?
int p new int10 //do stuff with p delete
p In order to delete an array, one must use the
syntax delete p
25- (27) What is wrong with this code?
class X private int a public X(int n) a(n)
main() X p new X new X calls the
default constructor for X, but the class does not
have any default constructor defined.
26- (28) (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_xx p 8 return 0 p is an int
pointer, but Xx is an int inside class X. One
needs to declare and use p like this int Xp
Xx my_x.p 8
27- (30) What is wrong with the following function?
int foo(const vectorltintgt v) n
v.size() if (n 0) //Check if vector is
empty cout ltlt Error Empty vector\n return
-1 return v0 n 0 should be n 0
28- (31) True or false the following code is legal.
include ltiostream.hgt include ltiostreamgt main()
cout ltlt Hello World return 0 True, it
is legal. The cout called is the one in iostream.h
29- (32) What is wrong with the following code?
string operator.(const string s, const string
t) string temp(s) temp.append(t) return
t It is wrong because a reference to a local
variable is returned. But that local object
passes out of scope when the function is
completed.
30- (33) For each of the following state whether the
code is legal or illegal and say why.
- int f1(int x)
- main()
-
- f1(5) 6
-
- Illegal, since f1 returns a temp value (hence no
lvalue).
31(b) int f2(int x) main() f2(5) 6
Legal, since f2 returns an lvalue.
(c) const_int f3(int x) main() f3(5)
6 Illegal, since f3 returns a const (no
lvalue).
32- (34) Whats wrong with the following code?
template ltclass Containergt void fun(const
Container x) Containeriterator i for(i
x.begin() i ! x.end() i) cout ltlt
i The compiler wont know what kind of
object Containeriterator is. Instead, one must
use typename Containeriterator i
33- (35) You have a string s. Write one line of C
code that prints (to standard output) the string
in reverse order.
reverse_copy(s.begin(), s.end(),
ostream_iteratorltchargt(cout))