Chapter 3 - Worked Examples
Question 1
Based on the code block below, predict the values of the variables after each line below has been executed. State, for each line, which of the following is occurring:
Declaration
Initialisation
Assignment
int a;
int b = 3;
int c;
c = 5;
int d = 0;
d = 1;Question 2
What are the 2 different casing conventions we have covered? Which convention should be used in each of the scenarios below?
Function names
Variable names
Question 3
For each of the scenarios below, what would be the appropriate data type to use?
(There isn't strictly one correct answer, so long as you justify your choice)
e.g. The age of a person: int (or unsigned short)
The price of a grocery store item
The global population of people
The first letter of a first name
The circumference of a circle
Question 4
What is the syntax for list initialisation, and what problems does it solve?
Based on the following code example, determine for each initialisation if there would be any errors:
double x = 0;
int y = 0;
// Determine if each of the below lines are
int x2 = x;
char c2 = y;
int x3 {x};
char c3 {y};
char c4 {24};
char c5 {264};
int x4 {2.0};Question 5
Based on the following statements, state if the statements are true or false.
An
autovariable can be re-assigned to values of any type (i.e. it is typeless)The underlying type of an
autovariable is determined by the compilerAll
autovariables have to be initialised on declaration
Question 6
What does the keyword typedef do, and when might a programmer decide to use it?
You are creating a program to calculate the total cost of a shopping list - which data type might you decide to use to represent the cost of each item? Use the typdef keyword to give this type an alias called CURRENCY_TYPE
Question 7
What are literal constants? Show an example where a literal constant is used.
How are constant variables defined?
What is the
constexprkeyword?What are the benefits to using constants?
Question 8
What are enumerations (enums), and when might a programmer decide to use it?
In each of the code blocks below, predict if the program code is valid (compiles without errors), and its output:
enum Flowers
{
Daisy,
Lily,
Sunflower,
Rose,
Orchid
}
int main() {
std::cout << Daisy << std::endl;
std::cout << Lily << std::endl;
std::cout << Sunflower << std::endl;
std::cout << Rose << std::endl;
std::cout << Orchid << std::endl;
}enum Flowers
{
Daisy = 10,
Lily,
Sunflower,
Rose,
Orchid
}
int main() {
std::cout << Daisy << std::endl;
std::cout << Lily << std::endl;
std::cout << Sunflower << std::endl;
std::cout << Rose << std::endl;
std::cout << Orchid << std::endl;
}enum Flowers
{
Daisy = 10,
Lily = 20,
Sunflower = 0,
Rose = 30,
Orchid = 5,
}
int main() {
std::cout << Daisy << std::endl;
std::cout << Lily << std::endl;
std::cout << Sunflower << std::endl;
std::cout << Rose << std::endl;
std::cout << Orchid << std::endl;
}Question 9
You run a grocery store that only opens during weekdays. Create an enum called Day, with 7 constants corresponding each day of the week. Then, create a function with the following signature:
bool storeIsOpen(Day day);that takes a variable day corresponding to one of the values you have defined in Day, and returns true for all weekdays (Monday to Friday), and false for all weekends (Saturday and Sunday).
You should use a switch-case statement for the function body.
Last updated
Was this helpful?