Latest Embedded Systems Interview Questions
1. What is pass by value and pass by reference? How are structure passed as arguments?
The parameter to a function can be a copy of a value that is represented by variable or can be a reference to a memory space that stores value of variable.
The former is referred to as pass by value and the latter is referred to as pass by reference. The difference is that when parameters are passed by value the changes made to the variable value within the function is not reflected in the caller function, but when passed as reference changes are reflected outside the called function.
The structures are always passed by reference.
2. What is difference between using a macro and a in line function?
The macro are just symbolic representations and cannot contain data type differentiations within the parameters that we give. The in line functions can have the data types too defined as a part of them. The disadvantage in using both is that the inclusion of condition checks may lead to increase in code space if the function is called many times.
3. What is the volatile keyword used for?
The volatile keyword is used to represent variables that point to memory in other mapped devices. In such a case the value of the variable can be changed outside of a program. The compiler does not do additional optimizations to the code if there is volatile keyword.
4. What are hard and soft Real time systems?
The hard real time systems are the once that depend on the output very strictly on time. Any late response or delay cannot be tolerated and will always be considered a failure. The soft real time systems on the other are not very rigid as the hard real time systems.
The performance of the system degrades with the lateness of response, but it is bearable and can be optimized to a certain level for reuse of the result.
5. What is a semaphore? what are the different types of semaphore?
The semaphore is an abstract data store that is used to control resource accesses across the various threads of execution or across different processes.
There are two types of semaphores:
• The binary semaphore which can take only 0,1 values. (used when there is contention for a single resource entity)
• The counting semaphore which can take incremental values to certain limit (used when number of resources is limited).
6. Write a constant time consuming statement lot finding out If a given number Is a power of 2?
If n is the given number, then the expression (n & (n-1)) = 0 gives the logical output depicting if it is a power of 2 or not, if (n & (n-1) == 0) printf (“The given
number is a power of 2”);
7. What are recursive functions? Can we make them in line?
The recursive functions refer to the functions which make calls to itself before giving out the final result. These can be declared as in-line functions and the compiler will allocate the memory space intended for the first call of the function.
8. What is the size of the int, char and float data types?
The size of the char and int are always dependent on the underlying operating system or firmware. This is limited to the number of address lines in the address bus.
The int usually takes up a value of 2 bytes or 4 bytes. The char can take up a space of 1 or 2 bytes. The float data type takes up a value of 4 bytes.
9. What does malloc do? What will happen if we have a statement like malloc(sizeof(0));
Malloc is the function that is used for dynamically allocating memory to the different variables. The malloc returns a memory pointer of void type (void *). The statement malloc(sizeof(0)) returns a valid integer pointer because sizeof(0) represents the size of memory taken up by the integer value of 0.
The memory allocated by memory is not automatically cleaned up by the compiler after execution of the functions and should be cleaned up by the programmer using the free() function.
10. What is meant by a forward reference in C?
The forward reference refers to the case when we point an address space of a smaller data type with a pointer of a bigger data type This can be pictured as allocating memory in single bytes and accessing it with integer pointer as chunks of 4.
11. What is the order of calling for the constructors and destructors in case of objects of inherited classes?
The constructors are called with base class first order and the destructors are called in the child first order. That is, the if we have 2 levels of inheritance A (base)-> B (inherit 1)-> C (inherit 2) then the constructor A is called first followed by B and C. The C destructor is called first followed by B and A
12. Explain the properties of a Object oriented programming language.
• Encapsulation: The data that are related to the specific object are contained inside the object structure and hidden from the other entities of the environment
• Polymorphism: The mechanism by which the same pointer can refer to different types of objects, which are basically linked by some generic commonality.
• Abstraction: Hiding the data and implementation details from the real objects. The framework of reference is still present to be used by the other objects.
• Inheritance: The way to take out the common features and have them as separate object entities only to be reused by the other objects in a modular fashion.
13. What do you mean by interrupt latency?
Interrupt latency refers to the time taken for the system to start the handler for the specific interrupt. The time from the time of arrival of interrupt to the time it is being handled.
14. What typecast is applied when we have a signed and an unsigned int in an expression?
The unsigned int is typecast into the signed value.
15. How are variables mapped across to the various memories by the C compiler?
The compiler maintains the symbol table which has the related information of all the variable names along with the length of the allocated space, the access unit length for the pointer (type of pointer) and the starting address of the memory space.
16. What is a memory leak? What is a segmentation fault?
The memory leak refers to the uncleared memory mat builds up across me lifetime of the process. When it comes to a huge value me system stalls its execution due to me unavailability of the memory. The segmentation fault on the other hand refers to me condition when our program tries to access a memory space that has already been freed up.
17. What is ISR? Can they be passed any parameter and can they return a value?
ISR refers to the Interrupt Service Routines. These are procedures stored at specific memory addresses which are called when certain type of interrupt occurs. The ISRs cannot return a value and they cannot be passed any parameters.
18. a=7; b=8; x=a++-b; printf(“%d”, x ); What does this code give as output?
The compiler understands the statement expression a–b by taking off as much operators as it makes sense to a variable. So (a++) is taken as a parameter and then the expression becomes 8-8 which in turn gives the x value as 0. Thus the output value is 0.
19. What are little endian and big endian types of storage? How can you identify which type of allocation a system follows?
The little endian memory representation allocates the least address to the least significant bit and the big endian is where the highest significant bit takes up the least addressed memory space. We can Identify the system’s usage by defining an integer value and accessing it as a character.
int p=0x2;
if(* (char *) &p == 0x2) printf (“little endian\n”); else printf (“big endian\n”);
20. What is the scope of a function that is declared as static?
The static function when declared within a specific module is scoped only in that module and can only be accessed from it.
21. What is a ‘volatile’ variable?
Volatile is a keyword to specify the compiler that this variable value can change any time, so compiler should always read its value from its address, and not to use temporary registers to store its value and use in later part of the code.
This is especially important to handle the memory mapped registers which are mapped as some variables or structures in embedded systems, such as hardware status registers etc, whose value can be changed anytime, depending on the hardware state.
Examples of volatile variables are,
• Hardware registers in peripherals (for example, status registers)
• Non-automatic variables referenced within an interrupt service routine
• Variables shared by multiple tasks in a multi-threaded application
22. Can a volatile be constant? Like
volatile const a;
Yes it can be, it means that, it can be changes by hardware state change, but its read only register in hardware, so code should not try to modify it.
23. Can a pointer be volatile ? Explain.
Yes, although this is not very common. An example is when an interrupt service routine modifies a pointer to a buffer
24. What is a ‘const’ variable?
In simple terms ‘const’ means ‘read only’. Its value is not changed by any part of the code executed. So compiler can optimize by taking this info, and also it will give warning when user try to change the value of this variable by mistake.
If the candidate gets the answer correct, I’ll ask him these supplemental questions:
25. What do the following declarations mean?
const int a;
int const a;
const int *a;
int * const a;
int const * a const;
The first two mean the same thing, namely a is a const (read-only) integer. The third means a is a pointer to a const integer (that is, the integer isn’t modifiable, but the pointer is). The fourth declares a to be a const pointer to an integer (that is, the integer pointed to by a is modifiable, but the pointer is not). The final declaration declares a to be a const pointer to a const integer (that is, neither the integer pointed to by a, nor the pointer itself may be modified). If the candidate correctly answers these questions, I’ll be impressed.
Even though any program can be written without this keyword, it’s good to use this, since it will help the one who is reading the program to understand the code easily by the information that the variable’s value won’t get changed. So using const keyword will help reducing the bugs in the code.
26. Which is the best way to write Loops?
The best way is to write count down loops and compiler can generate better machine code for it than the count up loops. In count down at loop termination, it needs to generate one instruction (SUBS), which subtracts as well as check the zero flag, but in count up case it has to add and compare with a constant, which takes two instructions.
27. What is loop unrolling?
Small loops can be unrolled for higher performance, with the disadvantage of increased codesize. When a loop is unrolled, a loop counter needs to be updated less often and fewer branches are executed. If the loop iterates only a few times, it can be fully unrolled, so that the loop overhead completely disappears.
int CountBitOne(uint n)
{
int bits = 0;
while (n != 0)
{
if (n & 1) bits++;
n >> = 1;
}
return bits;
}
int CountBitOne(uint n)
{
int bits = 0;
while (n != 0)
{
if (n & 1) bits++;
if (n & 2) bits++;
if (n & 4) bits++;
if (n & 8) bits++;
n >> = 4;
}
return bits;
}
28. How are local and Global variables are allocated by compiler.
Normally, cpu registers are allocated for local variables, but if the address of that local variable is used by some code, then it won’t allocate register but access from memory, thus result in un-optimized code. Gobal variables always use memory, so access is slower, so always use global only when it is absolutely necessary.
29. Which is better a char, short or int type for optimization?
Where possible, it is best to avoid using char and short as local variables. For the types char and short the compiler needs to reduce the size of the local variable to 8 or 16 bits after each assignment. This is called sign-extending for signed variables and zero extending for unsigned variables.
It is implemented by shifting the register left by 24 or 16 bits, followed by a signed or unsigned shift right by the same amount, taking two instructions (zero-extension of an unsigned char takes one instruction).
These shifts can be avoided by using int and unsigned int for local variables. This is particularly important for calculations which first load data into local variables and then process the data inside the local variables. Even if data is input and output as 8- or 16-bit quantities, it is worth considering processing them as 32bit quantities
30. How to reduce function call overhead in ARM based systems
• Try to ensure that small functions take four or fewer arguments. These will not use the stack for argument passing. It will copy into registers.
• If a function needs more than four arguments, try to ensure that it does a significant amount of work, so that the cost of passing the stacked arguments is outweighed.
• Pass pointers to structures instead of passing the structure itself.
• Put related arguments in a structure, and pass a pointer to the structure to functions. This will reduce the number of parameters and increase readability.
• Minimize the number of long long parameters, as these take two argument words. This also applies to doubles if software floating-point is enabled.
• Avoid functions with a parameter that is passed partially in a register and partially on the stack (split-argument). This is not handled efficiently by the current compilers: all register arguments are pushed on the stack.
• Avoid functions with a variable number of parameters. Varargs functions
31. What is a pure function in ARM terminology?
Pure functions are those which return a result which depends only on their arguments. They can be thought of as mathematical functions: they always return the same result if the arguments are the same. To tell the compiler that a function is pure, use the special declaration keyword __pure.
__pure int square(int x)
{
return x * x;
}
32. What are inline functions?
The ARM compilers support inline functions with the keyword __inline. This results in each call to an inline function being substituted by its body, instead of a normal call. This results in faster code, but it adversely affects code size, particularly if the inline function is large and used often.
33. Infinite loops often arise in embedded systems. How does you code an infinite loop in C?
There are several solutions to this question.
One is,
while(1)
{
}
Other is using for loop, but here things are not pretty clear as to what is going on.
for(;;)
{
}
Even though both serve the same purpose, its always better to know why you are using the first or second. So when you are asked about such questions you must answer with confidence that both are right, and just matter which way you wish to code it, anyways finally compiler would optimize and generate the same code for it. So don’t ever say, I was taught to do it this way, and so never thought about the other ways.
There is another way of doing it, i.e. by using goto statement, goto is very basic keyword, which is more like an assembly jump instruction, if one finds more comfortable with goto then possibly he works closely with assembly language, or with FORATN.
loop:
…
…
goto loop;
34. What are the uses of the keyword static?
This simple question is rarely answered completely.
Static has three distinct uses in C:
• A variable declared static within the body of a function maintains its value between function invocations
• A variable declared static within a module, (but outside the body of a function) is accessible by all functions within that module. It is not accessible by functions within any other module. That is, it is a localized global.
• Functions declared static within a module may only be called by other functions within that module. That is, the scope of the function is localized to the module within which it is declared candidates get the first part correct.
A reasonable number get the second part correct, while a pitiful number understand the third answer. This is a serious weakness in a candidate, since he obviously doesn’t understand the importance and benefits of localizing the scope of both data and code.
35. Accessing fixed memory locations
Embedded systems are often characterized by requiring the programmer to access a specific memory location. On a certain project it is required to set an integer variable at the absolute address 0x67a9 to the value 0xaa55. The compiler is a pure ANSI compiler. Write code to accomplish this task.
This problem tests whether you know that it is legal to typecast an integer to a pointer in order to access an absolute location. The exact syntax varies depending upon one’s style. However, I would typically be looking for something like this:
int *ptr;
ptr = (int *)0x67a9;
*ptr = 0xaa55;
A more obscure approach is:
*(int * const)(0x67a9) = 0xaa55;
Even if your taste runs more to the second solution, I suggest the first solution when you are in an interview situation.
36. What is the difference between embedded systems and the system in which rtos is running?
Embedded system can include RTOS and cannot include also. it depends on the requirement. if the system needs to serve only event sequentially, there is no need of RTOS. If the system demands the parallel execution of events then we need RTOS.
37. What is watchdog timer?
A watchdog timer (or computer operating properly timer) is a computer hardware timing device that triggers a system reset if the main program, due to some fault condition, such as a hang, neglects to regularly service the watchdog. The intention is to bring the system back from the hung state into normal operation.
38.What is semaphore?
In computer science, a semaphore is a protected variable or abstract data type which constitutes the classic method for restricting access to shared resources such as shared memory in a parallel programming environment. A counting semaphore is a counter for a set of available resources, rather than a locked/unlocked flag of a single resource.
39.What is mutex?
Mutual exclusion (often abbreviated to mutex) algorithms are used in concurrent programming to avoid the simultaneous use of a common resource, such as a global variable, by pieces of computer code called critical sections.
40. Can structures be passed to the functions by value?
yes structures can be passed by value. But unnecessary memory wastage.
41.Why cannot arrays be passed by values to functions?
When a array is passed to a function, the array is internally changed to a ‘pointer’. And pointers are always passed by reference.
42. Advantages and disadvantages of using macro and inline functions?
Advantage: Macros and Inline functions are efficient than calling a normal function. The times spend in calling the function is saved in case of macros and inline functions as these are included directly into the code.
Disadvantage: Macros and inline functions increased the size of executable code.
Difference in inline functions and macro
1) Macro is expanded by preprocessor and inline function are expanded by compiler.
2) Expressions passed as arguments to inline functions are evaluated only once while _expression passed as argument to inline functions are evaluated more than once.
More over inline functions are used to overcome the overhead of function calls. Macros are used to maintain the readability and easy maintenance of the code.
43. What happens when recursion functions are declared inline?
It is illegal to declare a recursive function as inline. Even a function is declared as inline compiler judges it to be inline or not. Many compilers can also inline expand some recursive functions; recursive macros are typically illegal.
44. Scope of static variables?
Scope of static variable is within the file if it is static global. Scope of static variable is within the function if variable is declared local to a function.
But the life time is throughout the program
45. What is the difference between a ‘thread’ and a ‘process’?
process
In computing, a process is an instance of a computer program that is being sequentially executed by a computer system that has the ability to run several computer programs concurrently.
Thread
A single process may contain several executable programs (threads) that work together as a coherent whole. One thread might, for example, handle error signals, another might send a message about the error to the user, while a third thread is executing the actual task of the..
46. What is a Inode?
In computing, an inode is a data structure on a traditional Unix-style file system such as UFS. An inode stores basic information about a regular file, directory, or other file system object.
47.What is the use of having the const qualifier?
The const qualifier identifies a specific parameter or variable as read-only attribute to the function or to the entire program. This can come in handy when we are dealing with static data inside function and in a program.
48. Why do we need a infinite loop in embedded systems development? What are the different ways by which you can code in a infinite loop?
The infinite loops are coded in to give a delay or sleep to the program execution for a specific amount of clock ticks.
They can be implemented as:
while(;;);
for();
(or)
Loop:
goto Loop;