UCT CSC3022 C++ Section correctly
answered to pass
What are the big 6? - correct answer ✔✔copy constructor, move constructor, copy
assignment, move assignment, destructor, constructor
how do you denote a generic pointer? - correct answer ✔✔void * ptr ; this has no
specific type so BEFORE it can be used it MUST be cast to the appropriate type
what is the relationship between arrays and pointers in C++? - correct answer
✔✔C++ considers the array variable name to be a POINTER to the first element of
the array
How do you avoid multiple file inclusions and what do they cause if not handled? -
correct answer ✔✔include gaurds prevent compiliation errors
use preprocessor directives as follows
#ifndef _SOMENAME
#define _SOMENAME
#include <filename>
void do_stuff();
#endif
given the macro definition
#define EXPR(y) y/3
,what will std::cout << EXPR(3); expand to? - correct answer ✔✔because macros
are simple in text substitutions the macro will expand simply to produce:
3/3
how does a copy constructor differ from a move constructor? - correct answer
✔✔- a copy constructor builds a new instance of the type by creating NEW copies
of ALL its data
- a move constructor creates an EMPTY object annd then transfers ALL the data
from the source object into the new object essentially deleting the previous
object.
given the operator overload function
Object Object::operator-(const Object & obj);
Can we return the obj by reference? - correct answer ✔✔this is a unary minus it
needs to modify the value passed in by the argument and pass back a modified
version. However the object passed in is const and so it cannot simply just modify
obj and pass that back. We must create a copy internally and pass back the copy
by VALUE.
what is the difference between pass by value and pass by reference in C++? -
correct answer ✔✔given: int x = 10; int y = 2;
pass by value
someFunction(x,y);
pass by reference
someOtherFunction(int *ptx,
,explain the variable actions in each of the following statements: - correct answer
✔✔int a = 5; // declare an integer simply
int * ptr = nullptr; // declare and int ptr and init to nullptr
ptr = &a; //set ptr to point to a by using the address operator to get the address of
a
int b = *ptr; // use the dereference operator * to set b to a
*ptr = 6; // use the dereference operator to set the variable ptr points to (a) to
have a value of 6
when do you not need any code in the destructor for a class? - correct answer
✔✔if the only variables are automatic variables (ints, strings) then you do not
need code in the destructors because when these variables go out of scope their
destructors will be called automatically
what is the difference between the following include statements?
#include <filename>
#include "filename" - correct answer ✔✔the carrots search default dirs (used for
built in files). the quotes searches explicit include dirs (used for header files
written by the user)
are lambdas and functors equivalent in application? if not, describe where and
how the different in use. if so, describe the most common use for them and what
makes them interchangeable. - correct answer ✔✔functors and lambdas are
equivalent in application. lambdas are a lightweight "syntactic sugar" alternative
to functors. they have the advantage over functors that one does not need to
define and instantiate a class. But they are INTERCHANGABLE as they are both
locally scoped "ad-hoc" (as needed/situation specific)
, what is the syntax for a tenary operator? - correct answer ✔✔condition ?
expression1 : expression 2
examples:
marks = 10
string result = (marks > 20) ? "passed" : "failed";
number = -1
bool eval = (number > 0) ? true: false;
what is the value of c++ templates? - correct answer ✔✔
what does the #include directive accomplish in a c++ source file? - correct answer
✔✔the #include directive in a c++ source file inserts the header file TEXT into the
current source file
Define a pre-processor macro, PRN(x) that writes the value x to std::cout? -
correct answer ✔✔#define PRN(x) std::cout << (x) << std::endl;
note: HAVE TO include the parenthesis around the x because macros do text
substitution and if you don't put the parenthesis for this case than an input like
PRN(x+y) is gonna cause a compilation error
what is the difference between a pointer and a reference in c++? - correct answer
✔✔references refer to an existing variable with another name whereas pointers
store the ADDRESS of a variable
int x = 3;
int & xr = x; // this is a reference
answered to pass
What are the big 6? - correct answer ✔✔copy constructor, move constructor, copy
assignment, move assignment, destructor, constructor
how do you denote a generic pointer? - correct answer ✔✔void * ptr ; this has no
specific type so BEFORE it can be used it MUST be cast to the appropriate type
what is the relationship between arrays and pointers in C++? - correct answer
✔✔C++ considers the array variable name to be a POINTER to the first element of
the array
How do you avoid multiple file inclusions and what do they cause if not handled? -
correct answer ✔✔include gaurds prevent compiliation errors
use preprocessor directives as follows
#ifndef _SOMENAME
#define _SOMENAME
#include <filename>
void do_stuff();
#endif
given the macro definition
#define EXPR(y) y/3
,what will std::cout << EXPR(3); expand to? - correct answer ✔✔because macros
are simple in text substitutions the macro will expand simply to produce:
3/3
how does a copy constructor differ from a move constructor? - correct answer
✔✔- a copy constructor builds a new instance of the type by creating NEW copies
of ALL its data
- a move constructor creates an EMPTY object annd then transfers ALL the data
from the source object into the new object essentially deleting the previous
object.
given the operator overload function
Object Object::operator-(const Object & obj);
Can we return the obj by reference? - correct answer ✔✔this is a unary minus it
needs to modify the value passed in by the argument and pass back a modified
version. However the object passed in is const and so it cannot simply just modify
obj and pass that back. We must create a copy internally and pass back the copy
by VALUE.
what is the difference between pass by value and pass by reference in C++? -
correct answer ✔✔given: int x = 10; int y = 2;
pass by value
someFunction(x,y);
pass by reference
someOtherFunction(int *ptx,
,explain the variable actions in each of the following statements: - correct answer
✔✔int a = 5; // declare an integer simply
int * ptr = nullptr; // declare and int ptr and init to nullptr
ptr = &a; //set ptr to point to a by using the address operator to get the address of
a
int b = *ptr; // use the dereference operator * to set b to a
*ptr = 6; // use the dereference operator to set the variable ptr points to (a) to
have a value of 6
when do you not need any code in the destructor for a class? - correct answer
✔✔if the only variables are automatic variables (ints, strings) then you do not
need code in the destructors because when these variables go out of scope their
destructors will be called automatically
what is the difference between the following include statements?
#include <filename>
#include "filename" - correct answer ✔✔the carrots search default dirs (used for
built in files). the quotes searches explicit include dirs (used for header files
written by the user)
are lambdas and functors equivalent in application? if not, describe where and
how the different in use. if so, describe the most common use for them and what
makes them interchangeable. - correct answer ✔✔functors and lambdas are
equivalent in application. lambdas are a lightweight "syntactic sugar" alternative
to functors. they have the advantage over functors that one does not need to
define and instantiate a class. But they are INTERCHANGABLE as they are both
locally scoped "ad-hoc" (as needed/situation specific)
, what is the syntax for a tenary operator? - correct answer ✔✔condition ?
expression1 : expression 2
examples:
marks = 10
string result = (marks > 20) ? "passed" : "failed";
number = -1
bool eval = (number > 0) ? true: false;
what is the value of c++ templates? - correct answer ✔✔
what does the #include directive accomplish in a c++ source file? - correct answer
✔✔the #include directive in a c++ source file inserts the header file TEXT into the
current source file
Define a pre-processor macro, PRN(x) that writes the value x to std::cout? -
correct answer ✔✔#define PRN(x) std::cout << (x) << std::endl;
note: HAVE TO include the parenthesis around the x because macros do text
substitution and if you don't put the parenthesis for this case than an input like
PRN(x+y) is gonna cause a compilation error
what is the difference between a pointer and a reference in c++? - correct answer
✔✔references refer to an existing variable with another name whereas pointers
store the ADDRESS of a variable
int x = 3;
int & xr = x; // this is a reference