Embedded Interview Questions
Embedded Interview Questions
1. What is static in C?
Static variable is one which maintains its value throughout the function invocations.
a. A static variable inside a function keeps its value between invocations.
b. A static global variable or a function is "seen" only in the file it's declared in.
BASIS FOR
ARRAY LINKED LIST
COMPARISON
When you free your pointer, it uses that address to find the special information it
added to the beginning (usually) of your allocated block. If you pass in a different
address, it will access memory that contains garbage, and hence its behaviour is
undefined (but most frequently will result in a crash)
7. What is the difference between class and object? does class or object create memory?
basically learn every detail about classes and objects and just the definition.
a. Classes are static, they just define properties of the object
b. When a class is instantiated, it is an object and takes up memory
Class: The building block of C++ that leads to Object Oriented programming is a Class. It is a
user defined data type, which holds its own data members and member functions, which
can be accessed and used by creating an instance of that class. A class is like a blueprint for
an object.
An Object is an instance of a Class. When a class is defined, no memory is allocated but
when it is instantiated (i.e. an object is created) memory is allocated.
Examples: If channel is a class, Star Sports, BBC, and ESPN are its objects.
a class "CAR" : Its objects are Hyundai, Ford, Suzuki. It will have the same methods but
different designs -> this is how you can relate objects and classes with the real world.
8. what are virtual functions? How are they used? Why are they used? When are they
used? Example?
a. To achieve runtime polymorphism
b. They are defined in base classes - and overridden in derived classes
c. For example, input vehicle...but don’t know if car, bus, or truck
A virtual function is a member function which is declared within base class and is re-
defined (Overridden) by derived class. When you refer to a derived class object using a
pointer or a reference to the base class, you can call a virtual function for that object and
execute the derived class’s version of the function.
● Virtual functions ensure that the correct function is called for an object,
regardless of the type of reference (or pointer) used for function call.
● They are mainly used to achieve Runtime polymorphism
● Functions are declared with a virtual keyword in base class.
● The resolving of function call is done at Run-time.
9. How is multiply is implemented?
a. Set result to 0
b. Repeat
c. Shift 2nd multiplicand left until rightmost digit is lined up with leftmost 1 in
first multiplicand
i. Add 2nd multiplicand in that position to result
ii. Remove that 1 from 1st multiplicand
d. Until 1st multiplicand is zero
e. Result is correct
10. Design an elevator system
a. I started from thinking about how many buttons (including buttons inside
the elevator, and outside for every floor) should be tracked in this system.
b. After that, considered how to implement an ISR (interrupt service routine)
and what data structure should be used (maybe a queue for the next targeted
store, and a array for status of all buttons) to track them, and use these
information to control the elevator.
11. How post increment works.
a. You use the value first and then increment it.
12. One questioner asked how to modify a malloc to guarantee that it was 32-byte
aligned
a. ptr & ~0x1F will set the last 5 bits to 0, making it 32 byte aligned
13. How do breakpoints in a C program work?
a. http://www.nynaeve.net/?p=80
14. How does a debugger work?
15. ISR & interrupt vector table
a. ISR addresses stored in vector table
16. Float to int conversion
17. Memory map of program
a. Output from linker script
b. Bss is uninitialized data
c. Data is initialized data
d. Text is names, symbols etc
18. If we declare more number of variables than the registers available on the
processor? Where they will be stored.
a. In some memory block and they are loaded back appropriately into registers
b. Have to remove something that’s in memory at that point
19. How to handle the Generic functions , like Void pointers
a. Function that accepts void pointers should only accept one type
20. Data Struct/Class memory padding
a. // char 1 byte
b. // short int 2 bytes
c. // int 4 bytes
d. // double 8 bytes
e. // pointer 4 bytes in 32 bit machine, 8 in 64-bit
f. Memory is padded to nearest multiple
g. For optimizing memory accesses for byte addressable memory
21. Difference between Macro and Inline?
a. Macros are used for repeating functions throughout the code, so you use
#define at the top and the preprocessor typically takes care of this
b. Inline functions tell the compiler that the function is also defined at that
spot -so you save time by not jumping to another location, jumping back,
c. Inline functions provides following advantages over macros.
● Since they are functions so type of arguments is checked by the
compiler whether they are correct or not.
● There is no risk if called multiple times. But there is risk in macros
which can be dangerous when the argument is an expression.
● They can include multiple lines of code without trailing backslashes.
● Inline functions have their own scope for variables and they can
return a value.
● Debugging code is easy in case of Inline functions as compared to
macros.
22. How to avoid overflow/underflow
a. Use types like long double for more precision and more bits
23. How do you send data over the network between two machines with different
endianess
a. https://www.ibm.com/developerworks/aix/library/au-endianc/index.html
b. https://barrgroup.com/Embedded-Systems/How-To/Big-Endian-Little-
Endian
Coding
General
SPI
I2C
● SDA/SCL
● Synchronized to the clock signal
● Start + Address Frame + ACK + Data Frames + ACK + Stop
● Both ways transfer
CAN
4G/LTE
5G
Sorting Algorithms
Bubble Sort O(n^2) O(n^2) O(n^2) O(1) O(1) O(1) completes some pre sorting
Adaptive
O(n^2) O(n^2) O(n^2) O(1) O(1) O(1)
Insertion
Bucket Sort O(n) O(n) O(n) O(1) O(1) O(1) good for small universes
1. Code Segment
● The code segment, also referred as the text segment, is the area of
memory which contains the frequently executed code.
● The code segment is often read-only to avoid risk of getting
overridden by programming bugs like buffer-overflow, etc.
● The code segment does not contain program variables like local
variable (also called as automatic variables in C), global variables, etc.
● Based on the C implementation, the code segment can also contain
read-only string literals. For example, when you do printf("Hello,
world") then string "Hello, world" gets created in the code/text
segment. You can verify this using size command in Linux OS.
● Further reading
Data Segment
The data segment is divided in the below two parts and typically lies below the
heap area or in some implementations above the stack, but the data segment
never lies between the heap and stack area.
4. Stack Segment
5. Heap Segment
● This segment is to support dynamic memory allocation. If the
programmer wants to allocate some memory dynamically then in C it
is done using the malloc, calloc, or realloc methods.
● For example, when int* prt = malloc(sizeof(int) * 2) then eight bytes will
be allocated in heap and memory address of that location will be
returned and stored in ptr variable. The ptrvariable will be on either
the stack or data segment depending on the way it is declared/used.