Exam 1 Flashcards

1
Q

What are 3 common errors in if statements?

A
  • Misplaced semicolons
  • Missing braces
  • Confusing = with ==
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What’s wrong with this?

If (2 < count < 9)

A

C++ does not allow you to check numeric ranges with expressions such as 5 < x < 20. Instead you must use a logical operator to connect two relational expressions.

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

How do you work around a floating point error (example: .66666667 * 6.0)?

A

Test for closeness within a range.

Example: if (abs(result - 4.0 < .0001))

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

What is executed if average = 80?

if (average == 100)
cout &laquo_space;“Congratulations! “;
cout &laquo_space;“That’s a perfect score!\n”;

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

How do you work around a floating point error (example: .66666667 * 6.0)?

A

Test for closeness within a range.

Example: if (abs(result - 4.0 < .0001))

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

How do you gather user input that may include spaces?

A

getline(cin, varName);

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

&& ____ conditions must be true
|| ____ conditions must be true
! ____

A

Both
One or more
Reverses the truth of the expression (NOT)

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

How do you gather user input that may include spaces?

A

getline(cin, varName);

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

What does cin.get(); do?

A

It reads a single character, including any whitespace character.

To store the character, use either:
cin.get(ch);
ch = cin.get();

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

What is wrong with this code?

char ch;
int number;
cout << "Enter a number. ";
cin >> number;
cout << "Enter a character. ";
ch = cin.get();
cout << "Thank you!\n"
A

cin and and cin.get() are combined, which can cause issues.

cin.get() is skipped, because the Enter key entered with the integer is stored in the buffer and then assigned to ch.

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

What does cin.ignore() or cin.ignore(n, c) do?

A

It ignores one or more characters in the keyboard buffer.

cin. ignore(n, c) can be used to skip n number of characters, or until character c is encountered.
cin. ignore() skips the next character.

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

What does string.lenth(); do?

A

It returns the number of characters in a string.

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

What does string.assign() do?

A

It is used to create a string with a certain number of characters.

Example: spaces.assign(22, ‘ ‘);

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

What is a c-string?

A

Array of characters used prior to the introduction of strings.

char word[10] = “Hello”;

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

What is buffer overrun?

A

When extra values spill over from the memory allocation for one variable into whatever was stored in the next bit of memory.

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

How many quotation marks are used for a string literal?

A character literal?

A

char letterOne = ‘A’;

string sentence = “okay”;

17
Q

How many bytes of memory does a character occupy?

A

1

18
Q

How many characters is ‘\n’?

A

1

19
Q

What marks the end of a string?

A

A null terminator ‘\0’; this is one character.

20
Q

What header file must you include when you use strings?

A

include

21
Q

What does sizeof(short) do?

A

Returns the number of bytes of memory used by any data type or variable. This is helpful, because not all machines use the same sizes for data types.

22
Q

What is wrong with this?

cout &laquo_space;value;
int value = 100;

A

A variable cannot be used before it is defined.

This is a scope rule.

23
Q

How are single line comments written in C++?

A

With //

24
Q

How are multiline comments written in C++?

A

With /*
and */

Everything between the asterisks is ignored.

25
Q

What is wrong with this statement?

cout &laquo_space;“Hello” &laquo_space;\n;

A

The \n character must be within the quotation marks.

26
Q

endl is a…

A

steam manipulator

27
Q

What is an example of an escape sequence?

A

\n

28
Q

What character does an escape sequence start with?

A

\

29
Q

What happens if an escape sequence is outside of a string

“word” \n

A

The code will not compile

30
Q

what is a literal?

A

a piece of data that is hard-coded

ex: 20, “text”, etc.

31
Q

Rules for variable names

A

cannot start with number
can contain alphanumeric characters and underscores
uppercase and lowercase characters are distinct

32
Q

short int range

A

-32768 to 32767

33
Q

unsigned short int range

A

0 to 65535