ICS 212 - Final Exam

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What do you need for Polymorphism?

A parent and child class with an overridden method Child object Parent pointer assigned to child object Call to that method using a parent pointer "virtual" infront of method in header

What's the difference between shallow and deep copy?

A shallow copy only copies the pointer and not everything in the object which would cause a dangling pointer if you delete one. A deep copy, allocated new memory and then copies all of the objects and values which is safe but slower.

What are the 5 things the CPU does?

Reserve space in RAM Unreserve space in RAM Arithmetic operations (simplistic / limited) Read in RAM Write in RAM

What are somethings to keep in mind about #define?

#define is handled before the compile and is handled by the preprocessor and be a substitute for global variables with extern. However it's less safe because there's no type checking and no scope control.

What happens to #include statements when being compiled and after?

#include directives are handled by the preprocessor before the compiler even runs and just copies the contents of the library or the header file into the file.

What are the different elements of a loop?

Initialization, condition, update, and body

What are the parameters that fgets() and scanf() takes for input from files?

fgets(char name[], size, FILE *) fscanf(FILE *, "%(c, d, f, etc.)", &var)

What are the parameters that fgets() and scanf() takes for input from the user?

fgets(char name[], size, stdin) scanf("%(c, d, f, etc.)", &var)

What are some basic UNIX commands that I need to know off hand?

ls, cd, mkdir, rmdir, cat, man, grep, chmod

How do you run a makefile that has a custom name?

make -f name.mk

What was the java method that used polymorphism?

toString() so that it can print the object with system.out.println()

What's the difference between private, protected, and public?

Public let's everyone and every class access it, protected makes it so that the class and its child classes can access it, private makes it so that only that class can access it.

What's the difference between procedural programming and object oriented programming?

Procedural programming or top down programming is usually used for less complex projects because it starts with main and then goes step by step until the end result. While object oriented programming or bottom up programming usually starts with classes and objects then ends with main where those classes and objects are then implemented.

What does extern do? Why would you use it?

Allows you to share variables across multiple C or C++ files so that you don't have to allocate space for it in each file unnecessarily.

What is the difference between prototypes and function definitions?

Prototypes just declare the function to the compiler that the function exists and will be defined later and is usually in a header file and has no function body. While a function definition is the whole function.

What are the rules for fgets?

Reads all input in buffer and stops after the first newline or when the array has no space and puts everything in the arry plus the null character

What's the difference between run-time and compile-time?

Compile time occurs before the program runs and is handled by the compiler which checks for errors and turns it into object code or a .o file from a .c or C file. While run time occurs while the program is running which is handled by the CPU and/or the operating system.

What is dereferencing? What's the difference between int \*name, &name, and *name?

Dereferencing is when you use the address of a variable and then change the value of it with *name. &name is the address of the variable and int *name is the initializing of a pointer or the address of a variable.

How does input work behind the scenes?

Every single key switch clicked then gets stored into a buffer (an array of characters) which is then pushed onto the stack of a variable typically.

How do you compile a java file with main and a c file with a function that the java file calls?

First the java file compiles and creates a .class file then the C file is compiled into a library instead of an executable file. Then the Java file loads the file using JNI. It'll look something like this: javac -h name.java (which turns it into a name.class file) then in order to create a library from the C file you do gcc -I/location/include -I/location/include/linux -fPIC - shared name.c -o name.so. Then to execute the file you do java -Djava.library path=. name.

What's the difference between a static vs instance method?

For instance methods, they create this or like pointers so that you're able to use methods from classes using this-> however static methods cannot use this->.

Is it safe to return a reference from a function? When is it unsafe?

It's safe to return a reference when the original variable is defined or exists outside of the function. Otherwise the reference is dangling if it returns a reference of a local variable.

How do you compile a java file vs a C vs a C++ file?

Java: compile command → javac name.java run command → java name C: compile command → gcc -c name.c -o name.o link command: → gcc name.o -o name run command → /.name C++: compile command → g++ -c name.cpp -o name.o link command: → g++ name.o -o name run command → /.name

What's the difference between overloading vs overriding?

Overloading functions have the same name and scope but their parameters are different. While overriding functions same name and parameters (type and # of parameters) but the scope is different.

Why might you use a const reference?

You could use const reference when passing large objects without altering them basically giving the ability of passing by value without using space on the stack.

What are the rules for scanf?

Skips over leading whitespace in buffer (unless you use %c) and reads matching input in buffer and stops before the first non-match or before the first white space (space, tab, newline) in buffer.

What's the difference between dynamic arrays vs static arrays?

Static arrays are a fixed size as its memory is allocated on the stack which makes it quick to access. However dynamic arrays are stored on the heap and their size is determined at runtime, meaning that you can resize them using realloc using dynamic memory allocation.

What are static variables? And why would you use them?

Static variables are kind of like creating a global variable in the class so that the value can be preserved between functions or in functions. You would use a static variable instead of a global variable because it makes it so that it can't be accessed from outside of the file.

What are dependencies and targets?

Targets are the files that are going to be created like an executable or object file. While a dependency is the prerequisites that are necessary in order to run the body that must exist or be up to date.

What's the difference between actual vs formal parameters?

The actual parameters are the values (arguments) being sent in while the formal parameters are the variable names that are being received

What happens when you set a virtual void function to 0?

The compiler will know that it doesn't have a definition in the parent class making the class abstract and so if you try to create that object it will cause a compile-time error. But every child class must have that function, basically overriding it.

What is the difference between heap, stack, and buffers?

The stack contains local variables and function calls and doesn't need to be allocated or freed and size is limited (if goes over limit then stack overflow). A heap is when memory is dynamically allocated through malloc or new and must be manually freed or there will be a memory leak. A buffer is a temporary storage area for input and output and doesn't necessarily have a location.

What's the difference between variable definition and an assignment statement?

The variable definition doesn't initialize it, there's no equal sign while an assignment statement has an equal assign, assigning it to a value of some kind

When would a segmentation fault occur?

Usually when a program tries to access memory that it's not allowed to access, for example: array out of bounds, using a pointer that doesn't point to a valid address, or writing more into a buffer than it can (buffer overflow).

When would a memory leak occur?

Usually when using malloc and free isn't used to get rid of memory that is no longer required.

What is the difference between passing by value, by reference, and by pointer?

When passing by value you're passing a copy of the variable so that the function doesn't affect the original. When passing by reference it gives you the ability to change the original variable without taking up space on the stack. When passing by pointer you're passing the address of the original variable which allows you to alter the original variable but it takes up space on the stack.

When would a dangling pointer occur?

When you free or delete any memory that was dynamically allocated but there is a pointer that still points to it.

What happens when you modify a parameter passed by reference?

When you modify a parameter passed by reference you're modifying the variable from the parameter in a function without taking up space on the stack and without needing a pointer.


Kaugnay na mga set ng pag-aaral

Chem AP Classroom Midterm Review

View Set

Biol 2401, Chapter 16 cap questions

View Set

AD 102 Physiology and Pharmacology III

View Set