Você está na página 1de 4

# Math 173: Object Oriented Programming

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

## 3. Convert the hexadecimal number AA to decimal.

4. Compute (0x94|0x1A).
5. Circle all the valid C++ identifiers.
(a) #include
(c) double
(e) invalid
(g) ELAC4EVA

(b) constant
(d) more2
(f) 000
(h) 2more

## 6. Give the exact console output of the following code.

std::cout << "H\n e\t l\n lo!\n";
7. Give the exact console output of the following code.
std::cout << 34/10;
8. Give the console output of
int i = 0, sum = 0;
while (i++ < 3) sum += i;
std::cout << sum;
9. After the following code is run
int x = 3;
int z = x;
z += 4;
what is the value of x?
10. After the following code is run
int x = 3;
int& z = x;
z += 4;
what is the value of x?
1

## 11. After the following code is run

int x = 3;
int* p = &x;
*p += 4;
what is the value of x?
12. Assuming int nums[3]; what is the problem with the statement nums[3] = 5; ?
13. How many times is Hello! output by the following code?
for(int i=0; i<101; i++) cout << "Hello!\n";
14. How many times is Hello! output by the following code?
for(int i=0; i<101; i++); cout << "Hello!\n";
15. How many times is Hello! output by the following code?
int i = 0;
do { std::cout << "Hello!\n"; } while (++i < 10);
16. Given
vector<int> nums = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6};
what is output by the statement std::cout << nums[3]; ?
17. In the following code fragment, give the line numbers of every line of code run, in the
order in which they are run. (Always include the first line when the loop restarts from
the beginning.)
1
2
3
4
5
6

## for(int i=0; i<10; ++i)

{
if (0 == i%2) continue;
else if (3 == i) break;
}
std::cout << "END!\n";

## 18. Explain the error in the following code.

const int x;
x = 3;
19. Find all errors in the following complete program.
1 #include iostream
2 using namespace std;
3 char main()
4 {
5
int num;
6
cout << "Enter a number: " <<;
7
cin << num;
8
if (num<0 or num>10) cout << num << " is out of range.\n";
9
cout << "The square of num is " << (num)(num) << endl;
10
return 0;
11 }
2

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

## 25. Give the console output of the following program.

#include <iostream>
using std::cout;
void f(int& x, int y)
{
x += y;
}
int main()
{
int a = 3, b = 5;
f(a,b);
cout << a << \t << b << \n;
return 0;
}
26. Give the console output of the following code.
int x=0;
std::cout << x;
{
int x=5;
std::cout << x;
}
std::cout << x;