logo

UTK Notes


Midterm 1

Question 1

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

Question 2

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

Question 3

Resizing a vector through the .resize() method can only make a vector smaller, not larger.

False

Question 4

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

Question 5

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

Question 6

How do you append an element called “item” to the end of a vector called “numbers”?

1
numbers.push_back(item);

Question 7

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;

Question 8

Fill in the blank to open myfile.txt to read from the file using file streams.

1
ifstream fin;
1
fin.open("myfile.txt");

Question 9

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

Question 10

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

Question 11

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 in argv[1], b is in argv[2].

Question 12

What is Unix?

A operating system

Question 13

What is the data type of argv?

array of c-style strings

Question 14

When the getline() function grabs a line from a stream, it also grabs the newline character at the end of the line.

False

Question 15

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()

Question 16

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

Question 17

Getline() can be used with string streams and with file streams.

True

Question 18

Getline() stores a line from stream in a string variable by returning a string and passing in the stream as a single argument.

False

Question 19

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

Question 20

What header must you include to use file streams?

1
#include <fstream>

Question 21

Which of the following I/O manipulators is NOT persistent?

1
setw()

Question 22

Type the command to open a file called myprogram.cpp in VIM:

1
vim myprogram.cpp

Question 23

One should compile a C++ file in normal mode in VIM.

False

Question 24

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

Question 25

My vector called numbers looks as follows: {1, 2, 4, 10, 11}

After calling numbers.resize(2), the vector will contain:

{1, 2}

Question 26

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

Question 27

What is the index of the last argument in command line arguments?

argc - 1

Question 28

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

Question 29

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();

Question 30

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()

Question 31

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

Question 32

Academic Dishonesty and Cheating

Question 33

argv[0] in command line arguments, is

The name of the program invoked

Question 34

Vectors are equivalent to ArrayList in Java.

Question 35

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

Question 36

Fill in the following #include statement in order to use cin and cout in a program.

1
#include <iostream>

Question 37

What header must you include to use i/o manipulators?

1
#include <iomanip>

Question 38

Which of the following will declare a vector of vectors that contains integers called grid?

1
vector < vector <int> > grid

Question 39

What header must you include to use string streams?

1
#include <sstream>

Question 40

What is VIM?

A text editor