Skip to content

@matelau /
Last active

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
Study Guide for cs3505 Midterm
/*CS3505 Practice Exam #1
* Asaeli Matelau
//Declare a function named "compute" that takes two integer parameters and returns a double value
double compute(int, int);
//Declare a function named "vote_check" that takes references to two string values as parameters and returns true/false value.
//Do not assume that using namespace std: has been done
#include <string>
bool vote_check(std::string, std::string);
//define a function named "mpg" that takes two double parameters (gallons and miles) and returns a
//double value computing miles per gallons
double mpg(double gallons, double miles)
return miles/gallons;
//write a fragment of code that will read in 10 integers from the user, then
//display the sum of those ten integers
void summation()
int sum=0;
for(int i=0; i < 10; i++)
std::cout << "Please enter the next integer" <<std::endl;
int current = 0;
cin >> current;
sum = sum + current;
std::cout << "The input was not valid" << std::endl;
std::cout << "The Summation is " << sum << std::endl;
//Define forward declaration, and describe why it is required in C++
forward declarations are function prototypes, which is a declaration of a function
that includes the paramters, the function name, and return type, but does not implement the function
//What is the purpose of a .h files? can .h files contain function declarations and definitions
.h files are header files
Forward declarations are required by the compiler to verify the correctness of your implementation,
you can include definitions within the header file (it will compile), but header files are typically used to separate
the implementation from the interface
//how does the compiler process #include statements?
copies and pastes text in exactly as is where the include statement is
//Show how you would use compiler directives in a .h file to prevent the .h file from being included
//twice. Assume the name of the file is file.h
#ifndef FILE_H
#define FILE_H
//What is the purpose of using the namespace statement?
//What does it allow you to avoid? what risks are associate with this statement?
using namespace helps in avoiding conflicts between member variable names and allows for the separation of classes,
objects and functions under a group identifier. The risks associated with namespace are if you state using namespace within a header file you could change
the meaning of the code held when some other file includes that header file and unintentionally also begins using the beforementioned namespace.
//what is the purpose of placing code within a namespace?
The namespace statement allows you to group class, functions and objects under a name. It allows you to avoid conflicts between variables and functions by
having an addtional identifier
//Consider the "compute" function question above. Show how you would declare the function within a namespace called cs3505
namespace cs3505{
double compute(int, int);
//assume that you are not within the cs3505 namespace. Write one line of code that would call the function from the previous function
cs3505::compute(int x, int y);
//Write two lines of code: the first line of code should create a double variable named 'd' and initialize it with 42.0.
//the second line of code should create a reference to an integer named 'pd' and initialize it so that it points to d
double d = 42.0;
double &pd = d;
//Write two lines of code: the first line of code should should create an integer variable named 'i' and initialize it with
//19. the second line of code should create a reference to an integer named 'ri' and initialize it so that it refers to i.
int i = 19;
int &ri = i;
//Write three small, separate code pieces (of one or more statements): create a new array of 100 characters on the heap,
//initialize the elements in the array (with a loop) to all contain 'X' and release the memory allocated for the array.
char *charArray = new char[100];
for (int i = 0; i < 100; i++)
charArray[i] = 'X';
delete [] charArray;
//Assume you have a variable n that is a pointer to a node in a linked list. Show three different ways of derefrencing n to get .next,
//and store it back into n. I have written the first one for you:
n = n[0].next;
//are these two correct?
n = n->next;
n = (*n).next;
//rewrite the follwing cast using the newer c++ cast syntax: (int) d
//Consider the following const declaration int *const x = &i; assuming variables i and j are
//integers, exacly one of these statements will fail:
x= &j; // this will fail but why?
//Identify which statement will fail to compile and correctly explain why it will fail
*x will dereference the pointer, and change i
x = &j; tries to change what x points to which causes an error because x is a *const
Note: & is the reference operator and can be read as "address of", * is the dereference operator and can be read as "value pointed by"
//Show the syntax of a makefile rule. How does the 'make' program determine if it should execute the command underneath a rule?
// Show an example sufficient to explain your answer
rule: rule_file1.h //rulename: followed by dependent files (things to make)
g++ -g -o example // g++ compilation command (how to make it)
make checks to see if files are out of date and if they are follows compilation commands to bring them up to date
//What is the purpose of placing an & after a shell command?
a & after a shell command allows it to operate in the background and leaves your terminal free to manipulate
//Give the general form and one example for the command-line command to compile a .cc file into an executable
g++ -o nameofExecutable.out
//what is .o file?
a .o file is an object file, the compiler creates a .o file for each source file
g++ -c //creates .o file for you to use later
//What is gdb? suppose a program causes a segmentation fault.
//how would you use gdb to find where the seg. fault occurs? (just give a brief overview, exact command syntax is not required)
GDB is a GNU debugger, to use GDB you have to enable it with the compiler by including the -g command ( g++ -g )
To run GDB you simply type GDB gdb in the current directory of your executable. To find where the seg. fault occurs, you need to run
gdb with the run command and after the seg fault occurs you use the backtrace command and read the frame state output. You can select
specific frame to get the precise location of where the seg fault occurs .ie frame x, where x is the frame of the inner most function call
/************************ --OO programming and C++-- *************************************************/
//Write a complete class declaration for a fraction class. Name the class 'fraction' and include only a constructor that takes a numerator and denominator (as integers), accessor
//functions, and overloaded operator for +,-,*, and /. Include fields for holding a denominator and numerator (in the appropriate places)
//Include a void private helper function with no parameters named 'reduce'.
class fraction
fraction(int , int ); //constructor
int getNumerator() const;
int getDenominator() const;
//overloaded operators
const fraction & operator+ (const fraction & rhs);
const fraction & operator- (const fraction & rhs);
const fraction & operator* (const fraction & rhs);
const fraction & operator/ (const fraction & rhs);
int num; // member variables
int denom;
void reduce();
//define the constructor for the fraction class
fraction(int numerator, int denominator)
this->num = numerator;
this->denom = denominator;
//Assume you have a class named 'measurement'. The measurement class does not inherit from the fraction class,
//but you would like to allow the measurement class access to the private section of the fraction class. How would you accomplish this? Write
// the line of code, and indicate where the line of code needs to be placed
/* you would need to include the friend statement within the declaration of the fraction class */
friend class measurement;
//Draw a UML class diagram that shows the following type relationship:
//A class named 'SkipList' inherits from a class named 'List'.
//We did not cover these in great detail, but I only expect rectangles with class names in them, and arrows (with hollow, open arrowheads) pointing from the edge of
//the subclass rectangle to the edge of the direct superclass rectangle
//Draw a UML class diagram that shows the following type relationship: A class named "Magazine" inherits
//from a class named "Publication". A class named "Website" inherits from the class named "Publication". A class named "Blog" inherits from "Magazine" and from "Website". Finally, the blog class inherits
// only once from "Publication" (the dreaded diamond)
//Write the first line of the class declaration for the "Skiplist" class and the "List"
//class from above. (Just write class headers that show the inheritance declarations - do not write a declaration body)
//Assume that the "SkipList" class would like to inherit the "List" class members without changing their visibility. (The default syntax won't work use the correct visbility modifier)
class Skiplist : public List{}; // at most the visibility will be public, List's visibility modifiers will be maintained
//Write the first lines of the class declarations for the "Publication", "Magazine", "Website", and "Blog" classes, as described above.
//(just write class headers that show the inheritance declarations - do not write a declaration body) Use visibility modifiers so that the Blog class cannot see any of the
//members inside of "Publication" class, even though it inherits them exactly once.
class Publication {};
class Magazine : private virtual Publication{};
class Website : private virtual Publication{};
class Blog : Magazine, Website{};
//Assume the "List" class has a function declared "int size()". Assume the "SkipList" class also has a function declared
//"int size()". If the following code runs, which size function will be executed?
SkipList *t = new Skiplist();
List *s = t;
I beleive List::size() will run. It will run as it is not declared virtual within list so it automatically calls list->size();
//The above problem shows that function overriding is not the default behavior when inheriting classes. How would you change the
//declaration of the size function to enable polymorphism (overriding)? ( since there are two declarations, one in each class, make sure to specify which declaration you are changing.)
Since SkipList inherits from List you would want to declare the functions virtual within List so that derived classes functions will override base class functions.
//Write the function declaration for a pure virtual function named "getStructure", assuming it will be in the List class
// The function takes no parameters and returns a string .
virtual std::string getStructure()= 0; //pure virtual must include =0
//Where else would you need to declare and define the "getStructure" function? explain
SkipList would need to declare and define the function as it inherits from List, so the function will be expected by the compiler.
//Show an example of return type covariance by showing two function declarations, one inside of "List", and the other inside of "SkipList". Use a function name of "sublist", and have one of the functions return a pointer to a List object.
Covariance: as the derived classes become more specific, so may the return types. Contravariance: as the derived classes become more specific, the parameters become more general.
Works on virtual functions
class List{
virtual List * sublist();
class SkipList{
SkipList * sublist();
//When are destructors invoked? explain
When the scope of the variable ends, You can also manually invoke destructors
//when do you need to delete an object? explain
Objects need to be deleted when they are alotted space on the heap. This happens by using the keyword new. Whenever we use the keyword new
we also need to delete the object from the heap.
//define "separation of concerns"
Separation of concerns is a design principle for separating a program into distinct sections, such that each section addresses a separate
concern. SoC is used to untangle solutions to a given task and encapsulating work done, so that it does not happen in multiple parts of a process.
.ie parse data -> output result 1 -> parse data again -> build a table -> show table results ask for input -> parse data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.