Programming Languages: Abstraction Levels, Paradigms, and C Fundamentals

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

1/95

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.

96 Terms

1
New cards

What are the 3 levels of abstraction of programming languages from high level to low level?

High level languages, assembly, machine language

2
New cards

How are they converted from high level to low level? (What are the names of these two "converters"

Compiler: HHL → ASM, Assembler: ASM → ML

3
New cards

Name and explain briefly the three programming language paradigms.

Object oriented - State and behavior are tied together in objects, hierarchical (ex. Java)

Imperative - Programs are sequences of statements that change program state step by step (ex. C)

Functional - Programs are composed of pure functions that map inputs to outputs without side effects. There is no state (ex. Haskell)

4
New cards

gcc -c program.c

Compile

5
New cards

./program

Run

6
New cards

gcc -o program program.o

Link

7
New cards

gcc -E program.c

See program after preprocessor

8
New cards

What is the basic unit of information in a computer?

Voltage, represented as 0 and 1 in digital systems.

9
New cards

What is machine language?

A low-level programming language that consists of binary code understood by a computer's CPU.

10
New cards

What does the instruction 'Addi $1, $2, 350' signify?

It adds the constant 350 to the value in register $2 and stores the result in register $1.

11
New cards

What is assembly language?

A low-level programming language that uses symbolic instructions to represent machine language.

12
New cards

What is the role of a compiler in programming?

It translates high-level language code into machine language for the computer to understand.

13
New cards

What is C in the context of programming languages?

C is a high-level programming language developed by Bell Labs in the 1970s, foundational for later languages like C++ and Java.

14
New cards

What are the characteristics of object-oriented programming?

Programs are collections of classes with encapsulated data types, bundling information and operations together.

15
New cards

What is imperative programming?

A programming paradigm where statements are commands that change the state of the program, often structured as a sequence of statements.

16
New cards

What defines functional programming?

Computation is treated as a series of function calls, producing reliable outputs from inputs without maintaining state.

17
New cards

What command is used to create a folder in a Unix-like system?

mkdir

18
New cards

What does the command 'gcc -c hello.c' do?

It compiles the source file hello.c into an object file without linking.

19
New cards

What is the purpose of the '#define' directive in C?

It defines a constant or macro that replaces occurrences in the code with a specified value.

20
New cards

What are the primary data types in C?

bool, char, int, float, double, and void.

21
New cards

What is a pointer in C?

A variable that stores the memory address of another variable.

22
New cards

What is the significance of the 'return 0;' statement in a C program?

It indicates that the program has executed successfully.

23
New cards

What does the command 'cp' do in a Unix-like system?

It copies files or directories.

24
New cards

What does the 'printf' function do in C?

It outputs formatted text to the standard output (usually the console).

25
New cards

What is the purpose of the 'int main()' function in a C program?

It serves as the entry point for program execution.

26
New cards

What does the 'void' data type indicate in C?

It specifies that a function does not return a value.

27
New cards

What is an array in C?

A collection of elements of the same data type, stored in contiguous memory locations.

28
New cards

What is the purpose of a for loop in C?

A for loop is used for iterating over a range of values, typically with a counter.

29
New cards

How does a while loop function in C?

A while loop repeatedly executes a block of code as long as a specified condition is true.

30
New cards

What does the if statement do in C?

An if statement executes a block of code if a specified condition evaluates to true.

31
New cards

What is the function of a switch statement in C?

A switch statement allows multi-way branching based on the value of a variable.

32
New cards

What is a nested for loop?

A nested for loop is a for loop inside another for loop, allowing for multi-dimensional iteration.

33
New cards

What are type specifiers in C?

Type specifiers define the data type of a variable, affecting its storage size and characteristics.

34
New cards

What is the difference between signed and unsigned integers?

Signed integers can represent both positive and negative values, while unsigned integers can only represent non-negative values.

35
New cards

What is the purpose of the static keyword in C?

The static keyword limits the visibility of a variable to the file it is declared in (global) or retains its value between function calls (local).

36
New cards

What are literal constants in C?

Literal constants are fixed values that do not change, such as integers, floating-point numbers, and characters.

37
New cards

What is implicit type conversion?

Implicit type conversion automatically converts one data type to another during assignment or operation.

38
New cards

What is explicit type casting?

Explicit type casting is the manual conversion of one data type to another using a cast operator.

39
New cards

What is the memory layout of a process in C?

The memory layout includes the stack, heap, BSS, data, and text segments.

40
New cards

What is the stack in memory layout?

The stack is used for static memory allocation and function call management, automatically managed by the system.

41
New cards

What is the heap in memory layout?

The heap is used for dynamic memory allocation, where memory management is the programmer's responsibility.

42
New cards

What is virtual memory?

Virtual memory abstracts physical storage, providing an illusion of a large, contiguous memory space.

43
New cards

What is the difference between segmentation and paging?

Segmentation is more space-efficient but less flexible than paging, which uses fixed-size pages for memory management.

44
New cards

What are the goals of an operating system kernel?

The goals include protection, abstraction, and resource management of processes.

45
New cards

What does the term 'CPU sharing' refer to?

CPU sharing refers to how multiple processes utilize the CPU, with good sharing ensuring efficient computation and resource use.

46
New cards

What is the significance of precedence and associativity in C?

Precedence determines the order of operations, while associativity defines the order of evaluation for operators of the same precedence.

47
New cards

What is the purpose of the `main` function in a C program?

It serves as the entry point for program execution.

48
New cards

What does the `#define` directive do in C?

It defines a macro that can be used to replace a specific value or expression throughout the code.

49
New cards

What is a null pointer?

A pointer that does not point to any valid memory location.

50
New cards

What is the difference between pass by value and pass by reference?

Pass by value copies the actual value to the function's parameters, while pass by reference passes the address of the actual parameters.

51
New cards

What is a memory leak?

It occurs when a program allocates memory but loses the reference to it, preventing deallocation.

52
New cards

What is a dangling pointer?

A pointer that points to a memory location that has been deallocated.

53
New cards

What does the `strcmp` function do?

It compares two strings and returns an integer indicating their lexicographical order.

54
New cards

What is the purpose of the `printf` function?

It outputs formatted text to the standard output.

55
New cards

How do you dynamically allocate memory in C?

Using functions like `malloc`, `calloc`, or `realloc`.

56
New cards

What is the significance of the `void*` type in C?

It is a generic pointer type that can point to any data type.

57
New cards

What is the result of dereferencing a null pointer?

It leads to undefined behavior, often causing a program crash.

58
New cards

How do you copy a string in C?

Using the `strcpy` function from the `string.h` library.

59
New cards

What is the purpose of the `return` statement in a function?

It exits the function and optionally returns a value to the caller.

60
New cards

What does the `&` operator do in C?

It retrieves the address of a variable.

61
New cards

What is the output of `printf("%d", &&*ptr);`?

It prints the value pointed to by `ptr`, effectively dereferencing it multiple times.

62
New cards

What is the role of the `char` data type?

It is used to store single characters.

63
New cards

What does the `sizeof` operator do?

It returns the size, in bytes, of a data type or variable.

64
New cards

How can you concatenate two strings in C?

Using the `strcat` function from the `string.h` library.

65
New cards

Name Data Types

Char, Double, int, void, float, void

66
New cards

. Write a full program that takes a command line argument which is a name, and prints "Hi, [name]!".

Example executing and expected output:

./program Miles

Hi, Miles!

#include

int main(int argc, char* argv[]) {

printf("Hi, %s!\n", argv[1]);

}

67
New cards

What is the output of the following program?

#include

#define PI 3.14

void makePIMoreAccurate() {

PI = 3.14159265358979;

}

int main() {

printf("PI: %f\n", PI);

makePIMoreAccurate();

printf("PI: %f\n", PI);

}

Errors! You cannot reassign a macro, because it's not a variable. Macros are like "find and replace"

68
New cards

What would this program look like after running: gcc -E program.c? and what would be the output

#include

#define A 5+4

#define B A*A

int main() {

printf("B = %d\n", B)

#include

int main() {

printf("B = %d\n", 5+4*5+4);

}

Output is 29

69
New cards

What is the output of the following program?

#include

int main() {

int a = -1;

int b = 0;

int c = -3;

printf("%d\n", ((a = 6) < b) + (b || c) * (!a + !!c) + ++a + b++);

}

0 + 1 * (0 + 1) + 7 + 0

= 8

70
New cards

Rewrite the following function to use a switch statement and explain why it's faster that way.

void foo(char grade) {

if (grade == 'A') {

printf("Excellent\n");

} else if (grade == 'B') {

printf("Good\n");

} else if (grade == 'C') {

printf("Average\n");

} else if (grade == 'D') {

printf("Poor\n");

} else if (grade == 'F') {

printf("Fail\n");

} else {

printf("Invalid grade\n");

}

}

void foo(char grade) {

switch (grade) {

case 'A':

printf("Excellent\n");

break;

case 'B':

printf("Good\n");

break;

case 'C':

printf("Average\n");

break;

case 'D':

printf("Poor\n");

break;

case 'F':

printf("Fail\n");

break;

default:

printf("Invalid grade\n");

}

}

It's faster because instead of checking each if statement, it jumps right to the corresponding case

71
New cards

You can exit a loop early using what?

break

72
New cards

You can skip to the next iteration of a loop using what?

continue

73
New cards

What is the output of the following code?

#include

int x = 5;

void foo(int x) {

printf("x = %d\n", x);

x = 0;

}

void bar(int y) {

printf("x + y = %d\n", x + y);

}

int main(void) {

printf("x = %d\n", x);

int x = 12;

foo(x);

bar(x);

}

x = 5

x = 12

x + y = 17

74
New cards

Why should you avoid using labels & gotos? When might it be a good time to use them?

Makes code less readable. Could maybe be used if you want to break out of multiple loops at once, but generally avoid using them.

75
New cards

What category do these specifiers go in?

unsigned

Sign

Makes a variable only use positive values, doubling its maximum value

76
New cards

What category do these specifiers go in?

Short and Long

Size

Short: Makes the variable shorter, saving memory, but not allowing as large values

Long: Makes the variable longer, taking more memory, and allowing larger values

77
New cards

What category do these specifiers go in?

Const

modifiability

Makes a variable immutable

78
New cards

What category do these specifiers go in?

Static(local)

Static(Global)

Access/Scope

Local: Makes a variable in a function maintain its value between function calls

Global: Makes a global variable private to that file

79
New cards

What's the output of this program? How would you fix it?

#include

int main(void) {

long int x = 1 << 40;

printf("%ld\n", x);

}

0 because the literal "1" isn't a long int, so shifting it left 40 makes it 0

Change the "1" to a "1l"

80
New cards

What's the output of this program?

#include

int main(void) {

int a = 5;

double b = 2.5;

char c = 'A';

double x = a / 2;

double y = a / 2.0;

int z = b + c;

int w = (int)b + (int)c;

double k = (double)(a / 2);

int m = b;

printf("%f %f %d %d %f %d\n", x, y, z, w, k, m);

}

2.000000 2.500000 67 67 2.000000 2

81
New cards

What is the output of the following program?

#include

int main(void) {

int x = 123;

int* ptr;

ptr = &x;

x = 321;

printf("%d\n", *ptr);

*ptr = 456;

printf("%d\n", x);

ptr = 0;

printf("%d\n", x);

}

321

456

456

82
New cards

17. After declaring an array like so:

int arr[] = { 1, 2, 3 };

What will the value of the variable arr be?

What will the value of *arr be

What is arr[i] shorthand for

The memory address of the 0th element

1

*(arr+1)

83
New cards

If You have a memory layout process of

Stack

Heap

BSS

Data

Text

Assign these local variables to one of the slots

Local variables | non-zero initialized global variables | instructions | dynamically allocated memory | uninitialized or zero'd global variables

In order is

Local variables, function calls;

programmer doesn't have to manage

Dynamically allocated memory;

programmer must manage themself

Uninitialized or zero'd global variables

Non-zero, initialized global variables

Instructions

84
New cards

Why do the stack and heap grow toward each other instead of having a defined division?

Because fixed divisions waste space. Growing toward each other maximizes usable memory.

85
New cards

________is when you add a constant to the virtual memory address to get the physical memory address, and then you check the bounds to ensure it's not accessing memory from another process.

Segmentation

86
New cards

The goal of virtual memory is to give each process the illusion of having ____, ______ and ______ memory

Large, private and contiguous

87
New cards

What is the kernel, and what are the three goals of the kernel?

The kernel is the core of an operating system.

Protection - processes can't use each other's memory

Abstraction - hide details of underlying hardware

Resource management - how processes share hardware

88
New cards

What is the difference between pass by value and pass by reference? Are pointers passed by value or reference?

Pass by value copies the value into the function parameter

Pass by reference passes the memory address of a variable into the function

Pointers themselves are still passed by value! (You're copying the memory address into the function). So everything is passed by value in C.

89
New cards

Write a function, swap, that allows you to pass in your integer variables by reference, and it swaps them for you.

void swap(int &a, int &b) {

int temp = a;

a = b;

b = temp;

}

90
New cards

Write a function swap_ptrs, that does the same thing as swap, but swaps two int pointers for you, instead of two ints.

void swap_ptrs(int* x, int* y) {

int temp = x;

x = y;

*y = temp;

}

91
New cards

What is the output of the following code?

#include

void setTo321(int* arr) {

int newArray[] = { 3, 2, 1 };

arr = newArray;

}

int main() {

int arr[] = { 1, 2, 3 };

printf("%d %d %d\n", arr[0], arr[1], arr[2]);

}

1 2 3

Side node: never create new arrays on the stack and return them like that!

92
New cards

Fix the function so that it sets to { 3, 2, 1 } correctly.

#include

void setTo321(int* arr) {

int newArray[] = { 3, 2, 1 };

arr = newArray;

}

int main() {

int arr[] = { 1, 2, 3 };

printf("%d %d %d\n", arr[0], arr[1], arr[2]);

}

void setTo321(int* arr) {

arr[0] = 3;

arr[1] = 2;

arr[2] = 1;

}

93
New cards

int main() {

char* c[] = { "Questions", "like", "this", "are", "very", "mean" };

char** c2 = c;

char** cp[] = { c+2, c+5, c+1, c+4, c, c+3 };

char*** cpp = cp;

printf( "%s\n", ((++cpp)) );

printf( "%s\n", (--((++cpp))) + 3 );

printf( "%c\n", ***(cpp - 1) );

printf( "%s\n", ((cpp + 2)) + 1 );

printf( "%c\n", ((*(cpp - 2)) + 2) );

printf( "%c\n", ((int)((c2++)))[1] );

printf( "%s\n", c2[1] );

mean

stions

m

uestions

i

t

this

94
New cards

Write a function, str_cpy(char src, char dst), that copies the src string into the dst string. Assume the dst string has already been allocated with sufficient space for src. Ex:

char src[] = "Hello World!";

char dst[100];

str_cpy(src, dst);

void str_cpy(char src, char dst) {

int i = 0;

while (src[i] != '\0') {

dst[i] = src[i];

i ++;

}

dst[i] = '\0';

}

95
New cards

What is the output of the following program?

#include

char* foo(int index, char c) {

static char str[] = "cat";

str[index] = c;

return str;

}

int main() {

foo(0, 'f');

char* result = foo(1, 'r');

foo(2, 'o');

foo(3, 'g');

printf("%s\n", result);

}

"frog" + whatever garbage was in memory after that point until it hits a 0, since we overwrote the '\0'

Ex: frog≈`°_JêDφ

96
New cards

How to manually copy a string in C

#include

#include

int main() {

char source[] = "Hello, world!";

char destination[100];

strcpy(destination, source);

printf("Source: %s\n", source);

printf("Copied: %s\n", destination);

return 0;

}

OR

#include

int main() {

char source[] = "Hello, world!";

char destination[100];

int i = 0;

while (source[i] != '\0') {

destination[i] = source[i];

i++;

}

destination[i] = '\0';

printf("Source: %s\n", source);

printf("Copied: %s\n", destination);

return 0;

}