Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
2015-10-17
Exam 1 Practice
In code snippets, always assume standard library types (std::string, std::vector, etc.)
have been properly #included. On the other hand, if a complete program is given, do not
assume anything except what is printed on the page.
1. How many bits are there in a byte?
2. The largest value of an 8 bit, unsigned integer is
(a) 0
(b) 255
(c) 256
(b) constant
(d) more2
(f) 000
(h) 2more
20. Explain what happens if a user enters 3 when running the following program.
#include <iostream>
using std::cout; using std::cin; using std::endl;
int main()
{
int size;
cout << "Enter a number: ";
cin >> size;
for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
{
if(0==i || (size-1)==i || 0==j || (size-1)==j)
cout <<*;
else cout << ;
}
cout << endl;
}
return 0;
}
Explain what happens if a user enters an arbitrary positive int.
21. Write a complete program which determines whether a triangle is equilateral. The
program must take the lengths of the sides as input, and output a statement as
follows (user input in bold type).
Enter the lengths of the sides: 3.1 3 3
The triangle with sides 3.1, 3, and 3 is not equilateral.
Enter the lengths of the sides: 5.2 5.2 5.2
The triangle with sides 5.2, 5.2, and 5.2 is equilateral.
The output of your program must appear as above.
22. Give the definition of a function which takes a reference to a vector<double> and
returns the mean of its members.
23. Give the definition of a function numDigits() which takes an unsigned int as input
and returns its number of digits. For example, numDigits(957) must return 3.
24. Explain what the following function does. Determine a descriptive name for the
function.
bool foo(char c)
{
return (c >= 0) && (c <= 9);
}