CS21003 - Multi-Paradigm Programming

5.0(4)
studied byStudied by 1 person
5.0(4)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/116

flashcard set

Earn XP

Description and Tags

NEED TO ADD: stack, deq, file handling, all C++, hash tables

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No study sessions yet.

117 Terms

1
New cards

What is the file extension for files in C?

.c

2
New cards

C - get input function

scanf()

3
New cards

Format specifier for int

%d

4
New cards

Format specifier for float

%f

5
New cards

C - display output function

printf()

6
New cards

Format specifier for double

%lf

7
New cards

Format specifier for char

%c

8
New cards

Format specifier for string

%s

9
New cards

#include <> vs ““

<> is used for libraries and “" is used for header files

10
New cards

Is C an OOP?

No

11
New cards
int numbers[?][?] = {
	{1,2.3},
	{4,5,6}
};

Fill in the question marks

2 3

12
New cards

What is the keyword for setting a constant (at the top of file)

#define

13
New cards

Flag to rename .exe file when using gcc compilation

-o (gcc hello.c -o my-prog)

14
New cards

What do structs contain?

attributes

15
New cards

What is the include we need for printf?

<stdio.h>

16
New cards

How to guard header files?

#ifndef __FILE_H__

#define __FILE_H__

#endif

17
New cards

Write a hello world program in C

#include <stdio.h>
int main()
{
	printf("Hello World!\n");
	return 0;
}

18
New cards

Fill in the blanks (put commas between answers)

#....... (1) <stdio.h> 
.......... (2)
{
	printf("Welcome!\n").. (3)
}

include, int main(), ;

19
New cards

n++ vs ++n

The expression ++n increments n before its value is used, while n++ increments n after its value has been used.

(ex: int n =5;
x = n++; sets x to 5
x = ++n; sets x to 6)

20
New cards

What is a pointer?

A variable that contains the address of a variable

21
New cards

Declare a pointer with variable name: age and type: int

int *age;

22
New cards

Fill in the blank line

int myAge = 20;
//pointer age stored address of myAge
.................. 
printf("%d", *age);

int *age = &myAge;

23
New cards

Declare a pointer with variable name: name and type: char

char *name;

24
New cards

Find the error

char myName[] = "Maddie";
char *name = &myName;

printf("%s", name);

There should not be an & before myName

25
New cards

int a[10];


1. Defines an array 'a' of size 10
2. A block of 10 consecutive objects named a[0], a[1], ... , a[9]

26
New cards

Name an alternative to scanf()

gets(), fgets(), getline(), getchar(), getc(), fgetc()

27
New cards

What is the difference between strlen() and sizeof()?

sizeof includes the \0 char when counting

28
New cards

strncpy(char `destination, char `source, size_t n);

(` = *)

Copy up to ‘n’ chars from one string to another

29
New cards

strncmp(str1, str2, n)

Compare up to ‘n’ chars between 2 strings

30
New cards

What does strcmp return if both strings are the same?

0

31
New cards

strlen()

Get length of string

32
New cards

strdup()

Duplicate a string by making another copy of it in memory

33
New cards

What library do we need to include to use atoi/atof?

<stdlib.h>

34
New cards

What does atoi() do?

Converts string to int

35
New cards

What does atof() do?

Converts string to float

36
New cards

Is there an error if functions are declared before main() but defined after main()?

No

37
New cards

Create a struct ‘person’ with attributes ‘name’ (50 chars) and ‘age’

struct person{
	char name[50];
	int age;
);

38
New cards

Set int pointer ‘pInt’ to null then set it to the memory address of ‘num’

int *pInt = NULL;
pInt = &num;

39
New cards

What will this output?

int number = 10;

int *numPtr = &number;
*numPtr = 22;
printf("%d\n", number);

22

40
New cards

Find the error (what does it output?)

int a[5] = {0,1,2,3,4};
    
    for (int *p = a; p < a + 5; p++){
        printf("%d\n", p);
    }

p in the print statement should be *p (currently outputs memory addresses)

41
New cards

Fill in the blanks

.......... student {
    char name[20];
    int marks;
} Student;

typedef struct

42
New cards

How do you limit the number of characters a user can input?

Put a number between % and s (e.g. %99s)

43
New cards

Write ONE line of code in C to get an ‘ID’ of 10 characters or less

scanf("%10s", ID);

44
New cards

Fill in the blanks (comma between)

printf("The length of (1) is (2)", col, strlen(col));

%s, %zu

45
New cards
int cmp = strcmp(myStr1, myStr2)
if (.......){
	printf("SNAP!");
}

Fill in the blank

cmp == 0

46
New cards

What is the difference between static and dynamic memory allocation?

Static memory is allocated at compile time, has a fixed size, and is automatically freed whereas dynamic memory is allocated at runtime, can change size, and must be manually freed

47
New cards

Which header file needs to be included for dynamic memory allocation?

<stdlib.h>

48
New cards

Name memory allocation functions in C

malloc(), calloc(), free() realloc()

49
New cards

malloc()

Allocates a specified number of bytes of memory and returns a pointer to the first allocated byte

50
New cards

calloc()

Similar to malloc but initialises all of the bytes allocated to zero – useful when allocating arrays of numbers or other values

51
New cards

free()

Frees the memory allocated. You must free any memory that you allocate with malloc() or calloc()

52
New cards

realloc()

Can be used to resize a previously allocated space

53
New cards

Fill in the blank

int (1)ptr1 = malloc(size);

*

54
New cards

Fill in the blanks

newNode->(1) = pStack->top;
pStack->top = (2);

next, newNode

55
New cards

What is a graph?

A data structure that consists of a finite collection of vertices/nodes and edges

56
New cards

How are graphs represented in a computer?

As an adjacency matrix/adjacency list

57
New cards

What is an unweighted edge recorded as?

1

58
New cards

What is Breadth-First Traversal?

A graph traversal algorithm that explores all nodes at the current "level" before going through all deeper levels

59
New cards

Name 3 applications of BFS

Indexing web pages, GPS, social media friend recommendations

60
New cards

What is Depth-First Traversal?

A graph traversal algorithm that explores one node at the current "level" before going through all deeper levels of that node

61
New cards

Name 3 applications for DFS

Path finding, exploring dependencies, solving puzzles

62
New cards

What are the two main steps of implementing push operation in a stack?

top++ //increment top

stack[top] = push_value //put value in next available location

63
New cards

Steps for DFT

Choose start node (current_node)

Mark current_node as visited (node location)

Find first non-zero value in row current_node. If not visited, visit

Stack will keep record of nodes traversed

Make next node current_node (e.g. if node 0 then node 1 becomes current_node)

Repeat from 2

64
New cards

How to backtrack during DFT?

Pop current_node to backtrack to previous node

Go through new current_node to find path

If no new path backtrack further (new current_node)

65
New cards

What does this code do?

int visited[VERTEX_NUM];
for (inti i=0; i<VERTEX_NUM; i++)
     visited[i] = 0;

//An array to store the nodes we have visited already

66
New cards

What does this code do?

67
New cards

What does this code do?

top++;
stack[top] = node;

Adds a node to the stack

68
New cards

What does this code do?

node = stack[top];
top--;

Removes a node from the stack

69
New cards
70
New cards

What notation is file complexity expressed in?

Big O

71
New cards

What is the best, worst and average case for finding a number in an unsorted array?

Best: O(1)

Worst: O(n)

Average: O(n/2)

72
New cards

What is the best and worst case for conducting binary search?

Best: O(1) (first mid-point)

Worst: O(log n)

73
New cards

What is the best worst and average time complexity for a binary tree?

Best: O(1) (empty tree)

Worst: O(n) (unbalanced tree linked list basically)

Average: O(log n) (reasonably balanced)

74
New cards

What is the best and worst time complexity for a bubblesort?

Best: O(n) (list is already sorted)

Worst: O(n2)

75
New cards

What is an exponential complexity?

The complexity increases exponentially as ‘n’ increases - O(2n)

76
New cards

What is an example of a situation with exponential complexity?

Password cracking

77
New cards

Write code to open (and close) a file in write mode (no input val)

pointer var: fptr, filename: filename.txt

FILE *fptr;

fptr = fopen("filename.txt", "w");

fclose(fptr);

78
New cards

What line of code can be used to check a file (pointer var: fp) is created successfully?

if (fp != NULL)

79
New cards

Name the file access modes

“r”, “w”, “a”, “r+”, “w+”, “a+”

80
New cards

“r”

Open a file for reading. The file must exist.

81
New cards

“w”

Open a file for writing. The file is created if it doesn’t exist. If a file of the same name already exists, it is truncated (made empty)

82
New cards

“a”

Append data at the end of an existing file. The file is created if it doesn’t exist

83
New cards

“r+”

Open an existing file for reading and writing

84
New cards

“w+”

Open a file for reading and writing. The file is created if it doesn’t exist. If a file of the name name already exists, it is truncated (made empty)

85
New cards

“a+”

Open a file for reading and appending

86
New cards

C++ - display output function

cout

87
New cards

C++ - get input fuction

cin

88
New cards

What is the include we need for cout?

<iostream>

89
New cards

What is the file extension for files in C++?

.cpp

90
New cards

What is the namespace we need for cout?

std

91
New cards

Is C++ an OOP?

Yes

92
New cards

Write a hello world program in C++

#include <iostream>
using namespace std;

int main()
{
	cout << "Hello World!" << endl;
	return 0;
}

93
New cards

What does this code do?

for (int i = 0; i < w; i++){
        for (int j = 0; j < l; j++){
            cout << "*";
        }
        cout << "\n";
    }

Displays a rectangle of stars (w x l)

94
New cards

Fill in the blanks (comma between answers)

#include <iostream>
#include (1)
using namespace std;


int main()
{
    (2) file("file.txt");

    if (3()) {
        cerr << "Error opening file!" << endl;
        return 1;
    }

    if (file.4()) {
        cerr << "Serious error" << endl;
        return 1;
    }
    else if (file.5()) {
        cerr << "Non-fatal error" << endl;
        return 1;
    }
    else if (file.6()) {
        cout << "\nEnd of file reached." << endl;
    }

    if (file.7()) {
        cout << "File is good for I/O operations." << endl;
    }

    file.8();
    return 0;

<fstream>, ifstream, !file.is_open, bad, fail, eof, good, close

95
New cards

When is ‘cerr’ used?

To display an error message

96
New cards

The ___ function is the entry and exit point in a C/C++ program

main
97
New cards
___ and ___ are the compilers for C and C++ respectively
gcc, g++
98
New cards
One of the following is not a possible instance of an error occurring in C / C++: compilation, linking, runtime, debugging
Debugging
99
New cards
t/f 'string' is one of the built-in data types in C.
False
100
New cards
t/f It is possible to create new names for existing data types in C
True