Coding Exam Flashcards
Declare a comment with your student number.
//01029384
Declare a variable for storing the number of students in a lecture.
int numOfStudents;
Declare a variable for storing the name of car.
std::string carName;
Declare a variable for storing someone’s height.
float height;
Declare a variable for storing the state of a door lock.
bool doorState;
Write statement to print ‘Hello Sam’.
//Hello sam std::cout << "Hello sam";
Write statement to print the value stored in an integer variable called age.
//The value stored in a integer variable called age std::cout << age;
Write statement to print the value of a variable called time followed by a new line but proceeded by a tab space.
//The value of a variable called time followed by a new line but proceeded by a tab space std::cout << "\t" << time << "\n";
Write statement to declare and read a integer number from the QWERTY keyboard
int num;
std::cin»_space; num;
Write statement to declare and read a float from the QWERTY keyboard
float num;
std::cin»_space; num;
Write statement to declare and read a string from the QWERTY keyboard
std: :string name;
std: :cin»_space; name;
Declare a constant value called twoPi with the value 6.28318
const float twoPi = 6.28318;
Declare a constant value called name with your name
const std::string name = “dave”;
Write the following in C++ code: area = 2piR(squared)
const float twoPi = 6.28318;
float r = 1;
float area = twoPi * pow(r, 2);
Write an if statement that prints “lift-off” if a variable named time reaches 0.
if (time == 0) {
std::cout «_space;“Lift-off”;
}
Write an if…else statement that will print “light” if a variable named weight has a value less than 50 and print “heavy” otherwise
if (weight < 50) { std::cout << "light \n"; } else { std::cout << "heavy \n"; }
Write a switch case for all the days of the week, whereby the input is a number representing the day and each case should print the day in text form.
int day; switch (day) { case 0: // same as saying if (day == 0) std::cout << "Monday \n"; break; case 1: // same as saying else if (day == 1) std::cout << "Tuesday \n"; break; case 2: std::cout << "Wednesday \n"; break; case 3: std::cout << "Thursday \n"; break; case 4: std::cout << "Friday \n"; break; default: // error handling for values that don't match break; }
Write a function declaration for a function called bang, that takes no arguments and does not return anything.
void bang ();
Write a function declaration for a function called square, that takes a single float argument and returns a float.
float square (float x);
Write a function declaration for a function called printVector, that takes a single int vector as an argument and does not return anything.
void printVector (std::vector vector);
Write a function definition for a function called bang, that takes no argument and does not return anything.
void IAP::bang ()
{
}
Write a function definition for a function called square, that takes a single float argument and returns a float.
float IAP::square (float x)
{
}
Write a function definition for function called printVector, that takes a single int vector as an argument and does not return anything
void IAP::printVector (std::vector vector)
{
}
Declare an array of an appropriate type that could store
30 MIDI note numbers.
std::array midiNotes;
Declare a vector of an appropriate type that could store amplitude values.
std::vector amps;
Write a statement that stores the number 15 in the 2nd index of an array called numbers.
std::array numbers;
numbers[1] = 15;
Write a statement that appends the value 60
to a vector called noteList.
std::vector noteList;
noteList.push_back(60);
Write a for loop that starts at 0 and goes up to and including 10.
for (int i = 0; i <= 10; i++)
Write a for loop that starts at 5 and goes down to and including -5
for (int i = 5; i >= -5; i–)
Copy the value of a string called nameA to another string called nameB and then append the value of a string called nameC to the string called nameB.
std::string stringA;
std::string stringB;
std::string stringC;
stringB = stringA;
stringB += StringC;
Class (1): Declare a class called MIDINote that has 3 integer member variables called - Status, data1, data2
class MIDINote { public: int status, data1, data2; }
Class (2): Declare an instance of MIDINote
MidiNote mNote;
Class (3): Set each of MIDINote’s member variables to
144, 60, 127
mNote.Status = 144;
mNote.data1 = 60;
mNote.data2 = 127;
Class(4): Add a class method called print that prints all of the class’ member variables
void print () { std::cout << "Status: " << status << "Data 1: " << data1 << "Data 2: " << data2 << "\n"; }
Write the number 57 in hex.
0x39
Write the number 57 in binary.
0011 1001
How many bits are needed to store the number 502
9 bits
How many bits are needed to store the number 255
8 bits
How many bits are needed to store the number 16
5 bits