Section Two Flashcards

1
Q

What are assignment operators?

A

=
+=
-=
and so on

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

What are arithmetic operators

A

*
etc

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

What are comparison operators

A

==
!=
>
<
>=
<=

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

What are logical Operators

A

&& and
|| or
! not
Reverse the result, returns false if the result is true !(x < 5 && x < 10)

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

Create a first and last name
append the last to the first

A

string firstName = “John “;
string lastName = “Doe”;
string fullName = firstName.append(lastName);
cout &laquo_space;fullName;

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

Get the length of a string

A

string txt = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
cout &laquo_space;“The length of the txt string is: “ &laquo_space;txt.length();

size() does the same thing

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

Get the H from a string variable with the value of Hello

A

string myString = “Hello”;
cout &laquo_space;myString[0];

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

Using lenght(), get the last letter of a string variable

A

string myString = “Hello”;
cout &laquo_space;myString[myString.length() - 1];

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

Change one of a string variable’s characters

A

string myString = “Hello”;
myString[0] = ‘J’;
cout &laquo_space;myString;

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

What do you use to get a string character other than []

A

cout &laquo_space;myString.at(0)

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

Show how to use quotes in a string with an escape character

A

string txt = “We are the so-called "Vikings" from the north.”;

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

If you don’t add the below, how do you access strings and cout?

using namespace std;

A

include <iostream></iostream>

#include <string>
// using namespace std; - Remove this line</string>

int main() {
std::string greeting = “Hello”;
std::cout &laquo_space;greeting;
return 0;
}

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

How would you create a string in c-style

A

C doesn’t let you create strings, just characters. If you wanted to print a string though you could add all the letters into an array and print that

char greeting2[] = “Hello”; // C-Style String (an array of characters)

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

Find the highest value of 5 and 10

find the lowest value

A

cout &laquo_space;max(5, 10)
cout &laquo_space;min(5, 10)

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

If you want more math functions, what header library do you include?

find the square root of 64
round 2.6
natural logarithm of 2

A

include <cmath></cmath>

cout &laquo_space;sqrt(64);
cout &laquo_space;round(2.6);
cout &laquo_space;log(2);

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

Use a boolean expression

A

int x = 10;
int y = 9;
cout &laquo_space;(x > y);

17
Q

Create an if then statement that takes your age and the voting age and let’s you know if you’re old enough to vote.

A

include <iostream></iostream>

using namespace std;

int main() {
int myAge = 25;
int votingAge = 18;

if (myAge >= votingAge) {
cout &laquo_space;“Old enough to vote!”;
} else {
cout &laquo_space;“Not old enough to vote.”;
}
return 0;

18
Q

Perform a switch statement

A

include <iostream></iostream>

using namespace std;

int main() {
int num = 2;

switch (num) {
    case 1:
        cout << "One" << endl;
        break;
    case 2:
        cout << "Two" << endl;
        break;
    default:
        cout << "Other" << endl;
}

return 0; }
19
Q

What is a ternary operator?

A

Short hand if-else

int time = 20;
string result = (time < 18) ? “Good day.” : “Good evening.”;
cout &laquo_space;result;

20
Q

If you go into a switch block, how do you get out?

21
Q

Make a loop and a variable with the value of 0
The loop should continue if i is less than 5
increment i by 1 each iteration

A

int i = 0;
while (i < 5) {
cout &laquo_space;i &laquo_space;“\n”;
i++;
}

22
Q

Create a do/while loop.

What makes it different?

A

It will always go through one iteration

int i = 0;
do {
cout &laquo_space;i &laquo_space;“\n”;
i++;
}
while (i < 5);

23
Q

Show the structure of a for loop.

A

for (execute once, condition to keep running code block, what happens every iteration) {
// code block to be executed
}

Statement 1 is executed (one time) before the execution of the code block.

Statement 2 defines the condition for executing the code block.

Statement 3 is executed (every time) after the code block has been executed.

24
Q

Using a for loop, give it an integer variable (i) that equals 0 to start, the code loop should continue as long as ‘i’ is less than five.
For each loop increase ‘i’ by 1

A

include <iostream></iostream>

using namespace std;

int main() {
for (int i = 0; i < 5; i++) {
cout &laquo_space;i &laquo_space;“\n”;
}
return 0;
}

25
Q

Create a nested loop that increments by one and goes to 2
The nested loop should should iterate 3 times and increment a number by 1

A

include <iostream></iostream>

using namespace std;

int main() {
// Outer loop
for (int i = 1; i <= 2; ++i) {
cout &laquo_space;“Outer: “ &laquo_space;i &laquo_space;“\n”; // Executes 2 times

// Inner loop
for (int j = 1; j <= 3; ++j) {
  cout << " Inner: " << j << "\n";  // Executes 6 times (2 * 3)
}   }   return 0; }
26
Q

What is a for-each loop? How would you perform one?

A

This is basically a normal loop for us:

int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i : myNumbers) {
cout &laquo_space;i &laquo_space;“\n”;
}

27
Q

How would you create an array of 4 cars?

A

string cars[4] = {“Volvo”, “BMW”, “Ford”, “Mazda”};

28
Q

Loop through an array using a:
for loop
for-each loop

A

// Create an array of strings
string cars[5] = {“Volvo”, “BMW”, “Ford”, “Mazda”, “Tesla”};

// Loop through strings
for (int i = 0; i < 5; i++) {
cout &laquo_space;cars[i] &laquo_space;“\n”;
}

or

// Create an array of integers
int myNumbers[5] = {10, 20, 30, 40, 50};

// Loop through integers
for (int i : myNumbers) {
cout &laquo_space;i &laquo_space;“\n”;
}

29
Q

Do you have to specify an array size to make one?

A

No, you can omit it like below

string cars[] = {“Volvo”, “BMW”, “Ford”};