General Flashcards

(16 cards)

1
Q

what is the structure for fprintf

A

fprintf(‘format string’, variables);

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

what are the specifiers for an integer, a floating point number, float with 2 decimal places, scientific notation, string and a new line

A

Specifier | Meaning |
| ————- | ———————————– |
| %d | Integer (decimal) |
| %f | Floating-point number |
| %.2f | Float with 2 decimal places |
| %e | Scientific notation (e.g.1.23e+03)|
| %s | String |
| \n | New line |

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

structure of for loop matlab

A

for variable = start_value : increment : end_value
% statements (code to execute in each loop)
end

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

structure of while loop matlab

A

while condition
% statements (code to execute repeatedly)
end

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

structure of if statement matlab

A

if condition
% statements if condition is true
elseif another_condition
% statements if the elseif condition is true
else
% statements if all above conditions are false
end

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

structure of function matlab

A

function [output1, output2, …] = functionName(input1, input2, …)
% function body (statements to perform)
end

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

List 5 of the ways you can make your code more readable and maintainable

A

Use Meaningful Variable and Function Names
Add Comments
Use Proper Indentation and Formatting
Avoid Hardcoding Values (Use Constants or Variables)
Break Code into Functions

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

Libraries make coding faster, easier, safer, and more powerful by giving you tools that are ready to use.
🔹 Roles of Libraries in Programs:
Provide Predefined Functions & Tools

Libraries contain useful functions (e.g., for math, plotting, file handling) that can be used directly.

Increase Development Speed

You can focus on solving your problem rather than writing low-level code from scratch.

Enhance Code Reliability

Libraries are usually well-tested and debugged by experts, reducing the chance of errors in your code.

Improve Code Readability & Maintainability

Complex tasks can be handled by a single library function, making code shorter and easier to understand.

Enable Access to Advanced Features

Specialized libraries offer capabilities like machine learning, image processing, or signal analysis, which would be hard to develop on your own.

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

examples of libraries matlab

A

imread imshow

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

What does it mean for a variable to be “in scope”? C

A

A variable is “in scope” when it is accessible or can be referred to directly in a certain part of the program. If a variable is in scope, you can use its name to read or modify its value. If it’s out of scope, the variable is not visible or accessible from that part of the code.

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

What determines the scope of a variable in C?

A

The scope depends on where the variable is declared — inside a function/block (local scope) or outside (global/file scope).

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

List and give an example of three different defensive programming practices C

A

Input Validation
What: Always check that inputs to functions or programs are valid before processing them.

Why: Prevents unexpected behavior or crashes due to invalid data.
heck for NULL Pointers
What: Always verify pointers before dereferencing to avoid segmentation faults.

Why: Prevents crashes caused by dereferencing invalid or uninitialized pointers.
Use Assertions
What: Use assertions to check assumptions during development, catching logic errors early.

Why: Helps identify bugs by verifying conditions that should always be true.

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

Explain the difference between: 1) an int and a long int Include in your explanation when you would use each. C

A

int:

Usually a 32-bit signed integer on most modern systems (can represent values roughly from –2 billion to +2 billion).

Used for whole numbers within a moderate range.

Requires less memory (usually 4 bytes).

long int:

Typically also 32 bits on some systems, but often 64 bits on modern 64-bit systems, allowing much larger integer values (up to about ±9 quintillion for 64-bit).

Used when you need to store very large integer values beyond the range of int.

Takes more memory (4 or 8 bytes depending on system).

When to use:

Use int for most whole number calculations when you are confident the values won’t exceed the typical 32-bit range.

Use long int when working with large integers, such as file sizes, counts, or IDs that can be very large.

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

difference between float and double

A

Difference between float and double
float:

Single-precision floating-point number, typically 32 bits.

Has about 7 decimal digits of precision.

Uses less memory and is faster on some hardware but less accurate.

double:

Double-precision floating-point number, typically 64 bits.

Has about 15–16 decimal digits of precision.

Uses more memory but provides much higher precision and range.

When to use:

Use float when memory is limited or performance is critical and precision is less important (e.g., graphics, embedded systems).

Use double for scientific calculations, financial applications, or whenever more accuracy and precision are needed.

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

Explain the difference between call-by-reference and call-by-value. Give an
example of each in your explanation. C

A

Call-by-Value
When a function is called by value, the actual value of the argument is copied into the function’s parameter.

Inside the function, you work with this copy, so any changes to the parameter do not affect the original variable outside the function.

This is the default behavior in C.

Call-by-Reference
When a function is called by reference, the function receives the address (pointer) of the variable.

Changes made inside the function directly modify the original variable.

C does not have native call-by-reference, but this behavior is simulated by passing pointers.

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