Skip to content

Instantly share code, notes, and snippets.

@jotux
Last active April 14, 2023 20:32
Show Gist options
  • Save jotux/1191965 to your computer and use it in GitHub Desktop.
Save jotux/1191965 to your computer and use it in GitHub Desktop.
Embedded C Interview Questions
// What is the expected output?
main()
{
int a;
int b;
for (a = 0,b = 0;a < 10,b < 5;a++,b++)
{
printf("%d %d\n",a,b);
}
return 0;
}
// This shows they know:
// - What the comma operator does
// Bonus points for the phrase "Why the hell would you do that?"
// What is the output? Why?
// What would happen if you strcpy a1?
void main()
{
char a1[3] = "abc";
char a2[8] = "abcdefgh";
char b1[] = "abc";
char b2[] = "abcdefgh";
char* c1 = "abc";
char* c2 = "abcdefgh";
printf("a1:%s is size: %d\n",a1,sizeof(a1));
printf("a2:%s is size: %d\n",a2,sizeof(a2));
printf("b1:%s is size: %d\n",b1,sizeof(b1));
printf("b2:%s is size: %d\n",b2,sizeof(b2));
printf("c1:%s is size: %d\n",c1,sizeof(c1));
printf("c2:%s is size: %d\n",c2,sizeof(c2));
}
// This shows they know:
// -How memory is allocated for arrays, strings, and pointers
// Complete the code. What is the output?
typedef struct
{
int a;
int b;
} new_type;
void f1(void *in);
main()
{
new_type mine = {0,1};
printf("%d %d\n",mine.a,mine.b);
f1( );
printf("%d %d\n",mine.a,mine.b);
return 0;
}
void f1(void *in)
{
new_type *blah = ;
printf("-%d %d-\n",blah->a,blah->b);
blah->a = 1;
}
// This shows they know:
// -Passing and casting structures
// -Typedefs
// What is the expected output?
// Is this code portable?
main()
{
char foo[8] = {1,0,2,0,3,0,4,0};
short int *bar = &foo;
long int *derp = &foo;
int i;
for (i = 0;i < 4;i++)
{
printf("0x%x\n",bar[i]);
}
for (i = 0;i < 2;i++)
{
printf("0x%x\n",derp[i]);
}
return 0;
}
// This shows they know:
// -Pointer types
// -Endianness
// -Casting
// Complete the following macros
#define SET_BIT(reg,n)
#define CLEAR_BIT(reg,n)
#define TOGGLE_BIT(reg,n)
// This shows they know:
// -Basic embedded operations
// -Binary operators
// Describe static/extern/const/volatile
// What is the expected output?
// Is this code portable?
// Are there problems with this code you would fix?
typedef struct
{
char foo;
char bar;
} new_type;
typedef struct
{
char my_str[3];
new_type dat;
} other_type;
void f1(void *in);
main()
{
other_type stuff = {'a','b','c',{1,2}};
f1((void *)&stuff);
return 0;
}
void f1(void *in)
{
new_type *other_stuff = (new_type *)(in + 3);
printf("%d %d\n",other_stuff->foo,other_stuff->bar);
}
// This shows they know:
// -Off-boundary memory access
// Complete the declaration for CallFuncNumTimes. What is the expected output?
void func1()
{
printf("1\n");
}
void func2()
{
printf("2\n");
}
void CallFuncNumTimes( , int num_of_times)
{
int cnt;
for (cnt = 0;cnt < num_of_times;cnt++)
{
function();
}
}
main()
{
CallFuncNumTimes(func1, 5);
CallFuncNumTimes(func2, 5);
return 0;
}
// This shows they know:
// -Simple function pointers
// You are using SimpleDelay() as a mechanism to delay execution.
// When you enable optimization the function no longer works.
// How can you change SimpleDelay() to make it work?
void SimpleDelay(unsigned long int cnt)
{
unsigned long int i = 0;
while (i++ < cnt);
}
// This shows they know:
// -Compiler optimization
// -Volatile
// What is the expected output?
main()
{
int a = 0x05;
if (a & 0xF0 > 3)
{
printf("true");
}
else
{
printf("false");
}
return 0;
}
// This shows they know:
// -Order of execution in compilers
// What is the expected output?
void func1(int var)
{
static int foo;
printf("%d\n",foo);
foo = var;
}
void main()
{
func1(1);
func1(2);
func1(3);
}
// This shows they know:
// -Static variables
@cityprince143
Copy link

Answer :- test6.c
void CallFuncNumTimes(void (*function) (), int num_of_times) // it will allow you to pass function pointer which return void and takes void/no arguments

@cityprince143
Copy link

cityprince143 commented May 7, 2018

Answer :- test1.c
//Below is the output i got after running it. Condition a < 10 is ignored.
// try editing the condition a < 10 to a <1 or a < 0 which will yield the same result as below

0 0
1 1
2 2
3 3
4 4

@cityprince143
Copy link

Answer :- test10.c

Output
a1:abc is size: 3 // char is 1 byte *3
a2:abcdefgh is size: 8 //char is 1 byte * 8
b1:abc is size: 4 // array 3 char byte + terminator 1 byte
b2:abcdefgh is size: 9 // array 8 char byte + terminator 1 byte
c1:abc is size: 8 // size of char pointer
c2:abcdefgh is size: 8 //size of char pointer

@cityprince143
Copy link

Answer : test2.c
Output
0 1
-0 1-
1 1

code
#include <stdio.h>

typedef struct
{
int a;
int b;
} new_type;

void f1(void *in);

int main()
{
new_type mine = {0,1};
printf("%d %d\n",mine.a,mine.b);
f1(&mine);
printf("%d %d\n",mine.a,mine.b);
return 0;
}

void f1(void *in)
{
new_type *blah =in;
printf("-%d %d-\n",blah->a,blah->b);
blah->a = 1;
}

@ajithmaniac
Copy link

ajithmaniac commented Mar 14, 2019

Answer-test5.c

In (line 27) the void * is incremented, which is undefined behavior or implementation specific behavior.
This makes it non portable.
void * should be casted to char * to increment deterministically.

@madhumithashankar29
Copy link

madhumithashankar29 commented Mar 27, 2019

Answer-test4.c
// Complete the following macros
#define SET_BIT(reg,n)
reg|=(1<<(n-1))
#define CLEAR_BIT(reg,n)
reg&=~(1<<(n-1))
#define TOGGLE_BIT(reg,n)
reg^=(1<<(n-1))
// This shows they know:
// -Basic embedded operations
// -Binary operators

// Describe static/extern/const/volatile

@madhumithashankar29
Copy link

Answer Test5.c
Output: 1 2

@ck989
Copy link

ck989 commented Jun 10, 2020

test 2.c

typedef struct
{
int a;
int b;
} new_type;

void f1(void *in);

main()
{
new_type mine = {0,1};
printf("%d %d\n",mine.a,mine.b);
f1((void*)&mine);
printf("%d %d\n",mine.a,mine.b);
return 0;
}

void f1(void *in)
{
new_type *blah = in;
printf("-%d %d-\n",blah->a,blah->b);
blah->a = 1;
}

@siddug
Copy link

siddug commented Jan 24, 2021

We were building similar helpful resource. There are only three questions public at the moment but hope it helps-

https://www.adaface.com/assessment-test/embedded-c-online-programming-test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment