Computer Science I Midterm

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/35

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

36 Terms

1
New cards

What are the typical architecture and components of a computer?

The von Neumann architecture use a single storage structure to hold the set of instructions and data.

The CPU actually runs programs.

2
New cards

What are the different types of memory?

Main Memory: RAM (random access memory) - the computer can read or change in any order, usually stores working data and machine code, but requires power (volatile)

Secondary Storage: Direct-access data storage media, such as hard drives or CDs; sequential access data storage media, such as a tape. Is nonvolatile, and retains info after power is removed.

3
New cards

Compiled Language

must be translated to machine-readable instructions using compilers (human-readable source code becomes executable file); ex. C++

4
New cards

Interpreted Language

instructions are not precompiled for the target machine in a machine-readable form, and are assisted by an interpreter (source code becomes target code line by line); ex. Python, JavaScript

5
New cards

Compiled v. Interpreted Languages

Compiled: entire source code is translated to machine code before execution, binary code is platform-dependent, and code must be recompiled if changed

Interpreted: each source code command is translated to machine code and then executed, is platform-independent because interpreters themselves execute the source code, and there is no need for compilation

6
New cards

Dynamically Typed Language vs. Statically Typed Language

Dynamically Typed: no variable declaration, only variable assignments, type safety occurs at runtime (ex. Python)

Statically Typed: variables are declared, source code is analyzed to verify type safety

7
New cards

How to use std::cout with any number of outputs cascaded using the stream insertion operator <<?

std::cout « “phrase to be executed” OR

std::cout « variable «

8
New cards

How to use literals and variables in an output statement, including strings, characters, integers, floating points?

9
New cards

Block Comment v. Line Comment

Line Comment: used for short comments (//)

Block Comment: used for long comments and documentation (/*, */)

10
New cards

What are variables in a C++ program?

a named storage location in computer memory where you can store and manipulate data

11
New cards

Why do we use variables?

they keep values accessible and can be easily reused

variables give values context

12
New cards

What are the rules governing the naming of variables (or more generally identifiers) and the conventions typically used?

names can begin with an upper or lowercase letter, or an underscore

the rest of the name can have letters, digits (0-9), or underscores, but no special symbols

names are case sensitive, and some reserved C++ keywords cannot be used

13
New cards

How to declare and initialize a variable, and different ways to initialize a variable in declaration?

type of variable and then variable name

ex: int count, weight, height

14
New cards

How to swap the value of two variables?

in the case of swapping a and b:

declare a = value, b = value, and tmp (temporary placeholder variable)

tmp = a

a = b

b = tmp

15
New cards

How to declare variable using type inference?

type inference infers the type of variable you are declaring

ex: auto x = 5; x is inferred to be int

auto y = 3.14; y is inferred to be double

auto z = “Hello”; z is inferred to be char

16
New cards

Numeric literals

if a numeric literal is provided without a suffix:

  • if it does not have a decimal point, then it is treated as int

  • if it has a decimal point, then it is treated as double

17
New cards

type suffix in integer literals:

l or L: long

u or U: unsigned integer

ul or UL or lu or LU: unsigned long

ll or LL: long long

ull or ULL or LLU or llu: unsigned long long

18
New cards

type suffix in floating point literals:

f or F: float

l or L: long double

19
New cards

Integer Overflow

when you attempt to store a value in an integer variable that is outside of the range that can be represented by the variable- leads to undefined behavior

20
New cards

Arithmetic and Arithmetic Operators

addition (+)

subtraction (-)

multiplication (*)

division (/)

integer division (/) (rounded)

modulus (%) -remainder of a/b

21
New cards

Rounding

round to nearest, ties to even

ex: 1.5 =2 -1.5 = -2

2.5 = 2 -2.5 = -2

3.5 = 4 -3.5 = -4

4.5 = 4 -4.5 = -4

1.15 = 1.1

1.25 = 1.2

1.35 = 1.4

1.45 = 1.4

22
New cards

String Type

#include <string>

std::string variable

23
New cards

string sentencing

std::string variable

std::cout « “Statement”

std::getline(std::cin, variable)

std::cout«”echo of input”

24
New cards

String Literals

sequence of characters that together form a null-terminated string; enclosed between double quotation marks

ex: “Hello World!”

25
New cards

Escape Sequences

new line: \n

horizontal tab: \t

backslash: \\

double quote: \"

26
New cards

Concatenating String Literals

string literals can be directly concatenated:

std::cout «”statement”

27
New cards

Raw String

text is included exactly as written, including newlines and quotations

std::cout« R”(“…”)”;

28
New cards

Namespace/Resolving Scope of a Name/using directive

a namespace is a declarative region that provides a scope to the identifiers (names of types, functions, variables) inside it.

they organize code into logical groups to prevent name collisions

scope resolution: std::string variable1; ms::string variable2

using directive for a single identifier: using std::string; string variable 1; ms:: string variable2

using directive for all the identifiers in a namespace: using namespace std;, string var1, cout «”text”, ms::string var2

29
New cards

Operator Precedence Rules

start from left to right, but multiplication has higher precedence than addition.

<p>start from left to right, but multiplication has higher precedence than addition.</p><p></p>
30
New cards

Integer to/from signed or unsigned binary numbers

positive number to unsigned integer: divide integer by wo until you reach 0, read remainder from bottom to top, and pad 0 in front for a total of 8 bits

binary value of a negative integer: find binary representation of the positive value, invert the bits, and add 1 to the result.

31
New cards

Size of basic data types?

one byte is 8 bits, 256 diff values;

char: 1 byte

bool: 1 byte

int: 4 bytes

short: 2 bytes

long: 4 or 8 bytes

long long: 8 bytes

float: 4 bytes

double: 8 bytes

long double: 8, 12, 16

32
New cards

decimal precision of basic data types:

float = 6-7 decimal digits

double = 15 decimal digits

long double = 18 decimal digits

33
New cards

Integer and Floating Point Types/Range based on bytes/Precision

Floating Point: FP 32 (single precision), sign bit = 1, exponent width = 8, precision = 24

FP 64 (double precision), sign bit = 1 , exponent width = 11, precision = 53

34
New cards

precision

std::cout.setf(std::ios::fixed;

std::cout.setf(std::ios::showpoint)

std::cout.precision(number)

35
New cards

Implicit Type Conversions

in arithmetic operations, if operants are of mixed types, then the types of operants are matched, and the result is typically of the same type as the operant with a higher precision.

36
New cards

std::cin»

std::cin » variable