FW: examine the best 16 questions for Embedded C developers)

Source: Internet
Author: User

Measure the test taker's knowledge about the basic C skills of a junior embedded system developer. Answers and questions are prepared by senior embedded system experts,

The objective is to examine the entry-level embedded software developer Gavin Shaw to provide detailed answers.

Editor's note: very basic questions about C language, an Information Class (computer, Information Engineering, electronic engineering, Communication Engineering)

The level that a Professional Undergraduate graduates should achieve. If you have more than three questions that cannot be answered correctly, it is basically not good for us.

What have you said... it is not difficult to answer questions. All of them can be answered quickly. Of course, a certain amount of knowledge is required.

Conventions:
1) in the following test questions, all the necessary header files have been correctly included
2) Data Type
Char 1 byte
Int two bytes 2 bytes (16-bit system, think integer is 2 bytes)
Long int, four bytes, 4 bytes
Float four bytes 4 byet
Double, eight bytes, 8 bytes
Long double 10 bytes 10 bytes
Pointer two bytes 2 byte (note that the address bus is only 16 bits)

Question 1st: measure the test taker's knowledge about the volatile keyword.

#include<setjmp.h>
static jmp_buf buf;

main() 
{
volatile int b;
b =3;

if(setjmp(buf)!=0) 
{
printf("%d ", b); 
exit(0);
}
b=5;
longjmp(buf , 1);
}

Excuse me, the output of this program is
(A) 3
(B) 5
(C) 0
(D) None of the above

Question 2nd: Test type conversion

main()
{
struct node 
{
int a;
int b;
int c; 
};
struct node s= { 3, 5,6 };
struct node *pt = &s;
printf("%d" , *(int*)pt);

}

The output of this program is:
(A) 3
(B) 5
(C) 6
(D) 7

3rd question: Examine recursive calls

int foo ( int x , int n) 
{
int val;
val =1;

if (n>0) 
{
if (n%2 == 1) val = val *x;

val = val * foo(x*x , n/2);
}
return val;
}

What functions (Operations) Does this code perform on X and N )?
(A) XN
(B) x * n
(C) NX
(D) None of the above

Question 4th: Test pointer

main() 
{
int a[5] = {1,2,3,4,5};
int *ptr = (int*)(&a+1);

printf("%d %d" , *(a+1), *(ptr-1) );

}

The output of this program is:

(A) 2 2
(B) 2 1
(C) 2 5
(D) None of the above

Question 5th: multi-dimensional arrays and pointers

void foo(int [][3] ); 

main()
{
int a [3][3]= { { 1,2,3} , { 4,5,6},{7,8,9}};
foo(a);
printf("%d" , a[2][1]);
}

void foo( int b[][3]) 
{
++ b;
b[1][1] =9;
}

The output of this program is:

(A) 8
(B) 9
(C) 7
(D) None of the above


6th question: comma-separated expressions

main()
{
int a, b,c, d;
a=3;
b=5;
c=a,b;
d=(a,b);

printf("c=%d" ,c);
printf("d=%d" ,d);

}

The output of this program is:

(A) C = 3 D = 3
(B) C = 5 d = 3
(C) C = 3 D = 5
(D) C = 5 d = 5

Question 7th: pointer Array

main()
{
int a[][3] = { 1,2,3 ,4,5,6};
int (*ptr)[3] =a;

printf("%d %d " ,(*ptr)[1], (*ptr)[2] );

++ptr;
printf("%d %d" ,(*ptr)[1], (*ptr)[2] );
}

The output of this program is:

(A) 2 3 5 6
(B) 2 3 4 5
(C) 4 5 0 0
(D) None of the above

Question 8th: measure the test taker's knowledge about function pointers.

int *f1(void)
{
int x =10;
return(&x);
}

int *f2(void)
{
int*ptr;
*ptr =10;
return ptr;
}

int *f3(void)
{
int *ptr;
ptr=(int*) malloc(sizeof(int));
return ptr;
}

Which of the above three functions is most likely to cause pointer problems?

(A) Only F3
(B) Only F1 and F3
(C) Only F1 and F2
(D) F1, F2, F3

9th question: test self-added operations (++)

main()
{
int i=3;
int j;

j = sizeof(++i+ ++i);

printf("i=%d j=%d", i ,j);
}

The output of this program is:

(A) I = 4 J = 2
(B) I = 3 J = 2
(C) I = 3 J = 4
(D) I = 3 J = 6

Question 10th: test form parameters, actual parameters, pointers, and Arrays

void f1(int *, int); 
void f2(int *, int); 
void(*p[2]) ( int *, int);

main()
{
int a;
int b;

p[0] = f1;
p[1] = f2;
a=3;
b=5;

p[0](&a , b);
printf("%d\t %d\t" , a ,b);

p[1](&a , b);
printf("%d\t %d\t" , a ,b);
}

void f1( int* p , int q)
{
int tmp;
tmp =*p;
*p = q;
q= tmp;
}

void f2( int* p , int q)
{
int tmp;
tmp =*p;
*p = q;
q= tmp;

The output of this program is:

(A) 5 5 5 5
(B) 3 5 3 5
(C) 5 3 5 3
(D) 3 3 3 3

11th question: test the auto-increment operation (--)

void e(int ); 

main()
{
int a;
a=3;
e(a);
}

void e(int n)
{
if(n>0)
{
e(--n);
printf("%d" , n);
e(--n);
}
}

The output of this program is:

(A) 0 1 2 0
(B) 0 1 2 1
(C) 1 2 0 1
(D) 0 2 1 1

12th question: test the typedef type definition and function pointer

typedef int (*test) ( float * , float*)
test tmp;

The TMP type is

(A) pointer to the function. This function uses two pointers pointing to float as parameters (arguments)
Pointer to function of having two arguments that is pointer to float
(B) Integer
(C) pointer to a function. The function uses two pointers pointing to float as parameters (arguments) and the return value type of the function is integer.
Pointer to function having two argument that is pointer to float and return int
(D) None of the above


Question 13th: Differences and relationships between arrays and pointers

main()
{
char *p;
char buf[10] ={ 1,2,3,4,5,6,9,8};
p = (buf+1)[5];
printf("%d" , p);
}

The output of this program is:

(A) 5
(B) 6
(C) 9
(D) None of the above

Question 14th: Examine the pointer Array

Void f(char**);

main()
{
char * argv[] = { "ab" ,"cd" , "ef" ,"gh", "ij" ,"kl" };
f( argv );
}

void f( char **p )
{
char* t;

t= (p+= sizeof(int))[-1];

printf( "%s" , t);
}

The output of this program is:

(A) AB
(B) CD
(C) EF
(D) GH

Question 15th: This question examines the variable length parameter of C. Just like printf () in the standard function library, this topic is generally not discussed in Chinese university classes, and it will not be excitable,

#include<stdarg.h>
int ripple ( int , ...);

main()
{
int num;
num = ripple ( 3, 5,7);
printf( " %d" , num);
}

int ripple (int n, ...)
{
int i , j;
int k; 
va_list p;

k= 0;
j = 1;
va_start( p , n); 

for (; j<n; ++j) 
{
i = va_arg( p , int);
for (; i; i &=i-1 )
++k;
}
return k;
}

The output of this program is:

(A) 7
(B) 6
(C) 5
(D) 3

Question 16th: measure the test taker's knowledge about static variables.

int counter (int i)
{
static int count =0;
count = count +i;
return (count );
}
main()
{
int i , j;

for (i=0; i <=5; i++)
j = counter(i);
}

The value of J is as follows:

(A) 10
(B) 15
(C) 6
(D) 7







For more information, see answers.

Question 1st: (B)
Volatile literally means volatile. When this keyword is used to describe a variable, it means to assign a value to the variable.

(Write) and then read again immediately. The written value may be different from the read value, so it is "volatile.
This is because this variable may be a register that is directly connected to external devices. After you write this variable, this register can also

Can be changed by write operations on external devices; or, the variable is interrupted by one program or another process
Changed.
Volatile variable isn' t affected by the optimization. Its value

After the longjump is the last value variable assumed.

B Last value is 5 hence 5 is printed.

Setjmp: sets up for nonlocal GOTO

Volatile will not be affected by Compiler Optimization. After longjump, its value is the variable value assumed later, and B's final value is 5, so 5 is printed.

Setjmp: set non-local jump

Stores context information such as register values so that the lomgjmp

Function can return control to the statement following the one calling

Setjmp. returns 0 when it is initially called.

Lonjjmp: longjmp performs nonlocal GOTO

Lonjjmp: executes a non-local jump

Transfers control to the statement where the call to setjmp (which

Initialized BUF) was made. execution continues at this point as if

Longjmp cannot return the value 0.a nonvolatile automatic variable

Might be changed by a call to longjmp. When you use setjmp and longjmp,

The only automatic variables guaranteed to remain valid are those

Declared volatile.

Note: test program without volatile qualifier (result may very)
For more information, see setjmp and longjmp in C.

2nd Question: ()
The members of structures have address in increasing order of their

Declaration. If a pointer to a structure is cast to the type of

Pointer to its first member, the result refers to the first member.

The addresses of the structure questions in the memory increase in sequence according to their defined locations. If a struct

The pointer is regarded as the pointer of its first member, so the pointer does point to the first Member


3rd question: ()

This question is difficult.

Non-recursive version of this program

int what ( int x , int n)

{

int val;

int product;

product =1;

val =x;



while(n>0)

{

if (n%2 == 1) 

product = product*val; 

val = val* val; 

n = n/2; 

}

return product;

}


Algorithm Description

(while n>0) 

{

if next most significant binary digit of n( power) is one


then multiply accumulated product by current val ,


reduce n(power) sequence by a factor of two using integer division .


get next val by multiply current value of itself 

}
Non recursive version of the program
int what ( int x , int n)
{
int val;
int product;
product =1;
val =x;

while(n>0)
{
if (n%2 == 1) 
product = product*val;
n = n/2; 
val = val* val;
}
}


Algorithm Description

(while n>0) 
{
if next most significant binary digit of n( power) is one

then multiply accumulated product by current val ,

reduce n(power) sequence by a factor of two using integer division .

get next val by multiply current value of itself 
}



4th question: (c)

A is an integer array with 5 members.
& A is a pointer to an integer array.
So where & A + 1 points is equivalent to a [6]

Type of A is array of int
Type of & A is pointer to array of int
 
Therefore, * (a + 1) is equivalent to a [1].
PTR is equivalent to a [6], and ptr-1 is equivalent to a [5]

Taking a pointer to the element one beyond the end of an array

Is sure to work.

Question 5th: (B)

The question itself gives enough tips
B [0] [0] = 4
B [1] [0] = 7





6th question: (c)

Test the comma expression. The priority of a comma expression is very low, which is lower than that of a value (= ).

Is the value of the last element.

Another function of the comma expression is to split the parameter list of the function ..

E1, E2,..., en
In the preceding expression, the values of E1, E2,... en are calculated separately. The structure calculated by en is assigned

Entire comma expression

c=a,b; / *yields c=a* /

d=(a,b); 
The comma separates the elements of a function argument list. The comma is also
used as an operator in comma expressions. Mixing the two uses of comma is legal,
but you must use parentheses to distinguish them. the left operand E1 is
uated as a void expression, then E2 is uated to give the result and type
of the comma expression. By recursion, the expression
E1, E2, ..., En

results in the left-to-right uation of each Ei, with the value and type of
En giving the result of the whole expression.
c=a,b; / *yields c=a* /
d=(a,b); 



7th question: ()

PTR is an array pointer, which has three int members.



8th question: (c)
F1 obviously has a problem. It returns a pointer to a local variable. The local variable is saved in the stack. After exiting the function

The partial variable is destroyed, and it makes no sense to keep its pointer because the stack space it points to may be overwritten by other variables.


F2 also has a problem. PTR is a local variable and has not been initialized. Its value is unknown. * PTR does not know where to direct it.

Assigning a value to * PTR may overwrite important system variables.

Question 9th: (B)
The sizeof operator gives the space required by its operations. It can be determined at compilation, so its

Even an expression of an operand does not need to be computed at runtime. (++ I) will not be executed,

So the I value is 3.

Int A, double B;

Sizeof (a + B); 4


10th question: ()
Apparently, select.
F1 exchanges * p and q values. After F1 is executed, * p and q values are indeed exchanged, but Q changes do not affect

B's change, * P is actually a, so after F1 is executed, a = B = 5 has a wide range of knowledge, including

Typedef custom type, function pointer, pointer array void (* P [2]) (int *, INT); defines

The array P and P of the function pointer have two pointer elements. The element is the pointer of the function, and the function pointer points to

Two Parameters. Return the void function. The two parameters are the pointer to the integer type and the integer type.


P [0] = F1; P [1] = F2 contain address of function. Function Name

Without parenthesis represent address of function value and address

Variable is passed to function only argument that is specified TED is

(Address is passed). Because of call by value F1, F2 can not effect B

11th question: ()

Test-operation and recursive call. You can analyze it carefully.

12th question: (c)
It is not recommended to look at C expert Programming
From the left to the left, when there is a bracket, stop and think of the things in the first bracket as a whole.



13th question: (c)

Check when an array is a pointer. For some types of T, if an expression is a T [] (T array ),

The value of this expression is actually a pointer to the first element of the array. So (BUF + 1) [5] is actually

Yes * (BUF + 6) or Buf [6]


Question 14th: (B)

The value of sizeof (INT) is 2, so P + = sizeof (INT) points to argv [2]. It is estimated that everyone has nothing

Question (p + = sizeof (INT) [-1] points to argv [1]. Can you understand it, because (p + = sizeof (INT) [-1]

It is equivalent to (p + = 2) [-1], that is (p + 2-1)

15th question: (c)

The C compiler usually provides a series of macros for processing variable parameters to shield the differences caused by different hardware platforms,

Increase Program portability. These macros include va_start, va_arg, and va_end. ANSI standard

Form, the prototype declaration of a function with a variable number of parameters is:
Type funcname (type para1, type para2 ,...)
This form requires at least one common form parameter, and the ellipsis behind it does not mean to be omitted, but is part of the function prototype.

Type is the type of function return values and formal parameters. Different compilers have different implementations for this variable length parameter,

Gcc4.x is a built-in function.

For variable length parameters, see

Http://www.upsdn.net/html/2004-11/26.html

Http://www.upsdn.net/html/2004-11/24.html

 

Program Analysis

va_list p; 
va_start( p , n); 
for (; j<n; ++j) 
{
i = va_arg( p , int); 
for (; i; i &=i-1 ) 
++k; 
}

When we call the ripple function, the first parameter n in the parameter list passed to the ripple function is 3.

Va_start initialize P. Morale points to the first unnamed parameter (N is a parameter with a name), that is, is 5

(First). Each call to va_arg returns a parameter and Points P to the next parameter.

Va_arg uses a type name to determine the type of the returned parameter, and determines in the internal implementation of var_arg

The distance to the next Parameter

(; i; i&=i-1) k++ 


5. In binary format, it is (101) 2.
7 In binary format (111) 3
Therefore, K returns 5 (2 + 3), that is, C should be selected.

For example, it is easy to understand.

Make I = 9 = 1001
I-1 = 1000
(I-1) + 1 = I
1000
+ 1
1 001

Because I and the I-1's rightmost (DIGIT) must be different, if i1, The I-1 must be 0, and vice versa.

The I & I-1 operation removes the rightmost 1 bit in the binary complement numeric system

 

Question 16th: (B)
The answer is (B)
It is said that when the first year of Gaussian elementary school, this class of proportional series will be used. This question examines the knowledge of static variables,

After each function call, the static variable value is not lost, which is obviously different from the temporary local variable in the stack.


Therefore, after counter (0) is called for the first time, Count = 0
Count = 0 + 1 after the second counter (1) call;
The third counter (2) Count = 1 + 2 call;
The fourth counter (3) Count = 3 + 3;
The fifth counter (4) Count = 6 + 4 call;
The sixth counter (5) Count = 10 + 5 call;

FW: examine the best 16 questions for Embedded C developers)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.