logo

UTK Notes


Clicker Questions - 06-Multiple-Choice


Question 1: When I put the keyword const in front of a reference parameter, it means that:

  • It will seg-fault if you try to change it.
  • The procedure or method will not modify it.
  • It is a constant, like Pi.
  • The parameter is a fixed size data type, like int or char.
  • The compiler will emit lots of errors and warnings.
Answer When I put the keyword const in front of a reference parameter, it means that the procedure or method will not modify it. If it does, then the compiler will flag an error. The keyword is nice, because whoever calls such a procedure or method knows that its parameter won't change. Please see https://web.eecs.utk.edu/~jplank/plank/classes/cs202/Notes/Procedures/index.html for more discussion.

Question 2: When I put the keyword public in front of a class' variable, it means that:

  • A procedure that has an instance of the class may access or modify the variable.
  • It is const by default.
  • It is a global variable.
  • The variable can only be modified in one of the class' methods.
  • The compiler will emit lots of errors and warnings.
Answer When I put the keyword public in front of a class' variable, it means that a procedure that has an instance of the class may access or modify the variable. If it's private or protected, then it may only be accessed or modified within implementations of the class (there's a little more to it than this because of inheritance, but just go with that for now).

Questions 3-8: In all of the following, the answers are either “Like”, “Dislike” or “Sometimes Like / Sometimes Dislike”.

  • Question 3: What does Dr. Plank think of declaring variables in the middle of procedures.
Answer Dislike. I like my variable declarations at the top of a procedure or method. That way, you know where to find them.
  • Question 4: What does Dr. Plank think of tertiary expressions?
Answer Sometimes like, sometimes dislike. I like them when they are simple and readable. When they are convoluted, or embedded in convoluted expressions, I dislike them.
  • Question 5: What does Dr. Plank think of Python?
Answer Dislike. Maybe that will change someday, but I find the language to be too much of a free-for-all. I like structure and explicit types. This doesn't mean that you have to dislike Python -- I think it is an important language that has expanded the reach of machine learning, and that if you learn it, it will only help your careers. I simply don't like it.
  • Question 6: What does Dr. Plank think of putting executable code in header files?
Answer Dislike. In my opinion, header files should have procedure prototypes and class definitions, and no running code. Headers should be static, and implementations should be dynamic. It's a matter of readability, and being able to trace through what your code is doing.
  • Question 7: What does Dr. Plank think of reference parameters?
Answer Like. They avoid copying and read better than pointers, and sometimes you want your procedure to be able to modify its arguments.
  • Question 8: What does Dr. Plank think of The const keyword?
Answer Like. It's really nice to know when things are being modified and when they aren't, and const lets you know.

Clicker Questions - 05-More-Review

(BTW, I’m omitting the “include” and “using” lines to conserve space. Just pretend that the correct ones are there.)

(Also, don’t print the final “newline” – just answer with the string that is printed.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
  int i, n;
  string s;
  string rv;

  rv = "";
  while (cin >> n) {
    for (i = 0; i < n; i++) {
      if (cin >> s) rv.push_back(s[0]);
    }
  }
  cout << rv << endl;
  return 0;
}

input-1.txt

1 Fred
  • Question 1: What is the output of the program on the right when it runs with input-1.txt as standard input?
Answer
  • The program reads an integer n, and then iterates n times.
  • For each iteration, it reads a string, and if that was successful, appends the first character of the string to the string rv.
  • The program repeats this process until reading n fails.
  • Then it prints out rv.

So, with input 1, it reads the integer 1, then reads the string "Fred", and appends the 'F' to rv. The answer is

F

input-2.txt

2
Marco
Polo
3
Drop
Dead
Fred
  • Question 2: What is the output of the program on the right when it runs with input-2.txt as standard input?
Answer
  • It reads the number 2 and then "Marco" and "Polo". At this point, rv is "MP".
  • It reads the number 3 and then "Drop", "Dead" and "Fred". At this point, rv is "MPDDF".

The answer is:

MPDDF

input-3.txt

3 A BB
CCC 2 Xerxes
Yassin Zelda
  • Question 3: What is the output of the program on the right when it runs with input-3.txt as standard input?
Answer
  • It reads the number 3 and then "A", "BB" and "CCC". At this point, rv is "ABC".
  • It reads the number 2 and then "Xerxes" and "Yassin". At this point, rv is "ABCXY".
  • It tries to read a number, but it fails because the next word is "Zelda". So it falls out of the while() loop.

The answer is:

ABCXY

input-4.txt

3 7 5
3 4 7
6 8 3
9 8 3
  • Question 4: What is the output of the program on the right when it runs with input-4.txt as standard input?
Answer
  • It reads the number 3 and then "7", "5" and "3". At this point, rv is "753".
  • It reads the number 4 and then "7", "6", "8" and "3". At this point, rv is "7537683".
  • It reads the number 9 and then "8" and "3". At this point, rv is "753768383".
  • It tries to continue reading strings, but it reaches EOF, so the cin statement fails.
  • It now tries to read an integer, but the cin is still in a failed state, so it out of the while() loop.

The answer is:

753768383

Class Stats, PDF Download