Note: Not all think pair share solutions are here. Some are in the notes for the corresponding day. Others may be on Canvas (in the event of a multiple choice TPS). Regardless of where the solutions are for a given TPS, the material covered is still fair game for quizzes and exams.

Contents

Jan. 30

Question 1

Which of the following is the correct way to create a new folder name "folder1" on the command line?

  1. cd folder1
  2. mkdir folder1
  3. ls folder1
  4. pwd

Answer: mkdir folder1

Question 2

Why would a programmer want to use pseudo code as opposed to just diving right into a real programming language like C++?

Answer: There are lots of reasons! One is that pseudo code avoids syntax, which helps us write steps that can be implemented in any language and allows writing at a higher conceptual level. Also, you don't need to worry about anything compiling or running.

(Back to top)

Feb. 4

Question 1

For each of the following values, list the appropriate type for a C++ variable that will hold it:

  1. 3.564
  2. "Howdy!"
  3. 5
  4. 'M'
  5. 39392929394

Answer:

  1. 3.564 (float or double)
  2. "Howdy!" (string)
  3. 5 (int or char)
  4. 'M' (char)
  5. 39392929394 (long or double)

Question 2

Why would a programmer want to use a constant over a variable?

Answer: Constants are useful when the value needed won't change during the lifetime of the program. For examples, if a program is using π, he or she may use a constant named PI to store an approximate value, e.g., const float PI = 3.14;.

(Back to top)

Feb. 11

Question 1

What will the following snippet of code print?

Answer: You completed your mile in under seven minutes!

Question 2

What will the following snippet print at the end if the user enters the number 70? This one is tricky, so really think about it!

Answer: Your number is above 60.

(Back to top)

Feb. 18

Question 1

Define a struct that hold three fields: firstName, lastName, phoneNumber (don't worry about writing any other code). Also, indicate where the struct definition will go: above main or inside of main.

Answer: It goes above main. Here's what it looks like:

Question 2

Now create an instance of the struct and set the fields such that firstName is Willy, lastName is Wonka, and phone number is 555-333-1111.

Answer:

(Back to top)

Mar. 6

Question 1

Consider this code:

What does the first cout statement in main output? What does the second cout statement output?

Answer:

10
15

Question 2

What's the following program output (for both the first and second cout statements)?

Answer:

11
16
(Back to top)

Apr. 17

Question 1

What library must be included in order to read from or write to a file?

Answer: fstream, e.g., #include <fstream>

Question 2

The function below takes a file name and a message to write to that file. Complete the function so that it writes that message to the file. Hint: think back to the 4 steps of writing to a file we went over in class on Tuesday.

Answer:

(Back to top)

Apr. 22

Question 1

What is the difference between a struct and a class in C++?

Answer: The default access level of a struct is public, whereas it is private for a class.

Question 2

Say I have the following class:

One way to set each of the data members for an instance is the following:

It is pretty tedious to set each and every one of the data members. What could we add to the Movie interface to make this easier? Write the C++ code for that.

Answer: A constructor, like this one:

(Back to top)

Apr. 24

Question 1

Suppose I want to create a new class called Foo. What are the two files I should make for Foo? What goes in each?

Answer:

Question 2

Now, supposing that main is in a file called main.cpp, what is the terminal command to compile main.cpp along with the Foo class into an executable called main? Hint: take a look at what we did for parts 2--4 of Lab 11.

Answer: Any of the following are okay (the order of the arguments to g++ don't matter, except that main must follow the -o.:

g++ Foo.cpp main.cpp -o main
g++ main.cpp Foo.cpp -o main
g++ -o main main.cpp Foo.cpp
g++ -o main Foo.cpp main.cpp 
g++ main.cpp -o main Foo.cpp
g++ Foo.cpp -o main main.cpp
(Back to top)