What is the output of the program.docx

Embed Size (px)

Citation preview

  • 7/27/2019 What is the output of the program.docx

    1/34

    What is the output of the program?

    #include

    int main()

    {

    union a{

    int i;

    char ch[2];

    };

    union a u;

    u.ch[0] = 3;

    u.ch[1] = 2;

    printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);

    return 0;

    }

    A.

    3, 2, 515 B.515, 2, 3C.3, 2, 5 D.None of these

    Answer & Explanation

    Answer: Option A

    Explanation:

    printf (" %d, %d, %d\n" , u.ch[0], u.ch[1], u.i);It prints the value ofu.ch[0] = 3, u.ch[1] = 2

    and it prints the value ofu.imeans the value of entire union size.

    So the output is 3, 2, 515.

    #include

    int main()

    {

  • 7/27/2019 What is the output of the program.docx

    2/34

    unsigned int i = 65535; /* Assume 2 byte integer*/

    while(i++ != 0)

    printf("%d",++i);

    printf("\n");

    return 0;

    }A.Infinite loop

    B. 0 1 2 ... 65535

    C.0 1 2 ... 32767 - 32766 -32765 -1 0

    D.No output

    Answer & Explanation

    Answer: Option A

    Explanation:

    Here unsigned in tsize is 2 bytes. It varies from 0,1,2,3, ... to 65535.

    Step 1:unsigned int i = 65535;

    Step 2:

    Loop 1: while(i++ != 0)this statement becomes whi le(65535 != 0). Hence the while(TRUE)

    condition is satisfied. Then the printf(" %d" , ++i);prints '1'(variable ' i 'is already

    increemented by '1' in while statement and now increemented by '1' in printf statement)

    Loop 2: while(i++ != 0)this statement becomes while(1 != 0). Hence the while(TRUE)

    condition is satisfied. Then the printf(" %d" , ++i);prints '3'(variable ' i 'is already

    increemented by '1' in while statement and now increemented by '1' in printf statement)

    ........

    The while loop will never stops executing, because variable iwill never become '0'(zero).

    Hence it is an 'Infinite loop'.

    What will be the output of the program?

    #include

    int main()

    {

    float a = 0.7;

    if(0.7 > a)printf("Hi\n");

    else

    printf("Hello\n");

    return 0;

    }

    A.Hi B.Hello

    C.Hi Hello D.None of above

  • 7/27/2019 What is the output of the program.docx

    3/34

    Answer & Explanation

    Answer: Option A

    Explanation:

    if (0.7 > a)here ais a float variable and 0.7is a double constant. The double constant 0.7is

    greater than the float variable a. Hence the i fcondition is satisfied and it prints 'H i '

    Example:

    #include

    int main()

    {

    float a=0.7;

    printf("%.10f %.10f\n",0.7, a);

    return 0;

    }

    Output:

    0.7000000000 0.6999999881

    #include

    int main()

    {

    int i=3;

    switch(i)

    {

    case 1:

    printf("Hello\n");

    case 2:

    printf("Hi\n");

    case 3:

    continue;

    default:

    printf("Bye\n");

    }

    return 0;

    }

    A.Error: Misplaced continue B.Bye

    C.No output D.Hello Hi

    Answer & Explanation

    Answer: Option A

  • 7/27/2019 What is the output of the program.docx

    4/34

    Explanation:

    The keyword continuecannot be used in swi tch case. It must be used in foror whileor do

    whileloop. If there is any looping statement in switch case then we can use continue.

    What will be the output of the program?

    #include

    int main()

    {

    int i = 1;

    switch(i)

    {

    printf("Hello\n");

    case 1:

    printf("Hi\n");

    break;

    case 2:printf("\nBye\n");

    break;

    }

    return 0;

    }

    A.Hello

    HiB.

    Hello

    Bye

    C.Hi D.Bye

    Answer & Explanation

    Answer: Option C

    Explanation:

    switch(i)has the variable iit has the value '1'(one).

    Then case 1:statements got executed. so, it prints "Hi". The break;statement make the

    program to be exited from switch-case statement.

    switch-casedo not execute any statements outside these blocks caseand default

    Hence the output is "Hi".Which of the following statements are correct about the below program?

    #include

    int main()

    {

    int i = 10, j = 20;

    if(i = 5) && if(j = 10)

  • 7/27/2019 What is the output of the program.docx

    5/34

    printf("Have a nice day");

    return 0;

    }

    A.Output: Have a nice day

    B. o output

    C. rror: Expression syntaxD. rror: Undeclared identifier i f

    Answer & Explanation

    Answer: Option C

    Explanation:

    "Expression syntax" error occur in this line i f(i = 5) & & if(j = 10).

    It should be like if((i == 5) && (j == 10)).

    Assunming, integer is 2 byte, What will be the output of the program?#include

    int main()

    {

    printf("%x\n", -2

  • 7/27/2019 What is the output of the program.docx

    6/34

    Answer & Explanation

    Answer: Option A

    Explanation:

    The range oflong doubleis 3.4E-4932

    to 1.1E+4932

    #include

    int main()

    {

    float f=43.20;

    printf("%e, ", f);

    printf("%f, ", f);

    printf("%g", f);

    return 0;

    }

    A.

    4.320000e

    +

    , 43.200001, 43.2 B.4.3, 43.22, 43.21C.4.3e, 43.20f, 43.00 D.Error

    Answer & Explanation

    Answer: Option A

    Explanation:

    printf(" %e, " , f);Here '%e' specifies the "Scientific Notation" format. So, it prints the

    43.20 as 4.320000e+01

    .

    printf(" %f, " , f);Here '%f' specifies the "Decimal Floating Point" format. So, it prints the43.20 as 43.200001.

    printf(" %g, " , f);Here '%g' "Use the shorter of %e or %f". So, it prints the 43.20 as 43.2.

    What will be the output of the program?

    #include

    int reverse(int);

    int main()

    {

    int no=5;reverse(no);

    return 0;

    }

    int reverse(int no)

    {

    if(no == 0)

    return 0;

  • 7/27/2019 What is the output of the program.docx

    7/34

    else

    printf("%d,", no);

    reverse (no--);

    }

    A.Print 5, 4, 3, 2, 1 B.Print 1, 2, 3, 4, 5

    C.

    Print 5, 4, 3, 2, 1, 0 D.Infinite loopAnswer & Explanation

    Answer: Option D

    Explanation:

    Step 1: int no=5;The variable nois declared as integer type and initialized to 5.

    Step 2: reverse(no);becomes reverse(5);It calls the function reverse()with '5' as parameter.

    The function reverse accept an integer number 5 and it returns '0'(zero) if(5 == 0) if thegiven number is '0'(zero) or else printf(" %d," , no);it prints that number 5 and calls the

    function reverse(5);.

    The function runs infinetely because the there is a post-decrement operator is used. It will

    not decrease the value of 'n' before calling the reverse() function. So, it calls reverse(5)

    infinitely.

    Note: If we use pre-decrement operator like reverse(--n), then the output will be 5, 4, 3, 2, 1.

    Because before calling the function, it decrements the value of 'n'.

    5. What will be the output of the program?

    #include

    void fun(int);

    typedef int (*pf) (int, int);

    int proc(pf, int, int);

    int main()

    {

    int a=3;

    fun(a);

    return 0;

    }void fun(int n)

    {

    if(n > 0)

    {

    fun(--n);

    printf("%d,", n);

    fun(--n);

  • 7/27/2019 What is the output of the program.docx

    8/34

    }

    }

    A. , 2, 1, 0 B. 1, 1, 2, 0

    C. , 1, 0, 2 D.0, 1, 2, 0

    Answer & Explanation

    Answer: Option D

    What will be the output of the program?

    #include

    int fun(int(*)());

    int main()

    {

    fun(main);

    printf("Hi\n");

    return 0;

    }int fun(int (*p)())

    {

    printf("Hello ");

    return 0;

    }

    A. nfinite loop B. Hi

    C. ello Hi D.Error

    Answer & Explanation

    Answer: Option C

    #include

    int main()

    {

    int a=10;

    void f();

    a = f();

    printf("%d\n", a);

    return 0;

    }

    void f()

    {

    printf("Hi");

    }

    A.Error: Not allowed assignment

    B. Error: Doesn't print anything

    C.No error

    D.None of above

    Answer & Explanation

  • 7/27/2019 What is the output of the program.docx

    9/34

    Answer: Option A

    Explanation:

    The function void f ()is not visible to the compiler while going through main() function. Soe have to declare this prototype void f ();before to main() function. This kind of error will

    not occur in modern compilers.

    #include

    int main()

    {

    printf("%p\n", main());

    return 0;

    }

    A.It prints garbage values infinitely

    B.

    Runs infinitely without printing anythingC.Error: main()cannot be called inside printf()

    D.No Error and print nothing

    Answer & Explanation

    Answer: Option B

    Explanation:

    In printf(" %p\n" , main());it calls the main() function and then it repeats infinetly, untill

    stack overflow.

    If return type for a function is not specified, it defaults to intA. rue B.False

    Answer & Explanation

    Answer: Option A

    Explanation:

    True, The default returntype for a function is int.

    #include

    #define JOIN(s1, s2) printf("%s=%s %s=%s \n", #s1, s1, #s2, s2);

    int main(){

    char *str1="India";

    char *str2="BIX";

    JOIN(str1, str2);

    return 0;

    }

    A.str1=IndiaBIX str2=BIX B.str1=India str2=BIX

  • 7/27/2019 What is the output of the program.docx

    10/34

    C.str1=India str2=IndiaBIX D.Error: in macro substitution

    Answer & Explanation

    Answer: Option B

    #include

    #define CUBE(x) (x*x*x)

    int main()

    {

    int a, b=3;

    a = CUBE(b++);

    printf("%d, %d\n", a, b);

    return 0;

    }

    A.9, 4 B.27, 4

    C.27, 6 D.Error

    Answer & Explanation

    Answer: Option C

    Explanation:

    The macro function CUBE(x) (x*x* x)calculates the cubic value of given number(Eg: 103.)

    Step 1: in t a, b=3;The variable aand bare declared as an integer type and varaible bid

    initialized to 3.

    Step 2: a = CUBE(b++);becomes

    => a = b++ * b++ * b++;

    => a = 3 * 3 * 3;Here we are using post-increement operator, so the 3 is not incremented in

    this statement.

    => a = 27;Here, 27 is store in the variable a. By the way, the value of variable bis

    incremented by 3. (ie: b=6)

    Step 3: printf(" %d, %d\n" , a, b);It prints the value of variable aand b.

    Hence the output of the program is 27, 6.

    #include

    int main()

    {

    static char *s[] = {"black", "white", "pink", "violet"};

  • 7/27/2019 What is the output of the program.docx

    11/34

    char **ptr[] = {s+3, s+2, s+1, s}, ***p;

    p = ptr;

    ++p;

    printf("%s", **p+1);

    return 0;

    }A.ink B.ack

    C.ite D.let

    Answer & Explanation

    Answer: Option A

    What will be the output of the program?

    #include

    int main()

    {

    int arr[3] = {2, 3, 4};

    char *p;

    p = arr;

    p = (char*)((int*)(p));

    printf("%d, ", *p);

    p = (int*)(p+1);

    printf("%d", *p);

    return 0;

    }

    A.2, 3 B. 2, 0

    C.2, Garbage value D.0, 0

    Answer & Explanation

    Answer: Option B

    15. What will be the output of the program ?

    #include

    int main()

    {

    printf("%c\n", 7["IndiaBIX"]);

    return 0;

    }

    A.Error: in printf B. Nothing will print

  • 7/27/2019 What is the output of the program.docx

    12/34

    C.print "X" of IndiaBIX D.print "7"

    Answer & Explanation

    Answer: Option C

    20. What will be the output of the program ?

    #include

    int main()

    {

    char str1[] = "India";

    char str2[] = "BIX";

    char *s1 = str1, *s2=str2;

    while(*s1++ = *s2++)

    printf("%s", str1);

    printf("\n");

    return 0;

    }

    A.IndiaBIX B. BndiaBIdiaBIXia

    C.India D.(null)

    Answer & Explanation

    Answer: Option B

    21. What will be the output of the program ?

    #include

    #include

    int main()

    {

    int i, n;

    char *x="Alice";

    n = strlen(x);

    *x = x[n];

    for(i=0; i

  • 7/27/2019 What is the output of the program.docx

    13/34

    A.Alice B. ecilA

    C.Alice lice ice ce e D.lice ice ce e

    Answer & Explanation

    Answer: Option D

    Explanation:

    If you compile and execute this program in windows platform with Turbo C, it will give

    "lice ice ce e".

    It may give different output in other platforms (depends upon compiler and machine).

    The online C compiler given in this site will give the Option C as output (it runs on

    Linux platform).

    22. What will be the output of the program ?

    #include

    int main()

    {

    int i, a[] = {2, 4, 6, 8, 10};

    change(a, 5);

    for(i=0; i

  • 7/27/2019 What is the output of the program.docx

    14/34

    float a=3.14;

    char *j;

    j = (char*)&a;

    printf("%d\n", *j);

    return 0;

    }A.

    It prints ASCII value of the binary number present in the first byte of a float variable

    a.

    B.It prints character equivalent of the binary number present in the first byte of a float

    variable a.

    C.It will print 3

    D.It will print a garbage value

    Answer & Explanation

    Answer: Option A

    In the following program add a statement in the function fact()such that the factorial

    gets stored inj.

    #include

    void fact(int*);

    int main()

    {

    int i=5;

    fact(&i);

    printf("%d\n", i);

    return 0;

    }

    void fact(int *j)

    {

    static int s=1;

    if(*j!=0)

    {

    s = s**j;

    *j = *j-1;

    fact(j);

    /* Add a statement here */

    }

    }

    A. =s; B. *j=s;

    C.*j=&s; D.&j=s;

  • 7/27/2019 What is the output of the program.docx

    15/34

    Answer & Explanation

    Answer: Option B

    1. What will be the output of the program ?

    #include

    int main()

    {

    int a[5] = {5, 1, 15, 20, 25};

    int i, j, m;

    i = ++a[1];

    j = a[1]++;

    m = a[i++];

    printf("%d, %d, %d", i, j, m);

    return 0;

    }A.2, 1, 15 B. 1, 2, 5

    C.3, 2, 15 D.2, 3, 20

    Answer & Explanation

    Answer: Option C

    Explanation:

    Step 1: in t a[5] = {5, 1, 15, 20, 25};The variable arr is declared as an integer array witha size of 5 and it is initialized to

    a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25.

    Step 2: int i , j, m;The variable i,j,m are declared as an integer type.

    Step 3: i = ++a[1];becomes i = ++1;Hence i = 2and a[1] = 2

    Step 4:j = a[1]++;becomesj = 2++;Hencej = 2and a[1] = 3.

    Step 5: m = a[i++];becomes m = a[2];Hence m = 15and iis incremented by 1(i++means 2++ so i=3)

    Step 6: printf (" %d, %d, %d" , i, j, m);It prints the value of the variables i, j, m

    Hence the output of the program is 3, 2, 15

    . What will be the output of the program If characters 'a', 'b' and 'c' enter are supplied

  • 7/27/2019 What is the output of the program.docx

    16/34

    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 & Explanation

    Answer: Option D

    Explanation:

    Step 1: void fun ();This is the prototype for the function fun().

    Step 2: fun();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

    7. What will be the output of the program ?

    #include

    int main()

    {

    printf("India", "BIX\n");

    return 0;

  • 7/27/2019 What is the output of the program.docx

    17/34

    }

    A.Error B. India BIX

    C.India D.BIX

    Answer & Explanation

    Answer: Option C

    Explanation:

    printf (" India" , " BIX\n" );It prints "India". Because ,(comma) operator has Left to

    Right associativity. After printing "India", the statement got terminated.

    12. What will be the output of the program ?

    #include

    #include

    int main()

    {

    static char s[] = "Hello!";

    printf("%d\n", *(s+strlen(s)));

    return 0;

    }

    A.8 B. 0

    C.16 D.Error

    Answer & Explanation

    Answer: Option B

    13. What will be the output of the program ?

    #include

    int main()

    {

    static char s[25] = "The cocaine man";

    int i=0;

    char ch;

    ch = s[++i];

    printf("%c", ch);

    ch = s[i++];

    printf("%c", ch);

    ch = i++[s];

    printf("%c", ch);

    ch = ++i[s];

  • 7/27/2019 What is the output of the program.docx

    18/34

    printf("%c", ch);

    return 0;

    }

    A.hhe! B. he c

    C.The c D.

    Hhec

    Answer & Explanation

    Answer: Option A

    20. What will be the output of the program ?

    #include

    int main()

    {

    char str[] = "India\0BIX\0";

    printf("%d\n", sizeof(str));

    return 0;

    }

    A.10 B. 6

    C.5 D.11

    Answer & Explanation

    Answer: Option D

    Explanation:

    The following examples may help you understand this problem:

    1. sizeof(" " )returns 1 (1*).

    2. sizeof(" I ndia" )returns 6 (5 + 1*).

    3. sizeof(" BI X" )returns 4 (3 + 1*).

    4. sizeof(" I ndia\0BI X" )returns 10 (5 + 1 + 3 + 1*).

    Here '\0' is considered as 1 char by sizeof() function.

    5. sizeof(" I ndia\0BI X\0" )returns 11 (5 + 1 + 3 + 1 + 1*).

    Here '\0' is considered as 1 char by sizeof() function.

    21. What will be the output of the program ?

    #include

  • 7/27/2019 What is the output of the program.docx

    19/34

    int main()

    {

    char str[25] = "IndiaBIX";

    printf("%s\n", &str+2);

    return 0;

    }A.Garbage value B. Error

    C.No output D.diaBIX

    Answer & Explanation

    Answer: Option A

    Explanation:

    Step 1: char str[25] = " I ndiaBIX" ;The variable stris declared as an array of

    characteres and initialized with a string "IndiaBIX".

    Step 2: printf(" %s\n" , & str+2);

    => In the printf statement %sis string format specifier tells the compiler to print the

    string in the memory of& str+2

    => & stris a location of string "IndiaBIX". Therefore & str+2is another memory

    location.

    Hence it prints the Garbage value.

    #include

    void swap(char *, char *);

    int main()

    {

    char *pstr[2] = {"Hello", "IndiaBIX"};

    swap(pstr[0], pstr[1]);

    printf("%s\n%s", pstr[0], pstr[1]);

    return 0;

    }

    void swap(char *t1, char *t2)

    {

    char *t;

    t=t1;

    t1=t2;

    t2=t;

    }

    A.IndiaBIX B.Address of "Hello" and "IndiaBIX"

  • 7/27/2019 What is the output of the program.docx

    20/34

    Hello

    C.Hello

    IndiaBIXD.

    Iello

    HndiaBIX

    Answer & Explanation

    Answer: Option C

    Explanation:

    Step 1: void swap(char * , char * );This prototype tells the compiler that the function swap

    accept two strings as arguments and it does not return anything.

    Step 2: char * pstr[2] = {" Hel lo" , " IndiaBIX" };The variable pstris declared as an pointer to

    the array of strings. It is initialized to

    pstr[0] = " Hello", pstr[1] = " I ndiaBIX"

    Step 3: swap(pstr [0], pstr [1] );The swapfunction is called by "call by value". Hence it does

    not affect the output of the program.

    If the swapfunction is "called by reference" it will affect the variable pstr.

    Step 4: printf (" %s\n%s" , pstr[0], pstr[1]);It prints the value ofpstr[0]and pstr[1].

    Hence the output of the program is

    Hello

    IndiaBIX

    27. What will be the output of the program (Turbo C in 16 bit platform DOS) ?

    #include

    #include

    int main()

    {

    char *str1 = "India";

    char *str2 = "BIX";char *str3;

    str3 = strcat(str1, str2);

    printf("%s %s\n", str3, str1);

    return 0;

    }

    A.IndiaBIX India B. IndiaBIX IndiaBIX

  • 7/27/2019 What is the output of the program.docx

    21/34

    C.India India D.Error

    Answer & Explanation

    Answer: Option B

    Explanation:

    It prints 'IndiaBIX IndiaBIX' in TurboC (in 16 bit platform).

    It may cause a 'segmentation fault error' in GCC (32 bit platform).

    2. Which of the following statements are correct about the below declarations?

    char *p = " Sanjay" ;

    char a[] = " Sanjay" ;

    1: There is no difference in the declarations and both serve the same purpose.

    2:pis a non-const pointer pointing to a non-const string, whereas ais a const pointer

    pointing to a non-const pointer.

    3:The pointer pcan be modified to point to another string, whereas the individual

    characters within array acan be changed.

    4: In both cases the '\0'will be added at the end of the string "Sanjay".

    A.1, 2 B. 2, 3, 4

    C.3, 4 D.2, 3

    Answer & Explanation

    Answer: Option B

    What will be the output of the program ?

    #include

    int main()

    {

    struct value{

    int bit1:1;

    int bit3:4;

    int bit4:4;

    }bit={1, 2, 13};

    printf("%d, %d, %d\n", bit.bit1, bit.bit3, bit.bit4);

  • 7/27/2019 What is the output of the program.docx

    22/34

    return 0;

    }

    A.1, 2, 13 B. 1, 4, 4

    C.-1, 2, -3 D.-1, -2, -13

    Answer & Explanation

    Answer: Option C

    Explanation:

    Note the below statement inside the struct:

    int bit1:1;--> 'int' indicates that it is a SIGNED integer.

    For signed integers the leftmost bit will be taken for +/- sign.

    If you store 1 in 1-bit field:

    The left most bit is 1, so the system will treat the value as negative number.

    The 2's complement method is used by the system to handle the negative values.

    Therefore, the data stored is 1. The 2's complement of 1 is also 1 (negative).

    Therefore -1 is printed.

    If you store 2 in 4-bits field:

    Binary 2: 0010 (left most bit is 0, so system will treat it as positive value)

    0010 is 2

    Therefore 2 is printed.

    If you store 13 in 4-bits field:

    Binary 13: 1101 (left most bit is 1, so system will treat it as negative value)

    Find 2's complement of 1101:

    1's complement of 1101 : 0010

    2's complement of 1101 : 0011 (Add 1 to the result of 1's complement)

  • 7/27/2019 What is the output of the program.docx

    23/34

    0011 is 3 (but negative value)

    Therefore -3 is printed.

    10. What will be the output of the program ?

    #include

    int main()

    {

    struct byte

    {

    int one:1;

    };

    struct byte var = {1};

    printf("%d\n", var.one);

    return 0;

    }A.1 B. -1

    C.0 D.Error

    Answer & Explanation

    Answer: Option B

    12. If a char is 1 byte wide, an integer is 2 bytes wide and a long integer is 4 bytes wide then

    ill the following structure always occupy 7 bytes?

    struct ex

    {

    char ch;

    int i;

    long int a;

    };

    A.Yes B.No

    Answer & Explanation

    Answer: Option B

    Explanation:

    A compiler may leave holes in structures by padding the first char in the structure with

    another byte just to ensures that the integer that follows is stored at an location. Also,

    there might be 2extra bytes after the integer to ensure that the long integer is stored at

    an address, which is multiple of 4. Such alignment is done by machines to improve the

    efficiency of accessing values.

  • 7/27/2019 What is the output of the program.docx

    24/34

    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

    int main(int argc, char **argv)

    {

    printf("%c\n", **++argv);

    return 0;

    }

    A.myprog one two three B. myprog one

    C.o D.two

    Answer & Explanation

    Answer: Option C

    8. What will be the output of the program (sample.c) given below if it is executed from the

    command line?

    cmd> sample fr iday tuesday sunday

    /* sample.c */

    #include

    int main(int argc, char *argv[])

    {

    printf("%c", **++argv);

    return 0;

    }

    A.s B. f

    C.sample D.friday

    Answer & Explanation

    Answer: Option B

    What will be the output of the program (myprog.c) given below if it is executed from the

    command line?

    cmd> myprog f r iday tuesday sunday

    /* myprog.c */

    #include

    int main(int argc, char *argv[])

    {

  • 7/27/2019 What is the output of the program.docx

    25/34

    printf("%c", *++argv[1]);

    return 0;

    }

    A.r B.f

    C.

    m D.y

    Answer & Explanation

    Answer: Option A

    10. What will be the output of the program (sample.c) given below if it is executed from the

    command line?

    cmd> sample one two thr ee

    /* sample.c */

    #include

    int main(int argc, char *argv[])

    {

    int i=0;

    i+=strlen(argv[1]);

    while(i>0)

    {

    printf("%c", argv[1][--i]);

    }

    return 0;

    }

    A.three two one B. owt

    C.eno D.eerht

    Answer & Explanation

    Answer: Option C

    15. 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

    int main(int argc, char *argv[])

    {

    int i;

    for(i=1; i

  • 7/27/2019 What is the output of the program.docx

    26/34

    }

    A.oot B. ott

    C.nwh D.eoe

    Answer & Explanation

    Answer: Option B

    21. 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)

    {

    int i;

    for(i=1; i

  • 7/27/2019 What is the output of the program.docx

    27/34

    Answer: Option C

    Explanation:

    Both ceil()and floor()return the integer found as a double.

    floor(2.5)returns the largest integral value(round down) that is not greater than 2.5. So

    output is 2.000000.

    ceil(2.5)returns 3, while converting the doubleto intit returns '0'.

    So, the output is '2.000000, 0'.

    If the binary eauivalent of 5.375 in normalised form is 0100 0000 1010 1100 0000 0000

    0000 0000, what will be the output of the program (on intel machine)?

    #include

    #includeint main()

    {

    float a=5.375;

    char *p;

    int i;

    p = (char*)&a;

    for(i=0; i 4

    0000 -> 0

    1010 -> A

    1100 -> C

    0000 -> 0

    0000 -> 0

    0000 -> 0

    0000 -> 0

  • 7/27/2019 What is the output of the program.docx

    28/34

    Since the PC's (intel processors) use " LITTLE ENDIAN" byte order, the higher order

    byte of number is stored in lowest address. The result is written from bottom to top.

    #include

    int main()

    {

    float a=0.7;

    if(a < 0.7)

    printf("C\n");

    else

    printf("C++\n");

    return 0;

    }

    A.

    C B.C++C.Compiler error D.Non of above

    if (a < 0.7)here ais a float variable and 0.7is a double constant. The float variable ais less

    than double constant 0.7. Hence the i fcondition is satisfied and it prints 'C'

    #include

    #define CUBE(x) (x*x*x)

    int main()

    {

    int a, b=3;

    a = CUBE(b++);printf("%d, %d\n", a, b);

    return 0;

    }

    A.9, 4 B.27, 4

    C.27, 6 D.Error

    Answer & Explanation

    Answer: Option C

    Explanation:

    The macro function CUBE(x) (x*x* x)calculates the cubic value of given number(Eg: 103.)

    Step 1: in t a, b=3;The variable aand bare declared as an integer type and varaible bid

    initialized to 3.

    Step 2: a = CUBE(b++);becomes

  • 7/27/2019 What is the output of the program.docx

    29/34

    => a = b++ * b++ * b++;

    => a = 3 * 3 * 3;Here we are using post-increement operator, so the 3 is not incremented in

    this statement.

    => a = 27;Here, 27 is store in the variable a. By the way, the value of variable bisincremented by 3. (ie: b=6)

    Step 3: printf(" %d, %d\n" , a, b);It prints the value of variable aand b.

    Hence the output of the program is 27, 6.

    What will be the output of the program?

    #include

    #define FUN(i, j) i##j

    int main(){

    int va1=10;

    int va12=20;

    printf("%d\n", FUN(va1, 2));

    return 0;

    }

    A.10 B.20

    C.1020 D.12

    Answer & Explanation

    Answer: Option B

    Explanation:

    The following program will make you understand about ## (macro concatenation) operator

    clearly.

    #include

    #define FUN(i, j) i##j

    int main()

    { int First = 10;

    int Second = 20;

    char FirstSecond[] = "IndiaBIX";

    printf("%s\n", Fun(First, Second) );

  • 7/27/2019 What is the output of the program.docx

    30/34

    return 0;

    }

    Output:

    -------

    IndiaBIX

    The preprocessor will replace Fun(F ir st, Second)as FirstSecond.

    Therefore, the printf (" %s\n" , Fun(F ir st, Second) );statement will become as printf(" %s\n" ,

    F ir stSecond );

    Hence it prints IndiaBIXas output.

    Like the same, the line printf(" %d\n" , FUN(va1, 2));given in the above question will

    become as printf(" %d\n" , va12 );.

    Therefore, it prints 20as output.

    We can prevent the same file from getting included again by using a preprocessor directive

    called #ifndef(short for "if not defined") to determine whether we've already defined a

    preprocessor symbol called XSTRING_H. If we have already defined this symbol, the

    compiler will ignore the rest of the file until it sees a #endif(which in this case is at the end

    of the file).

    Macro calls and function calls work exactly similarly.

    A.True B. False

    Answer & Explanation

    Answer: Option B

    Explanation:

    False, A macro just replaces each occurrence with the code assigned to it. e.g. SQUARE(3)

    ith ((3)*(3))in the program.

    A function is compiled once and can be called from anywhere that has visibility to the

    funciton.

  • 7/27/2019 What is the output of the program.docx

    31/34

    A macro must always be defined in capital letters.

    A.True B. False

    Answer: Option B

    Explanation:

    FALSE, The macro is case insensitive.

    Macros have a local scope.

    A.True B.False

    Answer & Explanation

    Answer: Option B

    The scope of macros is globals and functions. Also the scope of macros is only from the

    point of definition to the end of the file.

    After preprocessing all the macro in the program are removed.

    It is necessary that a header files should have a .h extension?

    A.Yes B.No

    Answer & Explanation

    Answer: Option B

    Explanation:

    No, the header files have any kind of extension.

    Will the program compile successfully?

    #include

    int main()

    {

    #ifdef NOTE

    int a;

    a=10;

    #else

    int a;

    a=20;

    #endif

    printf("%d\n", a);

    return 0;

  • 7/27/2019 What is the output of the program.docx

    32/34

    }

    A.Yes B. o

    Answer & Explanation

    Answer: Option A

    Explanation:

    Yes, this program will compile and run successfully and prints 20.The macro #ifdef NOTE evaluates the given expression to 1. If satisfied it executes the

    #ifdef block statements. Here #ifdef condition fails because the Macro NOTE is nowhere

    declared.

    Hence the #else block gets executed, the variable a is declared and assigned a value of 20.

    printf("%d\n", a); It prints the value of variable a 20.

    Will it result in to an error if a header file is included twice?

    A.Yes

    B. No

    C.It is compiler dependent.

    Answer & ExplanationAnswer: Option C

    Explanation:

    Unless the header file has taken care to ensure that if already included it doesn't get

    included again.

    Turbo C, GCC compilers would take care of these problems, generate no error.

    What will be the output of the program if the array begins at 65472 and each integer

    occupies 2 bytes?

    #include

    int main()

    {

    int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};

    printf("%u, %u\n", a+1, &a+1);

    return 0;

    }

    A.65474, 65476 B.65480, 65496

    C.65480, 65488 D.65474, 65488

    Answer & Explanation

    Answer: Option B

    Explanation:

    Step 1: in t a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};The array a[3][4] is declared as an

    integer array having the 3 rows and 4 colums dimensions.

  • 7/27/2019 What is the output of the program.docx

    33/34

    Step 2: printf(" %u, %u\n" , a+1, & a+1);

    The base address(also the address of the first element) of array is 65472.

    For a two-dimensional array like areference to array has type "pointer to array of 4 ints".

    Therefore, a+1is pointing to the memory location of first element of the second row inarray a. Hence 65472 + (4 ints * 2 bytes) = 65480

    Then, & ahas type "pointer to array of 3 arrays of 4 ints", totally 12 ints. Therefore, &a+1

    denotes "12 ints * 2 bytes * 1 = 24 bytes".

    Hence, begining address 65472 + 24 = 65496. So, &a+1= 65496

    Hence the output of the program is 65480, 65496

    3. What will be the output of the program (sample.c) given below if it is executed from thecommand line (Turbo C in DOS)?

    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. B. sample 6

    C. rror D.Garbage value

    Answer & Explanation

    Answer: Option C

    Explanation:

    Here argv[1], argv[2]and argv[3]are string type. We have to convert the string to integer

    type before perform arithmetic operation.

    Example:j = atoi (ar gv[1]) + atoi (ar gv[2]) + atoi (ar gv[3]);

    http://www.eskimo.com/~scs/cclass/int/sx4ab.html

    http://www.eskimo.com/~scs/cclass/int/sx4ab.htmlhttp://www.eskimo.com/~scs/cclass/int/sx4ab.htmlhttp://www.eskimo.com/~scs/cclass/int/sx4ab.html
  • 7/27/2019 What is the output of the program.docx

    34/34

    Because of the nature of base-2 arithmetic, it turns out that shifting left and shifting right

    are equivalent to multiplying and dividing by two. These operations are equivalent for the

    same reason that tacking zeroes on to the right of a number in base 10 is the same as

    multiplying by 10, and deleting digits from the right is the same as dividing by 10. You can

    convince yourself that 0x56 > 1 is the same as

    0x56 / 2. It's also the case that masking off all but the low-order bits is the same as taking aremainder; for example, 0x56 & 0x07 is the same as 0x56 % 8. Some programmers

    therefore use , and & in preference to *, /, and % when powers of two are involved,

    on the grounds that the bitwise operators are ``more efficient.'' Usually it isn't worth

    worrying about this, though, because most compilers are smart enough to perform these

    optimizations anyway (that is, if you write x * 4, the compiler might generate a left shift

    instruction all by itself), they're not always as readable, and they're not always correct for

    negative numbers.

    The issue of negative numbers, by the way, explains why the right-shift operator >> is not

    precisely defined when the high-order bit of the value being shifted is 1. For signed values,

    if the high-order bit is a 1, the number is negative. (This is true for 1's complement, 2'scomplement, and sign-magnitude representations.) If you were using a right shift to

    implement division, you'd want a negative number to stay negative, so on some computers,

    under some compilers, when you shift a signed value right and the high-order bit is 1, new

    1 bits are shifted in at the left instead of 0s. However, you can't depend on this, because not

    all computers and compilers implement right shift this way. In any case, shifting negative

    numbers to the right (even if the high-order 1 bit propagates) gives you an incorrect

    answer if there's a remainder involved: in 2's complement, 16-bit arithmetic, -15 is 0xfff1,

    so -15 >> 1 might give you 0xfff8shifted which is -8. But integer division is supposed to

    discard the remainder, so -15 / 2 would have given you -7. (If you're having trouble seeing

    the way the shift worked, 0xfff1 is 11111111111100012 and 0xfff8 is

    11111111111110002. The low-order 1 bit got shifted off to the right, but

    because the high-order bit was 1, a 1 got shifted in at the left.)