Embedded Software Engineer interview questions summary

Preprocessor (the Preprocessor)

 

1. #define statement pretreated with a constant instruction, to indicate the number of seconds (ignoring leap years the problem) has 1 year

#define SECONDS_PER_YEAR (60 * 60 * 24 * 365)UL

 

I would like to see a few things:

 

1) #define basics of grammar (for example: it can not end with a semicolon, use of parentheses, etc.).

 

2). You will know how to calculate the value of the preprocessor constant expression, therefore, to write directly instead of calculating the actual value How do you calculate how many seconds in a year, is more clear and without cost.

 

3) realize that this expression would make a 16-bit integer overflow machine - so use to sign long integer L, tells the compiler that this is a long integer constant.

 

4) If you use your expression UL (unsigned long integer), then you have a good starting point. Remember, first impressions are important.

 

2. Write a "standard" macro MIN, the macro input two parameters and returns the smaller one

 

#define MIN(A,B) ((A) <= (B) (A) : ))

C / C ++ Tests of C / C ++ Development This test is provided for the purposes of the following:

1) The identification #define basic knowledge in macro applications. This is important, because until embedded (inline) operator becomes part of standard C, macros are the only convenient to embed the code generated, for embedded systems, in order to achieve the required performance, the embedded code is often necessary Methods.

2) knowledge of the triplet conditional operator. C because of the presence of this operator that it allows the compiler to produce than if-then-else more optimized code, about this usage is very important.

3). Carefully to know the macro parameters in brackets

4) I also started to discuss this issue with the macro side effects, such as: What happened when you write the following code will happen?

Least = MIN(*p++, b);

 

3. What is the purpose preprocessor identifies #error is?

 

If you do not know the answer, see Reference 1. This problem of distinguishing a normal man and a nerd is useful. Only nerds will read Appendix C language textbooks to find out like this

The answer. Of course, if you're not looking for a nerd, the candidate better hope she does not know the answer.

 

Infinite loop (Infinite loops)

 

4. Embedded systems often use the infinite loop, how can you use C to write an infinite loop it?

 

The problem with few solutions. My preferred solution is:

while(1) { }

Some programmers prefer the following programs:

for(;;) { }

This construct puzzles me because the syntax does not exactly in the end how it was. If a candidate gives this as a solution, I will use this as an opportunity to explore them to do so

Fundamental. If their basic answer is: "I was taught to do so, but did not think about why." This will give me leave a bad impression.

The third option is to use goto

Loop:

goto Loop;

Candidates who are given the scenario above, this are either an assembly language programmers (which is probably a good thing) or is he wants to enter a BASIC / FORTRAN programmers new areas.

 

Data Statement (Data declarations)

 

5. the following definitions are variable a

 

a) an integer (An integer)

b) a pointer to an integer number (A pointer to an integer)

c) a pointer to a pointer that points to a pointer to an integer (A pointer to a pointer to an integer)

d) an array of 10 integers (An array of 10 integers)

e) an array of 10 pointers, a pointer to an integer the number (An array of 10 pointers to integers)

f) has a pointer 10 pointing to the array of integers (A pointer to an array of 10 integers)

g) a function pointer, the function has an integer argument and returns an integer (A pointer to a function that takes an integer as an argument and returns an integer)

h) an array of 10 pointers, the pointer to a function that takes an integer argument and returns an integer (An array of ten pointers to functions that take an integer argument and return an integer)

 

the answer is:

 

a) int a; // An integer

b) int *a; // A pointer to an integer

c) int **a; // A pointer to a pointer to an integer

d) int a[10]; // An array of 10 integers

e) int *a[10]; // An array of 10 pointers to integers

f) int (*a)[10]; // A pointer to an array of 10 integers

g) int (*a)(int); // A pointer to a function a that takes an integer argument and returns an integer

h) int (*a[10])(int); // An array of 10 pointers to functions that take an integer argument and return an integer

 

It is often claimed that there are several issues to be brief look at is the kind of book can answer the question, I agree. As I write this, in order to determine the correctness of grammar, I really checked the book.

But when I was interviewing, I expect to be asked this question (or similar). Because during this time of the interview, I make sure I know the answer to this question. Candidates who do not know

All the answers (or at least most of the answers), then there is no preparation for this interview, if the interviewer does not prepare for the interview, what will they be prepared for?

 

Static

 

6. What is the role the keyword static is?

 

This simple question is rarely answered completely. In C, the static keyword has three distinct roles:

 

1). In the body of the function, a constant is declared to maintain its value as a static variable in the function is called process.

2) in the module (but function in vitro), a variable is declared as static can be accessed within the module by a function, but other functions can not be accessed outside of the module. It is a local global variables.

3) Within the module, is declared as a static function may only be called by other functions within the module. That is, this function is limited to use in its declaration module local scope.

Most candidates correctly answer the first part, the second part of the answer correct part, with very few people understand the third. This is a serious weakness in a candidate, because he obviously does not understand the benefits and importance of localized data and code range.

 

Const

 

7. Const keyword is what meaning?

 

I just heard the interviewee said: "const means constant," and I knew I was dealing with an amateur. Last year, Dan Saks has been completely in his article outlines all the uses of const, so ESP (Translator: Embedded Systems Programming) every reader should be very familiar to const what can and can not do.

 

If you have not read that article, suffice it to say const means "read only" on it. Although this answer is not entirely answer, but I accept it as a correct answer. (If you want to know more detailed answers, carefully read Saks article, right) If the candidate can correctly answer this question, I'll ask him additional questions: What is the meaning of the following statement?

Const int a;

int const a;

const int *a;

int * const a;

int const * a const;

 

The first two are the same, a is a constant integer. The third means is a pointer to a const integer number (i.e., the integer is not modified, but the pointer). The fourth means is a pointer to a pointer of an integer constant (that is, integer pointer can be modified, but the pointer is not modified). The last implies a constant pointer is a pointer to a constant integer number (that is, integer pointer can not be modified, while the pointer is not modified). If the candidate can answer these questions correctly, then he gave me left a good impression. Incidentally, perhaps you may ask, even without the const keyword, it is still able to easily write programs function correctly, so why should I do so dear to the const keyword? I am also following a few reasons:

 

1) The role of const is to read your code conveys some very useful information, in fact, declare a parameter const tells the user application purpose of this parameter. If you spend a lot of time cleaning up trash left by other people, you will quickly learn to appreciate this extra information. (Of course, know how to let someone else clean up the use const, rarely leave the garbage.)

 

2) by the optimizer some additional information, keyword const may produce more compact code.

 

3) reasonable use of the const keyword allows the compiler to naturally protect those parameters do not want to be changed, to prevent the code changes it is unintentional. In short, thus reducing the bug occurs.

 

Volatile

 

8. What are the implications volatile keyword and give three different examples

 

Defined as a variable volatile is that this variable may be changed unexpectedly, so the compiler would not have to assume that the value of this variable. Precisely that, every time the optimizer must be carefully re-read the value of this variable when this variable is used, instead of using a backup stored in registers. Here are a few examples of volatile variables:

 

1) Hardware registers devices (eg: Status Register)

2) An interrupt service routine access to the non-automatic variable (Non-automatic variables)

3) variables in multi-threaded applications to be shared by multiple tasks

 

I can not answer that question people are not being hired. I think this is the distinction between fundamental question C programmers and embedded systems programmer. Embedded systems programmers often with hardware, interrupts, RTOS, etc. to deal with, the use of these require volatile variables. Do not know how volatile content will lead to disaster.

 

Assumption that the interviewee is correctly answer this question (ah, doubt whether this will be the case), I will get to the bottom a little bit, look at this guy really understand the full significance of volatile.

 

1) A parameter can be either const can also be volatile? Explain why.

2) A pointer can be volatile? Explain why.

3) What is wrong with the following function:

int square(volatile int *ptr)

{

return * ptr * ptr;

}

 

Here is the answer:

 

1) Yes. An example is a read-only status register. It is volatile because it may change unexpectedly. It is const because the program should not attempt to modify it.

 

2) Yes. Although this is not very common. An example is when one of the interrupt service routine modifies a pointer to a buffer.

 

3) There is a prank of this code. Purpose of this code is to return the pointer * ptr points to the square of the values, however, since the * ptr parameter points to a volatile, the compiler will generate code similar to the following:

int square(volatile int *ptr)

{

int a,b;

a = *ptr;

b = *ptr;

return a * b;

}

Since the value of the * ptr may be unexpectedly changed, and therefore a and b may be different. As a result, this code could return than you would expect squared value! The correct code is as follows:

long square(volatile int *ptr)

{

int a;

a = *ptr;

return a * a;

}

 

Bit operation (Bit manipulation)

 

9. embedded systems always require the user to register or a variable bit operation. Given an integer variable a, to write two codes, a first set of bit 3, a second clear the bit 3. In the above two operations, to maintain the other bits unchanged.

 

There are three basic responses to this question

 

1). I do not know how to start. The interviewee who had never done any work for embedded systems.

2) a bit fields. Bit fields are being thrown into the C language dead things, it ensures that your code between different compilers is not portable, but also to ensure your code is not reusable. Unfortunately, I recently saw Infineon drivers for more complex communications chips, which used bit fields and therefore completely useless to me, because my compiler other ways to achieve the bit fields. The moral: Never let a non-embedded side of the guy real piece of hardware.

3). #Defines manipulation and bit masks. This is a highly portable method is a method that should be used. The best solution is as follows:

 

#define BIT3 (0x1<<3)

static int a;

void set_bit3(void)

{

a |= BIT3;

}

void clear_bit3(void)

{

a &= ~BIT3;

}

Some people like to set and clear values ​​define a mask while defining some constants explanation, this is acceptable. I would like to see a few key points: Description constant, | = and & = ~ operator.

 

Accessing fixed memory locations (Accessing fixed memory locations) C / C ++ Development

 

10. The embedded systems often have particular requirements programmer to access a memory location characteristics. In a project it is required to set an absolute address integer variable 0x67a9 value 0xaa66. The compiler is a pure ANSI compiler. Write code to accomplish this task.

 

This question tests whether you know that in order to access an absolute address to an integer cast (typecast) is a pointer is legal. With the implementation of the problem of different individuals and different styles. A typical code similar to the following:

 

int *ptr;

ptr = (int *)0x67a9;

* Ptr = 0xaa55;

One method is the more obscure:

*(int * const)(0x67a9) = 0xaa55;

Even if your taste runs more to the second option, but I suggest you use the first option in the interview.

 

Interrupt (Interrupts)

 

11. interruption is an important part of embedded systems, which leads to a lot of compiler vendors offer an extension - let standard C to support interrupts. With representatives fact is, producing a new keyword __interrupt. The following code uses __interrupt keyword to define an interrupt service routine (ISR), please comment on this code.

 

__interrupt double compute_area (double radius)

{

double area = PI * radius * radius;

printf(" Area = %f", area);

return area;

}

This function has too many mistakes, and even hard to know where to start:

 

1). ISR can not return a value. If you do not understand this, then you will not be hired.

 

2). ISR may not pass. If you do not see this, your chances of being hired equivalent to the first item.

 

3) In many processor / compiler, the floating point operations are not reentrant. Some processors / compilers need to register at the amount of stack, some processor / compiler is allowed to do floating-point operations in the ISR. In addition, ISR should be short and efficient, do floating-point operations in the ISR is unwise.

 

4) and the third point of the same strain, printf () often has problems of reentrancy and performance. If you lost the third and fourth point, I would not be too hard on you. Needless to say, if you can get the two points, then your employment prospects are looking better and better.

 

Examples of codes (Code examples)

 

12. What is the output of the following code, why?

 

Void foo(void)

{

unsigned int a = 6;

int b = -20;

(a+b > 6) puts("> 6") : puts("<= 6");

}

 

This question tests whether you know the C language integer principles of automatic conversion, I found that some developers very poorly understood. No matter what, this unsigned integer answer to the question is output is "> 6." The reason is that expressions signed and unsigned types for all types of the operands are converted to unsigned type automatically. Thus -20 becomes a very large positive integer, and the expression evaluates to greater than 6. This should be used frequently for unsigned data types of embedded systems is an abundance of very important. If you get this one wrong, you will not get the job to the edge.

 

13. Evaluation following code fragment:

 

unsigned int zero = 0;

unsigned int compzero = 0xFFFF;

/*1's complement of zero */

Where an int is not a 16-bit processor is to say, the above code is not correct. It should be written as follows:

unsigned int compzero = ~0;

 

This question really gets to whether the candidate understands the importance of word length. In my experience, good embedded programmers are critically aware of the details of the hardware and its limitations, however, tend to program the PC hardware as a necessary annoyance.

 

At this stage, the candidate or completely dejected or determined to win confidence. If the candidate is not very good, then the test ends here. But if the candidate is doing well, then I throw the following additional issues, which is more difficult, I think only the very best candidates will do well. Ask these questions, I would like to see more candidate method to deal with the problem, not the answer. No matter what, you are the entertainment when it ...

 

Dynamic memory allocation (Dynamic memory allocation)

 

14. Although not so common non-embedded computer, embedded systems or processes have allocated memory from the heap (heap) dynamic. So embedded systems, dynamic memory allocation problem may occur what?

 

Here, I would expect the candidate to mention memory fragmentation, problems with garbage collection, variable execution time, and so on. This topic has been widely discussed in the ESP magazine (mainly PJ Plauger, his explanation is far more than any explanation I can here mentioned), go back and look at all these issues! After allowing the candidate into a false sense of security, I come up with this tidbit: What is the output of the following code fragment, why?

 

Char * ptr;

if ((ptr = (char *)malloc(0)) == NULL)

puts("Got a null pointer");

else

puts("Got a valid pointer");

 

This is an interesting question. Recently, after one of my colleagues inadvertently passed to the function value of 0 to malloc, get a valid pointer, I thought about this. This is the above code, the output code is "Got a valid pointer". I use this to begin to discuss such a question is to see whether the interviewee think library routines to do it right. Get the right answer is important, but the solution to the problem and you make decisions more important to some basic principles.

 

Typedef

 

15. Typedef in C language data types frequently used synonyms an existing statement. Preprocessor can also be used to do something similar. For example, consider the following example:

 

#define dPS struct s *

typedef struct s * tPS;

The intent in both cases to define dPS and tPS to pointers to structure s. Which method is better? (If any) Why?

This is a very delicate issue, anyone answer this question (justified reason) is to be congratulated. The answer is: typedef better. Consider the following example:

dPS p1,p2;

tPS p3,p4;

The first extended

struct s * p1, p2;

The above code defines p1 is a pointer to the structure, p2 is an actual structure, which is probably not what you want. The second example correctly defines two pointers p3 and p4.

Obscure syntax

 

16. C allows some appalling structure, the following structure is legal, and if so what is it doing?

 

Int a = 5, b = 7, c;

c = a+++b;

This issue will be used as a happy ending to this quiz. Whether you believe it or not, the above example is perfectly legal syntax. The question is how the compiler handle it? Level is not high compiler writers actually debate the issue, according to the best principles, the compiler should be able to handle all possible legal usage. Thus, the above code is treated as:

c = a++ + b;

Accordingly, after holding the line code a = 6, b = 7, c = 12.

If you know the answer, or guessed correctly, well done. If you do not know the answer, I would not consider this as a problem. I found the problem are the biggest benefits: This is the code writing style, good topic readability, modifiability of code.

 

- END -

Original articles published 0 · won praise 2 · Views 2241

Guess you like

Origin blog.csdn.net/lyh290188/article/details/104362778