What does the following program print?
int main() {
int x = 10;
cout << "Num is: " << x << endl;
}
Num is: 10
What does the following while loop print?
int main() {
int x = 0;
while (x < 3) {
cout << x + 2 << endl;
x++;
}
}
2 3 4
Create an array that is a double and has a size of 5. (variable name doesn't matter, but syntax does).
double arr[5] ;
What does the following function return? (x equals 2 and y equals 3)
int func(int x, int y) {
return x - y;
}
The function will return the number -1.
C++ is a compiler based language.
What does this program output?
int main () {
int x = 2;
int y = 5;
cout << "x divided by y is: " << y / x << endl;
}
x divided by y is: 2
What is the following error with the loop?
for (int i = 1; i > 0; i ++) {
cout << i << endl;
}
It will go on infinitely (infinite loop error).
Write a loop that will print out all the values of the following arrays
int nums[5] = {0, 1, 2, 3, 4};
(this can be done in like a billion ways, but here is a general method, let me check your work)
for (int i = 0; i < 5; i ++) {
cout << nums[i] << endl;
}
Which one is the reference parameter and which one is the value parameter in the following function declaration?
void myFunction(int x, char& y);
x is the value parameter and y is the reference parameter.
What does the following program print?
int main() {
int x = 0;
cout << sizeof(x) << endl;
}
4
What will the following program print?
int main() {
int x = 5;
if (x > 5)
cout << "Greater than 5" << endl;
else if (x == 5)
cout << "Equal to 5" << endl;
else if (x <= 5)
cout << "Less than or equal to 5" << endl;
}
Equal to 5
Name the 3 primary loop types in C++
For loop, while loop, do while loop.
Why won't the following program work?
int main() {
char arr[3] = {'a', 'b', 'c'};
for (int i = 0; i <= 3; i++) {
cout << arr[i] << endl;
}
}
The for loop will go out of the bounds of the array size.
What is the the name of the following procedure?
int divideByTwo(int x) {
return x / 2;
}
double divideByTwo(int x) {
return x / 2;
}
Function Overloading.
How many bytes are the following data types(in C++)?
int, bool, double, char, float
int = 4 bytes, bool = 1 byte, double = 8 bytes, char = 1 byte, float = 4 bytes.
(DAILY DOUBLE) Where is the error?
int main() {
char c = 'd'
switch (c) {
case 'a':
cout << "1" << endl;
break;
case 'b':
cout << "2" << endl;
break;
case 'c':
cout << "3" << endl;
break;
default:
cout << "101" << endl;
}
return 0;
}
No semicolon in " char c = 'd' ".
What does the following nested for loop print?
for (int i = 1; i <= 5; i++) {
for (int j = 0; j < i; j++) {
cout << "*";
}
cout << endl;
}
*
**
***
****
*****
(DAILY DOUBLE)
Declare a 2D array that has two rows and four columns, and write a nested for loop that would hypothetically print out every value of the 2D array.
int TwoDee[2][4];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
cout << TwoDee[i][j];
}
cout << endl;
}
Write a function that return false if a integer is odd and true if an integer is even
(There are a trillion ways to do this, as long as it's this logic, let me check your work)
bool isEven(int x) {
return x % 2 == 0;
}
What does this following program print?
int main() {
int x = 'B';
cout << x + 1 << endl;
}
67
What is the keyword that lets you automatically detect the type of a variable based on the value you assign to it.
The 'auto' keyword.
This keyword skips the rest of the current iteration and jumps directly to the next evaluation of the loop condition.
'continue' keyword.
What would the following loop print?
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
cout << i << " " << j << endl;
}
break;
}
0 0
0 1
0 2
0 3
0 4
Write a function that swaps the value of two integers in C++. (Name of the function doesn't matter, just the logic)
void swapInts(int& x, int& y) {
int temp = x;
x = y;
y = temp;
}
What does the following program output?
int main() {
int x;
int y = &x;
*y = 10;
cout << y << endl;
}
The memory address of variable x (it would look like this: 0xe91a7ffd84)