c Technical1

download c Technical1

of 23

Transcript of c Technical1

  • 7/28/2019 c Technical1

    1/23

    1. How many times the while loop will get executed if a short intis 2 byte wide?

    #include

    int main()

    {

    int j=1;while(j

  • 7/28/2019 c Technical1

    2/23

    Explanation:

    Simply called as BODMAS (Brackets, Order, Division, Multiplication, Addition andSubtraction).Mnemonics are often used to help students remember the rules, but the rules taught

    by the use of acronyms can be misleading. In the United States the acronym PEMDAS is

    common. It stands for Parentheses, Exponents, Multiplication, Division, Addition, Subtraction.In other English speaking countries, Parentheses may be called Brackets, or symbols of inclusion

    and Exponentiation may be called either Indices, Powers or Orders, and since multiplication and

    division are of equal precedence, M and D are often interchanged, leading to such acronyms asBEDMAS, BIDMAS, BODMAS, BERDMAS, PERDMAS, and BPODMAS.

    4. Which of the following cannot be checked in a switch-case statement?

    A. Character B. Integer C. Float D. enum

    Answer: Option C

    Explanation:

    The switch/case statement in the c language is defined by the language specification to use an intvalue, so you can not use a float value. The value of the 'expression' in a switch-case statement

    must be an integer, char, short, long. Float and double are not allowed.5. What will be the output

    of the program?

    #includeint main()

    {

    int i=0;for(; i

  • 7/28/2019 c Technical1

    3/23

    Loop 4: here i=3, the condition in for(; 3 10 means 5 > 10. Hence this condition will be failed. So it prints

    variable str.

    Hence the output is "C-program".7. What will be the output of the program?

    #include

    int main()

    {

    int x = 3;

    float y = 3.0;

  • 7/28/2019 c Technical1

    4/23

    if(x == y)

    printf("x and y are equal");

    else

    printf("x and y are not equal");

    return 0;

    }

    A. x and y are equal B. x and y are not equal C. Unpredictable D. No output

    Answer: Option A

    Explanation:

    Step 1: int x = 3; here variable x is an integer type and initialized to '3'.Step 2: float y = 3.0; here variable y is an float type and initialized to '3.0'

    Step 3: if(x == y) here we are comparing if(3 == 3.0) hence this condition is satisfied.

    Hence it prints "x and y are equal".

    8. 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 B. Hello C.Hi D. Bye

    Hi Bye

    Answer: Option CExplanation:switch(i) has the variable i it 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-case do not execute any statements outside these blocks case and default

    Hence the output is "Hi".

  • 7/28/2019 c Technical1

    5/23

    9.Point out the error, if any in the program#include

    int main(){

    int a = 10;

    switch(a){

    }

    printf("This is c program.");return 0;

    }A. Error: No case statement specified

    B. Error: No default specifiedC. No Error

    D. Error: infinite loop occurs

    Answer: Option CExplanation:

    There can exists a switch statement, which has no case.

    9. Point out the error, if any in the while loop.

    #include

    int main()

    {

    int i=1;while()

    { printf("%d\n", i++);if(i>10)

    break;

    }return 0;

    }

    A. There should be a condition in the while loop

    B. There should be at least a semicolon in the whileC. The while loop should be replaced with for loop.

    D. No error

    Answer: Option A

    Explanation: The while() loop must have conditional expression or it shows "Expression syntax"

    error.

    Example: while(i > 10){ ... }

  • 7/28/2019 c Technical1

    6/23

    10. Point out the error, if any in the program

    #include

    int main(){

    int a = 10, b;

    a >=5 ? b=100: b=200;printf("%d\n", b);return 0;

    }

    A.100 B.200 C. Error: L value required for b D. Garbage value

    Answer: Option C

    Explanation:

    Variable b is not assigned. It should be like: b = a >= 5? 100: 200;

    11. If the two strings are identical, then strcmp() function returns

    A.-1 B.1 C.0 D. Yes

    Answer: Option CExplanation:

    Declaration: strcmp(const char *s1, const char*s2);The strcmp return an int value that is

    if s1 < s2 returns a value < 0

    if s1 == s2 returns 0if s1 > s2 returns a value > 0

    12. How will you print \n on the screen?

    A. printf("\n"); B. echo "\\n"; C. printf('\n'); D. printf("\\n");

    Answer: Option D

    Explanation: The statement printf("\\n"); prints '\n' on the screen.

    13. What will happen if in a C program you assign a value to an array element whose subscriptexceeds the size of array?

    A. The element will be set to 0.B. The compiler would report an error.

    C. The program may crash if some important data gets overwritten.

    D. The array size would appropriately grow.

  • 7/28/2019 c Technical1

    7/23

    Answer: Option C

    Explanation: If the index of the array size is exceeded, the program will crash. Hence "option c"is the correct answer. But the modern compilers will take care of this kind of errors.

    14. In C, if you pass an array as an argument to a function, what actually gets passed?

    A. Value of elements in arrayB. First element of the array

    C. Base address of the array

    D. Address of the last element of arrayAnswer & Explanation

    Answer: Option C

    Explanation:

    The statement 'C' is correct. When we pass an array as a function argument, the base address of

    the array will be passed.

    15. What does the following declaration mean?

    int (*ptr)[10];A. ptr is array of pointers to 10 integers

    B. ptr is a pointer to an array of 10 integers

    C. ptr is an array of 10 integers

    D. ptr is an pointer to array

    Answer: Option B

    16. What will be the output of the program?

    #includeint 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

  • 7/28/2019 c Technical1

    8/23

    Answer: Option C

    Explanation:

    Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a 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 = 2 and a[1] = 2

    Step 4: j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.

    Step 5: m = a[i++]; becomes m = a[2]; Hence m = 15 and i is 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

    17. How will you free the allocated memory ?

    A. remove(var-name); B. free(var-name); C. delete(var-name); D.dalloc(var-name);

    Answer & Explanation

    Answer: Option B

    18. What is the similarity between a structure, union and enumeration?

    A. All of them let you define new values

    B. All of them let you define new data types

    C. All of them let you define new pointersD. All of them let you define new structures

    Answer: Option B

    19. What does fp point to in the program ?

    #include

    int main(){

    FILE *fp;

    fp=fopen("trial", "r");return 0;

    }

  • 7/28/2019 c Technical1

    9/23

    A. The first character in the file

    B. A structure which contains a char pointer which points to the first character of a file.C. The name of the file.

    D. The last character in the file.

    Answer: Option B

    Explanation:

    The fp is a structure which contains a char pointer which points to the first character of a file.

    19. Which of the following operations can be performed on the file "NOTES.TXT" using thebelow code?

    FILE *fp;

    fp = fopen("NOTES.TXT", "r+");

    A. Reading B. WritingC. Appending D. Read and Write

    Answer: Option D

    Explanation:

    r+ Open an existing file for update (reading and writing).

    20. To print out a and b given below, which of the following printf() statement will you use?

    #include

    float a=3.14;double b=3.14;

    A. printf("%f %lf", a, b);B. printf("%Lf %f", a, b);

    C. printf("%Lf %Lf", a, b);

    D. printf("%f %Lf", a, b);

    Answer: Option A

    Explanation:

    To print a float value, %f is used as format specifier.To print a double value, %lf is used as format specifier.

    Therefore, the answer is printf("%f %lf", a, b);

  • 7/28/2019 c Technical1

    10/23

    21. To scan a and b given below, which of the following scanf() statement will you use?

    #includefloat a;

    double b;

    A. scanf("%f %f", &a, &b); B. scanf("%Lf %Lf", &a, &b);C. scanf("%f %Lf", &a, &b); D. scanf("%f %lf", &a, &b);

    Answer: Option D

    Explanation:

    To scan a float value, %f is used as format specifier.

    To scan a double value, %lf is used as format specifier.Therefore, the answer is scanf("%f %lf", &a, &b);

    22. Out of fgets() and gets() which function is safe to use?A. gets() B. fgets()

    Answer: Option B

    Explanation:

    Because, In fgets() we can specify the size of the buffer into which the string supplied will bestored.

    23. Consider the following program and what will be content of t?

    #includeint main()

    {

    FILE *fp;int t;

    fp = fopen("DUMMY.C", "w");

    t = fileno(fp);printf("%d\n", t);

    return 0;

    }

    A. size of "DUMMY.C" file

    B. The handle associated with "DUMMY.C" file

    C. Garbage value

    D. Error in fileno()

    Answer: Option B

  • 7/28/2019 c Technical1

    11/23

    Explanation:

    fp = fopen("DUMMY.C", "w"); A file DUMMY.C is opened in write mode and returns the file

    pointer to fp

    t = fileno(fp); returns the handle for the fp stream and it stored in the variable t

    printf("%d\n", t); It prints the handle number.

    24. What is (void*)0?

    A. Representation of NULL pointer

    B. Representation of void pointer

    C. Error

    D. None of above

    Answer: Option A

    Explanation:

    No answer description available for this question. Let us discuss.

    View Answer C Compiler Report Discuss in Forum

    25. Can you combine the following two statements into one?

    char *p;

    p = (char*) malloc(100);

    A. char p = *malloc(100);B. char *p = (char) malloc(100);

    C. char *p = (char*)malloc(100);

    D. char *p = (char *)(malloc*)(100);

    Answer: Option C

    26. In which header file is the NULL macro defined?

    A. stdio.h B. stddef.h C. stdio.h and stddef.h D. math.h

    Answer: Option C

    Explanation:

    The macro "NULL" is defined in locale.h, stddef.h, stdio.h, stdlib.h, string.h, time.h, and

    wchar.h.

  • 7/28/2019 c Technical1

    12/23

    27.How many bytes are occupied by near, far and huge pointers (DOS)?

    A. near=2 far=4 huge=4 B. near=4 far=8 huge=8C. near=2 far=4 huge=8 D. near=4 far=4 huge=8

    Answer: Option A

    Explanation:

    near=2, far=4 and huge=4 pointers exist only under DOS. Under windows and Linux every

    pointers is 4 bytes long.

    28. If a variable is a pointer to a structure, then which of the following operator is used to access

    data members of the structure through the pointer variable?

    A. . B. & C. * D. ->

    Answer: Option D

    29. Which of the following is the correct order of evaluation for the below expression?z = x + y * z / 4 % 2 - 1

    A. * / % + - = B. = * / % + -

    C. / * % - + = D. * % / - + =

    Answer: Option A

    Explanation:C uses left associativity for evaluating expressions to break a tie between two operators having

    same precedence.

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

    31. Which of the following is the correct order if calling functions in the below code?

    a = f1(23, 14) * f2(12/4) + f3();

    A. f1, f2, f3

  • 7/28/2019 c Technical1

    13/23

    B. f3, f2, f1

    C. Order may vary from compiler to compiler

    D. None of above

    Answer: Option C

    Explanation:Here, Multiplication will happen before the addition, but in which order the functions would be

    called is undefined. In an arithmetic expression the parenthesis tell the compiler which operands

    go with which operators but do not force the compiler to evaluate everything within theparenthesis first.

    32. Which of the following are unary operators in C?

    1. !2. sizeof

    3. ~

    4. &&A. 1, 2 B. 1, 3 C. 2, 4 D. 1, 2, 3

    Answer: Option D

    Explanation:

    An operation with only one operand is called unary operation.Unary operators:

    ! Logical NOT operator.

    ~ bitwise NOT operator.

    sizeof Size-of operator.&& Logical AND is a logical operator.

    Therefore, 1, 2, 3 are unary operators.

    33. In which order do the following gets evaluated

    1. Relational

    2. Arithmetic3. Logical

    4. Assignment

    A. 2134 B. 1234 C. 4321 D. 3214

    Answer: Option A

    Explanation:1. Relational operators: >, =,

  • 7/28/2019 c Technical1

    14/23

    3. Logical operators : !, &&, ||

    4. Assignment operators: =

    34. What will be the output of the program?

    #include

    int main(){int x=12, y=7, z;

    z = x!=4 || y == 2;

    printf("z=%d\n", z);return 0;

    }

    A. z=0 B. z=1C. z=4 D. z=2

    Answer: Option B

    Explanation:

    Step 1: int x=12, y=7, z; here variable x, y and z are declared as an integer and variable x and yare initialized to 12, 7 respectively.

    Step 2: z = x!=4 || y == 2;becomes z = 12!=4 || 7 == 2;

    then z = (condition true) || (condition false); Hence it returns 1. So the value of z=1.

    Step 3: printf("z=%d\n", z); Hence the output of the program is "z=1"

    35. Assunming, integer is 2 byte, What will be the output of the program?

    #includeint main()

    {

    printf("%x\n", -2

  • 7/28/2019 c Technical1

    15/23

    1's complement of 00000000 00000010 is 11111111 11111101 (Change all 0s to 1 and 1s to 0).

    2's complement of 00000000 00000010 is 11111111 11111110 (Add 1 to 1's complement to

    obtain the 2's complement value).Therefore, in binary we represent -2 as: 11111111 11111110.

    After left shifting it by 2 bits we obtain: 11111111 11111000, and it is equal to "fff8" in

    hexadecimal system.

    36. The maximum combined length of the command-line arguments including the spaces

    between adjacent arguments is

    A. 128 charactersB. 256 characters

    C. 67 characters

    D. It may vary from one operating system to another

    Answer & Explanation

    Answer: Option D

    37. Which of the following statements are FALSE about the below code?

    int main(int ac, char *av[])

    {}

    A. ac contains count of arguments supplied at command-lineB. av[] contains addresses of arguments supplied at a command line

    C. In place of ac and av, argc and argv should be used.

    D. The variables ac and av are always local to main()

    Answer: Option C

    38.In which numbering system can the binary number 1011011111000101 be easily convertedto?

    A. Decimal system B. Hexadecimal system C. Octal system D. No need to convert

    Answer: Option B

    Explanation:

    Hexadecimal system is better, because each 4-digit binary represents one Hexadecimal digit.

    39. Which bitwise operator is suitable for turning off a particular bit in a number?A. && operator B. & operator C. || operator D. ! operator

    Answer: Option B

  • 7/28/2019 c Technical1

    16/23

    40.

    Which bitwise operator is suitable for turning on a particular bit in a number?

    A. && operator B. & operatorC. || operator D. | operator

    Answer & Explanation

    Answer: Option D

    Explanation:

    No answer description available for this question. Let us discuss.

    41. Which bitwise operator is suitable for checking whether a particular bit is on or off?

    A. && operator B. & operator C. || operator D. ! operator

    Answer: Option B

    42.Which of the following statements are correct about the program?#include

    int main()

    {

    unsigned int num;int i;

    scanf("%u", &num);

    for(i=0; i

  • 7/28/2019 c Technical1

    17/23

    43.Which of the following statements are correct about the program?#include

    int main()

    { unsigned int num;int c=0;

    scanf("%u", &num);

    for(;num;num>>=1){

    if(num & 1)

    c++;

    }printf("%d", c);

    return 0;

    }

    A. It counts the number of bits that are ON (1) in the number num.

    B. It counts the number of bits that are OFF (0) in the number num.

    C. It sets all bits in the number num to 1D. Error

    Answer: Option A

    Explanation:

    If we give input 4, it will print 1.Binary-4 == 00000000 00000100 ; Total number of bits = 1.

    If we give input 3, it will print 2.Binary-3 == 00000000 00000011 ; Total number of bits = 2.

    If we give input 511, it will print 9.Binary-511 == 00000001 11111111 ; Total number of bits = 9.

    44.What will be the output of the program?

    #include

    int main()

    {

    int y=128;const int x=y;

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

    return 0;}

  • 7/28/2019 c Technical1

    18/23

    A. 128 B. Garbage value C. Error D.0

    Answer: Option A

    Explanation:

    Step 1: int y=128; The variable 'y' is declared as an integer type and initialized to value "128".

    Step 2: const int x=y; The constant variable 'x' is declared as an integer and it is initialized with

    the variable 'y' value.

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

    Hence the output of the program is "128"

    45.What will be the output of the program?

    #include#include

    union employee{

    char name[15];

    int age;

    float salary;};

    const union employee e1;

    int main()

    {

    strcpy(e1.name, "K");printf("%s %d %f", e1.name, e1.age, e1.salary);

    return 0;

    }

    A. Error: RValue requiredB. Error: cannot convert from 'const int *' to 'int *const'

    C. Error: LValue required in strcpy

    D. No error

    Answer: Option D

    Explanation:

    The output will be (in 16-bit platform DOS): K 75 0.000000

  • 7/28/2019 c Technical1

    19/23

    46.What will be the output of the program?

    #include

    int fun(int **ptr);

    int main(){

    int i=10;

    const int *ptr = &i;fun(&ptr);

    return 0;

    }

    int fun(int **ptr){

    int j = 223;

    int *temp = &j;printf("Before changing ptr = %5x\n", *ptr);

    const *ptr = temp;

    printf("After changing ptr = %5x\n", *ptr);

    return 0;}

    A. Address of iAddress of j

    B. 10

    223

    C. Error: cannot convert parameter 1 from 'const int **' to 'int **'D. Garbage value

    Answer: Option C

    Explanation:

    No answer description available for this question. Let us discuss.

    47.What will be the output of the program?

    #include

    int main()

    {const int x=5;

    const int *ptrx;

    ptrx = &x;*ptrx = 10;

  • 7/28/2019 c Technical1

    20/23

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

    return 0;

    }

    A. 5 B. 10

    C. Error D. Garbage value

    Answer: Option C

    Explanation:

    Step 1: const int x=5; The constant variable x is declared as an integer data type and initialized

    with value '5'.

    Step 2: const int *ptrx; The constant variable ptrx is declared as an integer pointer.

    Step 3: ptrx = &x; The address of the constant variable x is assigned to integer pointer variableptrx.

    Step 4: *ptrx = 10; Here we are indirectly trying to change the value of the constant vaiable x.

    This will result in an error.

    To change the value of const variable x we have to use *(int *)&x = 10;

    View Answer C Compiler Report Discuss in Forum

    48.What will be the output of the program in TurboC?

    #include

    int fun(int **ptr);

    int main(){

    int i=10, j=20;

    const int *ptr = &i;printf(" i = %5X", ptr);

    printf(" ptr = %d", *ptr);

    ptr = &j;

    printf(" j = %5X", ptr);printf(" ptr = %d", *ptr);

    return 0;

    }

    A. i= FFE2 ptr=12 j=FFE4 ptr=24

    B. i= FFE4 ptr=10 j=FFE2 ptr=20

    C. i= FFE0 ptr=20 j=FFE1 ptr=30D. Garbage value

  • 7/28/2019 c Technical1

    21/23

    Answer: Option B

    49.What will be the output of the program?#include

    int main(){const char *s = "";

    char str[] = "Hello";

    s = str;while(*s)

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

    return 0;}

    A. Error B. H C. Hello D. Hel

    Answer: Option C

    Explanation:

    Step 1: const char *s = ""; The constant variable s is declared as an pointer to an array of

    characters type and initialized with an empty string.

    Step 2: char str[] = "Hello"; The variable str is declared as an array of charactrers type and

    initialized with a string "Hello".

    Step 3: s = str; The value of the variable str is assigned to the variable s. Therefore str contains

    the text "Hello".

    Step 4: while(*s){ printf("%c", *s++); } Here the while loop got executed untill the value of the

    variable s is available and it prints the each character of the variable s.

    Hence the output of the program is "Hello".

    50.What will be the output of the program?

    #include

    int get();

    int main(){

    const int x = get();

    printf("%d", x);return 0;

  • 7/28/2019 c Technical1

    22/23

    }

    int get()

    {return 20;

    }

    A. Garbage value B. Error C. 20 D. 0

    Answer: Option C

    Explanation:

    Step 1: int get(); This is the function prototype for the funtion get(), it tells the compiler returns

    an integer value and accept no parameters.

    Step 2: const int x = get(); The constant variable x is declared as an integer data type and

    initialized with the value "20".

    The function get() returns the value "20".

    Step 3: printf("%d", x); It prints the value of the variable x.

    Hence the output of the program is "20".

  • 7/28/2019 c Technical1

    23/23