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.

    Thursday, February 25, 2010

    Q 16.)

    #include
    using namespace std;

    class Base {
    public:
    Base()
    {
    foo();
    }

    virtual void f()
    {
    cout << "\nBase::f is being called\n";
    }

    void foo()
    {
    f();
    }
    };

    class Derive : public Base {
    public:
    virtual void f()
    {
    cout << "\nDerive::f is being called\n";
    }
    };

    int main()
    {
    Derive d; //Base::f is being called.. f being virtual.. Why derived f() is not being called..
    }

    why output is not expected..




    Q.17)

    int main()
    {
    int i=10, j=2;
    int *ip= &i, *jp = &j;
    int k = *ip/*jp;
    printf("%d",k);
    return 0;
    }

    Is it a valid c-program ? Give reasons.






    Q.18)

    int main()
    {
    float f=5,g=10;
    enum{i=10,j=20,k=50};
    printf("%d\n",++k);
    printf("%f\n",f<<2);
    printf("%lf\n",f%g);
    printf("%lf\n",fmod(f,g));
    }

    What is the problem with this code ?





    Q.19)

    What is the output of this two codes:

    int main()
    {
    signed char i=0;
    for(;i>=0;i++) ;
    printf("%d\n",i);
    }

    int main()
    {
    unsigned char i=0;
    for(;i>=0;i++) ;
    printf("%d\n",i);
    }


    Why is the difference?





    Q.20)

    int main()
    {
    char i=0;
    for(;i>=0;i++) ;
    printf("%d\n",i);
    }

    Is it a valid C program ? Give reasons.

    Wednesday, February 24, 2010

    Q 11.)
    Is there a way to multiply two matrix in less than O(n^3) time complexity..

    For this Q algorithm was asked, if you guys don't know the answer then make a comment below and I will tell the answer or just take some efforts to search it.



    Q 12.)

    #include
    using namespace std;

    int main()
    {
    int a,c,foo;
    char *p;
    cin>>a>>c;// give two numbers..

    p=(char *)a;
    foo= (int)&p[c];
    cout<return 0;
    }

    what this code snippet does...
    even if u get the answer explain the logic.
    without executing




    Q 13.)

    whats wrong with this program..

    #include
    using namespace std;

    class foo
    {
    const int a;
    public:
    foo():a(10)
    {}

    void show()
    {
    cout<}
    };

    int main()
    {
    const foo obj;
    obj.show();
    }



    Q 14.)

    #include
    using namespace std;

    int main()
    {
    int *p,*c;
    p=(int*)10;
    c=(int*)20;
    cout<<(int)p<<(int)c;
    }

    is this a valid program..
    if no why..
    if yes why..




    Q 15.) #include
    using namespace std;

    int main()
    {
    void *vptr = malloc(sizeof(int)*2);
    int p;
    p=(int)vptr;
    *((int*)p +0)=9;
    *((int*)p +1)=10;
    cout<<1[(int*)vptr]; }

    comment on this program and write the output if any
    Q 6.)
    #include
    int main()
    {
    int a=3, b = 5;

    printf(&"Ya!Hello! how is this? %s\n"[a], &b["junk/super"]);
    printf(&"WHAT%c%c%c %c%c %c !\n"[a], 1["this"],
    2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);
    return 0;
    }


    is this a valid c program....



    Q 7.)
    The following C program segfaults of IA-64, but works fine on IA-32.

    int main()
    { int* p;
    p = (int*)malloc(sizeof(int));
    *p = 10;
    return 0; }


    Why does it happen so?



    Q 8.)

    #include
    #define f(a,b) a##b
    #define g(a) #a
    #define h(a) g(a)

    int main()
    {
    printf("%s\n",h(f(1,2)));
    printf("%s\n",g(f(1,2)));
    return 0;
    }

    guess the output...




    Q 9.)

    #include
    int main()
    {
    int a = 1,2;
    printf("a : %d\n",a);
    return 0;
    }


    is this valid C program..
    if yes..
    what is the output of this C program..
    say it without executing..
    silly question..



    Q 10.)
    if this happens that u have to multiply a number by 7 without using * sign..
    which of the answer u will choose..
    justify ur answer....

    x = (x<<3)-x;

    or

    x=(x<<2) + (x<<1) + x
    Q1.)

    #include
    #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
    int array[] = {23,34,12,17,204,99,16};

    int main()
    {
    signed int d;
    printf("Total Elements in the array are => %d\n",TOTAL_ELEMENTS);
    for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
    printf("%d\n",array[d+1]);
    printf("Did u see the output..");
    return 0;
    }


    Q 2.) File1.c 1 int arr[80]; File2.c extern int *arr;
    int main()
    {
    arr[1] = 100;
    return 0;
    }
    what should be the output..


    Q 3.) #include
    int main()
    {
    int a=1;
    switch(a)
    { int b=20;
    case 1: printf("b is %d\n",b);
    break;
    default:printf("b is %d\n",b);
    break;
    }
    return 0;
    }



    Q.4)
    #include
    #include
    void Error(char* s)
    {
    printf(s);
    return;
    }

    int main()
    {
    int *p;
    p = (int*)malloc(sizeof(int));
    if(!p)
    {
    Error("memory error");
    Error("Quitting....\n");
    exit(1);
    }
    else
    {
    /*some stuff to use p*/
    }
    free(p);
    return 0;
    }

    Find potential problem with the error function.. a security concern..






    Q 5.)
    Easy..
    #include
    int main()
    {
    char c;
    scanf("%c",&c);
    printf("%c\n",c);
    scanf(" %c",&c);//see space before %c
    printf("%c\n",c);

    return 0;
    }

    why this runs perfect..
    but fails in this program..

    #include
    int main()
    {
    char c;
    scanf("%c",&c);
    printf("%c\n",c);
    scanf("%c",&c);//no space this time..
    printf("%c\n",c);

    return 0;
    }

    Even though we know that white spaces are ignored..
    which include space, newline and tab..
    so why the second time output is not desired..