3. Variables¶


Q) Which of the following is a valid variable name in C++?¶

a) countCars&Trucks
b) numberFarmersInRegion2
c) 10BiggestProvincesPopulationAverage
d) return

(Demo Copilot answering this questions!)


Q) If there are 5 adults and 12 babies, which of the following statements best creates a variable which stores how many babies each adult is holding?¶

a) double numHolding = 12.0 / 5.0;
b) int numHolding = 5 / 12;
c) int numHolding = 12 % 5;
d) int numHolding = 12 / 5;


Q) Given the following variable:¶

string medicine = "Aspirin";

Which of the following C++ statements prints the character A?

a) cout << medicine[0];
b) cout << medicine[0] + 'A';
c) cout << medicine[1];
d) cout << medicine.length();


Q) Given the following variable declaration:¶

int cost = 120;

What statement displays "$ 120CAD"?
Note one space between $ and the number.

a) cout << setw(4) << "$" << cost << "CAD";
b) cout << setw(4) << "$" << setw(4) << cost << setw(4) << "CAD";
c) cout << "$" << cost << "CAD";
d) cout << "$" << setw(4) << cost << "CAD";


Q) Consider the following C++ statements:¶

int age;
	cout << age;

What is the output of this program?

a) Always 0
b) Always either 0 or 1
c) Always prints nothing
d) None of the above


Answers¶

b) numberFarmersInRegion2
d) int numHolding = 12 / 5;
a) cout << medicine[0];
d) cout << "$" << setw(4) << cost << "CAD";
d) None of the above