Exam 1 Flashcards

(30 cards)

1
Q
  1. Assume a string object has been defined as follows:
    string description;

Write a cin statement that reads in one word to description.

A

cin >> description;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Assume a string object has been defined as follows:
string description;

Write a statement that reads into description multiple words separated by blanks.

A

getline(cin , description);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Assume the following variables are defined:

int age; 
double pay; 
char section; 

Write a single cin statement that will read input into each of these variables.

A

cin >> age >> pay >> section;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What header files must be included in the following program?

int main() { 
double amount = 89.7; 
cout << fixed << showpoint << setprecision(1); cout << setw(8) << amount << endl; return 0; 
} 
A
#include <iostream>
 // cin and cout 
#include <iomanip> 
// setw, showpoint, fixed and setprecision 
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Write a statement that defines a constant variable named RATE whose value is set to 0.12

A

const double RATE = 0.12;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What header file must be included to perform mathematical functions like sqrt?

A

#include <cmath>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What header file must be included to use cin and cout?

A

#include <iostream>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What header file must be included to use stream manipluators like setprecision?

A

#include <iomanip>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What header file must be included to use random numbers rand() and srand()?

A

#include <cstdlib>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What header file must be included to use time for random generator?

A

#include <ctime>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Find the Errors

Cout << "Enter a number: "; 
Cin << number1; 
Cout << "Enter another number: "; 
Cin << number2; 
number1 + number2 = sum; 
Cout "The sum of the two numbers is " << sum;
A
**c**out << "Enter a number: "; 
**c**in **>>** number1; 
**c**out << "Enter another number: "; 
**c**in >> number2; 
**sum =** number1 + number2; 
**c**out **<<** "The sum of the two numbers is " << sum ; 
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Find the errors:

const int number1, number2, product; 
cout << "Enter two numbers and I will multiply\n"; 
cout << "them for you.\n"; 
cin >> number1, number2; 
product = number1 * number2; 
cout << “product is” << “product”; 
A
int number1, number2, product; // **remove const keyword **
cin >> number1** >> **number2; 
cout << “product is “ << **product**; //no double quotes around the variable 
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Find the Error
The following program contains many syntax errors. Locate as many as you can.

2. #include iostream 
3. using namespace std; 
4. 
5. int main(); 
6. } 
7. int a, b, c \\ Define 3 integers 
8. a = 3 
9. b = 4 
10. c = a + b 
11. Cout >> "The value of c is " >> C; 
12. return 0; 
13. { 
A
2. #include **<iostream>** 
3. using namespace std; 
4. 
5. int main() //no semicolon after main() 
6. **{** //open 
7. int a, b, c**;** //Define 3 integers 
8. a = 3**; **
9. b = 4**; **
10. c = a + b**;** 
11. **c**out**<<** "The value of c is " **<<** **c**; 
12. return 0; 
13. } 
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What will the following program segment display?

int unus, duo, tres; 
unus = duo = tres = 5; 
unus += 4; 
duo *= 2; 
tres −= 4; 
unus /= 3; 
duo += tres; 
cout << unus << endl << duo << endl << tres << endl; 
A

3 11 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Convert the following pseudocode to C++ code. (Don’t need to write the whole program.)

Store 172.5 in the force variable.
Store 27.5 in the area variable.
Divide area by force and store the result in the pressure variable.
Display the contents of the pressure variable.

A
double force, area, pressure; 
force = 172.5; 
area = 27.5; 
pressure = area / force; 
cout << “ the pressure is “ << pressure;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the exact output when the following code is executed?

int price = 100; 
if (price = 50) 
    cout << "The total price is 50." << endl; 
else 
    cout << "The total price is not 50." << endl; 
A

The total price is not 50.

17
Q
  1. What is the exact output when the following code is executed?
int price = 100; 
if (price == 50) 
    cout << "The total price is 50." << endl; 
else 
    cout << "The total price is not 50." << endl; 
A

The total price is not 50.

18
Q
  1. What is the exact output when the following code is executed?
int price = 100; 
if (price == 100) 
    cout << "The total price is 50." << endl; 
else 
    cout << "The total price is not 50." << endl; 
A

The total price is 50.

19
Q

Rewrite the following program. Use a switch statement instead of the if/else if statement.

#include <iostream> 
using namespace std; 
int main() { 
   int selection; 
   cout << "Which formula do you want to see?\n\n"; 
   cout << "1. Area of a circle\n"; 
   cout << "2. Area of a rectangle\n"; 
   cout << "3. Area of a cylinder\n"; 
   cout << "4. None of them!\n"; 
   cin >> selection; 
   if (selection == 1) 
      cout << "Pi times radius squared\n"; 
   else if (selection == 2) 
      cout << "Length times width\n"; 
   else if (selection == 3) 
      cout << "Pi times radius squared times height\n"; 
   else if (selection == 4) 
      cout << "Well okay then, good bye!\n"; 
   else 
      cout << "Not good with numbers, eh?\n";
   return 0; 
}
A
#include <iostream> 
using namespace std; 

int main() { 
   int selection; 
   cout << "Which formula do you want to see?\n\n"; 
   cout << "1. Area of a circle\n"; 
   cout << "2. Area of a rectangle\n"; 
   cout << "3. Area of a cylinder\n"; 
   cout << "4. None of them!\n"; 
   cin >> selection; 

   switch (selection) { 
      case 1: 
         cout << "Pi times radius squared\n"; 
         break; 
      case 2: 
         cout << "Length times width\n"; 
         break; 
      case 3: 
         cout << "Pi times radius squared times height\n"; 
         break; 
      case 4: 
         cout << "Well okay then, good bye!\n"; 
         break; 
      default: 
         cout << "Not good with numbers, eh?\n"; 
      } 

   return 0; 
}
20
Q

What will the following program display?

#include <iostream> 
using namespace std;

int main() { 
   const int UPPER = 8, LOWER = 2;  
   int num1, num2, num3 = 12, num4 = 3;  
   num1 = num3 > num4 ? UPPER : LOWER; 
   num2 = num4 > UPPER ? num3 : LOWER; 
   cout << num1 << " " << num2 << endl; return
21
Q

What is the exact result?

int credits = 9; 
if (credits < 16)  { 
   if (credits > 11) 
      cout << "You have enough courses."; 
   } else 
      cout << "Add another course."; 
cout << "Go register for courses."; 
A

Go register for courses.

22
Q

What is the exact result?

int credits =13; 
if (credits < 16) { 
   if (credits > 11) 
      cout << "You have enough courses."; 
}
else { 
    cout << "Add another course."; 
cout << "Go register for courses."; 
}
A

You have enough courses

23
Q

Rewrite the following as an exactly equivalent do …while statement:

for ( int numComputers = 1; numComputers < 10; numComputers++)
cout << “Computer #” << numComputers << endl;
A

1```
int numComputers = 0;

do {
cout &laquo_space;“Computer #” &laquo_space;numComputers &laquo_space;endl;
numComputers++;
} while(numComputers < 10);
~~~

24
Q

Given int variables k and total that have already been declared, use a do..while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus your code should put 11 + 22 + 33 +… + 4949 + 50*50 into total. Use no variables other than k and total.

A
total=0;
k=1;

do {
total+=k*k;
k++;
} while(k<=50);
25
Write a cout statment so that the variable population is displayed in a field of 12 spaces, left-justified, in fixed-point notation, with a precision of 8 decimal places. The decimal point should always be displayed.
`cout << setw(12) << left << fixed << showpoint << setprecision(8) << population;`
26
Write the code to type cast an int to a double.
`static_cast(intVar);`
27
modulus operator
`%` returns the remainder after division. example: `8%2 = 0` `8%3 = 2` good for use in checking odd or even ``` if (num % 2 = 0) { // even } else { // odd } ```
28
Write code that reads the first character of the next line into `char c;`
``` char c; cin.ignore(100, ‘\n’); c = cin.get(); ```
29
Write a cin statment for `string name;` to take in one word.
``` string name; cin >> name; ```
30
Write a cin statment for `string name;` to take in multiple words.
``` string name; getline(cin, name); ```