Knowledge is Power.

  • Who you are ?

    Working on machines without understanding them ? Then you should be here..

  • Where you are ?

    Geographical location should not become a barrier to Share our knowledge.

  • What do you do ?

    Puzzles and Interview question are intended to be discussed here.

    Monday, March 8, 2010

    1.What is the output of the following program?
    #include
    #define clrscr() 10
    int main()
    {
    printf("result=%d\n",clrscr());
    return 0;
    }


    result=10
    #defines are used for textual replacement
    2.What is the output of the following program?
    #include
    #define fun(f1,f2) f1##f2
    int main()
    {
    int some100=100;
    printf("result=%d\n",fun(some,100));
    return 0;
    }
    Solution

    result=100
    To know about the ## operator Click here

    3.What is the output of the following program?
    #include
    #define FALSE -1
    #define TRUE 1
    #define NULL 0
    int main()
    {
    if(NULL)
    puts("NULL\n");
    else if(FALSE)
    puts("FALSE\n");
    else
    puts("TRUE\n");
    return 0;
    }
    Solution

    FALSE
    Preprocessor doesnt replace the vaues given inside the double quotes.The check by if condition is boolean value false. so it goes to else if part.-1 is boolean value true. hence it prints FALSE.
    4.What is the output of the following program?
    #include
    #define max 5
    #define int arr1[max]
    main()
    {
    typedef char arr2[max];
    arr1 list={0,1,2,3,4};
    arr2 name="name";
    printf("%d %s\n",list[0],name);
    return 0;
    }
    Solution

    Compier Error :arr1 undeclared
    arr2 is decared of ype array of size 5 of characters.so it can be used to declare the variable name of the type arr2.But it is not the case of arr1.Hence an error.
    #defines are used for textual replacement whereas typedefs are used for declaring new types.

    5.What is the output of the following program?
    #include
    #define int char
    main()
    {
    int i=65;
    printf("sizeof(i)=%d \n",sizeof(i));
    return 0;
    }
    Solution

    sizeof(i)=1
    #define replaces the string int by the macro char.The size of char is 1 byte

    6.What is the output of the following program?
    #include
    #define assert(cond) if(!(cond))\
    printf("assertion failed for condition %s\n",#cond)
    main()
    {
    int x=100;
    if(x==100)
    assert(x<100);
    else
    printf("No assert call\n");
    return 0;
    }
    Solution
    assertion failed for condition x<100


    7.What is the output of the following program?
    #include
    #define assert(cond) if(!(cond))\
    printf("assertion failed for condition %s\n",#cond)
    main()
    {
    int x=100;
    if(x==0)
    assert(x<100);
    else
    printf("No assert call\n");
    return 0;
    }
    Solution
    No output
    The else part in which the printf is there becomes the else for if in the assert macro.Hence nothing is printed.The solution is to use conditional operator instead of if statement.#define assert(cond) ((cond)?(0):printf(""))


    8.What is the output of the following Program?
    #include
    #define string_def char*
    main()
    {
    typedef char* string_type;
    string_type s1="My",s2="name";
    string_def s3="is",s4="Suni";
    printf("%s %s %s %s\n",s1,s2,s3,s4);
    return 0;
    }
    Solution
    Segmentation Fault
    The preprocessor output is
    main()
    {
    typedef char* string_type;
    string_type s1="My",s2="name";
    char* s3="is",s4="Suni";
    printf("%s %s %s %s\n",s1,s2,s3,s4);
    return 0;
    }
    s4 is of type char not char* so printing s4 as a string produce segmentation fault.

    9.What is the output of the following program?
    #include
    #define DEF(array,type) sizeof(array)/sizeof(type)
    main()
    {
    int arr[10];
    printf("Total number of array elements=%d",DEF(arr,int));
    return 0;
    }
    Solution
    Total number of array elements=10
    The size of integer array of 10 elements is 10*sizeof(int).The macro expands to sizeof(arr)/sizeof(int)=>10*sizeof(int)/sizeof(int).So the answer is 10


    10.What is the output of the following Program?
    #include
    #define max main()
    main()
    {
    max;
    printf("Hello Welcome\n");
    return 0;
    }

    1. Compilation error
    2. Preprocessing error
    3. runtime error
    4. executes until the stack overflows

    Solution
    4.executes until the stack overflows

    0 comments:

    Post a Comment