Given the following program run on the command line:
1
./myProgram 15 data 20 data1
Where is the argument ‘20’ in stored in the main function?
1 argv[3]
What is the data type of the argument ‘20’ stored by the main function?
c-style string
The following code snippet will compile.
1
2
3
4
5
int i = 3;
double d = 3.14;
string s = "string";
printf("%5d %.2f %10s", i, d, s);
True
Resizing a vector through the .resize()
method can only make a vector smaller, not larger.
False
A student’s UTK netid is samsmith1. This student wishes to SSH into the Tesla 4 machine. Write the command to accomplish this task:
1 ssh samsmith1@tesla4.eecs.utk.edu
Match the following Unix commands with their definition.
cp
- copy
scp
- secure copy
ls
- list contents
ssh
- secure shell
rm
- remove
cd
- change directory
mkdir
- create directory
How do you append an element called “item” to the end of a vector called “numbers”?
1 numbers.push_back(item);
A function called formatTime takes hours, minutes, and seconds as arguments and returns a string in HH:MM:SS format. For example, if hours, minutes and seconds are 3, 4, and 5 respectively, then the formatTime returns
03:04:05
Given we have an ostringstream object declared as sout, how do we format the return string using stream manipulators?
Use any non-persistent manipulators directly before the variable in your answers. Choose from the following manipulators (and you can use a manipulator more than once):
left, right, scientific, fixed, endl, setfill(), showpoint, setw(), setprecision(), hex, dec
1 sout << setfill('0') << setw(2) << hours << ":" << setw(2) << minutes << ":" << set setw(2) << seconds;
Fill in the blank to open myfile.txt to read from the file using file streams.
1
ifstream fin;
1 fin.open("myfile.txt");
What is printed to the screen after the following program executes?
1
./myprogram 1 2 3 4 5 6
1
2
3
4
int main(int argc, char *argv[]) {
cout << argc;
return 0;
}
7
A program called fileStreams.cpp
takes an input text file and an output text file on the command line by using argc and argv. Replace XXX to correctly error check argc given the following usage:
1
./fileStreams input.txt output.txt
1
2
3
4
5
6
7
int main(int argc, char** argv) {
if (argc != XXX) {
cerr << "usage: ./fileStreams inputfile.txt output.txt";
return 1;
}
// The rest of the program
}
XXX should be replaced with
3
Given the following command:
1
./myprogram a b
Write where the above command line arguments are stored for the int main()
function.
./myprogram is in
argv[0]
,a
is inargv[1]
,b
is inargv[2]
.
What is Unix?
A operating system
What is the data type of argv?
array of c-style strings
When the getline()
function grabs a line from a stream, it also grabs the newline character at the end of the line.
False
Given the following code, replace XXX with a way to check that the input file named myfile.txt
opened correctly.
1
2
3
4
5
ifstream fin;
// Code to open file
if (XXX) {
cout << "Could not open file myfile.txt\n";
}
Replace XXX with
1 !fin.is_open()
The following two code snippets will produce the same output (where mph and kph are variable names).
Code snippet 1:
1
cout << fixed << dec << setprecision(2) << mph << " " << setprecision(2) << kph << endl;
Code snippet 2:
1
2
cout << fixed << dec << setprecision(2);
cout << mph << " " << kph << endl;
True
Getline()
can be used with string streams and with file streams.
True
Getline()
stores a line from stream in a string variable by returning a string and passing in the stream as a single argument.
False
I have represented a grid as a 2-D vector (a vector of vectors) in my code where the outer vector contains my rows, and the inner vector contains my columns. I would access cell (i, j) as myvector[i][j].
True
What header must you include to use file streams?
1 #include <fstream>
Which of the following I/O manipulators is NOT persistent?
1 setw()
Type the command to open a file called myprogram.cpp
in VIM:
1
vim myprogram.cpp
One should compile a C++ file in normal mode in VIM.
False
I have opened a file called lab1.cpp with VIM on the command line for the very first time. I want to edit my file by typing the first three lines of my program. What should I type to enter into the correct mode to accomplish this task?
i
My vector called numbers looks as follows: {1, 2, 4, 10, 11}
After calling numbers.resize(2)
, the vector will contain:
{1, 2}
I have completed writing a program in VIM and I’m currently in normal mode. How do I save my program, but not exit VIM?
:w
How do I save my program and exit VIM simultaneously?
:wq
What is the index of the last argument in command line arguments?
argc - 1
The following two code snippets will produce the same output (where retailItem, price, and wholesaleCost are variable names).
Code snippet 1:
1
cout << right << setw(6) << fixed << setprecision(2) << retailItem << right << setw(6) << " $" << price << setw(6) << " $" << wholesaleCost << endl;
Code snippet 2:
1
2
cout << right << setw(6) << fixed << setprecision(2);
cout << retailItem << " $" << price << " $" << wholesaleCost << endl;
False
A function called stringList
takes three integers as arguments and returns a string with commas and a space in between each integer (example: 1, 2, 3).
The function uses an ostringstream
called sout
to create the list in a stringstream
with the following line:
1
sout << num1 << ", " << num1 << ", " << num3;
How do we return the string created by the stream?
1 return sout.str();
Replace XXX in order to loop through the entire vector called scores.
1
2
3
for (int i = 0; i < XXX; i++) {
cout << scores[i] << endl;
}
Replace XXX with
1 scores.size()
If I have a vector declared as
1
vector <int> v
and it’s size is currently 10
, then both v.clear()
and v.resize(0)
will both accomplish the same task of removing all the elements from the vector and changing it’s size to zero.
True
Academic Dishonesty and Cheating
argv[0]
in command line arguments, is
The name of the program invoked
Vectors are equivalent to ArrayList
in Java.
The following code snippet will compile.
1
2
3
4
5
int a = 5;
double b = 20.22;
char c = 'a';
printf("%5a %10.1b %10c", i, f, c);
False
Fill in the following #include statement in order to use cin
and cout
in a program.
1 #include <iostream>
What header must you include to use i/o manipulators?
1 #include <iomanip>
Which of the following will declare a vector
of vectors
that contains integers
called grid
?
1 vector < vector <int> > grid
What header must you include to use string streams
?
1 #include <sstream>
What is VIM?
A text editor