Skip to main content

Section 1.3 Homework 3

The goal of this assignment is to give you some practice writing simple C++ programs and to check your knowledge of some of the main concepts from:Β  functions, switch statement, randomness.

Exercises Exercises

1.

For a function declaration in C++, what are the required elements?
  • function body (implementation)
  • return type
  • function name
  • parameter list
  • access modifier

2.

For a function definitionΒ in C++, what are the required elements?
  • function body (implementation)
  • return type
  • function name
  • parameter list
  • access modifier

3.

Which of these are complete function definitions with no syntax errors?
  • int myFun();
  • void mystery() {}
  • int addEmUp(int x, int y) { return x + y; }
  • int thingy(a, b) { return a; }
  • yolo() { return "YOLO"; }

4.

Write a function add_doubles that takes two parameters of type double and returns the sum.

5.

Write a function add_strings that takes two parameters of type string and returns the concatenation.

6.

Which of these is NOT a proper function prototype?
  • char getChar();
  • void minString(string s1, string s2);
  • double(int x, int y);
  • int funfun17(char x);

7.

C++ allows function ___________ : you can use the same name for more than one function, as long as the functions all have different parameter lists.Β  (Fill in the blank.)
  • multinaming
  • emulating
  • overloading

8.

In C++, if you want a function that does not return a value -- for example, a function that just prints something to the screen -- you can declare its return type as ...
  • void
  • null
  • None
  • nada

9.

10.

A ___________ variable is a variable that is declared outside of any function.Β  These variables are available everywhere in the program past the point of the variable’s declaration.Β  (Fill in the blank.)
  • local
  • automatic
  • global
  • temporary

11.

Any variable declared in the scope of a function, or inside of a block, is called a ___________ variable.Β  (Fill in the blank.)
  • local
  • regular
  • global
  • normal

12.

13.

14.

Write a C++ functionΒ factorial that takes an integer, ’n’, and returns the value of n factorial (a.k.a., n!):
  • When n is positive, n! = 1 * 2 * 3 * ... * n.Β  For example, 5! = 1 * 2 * 3 * 4 * 5 = 120.
  • When n is zero, n! is defined to be 1.
  • When n is negative, n! is undefined.
You may assume that the parameter ’n’ will not be negative in this problem.

15.

The function accumulate_acronym has no arguments; it reads strings from standard input and accumulates the corresponding acronym.
The end of the input is signaled when the user enters the string "STOP".Β  For example, if the user enters "United Guinea Pig Supporters of America STOP", then the function returns the acronym string, "UGPSoA".Β  (The function does not print anything to standard output.)

16.

In a C++ switch statement, what is required to avoid falling through from one case to the next?
  • break;
  • end;
  • stop;
  • a semicolon

17.

18.

When writing a C++ program where you need to generate (pseudo) random numbers, when in your program should you use srand?
  • Every time you need a random number
  • Never, it’s just window dressing
  • Once, at the start of your program
  • Occasionally, after you’ve used rand for a while

19.

20.

21.

Write a C++ function weird_sum that takes two integers, ’n1’ and ’n2’. The function returns the sum of ’n1’ and ’n2’, unless their sum is a multiple of 13, in which case the function returns 3 less than their actual sum.

22.

Write a C++ function called first_alpha that takes three string arguments and returns the string that comes first alphabetically from among those three. You may assume that the strings are in all lowercase.

23.

In Linux, when naming a file, is it better to use embedded spaces ("this is my file"), or underscores ("this_is_my_file")?
  • underscores
  • embedded spaces

24.

25.

The ________ command is a Linux utility which can be used to read the contents of a text file one screen-full at a time, in the console.
It allows faster access to the initial contents of a large text file because it doesn’t load the entire file at once.
  • less
  • quick_read
  • skim
  • browse

26.

In Linux, the rm command is used to remove (delete) files and directories. rm can be dangerous, especially for new Linux users, because...
  • by default, it does not ask for *confirmation*, it just goes ahead and deletes
  • it can be combined with flags, like -r and -f, to delete the entire contents of the file system
  • it can be used with patterns, like *.*, to delete many many files at once
  • all of the above
You have attempted of activities on this page.