cmpt 214 part1: L00,L01,L02,L03,A1,Lab 1,L010 Flashcards

Midterm cmpt 214 part 1

1
Q

What is used to control access to a UNIX system?

A

A combination of a User ID and a password.

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

How is the UNIX filesystem structured?

A

It uses a hierarchical structure with directories and subdirectories.

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

What is meant by the UNIX filesystem being a single-root directory structure?

A

All files and directories stem from a single root directory, denoted by /. Every file or directory in UNIX can be traced back to the root.

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

How do you represent the absolute path of a file?

A

Absolute paths start from the root /, e.g., /home/user/file.txt.

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

What is the working directory in UNIX?

A

The working directory is the current directory where you are operating. You can find it using the pwd (print working directory) command.

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

What is an absolute path?

A

An absolute path starts from the root / and specifies the location of a file or directory fully, e.g., /home/user/documents.

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

What is the difference between external programs and built-in commands in UNIX?

A

Built-in commands are part of the shell itself (e.g., cd, echo), while external programs are separate executables stored in the filesystem (e.g., grep, ls, cat).

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

How can you check if a command is built-in?

A

Use the type command, e.g., type cd will tell you if cd is a shell built-in.

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

Command: let vs. $((…)) for Arithmetic
Q: How do you use let for arithmetic in UNIX?
Q: How do you use $((…)) for arithmetic in UNIX?

A

A: let is used for arithmetic evaluation. Example: let result=5+3 assigns 8 to the variable result.
A: $((…)) also performs arithmetic. Example: result=$((5 + 3)) assigns 8 to the variable result.

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

Redirection
Q: What is input redirection?
Q: What is output redirection?
Q: How do you append output to a file?

A

A: Input redirection takes input from a file instead of the keyboard. Example: command < input.txt.
A: Output redirection sends the output of a command to a file instead of the screen. Example: command > output.txt.
A: Use&raquo_space; to append output. Example: echo “Text”&raquo_space; file.txt.

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

Mounting Filesystems
Q: What does mounting a filesystem mean in UNIX?

A

A: Mounting means making a filesystem accessible under a directory. For example, mount /dev/sda1 /mnt mounts the /dev/sda1 partition at /mnt.

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

Pipelines
Q: What is a pipeline in UNIX?

A

A: A pipeline uses the | symbol to pass the output of one command as input to another. Example: cat file.txt | grep “keyword”.

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

Pipeline Example: tr
Q: How does the tr command work in a pipeline?

A

A: tr translates or deletes characters. Example: cat file.txt | tr ‘a-z’ ‘A-Z’ converts all lowercase letters to uppercase.

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

Recap of Key Spacing Rules:
Spaces required:
Between commands and their arguments.
Inside conditionals and loops.
Around comparison operators and arithmetic with expr.

A

**Spaces not allowed:
In variable assignments (x=5).
Inside arithmetic expansion ($((5+3))).
Between function names and parentheses (my_function()).

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

cat vs. echo
Q: What’s the difference between cat and echo?

A

A: cat is used to display file contents, whereas echo outputs text or the value of a variable to the terminal.

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

Command: cat
Q: What does the cat command do in UNIX?

A

: The cat command concatenates and displays the content of files.

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

Q: How would you use cat to display the content of a file named “file.txt”?

A

cat file.txt

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

Filesystem Layers
Q: Name the layers of the UNIX system.

A

A: Hardware, Drivers, Kernel, Utilities, and User Applications.

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

Q: What tools can be used to monitor processes in UNIX?

A

A: top and htop.

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

Command Structure
Q: What is the general format for UNIX commands?

A

A: command -[options] [arg1] [arg2] … [argn] RETURN.

(return-> keystroke for excution into the terminal)

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

Help and Manual Pages
Q: How can you display a help message for a UNIX command?
Help and Manual Pages
Q: How can you display a help message for a UNIX command?

A

: By using command –help.
:by using the man command (e.g., man ls)

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

Bash Scripting
Q: How do you make a Bash script executable?

A

A: By using the command chmod +x scriptname.sh.

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

Q: What is the purpose of #!/bin/bash in a Bash script?

A

A: It specifies that the script should run in the Bash shell.

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

Q: How do you assign and access variables in Bash?

A

A: Use = for assignment and $ for access (e.g., x=5, echo $x).

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

write an if-else statement in Bash

A

if [ condition ]
then
commands
else
commands
fi

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

while loop written in Bash

A

while [ condition ]
do
commands
done

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

Special Directories: . and ..
Questions:
What does the . directory represent in UNIX?
What does the .. directory represent in UNIX?
How do you move up two levels in the directory structure?
How would you list files in the current directory using .?
Can you execute a file in the current directory using .?

A

The . (dot) directory refers to the current directory you’re in. It is a shorthand way to refer to the working directory. For example, ./script.sh runs the script.sh file in the current directory.

The .. (double dot) directory refers to the parent directory of the current directory. For example, if you are in /home/user, using cd .. would move you to /home.

Use cd ../.. to move up two levels in the directory structure. Each .. moves up one level.

You can use ls . to list the files in the current directory, although simply typing ls without . also works.

Yes, to execute a script or program in the current directory, you prefix it with ./, like ./myscript.sh.

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

What are stdin, stdout, and stderr in UNIX?

A

These are special files that handle input (0), output (1), and error messages (2).

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

What is a computer program?

A

A computer program is a collection of instructions designed to solve a specific problem. These instructions form the steps of an algorithm.

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

Why are computers referred to as “dumb”?

A

Computers only perform operations they are instructed to do, not what we want. They execute simple instructions like addition or comparison.

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

What is an assembly language?

A

Assembly language is a low-level programming language where each statement corresponds to a specific machine instruction. It is not portable across different types of processors.

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

Why are high-level languages more portable than assembly languages?

A

High-level languages are machine-agnostic, meaning they can run on different systems because they don’t rely on the specific architecture of a processor. This is in contrast to assembly language, which is machine-dependent.

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

What’s an OS?

A

An operating system (OS) manages the computer’s hardware and software resources and handles the execution of processes. Examples include Unix, Linux, and Windows.

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

How does Unix differ in terms of portability?

A

Unix, being written in C and making few assumptions about the system’s architecture, has been ported to many different types of computers with minimal effort.

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

What are the steps in compiling a program?

A

Preprocessing
Compiling
Assembling
Linking These steps convert source code into machine code that can be executed.

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

What is the difference between compiling and linking?

A

Compiling: Converts the source code into assembly language or object code.

Linking: Combines the object code with libraries to produce the final executable

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

Why do we need to use ./ before executing a program in Unix?

A

In Unix, ./ is required to explicitly tell the system to run the program in the current directory (since the current directory is not part of the PATH environment by default).

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

purpose of the Makefile?

A

A Makefile automates the build process by specifying how to compile and link programs. It contains targets, dependencies, and recipes (commands) for building the software.

39
Q

What is a “target” in a Makefile?

A

A target in a Makefile is the file that is to be created (e.g., an executable or an object file). It is built using a set of instructions specified in the Makefile.

40
Q

What does the clean target do in a Makefile?

A

The clean target removes all files produced by the build process, usually with the rm command.

41
Q

the role of the .PHONY target in Makefiles?

A

The .PHONY target tells make that the targets like all and clean are not real files, preventing confusion if files with those names exist.

42
Q

: Explain the #include <stdio.h> line in C.</stdio.h>

A

This line includes the Standard Input Output (stdio) library, which allows the program to use functions like printf().

43
Q

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

A

It defines the main function of the program, where execution begins. It returns an integer value and takes no arguments (void).

44
Q

What does the printf() function do?

A

The printf() function outputs formatted text to the console. For example, printf(“x is %d”, x); prints the value of the variable x.

45
Q

compiling a C program using gcc?

A

the command gcc myprog.c -o myprog where myprog.c is the source file and myprog is the resulting executable.

46
Q

How is the %d specifier used in printf()?

A

%d is a format specifier used in printf() to print an integer value.

47
Q

Topic - Makefile Syntaxes

Explanation: A Makefile contains rules in the form of:

A

Target: The file you want to create.
Dependencies: Files that must exist before the target is built.
Recipe: Commands to execute, indented by a tab

Example:
target: dependencies
recipe

48
Q

Makefile Variables Allocation

Explanation: Variables in Makefiles store values that can be reused. They are assigned with = and referenced using $(VARIABLE_NAME).

A

CC = gcc
CFLAGS = -Wall
myprog: myprog.o
$(CC) $(CFLAGS) -o myprog myprog.o

(Here, CC is assigned the compiler gcc, and CFLAGS stores the flags passed to the compiler.)

49
Q

C Basics

A

Every program starts with the main() function.
Variables must be declared with a type (e.g., int, float).
Functions like printf() and scanf() handle input and output.
Statements are terminated with semicolons (;).
Memory is managed manually, and arrays, pointers, and structures are key data structures.

50
Q

7 Best Friends of Programmers

A

man: Displays manual pages for command-line tools.
make: Automates building processes with Makefiles.
find/grep: Search tools for finding files or patterns in files.
git/vcs: Version control systems for managing code history (e.g., Git).
gdb: A debugger for analyzing and controlling program execution.
awk/sed: Text-processing utilities for stream editing and data extraction.

51
Q

find: Searches for files in directories based on criteria like name or modification time.
grep: Searches within files for patterns using regular expressions.

A

find . -name “*.c”
grep “main” myprog.c

52
Q

git/vcs (Version Control Systems)

A

git init
git add .
git commit -m “Initial commit”
git push

53
Q

awk: Used for pattern scanning and processing. It reads input line by line, applies patterns, and performs actions.
sed: A stream editor that can search, find, and replace text

A

awk ‘{print $1}’ file.txt
sed ‘s/foo/bar/’ file.txt

54
Q

GDB and Its Commands

A

gcc -g myprog.c -o myprog
gdb ./myprog
(gdb) run
(gdb) break main # Break at the start of main()
(gdb) break 25 # Break at line 25
(gdb) next # to step over
(gdb) step #step into
(gdb) continue # to Resume execution after hitting a breakpoint.
(gdb) backtrace # to View the call stack to see the sequence of function calls.

55
Q

Data Types in C

A

int: Represents integers.
float: Represents floating-point numbers.
double: A floating-point type with double the precision of float.
char: Represents a single character.
_Bool: Represents boolean values (true or false) as of the C99 standard

56
Q

Constants

A

Constants are fixed values that do not change during the program execution. Examples include:

Integer: 58
Character string: “Programming in C is fun.\n”
Constant expressions consist entirely of constant values, e.g., 128 + 7 - 17.

57
Q

Integer Constants

A

12000 (valid)
12 000 (invalid)

The printf() function formats integers using %i or %d, and can display octal and hexadecimal with %o and %x respectively.

58
Q

Variable Storage and Ranges

A

Each data type has a defined range determined by its allocated storage. For example, int must be at least 16 bits but can vary based on the system (commonly 32 or 64 bits).

59
Q

Floating-Point Constants

A

A floating-point constant includes a decimal point. Examples include 3.0, 125.8, and -.0001. Use %f in printf() to display floating-point numbers.

60
Q

Floating-Point Scientific Notation

A

Floating-point constants can be expressed in scientific notation (e.g., 1.7e4). The mantissa precedes e, and the exponent follows. Use %e in printf().

61
Q

Extended Precision with double

A

The double type allows for approximately twice the precision of float, using 64 bits of storage. All floating-point constants are treated as double unless specified as float (e.g., 12.5f).

62
Q

:Considerations for Floats and Doubles- Precision and Representation

A

Floats and doubles are stored according to the IEEE 754 standard. Precision can affect calculations, as small differences may arise due to representation issues.

63
Q

Division (/)

A

int a = 20, b = 4;
int result = a / b; // result = 5
printf(“%d\n”, result);

64
Q

Character Constants - The char Type

A

A char variable stores a single character, represented in single quotes (e.g., ‘a’). It can also represent special characters like the newline character (‘\n’).

65
Q

Boolean Type in C

A

The _Bool type can only store values 0 (false) or 1 (true). It’s used for logical conditions, such as file reading status.

Insight:

66
Q

Type Specifiers in C

A

Type specifiers modify the storage size and range of variables:

long: Extended range (e.g., long int factorial;)
short: Smaller storage (e.g., short int age;)
unsigned: Only positive values (e.g., unsigned int counter;)

67
Q

Operator Precedence in C

A

Operators of the same precedence are evaluated based on their associativity (left-to-right or right-to-left).

68
Q

How does the size of an int in C influence the range of values it can store, and what implications does this have for variable selection?

A

The size of int can vary (commonly 32 or 64 bits).
Determines the range: at least ±32,768 for a 16-bit int and ±2,147,483,647 for a 32-bit int.

Implications:
Memory Efficiency: Use short for small numbers to save space.
Data Integrity: Choosing a type that fits data prevents overflow.

69
Q

Explain the difference between float and double, and provide an example of when you might prefer one over the other.

A

Difference:
float: 32 bits, ~7 decimal digits of precision.
double: 64 bits, ~15 decimal digits of precision.

Example:
Use float for graphics applications where performance is critical, and precision is less crucial.
Use double for scientific calculations requiring high precision.

70
Q

Modulus (%)

A

int a = 29, b = 5;
int result = a % b; // result = 4
printf(“%d\n”, result);

71
Q

What is a pointer in C?

A

A variable that stores the memory address of another variable.

72
Q

What does the & operator do?

A

It returns the memory address of a variable.

73
Q

What is the difference between a pointer and an array?

A

An array is a collection of elements, while a pointer holds the address of a single variable (or the first element of an array).

74
Q

Explain what NULL means in the context of pointers.

A

NULL is a special value that indicates a pointer does not point to any valid memory location.

75
Q

What is a dangling pointer?

A

A pointer that references memory that has been deallocated or released, leading to undefined behavior if accessed.

76
Q

What does pointer arithmetic allow you to do?

A

It allows you to navigate through an array by performing operations like addition or subtraction on pointer values.

77
Q

How do you initialize a pointer?

A

By assigning it the address of a variable, e.g., int *ptr = &variable;.

78
Q

What is a const pointer, and why is it used?

A

A const pointer is a pointer whose address cannot change after initialization, helping prevent accidental changes to what it points to.

79
Q

What does a double pointer represent?

A

A pointer to a pointer, which can be used to manage dynamic arrays or modify pointer values in functions.

80
Q

What is the purpose of the sizeof operator in pointer usage?

A

To determine the size of the type being pointed to, which helps in pointer arithmetic and memory allocation.

81
Q

Why is it important to free dynamically allocated memory?

A

to prevent memory leaks by releasing memory that is no longer needed back to the system.

82
Q

What does the const keyword signify when used with pointers?

A

It indicates that the data being pointed to should not be modified through that pointer.

83
Q

What does char * const charPtr = &c; signify?

A

charPtr is a constant pointer to a character, meaning its address cannot change, but the value at that address can.

84
Q

How can you declare a pointer that cannot change the value it points to?

A

Use const char *charPtr = &c; where charPtr cannot modify the value at the address it points to.

85
Q

What does const char * const charPtr = &c; represent?

A

Both the pointer and the value it points to are constant; neither can be changed.

86
Q

Why does valuesPtr = values; work without using the address operator?

A

The name of the array (values) acts as a pointer to its first element.

87
Q

What is the purpose of the strcpy1 function, and why is it incorrect for string literals?

A

It copies a string from src to dst, but it cannot handle string literals because they are stored in read-only memory.

88
Q

How can pointers facilitate in/out parameters in C functions?

A

Pointers allow functions to modify the original variable values, enabling multiple return values.

89
Q

In the greaterSum function, what does *largerArr = arr1; do?

A

It sets the pointer to point to the first array if it has a greater sum than the second array.

90
Q

What is a pointer to a pointer?

A

a variable that holds the address of another pointer, allowing multiple levels of indirection.

91
Q

how to use a function pointer to modify array values?

A

Pass a function pointer to a function that iterates through the array and applies the function to each element.

92
Q

final command to create a tarball of the submission files?

A

ex: tar -cvf le1.tar git.log org.sh

93
Q

to check if the git.log file is not empty?

A

test -s git.log; echo $?

94
Q

What bash construct is used to check if a directory exists in the script?

A

if [[ ! -d $target_dir ]]; then