Quiz 1 review
Objective
Review the course topics and content that will be assessed on the first quiz.
Quiz Details
- Paper quiz
- 5 multi-part questions (10 points each), you will be asked to review code and answer questions about the code or draw something like a memory diagram
- Total of 50 points
- 30 minutes to complete the quiz
What to Study
Other resources:
Unix Tutorial
- Read Tutorial 1 - 5
- Change directories
- Print current directory to terminal
- List files
- Copy files, including with wildcards (e.g. *)
- Create directories
- Change access control permissions for files and directories
- Redirect output
Text book: Programming Principles and Practice Using C++, 2nd Edition by Bjarne Stroustrup
- Chapter 2 Hello, World!
- Read Section 2.3 Compilation
- Read Section 2.4 Linking
- Chapter 17 Vector and Free Store
- Read Section 17.3 Memory, addresses, and pointers
- Read Section 17.4 Free store (heap) and pointers
- Read Section 17.9.1 Pointer and reference parameters
- Read Section 17.9.2 Pointers, references, and inheritance
Lectures:
- Slides: C++ Basics
- Slides: Strings and Functions
- Functions
- Strings
- Command-Line Arguments
- Slides: Collections and File I/O
- Vectors
- File I/O
- Slides: Pointers and Memory
- Memory
- Pointers
- Garbage Values
- Memory Layout
- Slides: References and Heap
- References
- Heap (Free store)
- Allocation
- Deallocation
Practice Questions
Know your Linux commands
- How to change directory?
- To the parent directory
- To your home directory
-
How to print the current working directory?
- How to copy files?
- Copy files with wildcards
-
How to move files?
-
How to create a new directory?
- How to change the permissions on a file with the chmod command?
Pass by value, pass by pointer, pass by reference
Consider the following C++ code.
- What will be the output of this code when run?
passby.cpp
#include <iostream>
using namespace std;
void passByValue(float copyOfValue)
{
copyOfValue = 1234.567;
}
void passByPointer(float* addressOfValue)
{
*addressOfValue = 1234.567;
}
void passByReference(float& referenceToValue)
{
referenceToValue = 9876.543;
}
int main()
{
float value = 5551.212;
passByValue(value);
cout << value << endl;
passByPointer(&value);
cout << value << endl;
passByReference(value);
cout << value << endl;
return 0;
}
Memory layout
Command line arguments
args.cpp
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
if (argc != 2) // argc counts the num of CLPs
{
cerr << "Usage: " << argv[0]
<< " <first name>" << endl;
exit(1);
}
cout << "Hello " << argv[1] << endl;
return 0;
}
Consider the C++ program args.cpp above.
- You compiled the code:
$ g++ args.cpp -o args
- What is the output of
$ ./args goodbye
Draw the State of Stack Memory
swap.cpp
#include <iostream>
using namespace std;
// Swap two int values.
void swap(int* a, int* b)
{
int temp = *a; // store contents of a in temp
*a = *b; // put contents of b into a
*b = temp; // put temp a into b
// Draw the state of memory here <----
}
int main()
{
int x = 12;
int y = 33;
swap(&x, &y); // pass by pointer
cout << "x == " << x << " y == " << y << endl;
return 0;
}
Consider the C++ program swap.cpp above.
- Fill in the state of the stack memory at the location shown in the C++ code above marked
// Draw the state of memory here
. Make sure to write in the name of the variable and the value of the variable. Use the memory address0x123
for the address ofx
and0x127
fory
. Draw an arrow from any pointers to their associated value. Put the letterG
in any squares that are garbage values in memory. Each of the grid boxes is one byte of memory.