嵌入式面试问题及解答(英文)_第1页
嵌入式面试问题及解答(英文)_第2页
嵌入式面试问题及解答(英文)_第3页
嵌入式面试问题及解答(英文)_第4页
嵌入式面试问题及解答(英文)_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Basic c question 1 Question where in memory the variables are stored Local variables global variables static Answer Local variables sit in Stack Global and static goto Data segment Dynamic memory comes from Heap 2 Question can you explian the meaning for the follwoing program char c1 c2 c3 c4 c5 char analysis 8 a n a l y s i s int main c5 c4 analysis c4 c3 c2 c5 2 c1 printf c t c t c t c t c c1 c2 c3 c4 c5 return 0 Answer c1 c2 c3 c4 and c5 are pointers which can hold addresses analysis is an array variable which is holding the string analysis c5 c4 analysis The starting address of the array is given to c5 and c4 Hence they point to first character a in the array c4 c4 is incremented by 1 Hence points to n in the array c3 c3 is given the address of 7th character count from 0 of the array Hence c3 points to character i c2 c5 2 c5 points to first character plus 2 means c2 points to 3rd character a second a in the string c1 c1 points to 3rd character from the end not counting the last character c1 holds the address c1 means the data strored at c1 Since c1 points to 3rd character from the end that is 5th character count starts from 0 c holds character y Hence c1 will print y on the screen Similarly for other pointer variables c2 c3 c4 and c5 3 Question a 5 b 10 c 7 a c a b c b c Answer 10 4 Question How do you declare an array of N pointers to functions returning pointers to functions returning pointers to characters A char a N B Build the declaration up incrementally using typedefs C Use the cdecl program which turns English into C and vice versa D All of the above Answer D 5 Question void main int count 10 temp sum 0 temp temp 20 temp temp count printf d d d count temp sum Answer 20 20 20 6 Question void main int i 7 printf d i i Answer 49 Note Don t change a variable twice in one expression 7 Question The number of syntax errors in the program int f void main f 1 f 1 2 f 1 2 3 f int i int j int k printf d d d i j k Answer None 8 Question void main float j j 1000 1000 printf f j A 1000000 B Overflow C Error D None of the above Answer D 9 Question Give the output of the program void main unsigned i 1 unsigned char k 1 k 255 signed j 1 char k 1 k 65535 unsigned or signed int k 1 k 65535 if ij printf greater else if i j printf equal Answer less 10 Give the output of the program void main char s 12345sn printf d sizeof s Answer 4 11 Question Give the output of the program void main int i for i 1 i 1 2 B g 1 2 C g 1 2 D g 1 2 Answer C 13 Question Can you have constant volatile variable Answer YES We can have a const volatile variable a volatile variable is a variable which can be changed by the extrenal events like an interrput timers will increment the voltile varible If you dont want you volatile varibale to be changed then declare them as const volatile 14 Question study the code include void main const int a 100 int p p p printf a d n p d n a p What is printed A 100 101 B 100 100 C 101 101 D None of the above Answer C Embedded C 1 Using the define statement how would you declare a manifest constant that returns the number of seconds in a year Disregard leap years in your answer Answer define SECONDS PER YEAR 60 60 24 365 UL I m looking for several things here Basic knowledge of the define syntax for example no semi colon at the end the need to parenthesize and so on An understanding that the pre processor will evaluate constant expressions for you Thus it is clearer and penalty free to spell out how you are calculating the number of seconds in a year rather than actually doing the calculation yourself A realization that the expression will overflow an integer argument on a 16 bit machine hence the need for the L telling the compiler to treat the variable as a Long As a bonus if you modified the expression with a UL indicating unsigned long then you are off to a great start And remember first impressions count 2 Write the standard MIN macro that is a macro that takes two arguments and returns the smaller of the two arguments Answer define MIN A B A B A B The purpose of this question is to test the following Basic knowledge of the define directive as used in macros This is important because until the inline operator becomes part of standard C macros are the only portable way of generating inline code Inline code is often necessary in embedded systems in order to achieve the required performance level Knowledge of the ternary conditional operator This operator exists in C because it allows the compiler to produce more optimal code than an if then else sequence Given that performance is normally an issue in embedded systems knowledge and use of this construct is important Understanding of the need to very carefully parenthesize arguments to macros I also use this question to start a discussion on the side effects of macros for example what happens when you write code such as least MIN p b 3 Infinite loops often arise in embedded systems How does you code an infinite loop in C There are several solutions to this question My preferred solution is Answer while 1 5 Using the variable a give definitions for the following a An integer b A pointer to an integer c A pointer to a pointer to an integer d An array of 10 integers e An array of 10 pointers to integers f A pointer to an array of 10 integers g A pointer to a function that takes an integer as an argument and returns an integer h An array of ten pointers to functions that take an integer argument and return an integer Answers 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 People often claim that a couple of these are the sorts of thing that one looks up in textbooks and I agree While writing this article I consulted textbooks to ensure the syntax was correct However I expect to be asked this question or something close to it when I m being interviewed Consequently I make sure I know the answers at least for the few hours of the interview Candidates who don t know all the answers or at least most of them are simply unprepared for the interview If they can t be prepared for the interview what will they be prepared for 6 What are the uses of the keyword static Answer 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 Most 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 7 What do the following declarations mean const int a int const a const int a int const a int const const a Answer 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 8 What does the keyword volatile mean Give three different examples of its use Answer A volatile variable is one that can change unexpectedly Consequently the compiler can make no assumptions about the value of the variable In particular the optimizer must be careful to reload the variable every time it is used instead of holding a copy in a register 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 9 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 10 What s wrong with the following function int square volatile int ptr return ptr ptr Answers This one is wicked The intent of the code is to return the square of the value pointed to by ptr However since ptr points to a volatile parameter the compiler will generate code that looks something like this int square volatile int ptr int a b a ptr b ptr return a b Because it s possible for the value of ptr to change unexpectedly it is possible for a and b to be different Consequently this code could return a number that is not a square The correct way to code this is long square volatile int ptr int a a ptr return a a 11 Embedded systems always require the user to manipulate bits in registers or variables Given an integer variable a write two code fragments The first should set bit 3 of a The second should clear bit 3 of a In both cases the remaining bits should be unmodified Answer Use defines and bit masks This is a highly portable method and is the one that should be used My optimal solution to this problem would be define BIT3 0 x1 6 puts 6 puts 6 The reason for this is that expressions involving signed and unsigned types have all operands promoted to unsigned types Thus 0 becomes a very large positive integer and the expression evaluates to greater than 6 This is a very important point in embedded systems where unsigned data types should be used frequently 14 What does the following code fragment output and why char ptr if ptr char malloc 0 NULL puts Got a null pointer else puts Got a valid pointer Answer Got a valid pointer This is a fun question I stumbled across this only recently when a colleague of mine inadvertently passed a value of 0 to malloc and got back a valid pointer That is the above code will output Got a valid pointer I use this to start a discussion on whether the interviewee thinks this is the correct thing for the library routine to do Getting the right answer here is not nearly as important as the way you approach the problem and the rationale for your decision 15 Typedef is frequently used in C to declare synonyms for pre existing data types It is also possible to use the preprocessor to do something similar For instance consider the following code fragment define dPS struct s typedef struct s tPS The intent in both cases is to define dPS and tPS to be pointers to structure s Which method if any is preferred and why The answer is the typedef is preferred Consider the declarations dPS p1 p2 tPS p3 p4 The first expands to struct s p1 p2 which defines p1 to be a pointer to the structure and p2 to be an actual structure which is probably not what you wanted The second example correctly defines p3 and p4 to be pointers Obscure syntax 16 C allows some appalling constructs Is this construct legal and if so what does this code do int a 5 b 7 c c a b This code is treated as c a b Thus after this code is executed a 6 b 7 and c 12 Real time question 1 What is real time system A real time system is one in which the correctness of the computations not only depends upon the logical correctness of the computation but also upon the time at which the result is produced If the ti

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论