Interview Preparation Question and Answer

From Embedded Systems Learning Academy
Jump to: navigation, search

What is the use of volatile keyword?

The C's volatile keyword is a qualifier that tells the compiler not to optimize when applied to a variable. By declaring a variable volatile, we can tell the compiler that the value of the variable may change any moment from outside of the scope of the program. A variable should be declared volatile whenever its value could change unexpectedly and beyond the comprehension of the compiler.


Can a variable be both const and volatile?

The const keyword make sure that the value of the variable declared as const can't be changed. This statement holds true in the scope of the program. The value can still be changed by outside intervention. So, the use of const with volatile keyword makes perfect sense.


Can a pointer be volatile?

If we see the declaration volatile int *p, it means that the pointer itself is not volatile and points to an integer that is volatile. This is to inform the compiler that pointer p is pointing to an integer and the value of that integer may change unexpectedly even if there is no code indicating so in the program.


What is size of character, integer, integer pointer, character pointer?

• The sizeof character is 1 byte.

• Size of integer is 4 bytes.

• Size of integer pointer and character is 8 bytes on 64 bit machine and 4 bytes on 32 bit machine.


What is void pointer and what is its use?

The void pointer means that it points to a variable that can be of any type. Other pointers points to a specific type of variable while void pointer is a somewhat generic pointer and can be pointed to any data type, be it standard data type(int, char etc) or user define data type (structure, union etc.). We can pass any kind of pointer and reference it as a void pointer. But to dereference it, we have to type the void pointer to correct data type.


What is ISR?

An ISR(Interrupt Service Routine) is an interrupt handler, a callback subroutine which is called when a interrupt is encountered.


What is return type of ISR?

ISR does not return anything. An ISR returns nothing because there is no caller in the code to read the returned values.


What is interrupt latency?

Interrupt latency is the time required for an ISR responds to an interrupt.


How to reduce interrupt latency?

Interrupt latency can be minimized by writing short ISR routine and by not delaying interrupts for more time.


Write a short code using C

1)Code to print out all odd number from 1 to 100 using a for loop


    for( unsigned int i = 1; i < = 100; i++ )

    if( i & 0x00000001 )

    printf(“i=%u”,&i);


2)Write out a function that prints out all the permutations of a string. For example, abc would give you abc, acb, bac, bca, cab, cba.


// C program to print all permutations with duplicates allowed
#include <stdio.h>
#include <string.h>
 
/* Function to swap values at two pointers */

void swap(char *x, char *y)

{
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}
 
/* Function to print permutations of string
   This function takes three parameters:
   1. String
   2. Starting index of the string
   3. Ending index of the string. */

void permute(char *a, int l, int r)
{
   int i;
   if (l == r)
     printf("%s\n", a);
   else
   {
       for (i = l; i <= r; i++)
       {
          swap((a+l), (a+i));
          permute(a, l+1, r);
          swap((a+l), (a+i)); //backtrack
       }
   }
}
 
/* Driver program to test above functions */
int main()
{
    char str[] = "ABC";
    int n = strlen(str);
    permute(str, 0, n-1);
    return 0;
}


Explain different memory section in processor?

In embedded system, Memory is divided in to 4 segments. text, data, heap and stack segment.

In text segment(code segment) is used for having the program code.

Heap segment is used for dynamic memory allocation. when malloc, calloc, realloc is used for memory allocation memory is allocate from heap section.

Stack segment is used for storing the local variables. Program can use some of the processor registers and stack for storing the local variable. when a function call is made register which is used for storing the local variable and register used for data manipulation(depends on compiler implementation, processor and number of register used in the function called) will be pushed into stack. when returning from the function these registers will also be popped from stack.

Data segment is divided into 2 segment, initialized and uninitialized data segment.

Initialized data segment contains all static and global variables initialized with non zero values.

Uninitialized data segment (also called as BSS – “Block Started by Symbol”) contains all uninitialized static and global variables initialized with zero. ‘C’ Compiler will initialized this segment with zero before calling main().

example

static int a; -> uninitialized segment

static int a =10; -> initialized segment

ROM – text

RAM – data + heap + stack.