CSCI 1 - Midterm 1

5.0(1)
studied byStudied by 29 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/29

flashcard set

Earn XP

Description and Tags

Middler

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

30 Terms

1
New cards

VARIABLE DECLARATIONS

A variable is a named storage location in memory. The line of code where a variable is defined is the variable declaration.

int item;

2
New cards

CONSTANT DECLARATIONS

A variable whose value cannot be altered anywhere in the program.

const double pi = 3.14;

3
New cards

DATA TYPES

INT

‘CHAR’

“STRING”

FLOAT: Float(4bytes), Double (8bytes), Long Double

BOOL: True (1) or False (0)

4
New cards

VARIABLE INITIALIZATION AND ASSIGNMENT

An assignment statement uses the = operator to store a value in a variable

int length = 12;

5
New cards

COMPOUND ASSIGNMENT

num += 3;

6
New cards

ORDER OF OPERATIONS (+)

  1. ()

  2. - (unary negation)

  3. *, /, %

  4. +, -

7
New cards

USING MATHEMATICAL OPERATORS

Mathematical expressions can be created using multiple mathematical operators.

Parentheses can be used to override the order of operations.

8
New cards

USING RELATIONAL OPERATORS

Relational Expressions are made with Relational Operators to make a true/false determination (boolean expressions)

<, >, >=, <=, ==, !=

9
New cards

USING LOGICAL OPERATORS

  1. NOT !: reverses the value of a bool expression

  2. AND &&: true if both bool expressions are true

  3. OR ||: true if either expression is true

*ordered in highest precedence

10
New cards

TRACING IF ELSE STATEMENTS

😀

11
New cards

NESTED IF STATEMENTS

if (expression) {

if (expression) {

statement;

}

}

12
New cards

SWITCH STATEMENTS

Used to select among statements from several alternatives.

switch (exp [integer]) {

case exp1: statement1;

case exp2: statement2;

default: (optional);

}

*include break

13
New cards

TYPE CONVERSIONS (IMPLICIT)

(Type coercion) Automatic conversion of an operand to another data type. Promotion (convert to a higher data type) or Demotion. Lower is promoted to higher.

float val1 = 3.6;

int val2 = 2;

float result = val1*val2;

*val2 remains int

14
New cards

TYPE CONVERSIONS (EXPLICIT)

Manual type conversion. Useful for floating point division with ints.

static_cast<type>(expression)

15
New cards

CURLY BRACKET INCLUSION/EXCLUSION

Use curly brackets when executing more than one statement.

if (score > 90)

grade = ‘A’;

if (score > 90) {

grade = ‘A’;
cout << “Good job!”;

}

16
New cards

FORMATTING OUTPUT

  1. setw(x): prints in a field at least x spaces wide

  2. fixed: displays floating point numbers in fixed point notation

  3. showpoint: causes a decimal point and trailing zeros to be displayed, even if there is no fractional part

  4. setprecision(x): when used with fixed, print floating point value using x-digits after the decimal

  5. << left: output left justified

  6. << right: output right justified

  7. \ escape character

cout << right << setw(6) << setprecision(2) << fixed << num;

17
New cards

WHILE LOOP

A loop allows us to write code that repeats code until a condition is met. Can be infinite if nothing makes the expression false.

while (expression) {

statement;

}

18
New cards

DO WHILE LOOP

A posttest loop, executes the loop first then tests the expression.

do {

statement;

} while (expression);

19
New cards

FOR LOOP

A more compact way of writing a while loop. Good when you know in advance how many times to repeat.

for (initialization; test; update) {

statement;

}

for (count = 10; count < 20; count++ {

cout << count;

}

20
New cards

HIERARCHY OF TYPES (+)

  1. Long Double

  2. Double

  3. Float

  4. Unsigned long

  5. Long

  6. Unsigned int

  7. Int

  8. Unsigned Char

  9. Char

*unsigned means no negative value

21
New cards

TERNARY CONDITIONAL OPERATOR

Most frequently used to perform an assignment

expr (tested) ? expr (done if true) : expre (done if false);

22
New cards
<p><strong>WHAT IS OUTPUT WHEN ‘F’ IS ENTERED?</strong></p>

WHAT IS OUTPUT WHEN ‘F’ IS ENTERED?

Report to the student union.

Report to the student attendance office.

Report to the science building.

23
New cards
<p><strong>WHAT IS OUTPUT WHEN ‘M’ IS ENTERED?</strong></p>

WHAT IS OUTPUT WHEN ‘M’ IS ENTERED?

Go home

24
New cards
<p><strong>WHAT IS OUTPUT WHEN ‘D’ IS ENTERED?</strong></p>

WHAT IS OUTPUT WHEN ‘D’ IS ENTERED?

Report to the small gym.

25
New cards
<p><strong>WHAT IS OUTPUT WHEN ‘I’ IS ENTERED?</strong></p>

WHAT IS OUTPUT WHEN ‘I’ IS ENTERED?

Report to the student attendance office.

Go home

26
New cards
<p><strong>GIVEN THE FOLLOWING CODE, HOW MANY TIMES IF “GOT ONE!” PRINTED?</strong></p>

GIVEN THE FOLLOWING CODE, HOW MANY TIMES IF “GOT ONE!” PRINTED?

2

27
New cards
<p><strong>GIVEN THE FOLLOWING CODE, FOR WHAT VALUES OF i WILL “GOT ONE!” BE PRINTED?</strong></p>

GIVEN THE FOLLOWING CODE, FOR WHAT VALUES OF i WILL “GOT ONE!” BE PRINTED?

6 and 9

28
New cards
<p><strong>CONSIDER THE FOLLOWING CODE SEGMENT. WHAT IS OUTPUT WHEN THE VALUE INPUT IS 15?</strong></p>

CONSIDER THE FOLLOWING CODE SEGMENT. WHAT IS OUTPUT WHEN THE VALUE INPUT IS 15?

AD

29
New cards
<p><strong>CONSIDER THE FOLLOWING CODE SEGMENT. UNDER WHAT CONDITIONS WILL ‘C’ BE PRINTED?</strong></p>

CONSIDER THE FOLLOWING CODE SEGMENT. UNDER WHAT CONDITIONS WILL ‘C’ BE PRINTED?

No conditions allow it

30
New cards
<p><strong>CONSIDER THE FOLLOWING CODE SEGMENT. WHAT IS OUTPUT WHEN THE VALUE INPUT IS 1?</strong></p>

CONSIDER THE FOLLOWING CODE SEGMENT. WHAT IS OUTPUT WHEN THE VALUE INPUT IS 1?

F