Tuesday, November 2, 2010

C INTERVIEW QUESTIONS : 2

Which of the following statements are correct about the program?
#include
int main()
{
    int x = 30, y = 40;
    if(x == y)
        printf("x is equal to y\n");

    else if(x > y)
        printf("x is greater than y\n");

    else if(x < y)
        printf("x is less than y\n")
    return 0;
}
A.
Error: Statement missing
B.
Error: Expression syntax
C.
Error: Lvalue required
D.
Error: Rvalue required
Answer: Option A
Explanation:
This program will result in error "Statement missing ;"
printf("x is less than y\n") here ; is added to the end of this statement.
Learn more problems on : Control Instructions
Discuss about this problem : Discuss in Forum
2.The modulus operator cannot be used with a long double.
A.
True
B.
False
Answer: Option A
Explanation:
fmod(x,y) - Calculates x modulo y, the remainder of x/y. 
This function is the same as the modulus operator. But fmod() performs floating point orlong double divisions.
Learn more problems on : Control Instructions
Discuss about this problem : Discuss in Forum
3.Which of the following correctly shows the hierarchy of arithmetic operations in C?
A.
/ + * -
B.
* - / +
C.
+ - / *
D.
/ * + -
Answer: Option D
Explanation:
Simply called as BODMAS (Bracket of Division, Multiplication, Addition and Subtraction).
How Do I Remember ? BODMAS !
  • B - Brackets first

  • O - Orders (ie Powers and Square Roots, etc.)

  • DM - Division and Multiplication (left-to-right)

  • AS - Addition and Subtraction (left-to-right)

  • Learn more problems on : Expressions
    Discuss about this problem : Discuss in Forum
    4.What will be the output of the program?
    #include
    
    int main()
    {
        void fun(char*);
        char a[100];
        a[0] = 'A'; a[1] = 'B';
        a[2] = 'C'; a[3] = 'D';
        fun(&a[0]);
        return 0;
    }
    void fun(char *a)
    {
        a++;
        printf("%c", *a);
        a++;
        printf("%c", *a);
    }
    
    A.
    AB
    B.
    BC
    C.
    CD
    D.
    No output
    Answer: Option B
    Learn more problems on : Functions
    Discuss about this problem : Discuss in Forum
    5.If the size of integer is 4bytes. What will be the output of the program?
    #include
    
    int main()
    {
        int arr[] = {12, 13, 14, 15, 16};
        printf("%d, %d, %d\n", sizeof(arr), sizeof(*arr), sizeof(arr[0]));
        return 0;
    }
    
    A.
    10, 2, 4
    B.
    20, 4, 4
    C.
    16, 2, 2
    D.
    20, 2, 2
    Answer: Option B
    Learn more problems on : Pointers
    Discuss about this problem : Discuss in Forum
    6.What will be the output of the program ?
    #include
    
    int main()
    {
        printf(5+"Good Morning\n");
        return 0;
    }
    
    A.
    Good Morning
    B.
    Good
    C.
    M
    D.
    Morning
    Answer: Option D
    Explanation:
    printf(5+"Good Morning\n"); It skips the 5 characters and prints the given string.
    Hence the output is "Morning"
    Learn more problems on : Strings
    Discuss about this problem : Discuss in Forum
    7.What will be the output of the program If characters 'a', 'b' and 'c' enter are supplied as input?
    #include
    
    int main()
    {
        void fun();
        fun();
        printf("\n");
        return 0;
    }
    void fun()
    {
        char c;
        if((c = getchar())!= '\n')
            fun();
        printf("%c", c);
    }
    
    A.
    abc abc
    B.
    bca
    C.
    Infinite loop
    D.
    cba
    Answer: Option D
    Explanation:
    Step 1void fun(); This is the prototype for the function fun().
    Step 2fun(); The function fun() is called here.
    The function fun() gets a character input and the input is terminated by an enter key(New line character). It prints the given character in the reverse order.
    The given input characters are "abc"
    Output: cba
    Learn more problems on : Strings
    Discuss about this problem : Discuss in Forum
    8.Which of the following statements are correct about the program below?
    #include
    
    int main()
    {
        char str[20], *s;
        printf("Enter a string\n");
        scanf("%s", str);
        s=str;
        while(*s != '\0')
        {
            if(*s >= 97 && *s <= 122)
                *s = *s-32;
            s++;
        }
        printf("%s",str);
        return 0;
    }
    
    A.
    The code converts a string in to an integer
    B.
    The code converts lower case character to upper case
    C.
    The code converts upper case character to lower case
    D.
    Error in code
    Answer: Option B
    Explanation:
    This program converts the given string to upper case string.
    Output:
    Enter a string: indiabix
    INDIABIX
    Learn more problems on : Strings
    Discuss about this problem : Discuss in Forum
    9.What will be the output of the program ?
    #include
    
        struct course
        {
            int courseno;
            char coursename[25];
        };
    int main()
    {
        struct course c[] = { {102, "Java"}, 
                              {103, "PHP"}, 
                              {104, "DotNet"}     };
    
        printf("%d", c[1].courseno);
        printf("%s\n", (*(c+2)).coursename);
        return 0;
    }
    
    A.
    103 Dotnet
    B.
    102 Java
    C.
    103 PHP
    D.
    104 DotNet
    Answer: Option A
    Learn more problems on : Structures, Unions, Enums
    Discuss about this problem : Discuss in Forum
    10.If the file 'source.txt' contains a line "Be my friend" which of the following will be the output of below program?
    #include
    
    int main()
    {
        FILE *fs, *ft;
        char c[10];
        fs = fopen("source.txt", "r");
        c = getc(fs);
        fseek(fs, 0, SEEK_END);
        fseek(fs, -3L, SEEK_CUR);
        fgets(c, 5, fs);
        puts(c);
        return 0;
    }
    
    A.
    friend
    B.
    frien
    C.
    end
    D.
    Error in fseek();
    Answer: Option C
    Explanation:
    The file source.txt contains "Be my friend".
    fseek(fs, 0, SEEK_END); moves the file pointer to the end of the file.
    fseek(fs, -3L, SEEK_CUR); moves the file pointer backward by 3 characters.
    fgets(c, 5, fs); read the file from the current position of the file pointer.
    Hence, it contains the last 3 characters of "Be my friend".
    Therefore, it prints "end".
    Learn more problems on : Input / Output
    Discuss about this problem : Discuss in Forum
    11.Point out the error in the program?
    #include
    
    int main()
    {
        FILE *fp;
        fp=fopen("trial", "r");
        fseek(fp, 20, SEEK_SET);
        fclose(fp);
        return 0;
    }
    
    A.
    Error: unrecognised Keyword SEEK_SET
    B.
    Error: fseek() long offset value
    C.
    No error
    D.
    None of above
    Answer: Option B
    Explanation:
    Instead of 20 use 20L since fseek() need a long offset value.
    Learn more problems on : Input / Output
    Discuss about this problem : Discuss in Forum
    12.What will be the output of the program (myprog.c) given below if it is executed from the command line?
    cmd> myprog one two three
    /* myprog.c */
    #include
    #include
    
    int main(int argc, char **argv)
    {
        printf("%s\n", *++argv);
        return 0;
    }
    
    A.
    myprog
    B.
    one
    C.
    two
    D.
    three
    Answer: Option B
    Learn more problems on : Command Line Arguments
    Discuss about this problem : Discuss in Forum
    13.What will be the output of the program (sample.c) given below if it is executed from the command line?
    cmd> sample 1 2 3
    /* sample.c */
    #include
    
    int main(int argc, char *argv[])
    {
        int j;
        j = argv[1] + argv[2] + argv[3];
        printf("%d", j);
        return 0;
    }
    
    A.
    6
    B.
    sample 6
    C.
    Error
    D.
    Garbage value
    Answer: Option C
    Learn more problems on : Command Line Arguments
    Discuss about this problem : Discuss in Forum
    14.What will be the output of the program (sample.c) given below if it is executed from the command line?
    cmd> sample friday tuesday sunday
    /* sample.c */
    #include
    
    int main(int sizeofargv, char *argv[])
    {
        while(sizeofargv)
            printf("%s", argv[--sizeofargv]);
        return 0;
    }
    
    A.
    sample friday tuesday sunday
    B.
    sample friday tuesday
    C.
    sunday tuesday friday sample
    D.
    sunday tuesday friday
    Answer: Option C
    Learn more problems on : Command Line Arguments
    Discuss about this problem : Discuss in Forum
    15.Which of the following statements are correct about the program?
    #include
    char *fun(unsigned int num, int base);
    
    int main()
    {
        char *s;
        s=fun(128, 2);
        s=fun(128, 16);
        printf("%s\n",s);
        return 0;
    }
    char *fun(unsigned int num, int base)
    {
        static char buff[33];
        char *ptr = &buff[sizeof(buff)-1];
        *ptr = '\0';
        do
        {
            *--ptr = "0123456789abcdef"[num %base];
            num /=base;
        }while(num!=0);
        return ptr;
    }
    
    A.
    It converts a number to a given base.
    B.
    It converts a number to its equivalent binary.
    C.
    It converts a number to its equivalent hexadecimal.
    D.
    It converts a number to its equivalent octal.
    Answer: Option A
    Learn more problems on : Bitwise Operators
    Discuss about this problem : Discuss in Forum
    16.Point out the error in the program.
    #include
    #define MAX 128
    
    int main()
    {
        char mybuf[] = "India";
        char yourbuf[] = "BIX";
        char const *ptr = mybuf;
        *ptr = 'a';
        ptr = yourbuf;
        return 0;
    }
    
    A.
    Error: cannot convert ptr const value
    B.
    Error: unknown pointer conversion
    C.
    No error
    D.
    None of above
    Answer: Option A
    Explanation:
    Step 1char mybuf[] = "India"; The variable mybuff is declared as an array of characters and initialized with string "India".
    Step 2char yourbuf[] = "BIX"; The variable yourbuf is declared as an array of characters and initialized with string "BIX".
    Step 3char const *ptr = mybuf; Here, ptr is a constant pointer, which points at achar.
    The value at which ptr it points is a constant; it will be an error to modify the pointed character; There will not be any error to modify the pointer itself.
    Step 4*ptr = 'a'; Here, we are changing the value of ptr, this will result in the error "cannot modify a const object".
    Learn more problems on : Const
    Discuss about this problem : Discuss in Forum
    17.Declare the following statement?
    "An array of three pointers to chars".
    A.
    char *ptr[3]();
    B.
    char *ptr[3];
    C.
    char (*ptr[3])();
    D.
    char **ptr[3];
    Answer: Option B
    Learn more problems on : Complicated Declarations
    Discuss about this problem : Discuss in Forum
    18.What do the following declaration signify?
    int *f();
    A.
    f is a pointer variable of function type.
    B.
    f is a function returning pointer to an int.
    C.
    f is a function pointer.
    D.
    f is a simple declaration of pointer variable.
    Answer: Option B
    Learn more problems on : Complicated Declarations
    Discuss about this problem : Discuss in Forum
    19.What will be the output of the program?
    #include
    typedef unsigned long int uli;
    typedef uli u;
    
    int main()
    {
        uli a;
        u b = -1;
        a = -1;
        printf("%lu, %lu"a, b);
        return 0;
    }
    
    A.
    4343445454, 4343445454
    B.
    4545455434, 4545455434
    C.
    4294967295, 4294967295
    D.
    Garbage values
    Answer: Option C
    Learn more problems on : Complicated Declarations
    Discuss about this problem : Discuss in Forum
    20.Can you use the fprintf() to display the output on the screen?
    A.
    Yes
    B.
    No
    Answer: Option A
    Explanation:
    Do like this fprintf(stdout, "%s %d %f", str, i, a);
    Learn more problems on : Library Functions

    No comments: