Would you like to submit the test?

C Language Questions And Answers Sample Test 8


Sample C Language Test 8 for you to Practice. Evaluate your C Language Test 8 test answering skills by trying the online C Language Sample Test 8 and know your score.

C Language Test 8


You have scored 3 out of 10
You Scored:2
Total Questions:2
Attended Questions:2
Correct Answered:2
1. The operator used to get value at address stored in a pointer variable is





2.
What will be the output of the program (in Turbo C)?

#include<stdio.h>

int fun(int *f)
{
    *f = 10;
    return 0;
}
int main()
{
    const int arr[5] = {1, 2, 3, 4, 5};
    printf("Before modification arr[3] = %d", arr[3]);
    fun(&arr[3]);
    printf("nAfter modification arr[3] = %d", arr[3]);
    return 0;
}



Explanation:

Step 1: const int arr[5] = {1, 2, 3, 4, 5}; The constant variable arr is declared as an integer array and initialized to

arr[0] = 1, arr[1] = 2, arr[2] = 3, arr[3] = 4, arr[4] = 5

Step 2: printf("Before modification arr[3] = %d", arr[3]); It prints the value of arr[3] (ie. 4).

Step 3: fun(&arr[3]); The memory location of the arr[3] is passed to fun() and arr[3] value is modified to 10.

A const variable can be indirectly modified by a pointer.

Step 4: printf("After modification arr[3] = %d", arr[3]); It prints the value of arr[3] (ie. 10).

Hence the output of the program is

Before modification arr[3] = 4

After modification arr[3] = 10


3.      A pointer is





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



Explanation:

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


5.
What will be the output of the program in DOS (Compiler - Turbo C)?

#include<stdio.h>
double i;

int main()
{
    (int)(float)(char) i;
    printf("%d", sizeof((int)(float)(char)i));
    return 0;
}



Explanation:

Due to the C language is being platform dependent:

In Turbo C (DOS - 16 bit platform), the output will be 2.

But in GCC (Unix/Linux - 32 bit platform), the output will be 4.


6.
What will be the output of the program?

#include<stdio.h>
#include<stdlib.h>

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;
}



Explanation:

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

K 75 0.000000


7.
What will be the output of the program?

#include<stdio.h>

int main()
{
    int y=128;
    const int x=y;
    printf("%dn", x);
    return 0;
}



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("%dn", x); It prints the value of variable 'x'.

Hence the output of the program is "128"


8. What would be the equivalent pointer expression for referring the array element a[i][j][k][l]





9.
Which of the following statements are correct about 6 used in the program?
int num[6];
num[6]=21;





10.
What will be the output of the program?

#include<stdio.h>
#define MAN(x, y) ((x)>(y)) ? (x):(y);

int main()
{
    int i=10, j=5, k=0;
    k = MAN(++i, j++);
    printf("%d, %d, %dn", i, j, k);
    return 0;
}



Explanation:

The macro MAN(x, y) ((x)>(y)) ? (x):(y); returns the biggest number of given two numbers.

Step 1: int i=10, j=5, k=0; The variable i, j, k are declared as an integer type and initialized to value 10, 5, 0 respectively.

Step 2: k = MAN(++i, j++); becomes,

=> k = ((++i)>(j++)) ? (++i):(j++);

=> k = ((11)>(5)) ? (12):(6);

=> k = 12

Step 3: printf("%d, %d, %dn", i, j, k); It prints the variable i, j, k.

In the above macro step 2 the variable i value is increemented by 2 and variable j value is increemented by 1.

Hence the output of the program is 12, 6, 12


11.
What do the following declaration signify?

void (*cmp)();





12.
What will be the output of the program?

#include<stdio.h>

int main()
{
    const char *s = "";
    char str[] = "Hello";
    s = str;
    while(*s)
        printf("%c", *s++);

    return 0;
}



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


13.
    What will be the output of the program ?

#include

int main()
{
    char str[20] = "Hello";
    char *const p=str;
    *p='M';
    printf("%sn", str);
    return 0;
}





14.
What will be the output of the program?

#include<stdio.h>
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 = %5xn", *ptr);
    const *ptr = temp;
    printf("After changing ptr = %5xn", *ptr);
    return 0;
}





15.
 
What will be the output of the program?

#include<stdio.h>
#define MESS junk

int main()
{
    printf("MESSn");
    return 0;
}



Explanation:

printf("MESSn"); It prints the text "MESS". There is no macro calling inside the printf statement occured.


16.
What do the following declaration signify?

char **argv;





17.
     What will be the output of the program?

#include
#define MAX(a, b, c) (a>b ? a>c ? a : c: b>c ? b : c)

int main()
{
    int x;
    x = MAX(3+2, 2+7, 3+7);
    printf("%dn", x);
    return 0;
}



Explanation:

The macro MAX(a, b, c) (a>b ? a>c ? a : c: b>c ? b : c) returns the biggest of given three numbers.

Step 1: int x; The variable x is declared as an integer type.

Step 2: x = MAX(3+2, 2+7, 3+7); becomes,

=> x = (3+2 >2+7 ? 3+2 > 3+7 ? 3+2 : 3+7: 2+7 > 3+7 ? 2+7 : 3+7)

=> x = (5 >9 ? (5 > 10 ? 5 : 10): (9 > 10 ? 9 : 10) )

=> x = (5 >9 ? (10): (10) )

=> x = 10

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

Hence the output of the program is "10".


18.
      What will be the output of the program ?

#include

int main()
{
    int i=3, *j, k;
    j = &i;
    printf("%dn", i**j*i+*j);
    return 0;
}





19. What will be the output of the program if the array begins at 65472 and each integer occupies 2 bytes?
#include<stdio.h>

int main()
{
    int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};
    printf("%u, %un", a+1, &a+1);
    return 0;
}





20.
What will be the output of the program?

#include<stdio.h>
#define SQR(x)(x*x)

int main()
{
    int a, b=3;
    a = SQR(b+2);
    printf("%dn", a);
    return 0;
}



Explanation:

The macro function SQR(x)(x*x) calculate the square of the given number 'x'. (Eg: 102)

Step 1: int a, b=3; Here the variable a, b are declared as an integer type and the variable b is initialized to 3.

Step 2: a = SQR(b+2); becomes,

=> a = b+2 * b+2; Here SQR(x) is replaced by macro to x*x .

=> a = 3+2 * 3+2;

=> a = 3 + 6 + 2;

=> a = 11;

Step 3: printf("%dn", a); It prints the value of variable 'a'.

Hence the output of the program is 11


21.
Declare the following statement?
"An array of three pointers to chars".





22.
What will be the output of the program?

#include<stdio.h>

int main()
{
    char huge *near *far *ptr1;
    char near *far *huge *ptr2;
    char far *huge *near *ptr3;
    printf("%d, %d, %dn", sizeof(**ptr1), sizeof(ptr2), sizeof(*ptr3));
    return 0;
}





23.
    What will be the output of the program?

#include
#define PRINT(i) printf("%d,",i)

int main()
{
    int x=2, y=3, z=4;
    PRINT(x);
    PRINT(y);
    PRINT(z);
    return 0;
}



Explanation:

The macro PRINT(i) print("%d,", i); prints the given variable value in an integer format.

Step 1: int x=2, y=3, z=4; The variable x, y, z are declared as an integer type and initialized to 2, 3, 4 respectively.

Step 2: PRINT(x); becomes printf("%d,",x). Hence it prints '2'.

Step 3: PRINT(y); becomes printf("%d,",y). Hence it prints '3'.

Step 4: PRINT(z); becomes printf("%d,",z). Hence it prints '4'.

Hence the output of the program is 2, 3, 4.


24.
What do the following declaration signify?

int *f();





25. 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?





26.
What will be the output of the program?

#include<stdio.h>

int main()
{
    const int x=5;
    const int *ptrx;
    ptrx = &x;
    *ptrx = 10;
    printf("%dn", x);
    return 0;
}



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 variable ptrx.

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;


27.
What will the SWAP macro in the following program be expanded to on preprocessing? will the code compile?

#include<stdio.h>
#define SWAP(a, b, c)(c t; t=a, a=b, b=t)
int main()
{
    int x=10, y=20;
    SWAP(x, y, int);
    printf("%d %dn", x, y);
    return 0;
}



Explanation:

The code won't compile since declaration of t cannot occur within parenthesis.


28.
What will be the output of the program ?

#include<stdio.h>

int main()
{
    int arr[1]={10};
    printf("%dn", 0[arr]);
    return 0;
}





29.
Declare the following statement?
"A pointer to an array of three chars".





30.
What will be the output of the program?

#include<stdio.h>

int main()
{
    char huge *near *far *ptr1;
    char near *far *huge *ptr2;
    char far *huge *near *ptr3;
    printf("%d, %d, %dn", sizeof(ptr1), sizeof(*ptr2), sizeof(**ptr3));
    return 0;
}







FreshersLive - No.1 Job site in India. Here you can find latest 2017 government as well as private job recruitment notifications for different posts vacancies in India. Get top company jobs for both fresher and experienced. Job Seekers can get useful interview tips, resume services & interview Question and answer. Practice online test free which is helpful for interview preparation. Register with us to get latest employment news/rojgar samachar notifications. Also get latest free govt and other sarkari naukri job alerts daily through E-mail.