Exam Flashcards

1
Q

Is C strongly typed? Why?

A

No, C is weakly typed
You can convert between data types (eg casting pointers)

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

What is gcc?

A

GNU Compiler Collection is a compiler for C and C++

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

Is C statically or dynamically typed?

A

Statically - you must declare the datatype of a variable

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

How much memory does it require to store a character?

A

Unknown - variable memory space is machine specific

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

How do you store a boolean?

A

There is no boolean datatype in C
You have the equivalent by using 0 to represent False and any other number to represent True

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

What is the missing line, for ANSI standard C?

int main(){
     for ( i=0; i<10; i++ ){
	     /* some code */
     }
}
A
int i;

In ANSI standard, the iterative variable must be declared before use in the for loop

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

True or False, it is guaranteed that arrays are stored in contiguous memory?

A

True

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

Strings are just _____________

A

an array of characters

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

Which string function returns the length of the string?

A
strlen(string)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which string function copies a string ‘string1’ into ‘string2’?

A
strcpy(string2, string1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which string function concatenates string2 onto the end of string1?

A
strcat(string1, string2)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What string function would you use to determine if string1 and string2 were equal?

A
strcmp(string1, string2)

Note that this returns 0 (which means false) if the strings are equal

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

How can you use string functions in your C program?

A
#include <string.h>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are four uses for random numbers?

A

Simulations
Machine Learning
Cryptography/Codes
Games

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

What are some characteristics of rand()?

A

The number returned is an integer
The number is very large
The number is the same each time the program is run

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

What does this code do?

int i;
for (i=1; i<=10; i++){
 printf("number %02d: %f\n",i,10.0*float)rand()/RAND_MAX);
}
A

Prints 10 random numbers, between 0 and 10

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

How would you ensure that rand() gives different numbers every time a program is run?

A

You should seed the random number generator using:

srand(/*current time*/)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the best method for generating random numbers?

A

Using the GNU Scientific Library (GSL)

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

What is the definition of a function?

A

A function is:
- a uniquely named group of program statements
- which accepts zero or more arguments
- and returns zero or more values to the calling code

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

What is a function prototype? Why use them?

A

Functions must be declared before being defined

The compiler (which is one-pass) needs to know a function definition’s format before it is used

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

What is Scope?

A

Variables that are declared within a function’s scope are only accessible within the function

In general, it is very bad practice to use global variables/scope

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

What is the name for a function which does not return any values?

A

Procedure

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

What is a static variable? How are they used? Give an example use for it

A

Static variables are initialised once, when the function is first called. They retain their value when they go out of scope.

They are declared using the static keyword

An example use would be counting page numbers within a function called ‘turnPage’

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

What is the definition of a pointer?

A

A variable which stores the address of another variable

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

What does &x mean?

A

The location in memory (its “address”) where x is currently stored

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

What does *x mean?

A

go to the memory address stored in x and bring back the value stored there

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

What is a void pointer?

A

A void pointer can point to any datatype, but must be cast when used

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

What are two runtime errors? Why are they caused?

A

Memory Fault and Segmentation Fault

Trying to access memory which is not allowed access. Trying to access memory in a way which is not allowed

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

When using malloc(), what must you do?

A

Ensure that malloc() was successful
free() the memory when you no longer have need for it

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

What does malloc() do?

A

Dynamically allocates memory at runtime, returning a void pointer to the memory that was allocated

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

What does pass by value mean? What is the opposite? How could this be achieved?

A

Pass by Value - creates a copy of the value held into the function, the original value remains unchanged

The opposite is pass by reference. In C, In order to update the value of a variable within a different function, it must be passed as a pointer.

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

How would you pass arguments to the main function?

A
int main(int argc, char **argv){

argc - the number of arguments passed
argv - the array of string values

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

What is scanf()? What is it used for?

A

Returns the number of data items successfully assigned a value

It is like the reverse of printf(), almost like input() from python

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

What is a Stream?

A
  • An abstraction of a file
  • Is buffered
  • May be text or binary
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

How do you open a stream? What else must you always do?

A
file = fopen("fileName.txt", "fileMode");

You should always ensure that it was successful, and close the stream when are finished

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

In C, what are the three standard streams?

A

stdin - input from the keyboard
stdout - output to the terminal (normal messages)
stderr - output to the terminal (error/warning messages)

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

What is a compiler directive? Why use them?

A

An instruction to the compiler for an action which occurs before compilation

Used for:
* Giving your programs access to useful library functions and values
* Controlling what code gets compiled
* Defining macros to make your code more readable
* Defining macros for values that might change in the future

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

How would you define and use a macro?

A

#define TEN 10 can be used int a = TEN * 5;

it is common practice for the macro to be in uppercase

39
Q

What is a Macro?

A
  • You can define a macro and replace any instances of the macro name with its definition
  • It is standard practice for them to be written in uppercase
  • The compiler replaces instances of macros with their definitions before compilation
40
Q

What does typedef do? Why use it?

A

Creates an alias for existing datatypes
It allows for better code readability

41
Q

What are the four Storage Classes?

A
  • Auto/Default - (should be) declared at the start of a block, storage allocated when block is entered and freed when block is finished
  • register - variable is stored in CPU registers, cannot be pointed to since it has no memory address
  • static - Variable continues to exist even after the block (which it is in scope for) ends, its value is retained between repeated calls to the same function
  • extern - scope is global (usually bad practice)
42
Q

What should be in a .h file?
What should be in a .c file?

A

.h - #includes any required header files, defines macros and typedefs, declares function prototypes

.c - #includes the .h file, fully defines each function in the .h file, (probably) contains a main function

43
Q

What is a Structure?

A

A type of variable that groups multiple related data items together (the closest thing C has to a class)

44
Q

What is a Linked List? Why use them?

A

A data structure containing an ordered sequence of nodes which link to one another

Can be used if you don’t know how many elements an array needs to hold; They are much more memory-efficient than inserting elements into arrays (and don’t have to be contiguous)

45
Q

What are the three layers of UNIX?

A

Kernel - purely for interacting with the hardware
Shell - user logs in to interact with the kernel
Programs - interact with the shell

46
Q

Every process has:

A
  • A unique PID (process identifier)
  • exactly one parent process
  • zero or more children processes
47
Q

What is Foreground and Background

Refering to processes

A

Foreground - your session waits until the process has finished
Background - you can continue entering commands while the process runs; the process is killed if the parent process finishes

48
Q

What does fork() do?

A

Creates a child process as a duplicate of the parent, returning the PID of the child

The parent and child share everything from before the fork() command, including variable values, stdout, etc

49
Q

What is a pipe?

A

a unidirectional (one-way) flow of data between two processes

50
Q

What are the three methods of communicating between processes?

A
  • Signals - need to know the PIDs, no data can be transferred, the receiving process doesn’t have to keep checking
  • Files - one processes writes into a file while the other reads from it, data can be transferred, the receiving process has to keep checking
  • Pipes - the processes must be parent-child/siblings, data can be transferred, receiving process has to keep checking
51
Q

What is the Virtual Address Space?

A
  • Each process runs in its own lump of memory
  • The virtual memory space starts from address 0
  • The memory space is usually contiguous
  • The OS maps the virtual address to actual, physical addresses (using page tables)
52
Q

What are the benefits of using Virtual Address Space?

A
  • Transparency - processes are unaware of virtualisation
  • Protection/Isolation - processes cannot interfere with one another
  • Flexible Memory Management - the OS can move things around as required
  • Shared Memory
  • Dynamic Memory Allocation - a process can request for its memory to be increased
  • Paging - gives the illusion of unlimited memory
53
Q

What is MMU?

Virtual Memory Space

A

Memory Management Unit (in the CPU)

54
Q

What is TLB?

Virtual Memory Space

A

Translation Lookaside Buffer - a cache of recently used address mappings

55
Q

What does setjmp() do?

A

Saves the contents of the registers to a buffer

56
Q

What does longjmp() do?

A

Restores the contents of the registers from a buffer

57
Q

What is the difference between Subroutines and Coroutines?

A

Subroutines - run to completion and then return to the same place they were called from

Coroutines - can switch between functions at multiple different points (they are concurrent, but not parallel)

58
Q

C++ is nearly a superset of C, what features does it add?

A
  • Classes
  • Function + Operator Overloading
  • Exception Handling
  • Default Parameter Values
  • Pass By Reference
59
Q

What is a precompiler?

A

A precompiler modifies the source code for the compiler

60
Q

What is function overloading? How is it used?

A

When two or more functions use the same name
Each function must have either:
* a different number of parameters
* different parameter types

61
Q

In C++, what do new and delete do?

A

new - malloc()s memory for the object, then calls the constructor
delete - calls the destructor for the object, then free()s the memory

62
Q

What does this mean, in C++? When should it be used? What is the Objective-C equivalent?

A

this points to the current object - to avoid ambiguities, to allow objects to be passed to/from functions
self is the Objective-C equivalent

63
Q

What are Static Member Variables?

A

Variables which are shared by all instance members of a class
They can be accessed even if no objects of the class exist

64
Q

Does C++ allow inheritance?

A

Yes, classes in C++ can inherit from base classes. In fact, C++ allows Multiple Inheritance, from any number of base classes

65
Q

When are the base and derived class constructors/destructors called?

A

Constructors are executed in order of derivation
Destructors are executed in reverse order of derivation

66
Q

How can parameters be passed to base class constructor functions?

A

The constructor of the derived class can call the constructors of the base classes

67
Q

What is a friend function? Is friendship inherited?

C++

A

A non-member function (defined outside of the class) which can access private and protected elements of the class (such as variables)

Friendship is not inherited nor transitive

68
Q

What is Operator Overloading? What is its intended purpose?

A

Creating more than one ‘meaning’ of an operator
They are to make life easier for users of a class, not to make shortcuts for developers

69
Q

What is a Function Template? How do you create one?

A

Instructions for how to build a family of similar looking functions (usually for parameters to be each datatype)

template<dataname T>
void sum (T a, T b){
     T sumAB = a + b;
}
70
Q

What is a Class Template? How do you create one?

A

Instructions for how to build a family of similar looking classes (usually for parameters to be each datatype)

template<typename T>
class Stack {
    T stck[SIZE];
    int index;
  public:
    static int count;
	Stack();
	Stack(Stack &);
	~Stack();
	void push(T);
	T pop(void);
}
71
Q

Some data structures, such as stacks/queues, are so widely used that C++ provides a set of standard templates through the _____?

A

Standard Template Library

72
Q

C++ allows for exceptions, how can this be done?

A

By doing:
~~~
try{
throw 20; // raises exception with integer 20
}
/* for specific exception type /
catch (int e){
printf(“Exception: %d\n”, e);
}
/
for any exception */
catch (…){
printf(“Exception”);
}
~~~

73
Q

What does typeid(x) do, and how could you use it?

A

Returns the type of object x
Could be used for:
~~~
if ( typeid(x) == typeid(MyClass) ) {
cout &laquo_space;“x is MyClass” &laquo_space;endl;
}
~~~

74
Q

What are the C++ equivalents of the standard streams in C?

A

std::cin - equivalent to stdin
std::cout - equivalent to stdout
std::cerr - equivalent to stderr

(Including using namespace std; at the beginning of your program removes the need for the std:: in the streams)

75
Q

Explain Little-Endian vs Big-Endian. Which is Linux?

A

Little Endian - Least significant (smallest) digit is first, eg 321 represents 3 + 20 + 100 (=123)
Big Endian - Most significant (largest) digit is first, eg 123 represents 100 + 20 + 3 (=123)

Linux is Little-Endian

76
Q

What is the benefit of Little-Endian?

A

Using a little-endian system is more efficient for type conversions, since the pointer (to the starting memory address) does not have to change when the size of the variable changes

77
Q

What does const int x mean?

A

That the value of x cannot change after initialisation, any attempt to do so results in a compile-time error

78
Q

What is a Variadic Function? What is/isn’t acceptable?

A

Functions which can accept a variable number of arguments
They must have at least one name parameter (which must come before …):
int badFunction (...) is not allowed
int goodFunction (int a, ...) is allowed

79
Q

Is C++ a Strict Superset of C? Is Objective-C?

A

C++ is not a superset of C (but it is nearly)
Objective-C is a strict superset of C

80
Q

What are two object-oriented frameworks for Objective-C?

A

Foundation - classes for basic data structures and for interacting with the OS
AppKit - classes for developing applications, windows, buttons, etc.

81
Q

Why use #import instead of #include?

In Objective-C

A

#import includes an implicit #import guard

82
Q

Is this C, C++, or Objective-C?

MyObject *obj = [MyObject new];
[obj callWith:parameter1 and:parameter2];
A

Objective-C

83
Q

Is this C, C++, or Objective-C?

myFunction(parameter1,parameter2);
A

C

84
Q

Is this C, C++, or Objective-C?

MyObject *obj = new MyObject;
obj->myMethod(parameter1,parameter2);
A

C++

85
Q

What is a Wrapper Class? Provide an example

Objective-C

A

a class which wraps one or more primitive data types, eg NSNumber, NSString, etc.

86
Q

Is “All objects are pointers” a true statement for C++ or for Objective-C?

A

It is true for Objective-C

Memory for objects is always allocated in the heap: NSNumber *num
not in the stack: NSNumber num (this gives a compile-time error)

87
Q

What is this called?

BOOL b = YES;
NSNumber *num1 = @43;
NSNumber *num2 = @'A';
NSNumber *num3 = @91.6;
NSNumber *num4 = @b;

NSLog(@"%@, %@, %@, %@,\n", num1, num2, num3, num4);
// 43, 65, 91.6, 1
A

Literal Syntax

88
Q

What do the + and - mean? Is age public or private?

@interface Person: NSObject 
{
	int age;
}
- (void)setAge:(int)age;
\+ (void)sayHello;
@end
A

+ - Class Methods
- - Class Instance Methods

age is private (since this is the default for Objective-C)

89
Q

What are the constructors and destructors for Objective-C?

A

Constructor:
* method name always starts with init
* called after alloc

Destructor:
* always called dealloc
* always called automatically by the runtime; never explicitly called

90
Q

What does (id) mean, in Objective-C?

A

(id) is a datatype which points to any type of object (similar to a void pointer, in C). It supports dynamic referencing (object type is defined at runtime)

91
Q

What is MRR?

A

Manual Retain-Release

  • you explicitly manage memory by keeping track of how many owners an object has
  • implemented using reference counting model
92
Q

What is Reference Counting?

A
  • You create an object - reference count is set to 1
  • You claim ownership of an object - reference count increases by 1
  • You relinquish ownership of an object - reference count decreases by 1

An object exists while the reference count is greater than 0
Object is destroyed (at some point by the garbage collector) when the reference count becomes 0

93
Q

What is ARC?

A

Automatic Reference Counting

does not run in gcc, needs to be switched on in Xcode. It works the same way as manual reference counting, except:
* it automatically inserts appropriate retain and release calls
* means you must not explicitly call retain, release, or autorelease
* means you, usually, do not need a custom dealloc method

94
Q

What is Memory Leakage?

A

When memory, which has been allocated (via malloc()), is no longer accessible - usually caused by a variable going out of scope before being freed