You are on page 1of 206

C Language Test 1

1.
What will be the output of the program?

#include<stdio.h>

int main()
{
    const int i=0;
    printf("%dn", i++);
    return 0;
}

 10
 11
 
 no output

 Error: ++needs a value


 

Explanation:
This program will show an error "Cannot modify a const object".

Step 1: const int i=0; The constant variable 'i' is declared as an integer and initialized with value of
'0'(zero).

Step 2: printf("%dn", i++); Here the variable 'i' is increemented by 1(one). This will create an error "Cannot
modify a const object".

Because, we cannot modify a const variable.

2.
What will be the output of the program?

#include<stdio.h>
int get();

int main()
{
    const int x = get();
    printf("%d", x);
    return 0;
}
int get()
{
    return 20;
}

 garbage value
 error
 

 20
 0
 

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

3.
What does the following declaration mean?
int (*ptr)[10];

 ptr is array of pointers to 10 integers

 ptr is a pointer to an array of 10 integers


 
 ptr is an array of 10 integers
 ptr is an pointer to array
 

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

 12,6,12
 11,5,11
 
 11,5,garbage
 12,6,garbage
 

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

5.    What is (void*)0?

 Representation of NULL pointer


 Representation of void pointer
 
 Error
 None of above
 
Walk-in Fresh Graduates @ TATA AIA - Get Rs.2.8 Lakhs salary - Apply
Now Apply Now

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

 25

 11
 
 error
 garbage value
 

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

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

 5
 9
 

 10
 3+7
 

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

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

 In the first statement 6 specifies a particular element, whereas in the second
statement it specifies a type
 In the first statement 6 specifies a array size, whereas in the second statement it

specifies a particular element of array.


 
 In the first statement 6 specifies a particular element, whereas in the second
statement it specifies a array size.
 In both the statement 6 specifies array size.
 

9.
What will be the output of the program ?

#include<stdio.h>
void fun(int **p);

int main()
{
    int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0};
    int *ptr;
    ptr = &a[0][0];
    fun(&ptr);
    return 0;
}
void fun(int **p)
{
    printf("%dn", **p);
}

 1
 2
 
 3
 4
 

10.
What will be the output of the program in Turb C (under DOS)?

#include<stdio.h>

int main()
{
    int arr[5], i=0;
    while(i<5)
        arr[i]=++i;

    for(i=0; i<5; i++)


        printf("%d, ", arr[i]);

    return 0;
}

 1,2,3

 garbage value 1,2,3,4


 
 1,2,3,4,5
 1,4,5
 

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

 stdio.h
 stddef.h
 

 stdio.h and stddef.h


 math.h
 

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

12.
    What do the following declaration signify?

char *scr;

 scr is a pointer to pointer variable.


 scr is a function pointer.
 

 scr is a pointer to char.


 scr is a member of function pointer.
 

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

 char *ptr[3]();
 char (*ptr)*[3];
 
 char (*ptr[3])();

 char (*ptr)[3];
 

14.
What do the following declaration signify?

int *f();

 f is a pointer variable of function type.

 f is a function returning pointer to an int


 
 f is a function pointer.
 f is a simple declaration of pointer variable.
 
15.
      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;
}

 30
 27
 
 9
 3
 

16.
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;
}
 Before modification arr[3] = 4 After modification arr[3] = 10
 Error: Invalid parameter
 
 both a and b
 none of these
 

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

17.
    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;
}
 mello
 hello
 
 hmello
 none of the above
 

18. The operator used to get value at address stored in a pointer variable is

 *
 &&
 
 !!
 $$
 

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

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

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

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

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

 error
 h
 

 hello
 hel
 

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


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

 65480,65490

 65480,65496
 
 65480,65499
 65480,65489
 

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

 .
 #
 
 &

 ->
 

23.
What do the following declaration signify?

int *ptr[30];
 ptr is a pointer to an array of 30 integer pointers.

 ptr is a array of 30 pointers to integers.


 
 ptr is a array of 30 integer pointers.
 ptr is a array 30 pointers.
 

24.
What do the following declaration signify?

void (*cmp)();

 cmp is a pointer to an void function type.


 cmp is a void type pointer function
 
 cmp is a function that return a void pointer.

 cmp is a pointer to a function which returns void .


 

25.
  What will be the output of the program?

#include

int main()
{
    const c = -11;
    const int d = 34;
    printf("%d, %dn", c, d);
    return 0;
}
 error

 -11,34
 
 11,34
 none of these
 

Explanation:
Step 1: const c = -11; The constant variable 'c' is declared and initialized to value "-11".

Step 2: const int d = 34; The constant variable 'd' is declared as an integer and initialized to value '34'.

Step 3: printf("%d, %dn", c, d); The value of the variable 'c' and 'd' are printed.

Hence the output of the program is -11, 34

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

 Address of i: Address of j
 10 , 223
 

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

27.
What will be the output of the program ?

#include<stdio.h>

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

 6,7,8
 4,5,6
 

 3,2,15
 2,3,4
 

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

 The element will be set to 0.


 The compiler would report an error.
 

 The program may crash if some important data gets overwritten


 The array size would appropriately grow.
 

29.
What will be the output of the program?

#include<stdio.h>
#define FUN(i, j) i##j

int main()
{
    int va1=10;
    int va12=20;
    printf("%dn", FUN(va1, 2));
    return 0;
}

 10

 20
 
 30
 40
 

Explanation:
The following program will make you understand about ## (macro concatenation) operator clearly.

30.

What will be the output of the program?

#include<stdio.h>
int main()
{
    char far *near *ptr1;
    char far *far *ptr2;
    char far *huge *ptr3;
    printf("%d, %d, %dn", sizeof(ptr1), sizeof(ptr2), sizeof(ptr3));
    return 0;
}

 2,4,2

 2,4,4
 
 2,5,6
 1,2,5
 
C Language Test 2

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

 It compiles
 Compiles with an warning
 

 Not compile
 Compiles and print nothing
 
Explanation:
The code won't compile since declaration of t cannot occur within parenthesis.

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

 128
 garbage value
 
 error
 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"

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

 2,3,4
 2,2,2
 
 3,3,3
 4,4,4
 

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.


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

 Value of elements in array


 First element of the array
 

 Base address of the array


 Address of the last element of array
 

Explanation:
The statement 'C' is correct. When we pass an array as a funtion argument, the base address of the array
will be passed.

5.
What do the following declaration signify?

int *f();

 f is a pointer variable of function type.

 f is a function returning pointer to an int


 
 f is a function pointer.
 f is a simple declaration of pointer variable.
 

Walk-in Fresh Graduates @ TATA AIA - Get Rs.2.8 Lakhs salary - Apply
Now Apply Now

6.
   What will be the output of the program ?

#include

int main()
{
    float arr[] = {12.4, 2.3, 4.5, 6.7};
    printf("%dn", sizeof(arr)/sizeof(arr[0]));
    return 0;
}

 5

 4
 
 7
 8
 

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

 5
 10
 

 error
 garbage value
 

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;

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

 1

 2
 
 4
 6
 

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.

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

#include<stdio.h>
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;
}
 i= FFE2 ptr=12 j=FFE4 ptr=24

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


 
 i= FFE2 ptr=20 j=FFE4 ptr=30
 garbage value
 

10.
What does the following declaration mean?
int (*ptr)[10];

 ptr is array of pointers to 10 integers

 ptr is a pointer to an array of 10 integers


 
 ptr is an array of 10 integers
 ptr is an pointer to array
 
11.
What will be the output of the program in Turb C (under DOS)?

#include<stdio.h>

int main()
{
    int arr[5], i=0;
    while(i<5)
        arr[i]=++i;

    for(i=0; i<5; i++)


        printf("%d, ", arr[i]);

    return 0;
}

 1,2,3

 garbage value 1,2,3,4


 
 1,2,3,4,5
 1,4,5
 

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

 5
 9
 
 10
 3+7
 

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

13.
     What will be the output of the program?

#include
typedef void v;
typedef int i;

int main()
{
    v fun(i, i);
    fun(2, 3);
    return 0;
}
v fun(i a, i b)
{
    i s=2;
    float i;
    printf("%d,", sizeof(i));
    printf(" %d", a*b*s);
}
 2,8
 2,4
 
 4,8

 4,12
 

14.
What will be the output of the program ?

#include<stdio.h>

int main()
{
    static char *s[] = {"black", "white", "pink", "violet"};
    char **ptr[] = {s+3, s+2, s+1, s}, ***p;
    p = ptr;
    ++p;
    printf("%s", **p+1);
    return 0;
}

 ink
 ack
 
 ite
 let
 

15.
Declare the following statement?
"An array of three pointers to chars".
 char *ptr[3]();

 char *ptr[3];
 
 char (*ptr[3])();
 char **ptr[3];
 

16.    What is (void*)0?

 Representation of NULL pointer


 Representation of void pointer
 
 Error
 None of above
 

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

 char *ptr[3]();
 char (*ptr)*[3];
 
 char (*ptr[3])();

 char (*ptr)[3];
 
18.
In which stage the following code 
#include<stdio.h> 
gets replaced by the contents of the file stdio.h

 During editing
 During linking
 
 During execution

 During preprocessing
 

Explanation:
The preprocessor replaces the line #include <stdio.h> with the system header file of that name. More
precisely, the entire text of the file 'stdio.h' replaces the #include directive.

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

 30
 27
 
 9
 3
 

20. The operator used to get value at address stored in a pointer variable is

 *
 &&
 
 !!
 $$
 

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

 near=2 far=4 huge=4


 near=4 far=8 huge=8
 
 near=2 far=4 huge=8
 near=4 far=4 huge=8
 

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

22.
What will be the output of the program if the array begins 1200 in memory?
#include<stdio.h>

int main()
{
    int arr[]={2, 3, 4, 1, 6};
    printf("%u, %u, %un", arr, &arr[0], &arr);
    return 0;
}

 1300,1200,1500

 1200,1200,1200
 
 1300,1400,1500
 1200,1200,1300
 

23.
Declare the following statement?
"A pointer to a function which receives an int pointer and returns float pointer".

 float *(ptr)*int;
 float *(*ptr)(int)
 

 float *(*ptr)(int*)
 float (*ptr)(int)
 

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

 stdio.h
 stddef.h
 
 stdio.h and stddef.h
 math.h
 

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

25.
What will be the output of the program ?

#include<stdio.h>
void fun(int **p);

int main()
{
    int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0};
    int *ptr;
    ptr = &a[0][0];
    fun(&ptr);
    return 0;
}
void fun(int **p)
{
    printf("%dn", **p);
}

 1
 2
 
 3
 4
 

26.
What do the following declaration signify?
void (*cmp)();

 cmp is a pointer to an void function type.


 cmp is a void type pointer function
 
 cmp is a function that return a void pointer.

 cmp is a pointer to a function which returns void .


 

27.
What will be the output of the program?

#include<stdio.h>
#define FUN(i, j) i##j

int main()
{
    int va1=10;
    int va12=20;
    printf("%dn", FUN(va1, 2));
    return 0;
}

 10

 20
 
 30
 40
 

Explanation:
The following program will make you understand about ## (macro concatenation) operator clearly.
28. 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;
}

 65480,65490

 65480,65496
 
 65480,65499
 65480,65489
 

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

 4,4,4
 2,4,4
 
 4,2,4
 2,2,2
 

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

 Error: RValue required


 Error: cannot convert from 'const int *' to 'int *const'
 
 Error: LValue required in strcpy

 No error
 

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

K 75 0.000000

C Language Test 3
1.
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;
}

 12,6,12
 11,5,11
 
 11,5,garbage
 12,6,garbage
 

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


2.      A pointer is

 A keyword used to create variables


 A variable that stores address of an instruction
 

 A variable that stores address of other variable


 All of the above
 

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

 near=2 far=4 huge=4


 near=4 far=8 huge=8
 
 near=2 far=4 huge=8
 near=4 far=4 huge=8
 

Explanation:
near=2, far=4 and huge=4 pointers exist only under DOS. Under windows and Linux every pointers is 4
bytes long.
4.
What will be the output of the program?

#include<stdio.h>
#define FUN(i, j) i##j

int main()
{
    int va1=10;
    int va12=20;
    printf("%dn", FUN(va1, 2));
    return 0;
}

 10

 20
 
 30
 40
 

Explanation:
The following program will make you understand about ## (macro concatenation) operator clearly.

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

 5
 10
 

 error
 garbage value
 

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;
Walk-in Fresh Graduates @ TATA AIA - Get Rs.2.8 Lakhs salary - Apply
Now Apply Now

6.
Which of the following statements are correct about an array?
1: The array int num[26]; can store 26 elements.
2: The expression num[1] designates the very first element in the array.
3: It is necessary to initialize the array at the time of declaration.
4: The declaration num[SIZE] is allowed if SIZE is a macro.

 1,2

 1,4
 
 1,5
 1,6
 

7.
What will be the output of the program?

#include<stdio.h>
int get();

int main()
{
    const int x = get();
    printf("%d", x);
    return 0;
}
int get()
{
    return 20;
}

 garbage value
 error
 

 20
 0
 

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

8.
     What will be the output of the program?

#include
typedef void v;
typedef int i;

int main()
{
    v fun(i, i);
    fun(2, 3);
    return 0;
}
v fun(i a, i b)
{
    i s=2;
    float i;
    printf("%d,", sizeof(i));
    printf(" %d", a*b*s);
}

 2,8
 2,4
 
 4,8

 4,12
 

9.
    What do the following declaration signify?

char *scr;

 scr is a pointer to pointer variable.


 scr is a function pointer.
 

 scr is a pointer to char.


 scr is a member of function pointer.
 

10.
What will be the output of the program in Turb C (under DOS)?

#include<stdio.h>

int main()
{
    int arr[5], i=0;
    while(i<5)
        arr[i]=++i;

    for(i=0; i<5; i++)


        printf("%d, ", arr[i]);

    return 0;
}

 1,2,3

 garbage value 1,2,3,4


 
 1,2,3,4,5
 1,4,5
 

11.
In which stage the following code 
#include<stdio.h> 
gets replaced by the contents of the file stdio.h

 During editing
 During linking
 
 During execution
 During preprocessing
 

Explanation:
The preprocessor replaces the line #include <stdio.h> with the system header file of that name. More
precisely, the entire text of the file 'stdio.h' replaces the #include directive.

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

 Address of i: Address of j
 10 , 223
 

 Error: cannot convert parameter 1 from 'const int **' to 'int **'
 Garbage value
 
13. What would be the equivalent pointer expression for referring the array element a[i][j][k][l]

 ((((a+i)+j)+k)+l)

 *(*(*(*(a+i)+j)+k)+l)
 
 (((a+i)+j)+k+l)
 ((a+i)+j+k+l)
 

14.
What will be the output of the program ?

#include<stdio.h>

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

 1

 10
 
 3
 4
 

15.
What does the following declaration mean?
int (*ptr)[10];
 ptr is array of pointers to 10 integers

 ptr is a pointer to an array of 10 integers


 
 ptr is an array of 10 integers
 ptr is an pointer to array
 

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

 It compiles
 Compiles with an warning
 

 Not compile
 Compiles and print nothing
 

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

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

 char *ptr[3]();

 char *ptr[3];
 
 char (*ptr[3])();
 char **ptr[3];
 

18.
What will be the output of the program ?

#include<stdio.h>

int main()
{
    void fun(int, int[]);
    int arr[] = {1, 2, 3, 4};
    int i;
    fun(4, arr);
    for(i=0; i<4; i++)
        printf("%d,", arr[i]);
    return 0;
}
void fun(int n, int arr[])
{
    int *p=0;
    int i=0;
    while(i++ < n)
        p = &arr[i];
    *p=0;
}

 0.1.2.3

 1,2,3,4
 
 1,3,4,5
 1,4,5,6
 

19.
What will be the output of the program if the array begins 1200 in memory?

#include<stdio.h>

int main()
{
    int arr[]={2, 3, 4, 1, 6};
    printf("%u, %u, %un", arr, &arr[0], &arr);
    return 0;
}

 1300,1200,1500

 1200,1200,1200
 
 1300,1400,1500
 1200,1200,1300
 

20.
What will be the output of the program ?

#include<stdio.h>
void fun(int **p);

int main()
{
    int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0};
    int *ptr;
    ptr = &a[0][0];
    fun(&ptr);
    return 0;
}
void fun(int **p)
{
    printf("%dn", **p);
}
 1
 2
 
 3
 4
 

21.
What will be the output of the program?

#include<stdio.h>
#define MAX(a, b) (a > b ? a : b)

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

 8

 9
 
 4
 7
 

Explanation:
The macro MAX(a, b) (a > b ? a : b) returns the biggest value of the given two numbers.

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

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


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

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

=> x = 9

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

Hence the output of the program is 9.

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

 In the first statement 6 specifies a particular element, whereas in the second
statement it specifies a type
 In the first statement 6 specifies a array size, whereas in the second statement it

specifies a particular element of array.


 
 In the first statement 6 specifies a particular element, whereas in the second
statement it specifies a array size.
 In both the statement 6 specifies array size.
 

23.
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;
}
 4,4,4
 2,4,4
 
 4,2,4
 2,2,2
 

24.
 
What will be the output of the program?

#include<stdio.h>
#define MESS junk

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

 junk

 MESS
 
 error
 nothing will print
 

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

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


 stdio.h
 stddef.h
 

 stdio.h and stddef.h


 math.h
 

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

26.
  What will be the output of the program?

#include

int main()
{
    const c = -11;
    const int d = 34;
    printf("%d, %dn", c, d);
    return 0;
}

 error

 -11,34
 
 11,34
 none of these
 

Explanation:
Step 1: const c = -11; The constant variable 'c' is declared and initialized to value "-11".

Step 2: const int d = 34; The constant variable 'd' is declared as an integer and initialized to value '34'.
Step 3: printf("%d, %dn", c, d); The value of the variable 'c' and 'd' are printed.

Hence the output of the program is -11, 34

27. The operator used to get value at address stored in a pointer variable is

 *
 &&
 
 !!
 $$
 

28.
What do the following declaration signify?

int *ptr[30];

 ptr is a pointer to an array of 30 integer pointers.

 ptr is a array of 30 pointers to integers.


 
 ptr is a array of 30 integer pointers.
 ptr is a array 30 pointers.
 

29.
What do the following declaration signify?

void (*cmp)();
 cmp is a pointer to an void function type.
 cmp is a void type pointer function
 
 cmp is a function that return a void pointer.

 cmp is a pointer to a function which returns void .


 

30.
What will be the output of the program?

#include<stdio.h>

int main()
{
    const int i=0;
    printf("%dn", i++);
    return 0;
}

 10
 11
 
 no output

 Error: ++needs a value


 

Explanation:
This program will show an error "Cannot modify a const object".

Step 1: const int i=0; The constant variable 'i' is declared as an integer and initialized with value of
'0'(zero).

Step 2: printf("%dn", i++); Here the variable 'i' is increemented by 1(one). This will create an error "Cannot
modify a const object".
Because, we cannot modify a const variable.

C Language Test 4

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

 char *ptr[3]();
 char (*ptr)*[3];
 
 char (*ptr[3])();

 char (*ptr)[3];
 

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

 ((((a+i)+j)+k)+l)

 *(*(*(*(a+i)+j)+k)+l)
 
 (((a+i)+j)+k+l)
 ((a+i)+j+k+l)
 

3.
In which stage the following code 
#include<stdio.h> 
gets replaced by the contents of the file stdio.h
 During editing
 During linking
 
 During execution

 During preprocessing
 

Explanation:
The preprocessor replaces the line #include <stdio.h> with the system header file of that name. More
precisely, the entire text of the file 'stdio.h' replaces the #include directive.

4.
What will be the output of the program if the array begins 1200 in memory?

#include<stdio.h>

int main()
{
    int arr[]={2, 3, 4, 1, 6};
    printf("%u, %u, %un", arr, &arr[0], &arr);
    return 0;
}

 1300,1200,1500
 1200,1200,1200
 
 1300,1400,1500
 1200,1200,1300
 

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

 near=2 far=4 huge=4


 near=4 far=8 huge=8
 
 near=2 far=4 huge=8
 near=4 far=4 huge=8
 

Explanation:
near=2, far=4 and huge=4 pointers exist only under DOS. Under windows and Linux every pointers is 4
bytes long.
Walk-in Fresh Graduates @ TATA AIA - Get Rs.2.8 Lakhs salary - Apply
Now Apply Now

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

 error
 h
 

 hello
 hel
 

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

7.
Which of the following statements are correct about an array?
1: The array int num[26]; can store 26 elements.
2: The expression num[1] designates the very first element in the array.
3: It is necessary to initialize the array at the time of declaration.
4: The declaration num[SIZE] is allowed if SIZE is a macro.

 1,2

 1,4
 
 1,5
 1,6
 

8. The operator used to get value at address stored in a pointer variable is

 *
 &&
 
 !!
 $$
 

9.
What will be the output of the program in Turb C (under DOS)?

#include<stdio.h>

int main()
{
    int arr[5], i=0;
    while(i<5)
        arr[i]=++i;

    for(i=0; i<5; i++)


        printf("%d, ", arr[i]);

    return 0;
}

 1,2,3

 garbage value 1,2,3,4


 
 1,2,3,4,5
 1,4,5
 

10.
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;
}
 5
 10
 

 error
 garbage value
 

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;

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

 25

 11
 
 error
 garbage value
 

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

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

 65480,65490

 65480,65496
 
 65480,65499
 65480,65489
 

13.
What will be the output of the program ?

#include<stdio.h>

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

 6,7,8
 4,5,6
 

 3,2,15
 2,3,4
 
14.
What do the following declaration signify?

char **argv;

 argv is a pointer to pointer.

 argv is a pointer to a char pointer.


 
 argv is a function pointer.
 argv is a member of function pointer.
 

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

 2,3,4
 2,2,2
 
 3,3,3
 4,4,4
 

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.

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

 128
 garbage value
 
 error
 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"

17.
What will be the output of the program ?

#include<stdio.h>

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

 1

 10
 
 3
 4
 

18.
   What will be the output of the program ?

#include

int main()
{
    char *str;
    str = "%s";
    printf(str, "Kn");
    return 0;
}

 error
 no output
 
 k
 %s
 

19.
What will be the output of the program?

#include<stdio.h>
#define FUN(i, j) i##j

int main()
{
    int va1=10;
    int va12=20;
    printf("%dn", FUN(va1, 2));
    return 0;
}

 10

 20
 
 30
 40
 

Explanation:
The following program will make you understand about ## (macro concatenation) operator clearly.

20.
What do the following declaration signify?

void (*cmp)();
 cmp is a pointer to an void function type.
 cmp is a void type pointer function
 
 cmp is a function that return a void pointer.

 cmp is a pointer to a function which returns void .


 

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

 .
 #
 
 &

 ->
 

22.
What do the following declaration signify?

char *arr[10];

 arr is a array of 10 character pointers.


 arr is a array of function pointer.
 
 arr is a array of characters.
 arr is a pointer to array of characters.
 

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

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

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

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

24.
    What do the following declaration signify?

char *scr;

 scr is a pointer to pointer variable.


 scr is a function pointer.
 

 scr is a pointer to char.


 scr is a member of function pointer.
 

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

 Before modification arr[3] = 4 After modification arr[3] = 10


 Error: Invalid parameter
 
 both a and b
 none of these
 

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


26.
What will be the output of the program?

#include<stdio.h>
#define MIN(x, y) (x<y)? x : y;
int main()
{
    int x=3, y=4, z;
    z = MIN(x+y/2, y-1);
    if(z > 0)
        printf("%dn", z);
    return 0;
}

 3
 4
 
 0
 no output
 

Explanation:
The macro MIN(x, y) (x<y)? x : y; returns the smallest value from the given two numbers.

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

Step 2: z = MIN(x+y/2, y-1); becomes,

=> z = (x+y/2 < y-1)? x+y/2 : y - 1;

=> z = (3+4/2 < 4-1)? 3+4/2 : 4 - 1;

=> z = (3+2 < 4-1)? 3+2 : 4 - 1;

=> z = (5 < 3)? 5 : 3;

The macro return the number 3 and it is stored in the variable z.

Step 3: if(z > 0) becomes if(3 > 0) here the if condition is satisfied. It executes the if block statements.

Step 4: printf("%dn", z);. It prints the value of variable z.


Hence the output of the program is 3

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

 4,4,4
 3,3,3
 
 2,2,2
 1,1,1
 

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

 1
 2
 
 4
 6
 

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.

29.
What do the following declaration signify?

int *f();

 f is a pointer variable of function type.

 f is a function returning pointer to an int


 
 f is a function pointer.
 f is a simple declaration of pointer variable.
 

30.
What will be the output of the program ?

#include<stdio.h>

int main()
{
    static char *s[] = {"black", "white", "pink", "violet"};
    char **ptr[] = {s+3, s+2, s+1, s}, ***p;
    p = ptr;
    ++p;
    printf("%s", **p+1);
    return 0;
}

 ink
 ack
 
 ite
 let
 

C Language Test 5

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

 25

 11
 
 error
 garbage value
 

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

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

 128
 garbage value
 
 error
 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"

3.      A pointer is

 A keyword used to create variables


 A variable that stores address of an instruction
 

 A variable that stores address of other variable


 All of the above
 

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

 Before modification arr[3] = 4 After modification arr[3] = 10


 Error: Invalid parameter
 
 both a and b
 none of these
 

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

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

 error
 h
 

 hello
 hel
 

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


Walk-in Fresh Graduates @ TATA AIA - Get Rs.2.8 Lakhs salary - Apply
Now Apply Now

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

 The element will be set to 0.


 The compiler would report an error.
 

 The program may crash if some important data gets overwritten


 The array size would appropriately grow.
 

7. The operator used to get value at address stored in a pointer variable is

 *
 &&
 
 !!
 $$
 

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

 char *ptr[3]();

 char *ptr[3];
 
 char (*ptr[3])();
 char **ptr[3];
 

9.
What do the following declaration signify?

int *f();

 f is a pointer variable of function type.

 f is a function returning pointer to an int


 
 f is a function pointer.
 f is a simple declaration of pointer variable.
 

10.
What will be the output of the program in Turb C (under DOS)?
#include<stdio.h>

int main()
{
    int arr[5], i=0;
    while(i<5)
        arr[i]=++i;

    for(i=0; i<5; i++)


        printf("%d, ", arr[i]);

    return 0;
}

 1,2,3

 garbage value 1,2,3,4


 
 1,2,3,4,5
 1,4,5
 

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

 It compiles
 Compiles with an warning
 

 Not compile
 Compiles and print nothing
 

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

12.
What will be the output of the program ?

#include<stdio.h>

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

 6,7,8
 4,5,6
 

 3,2,15
 2,3,4
 

13.
What will be the output of the program?

#include<stdio.h>
#define FUN(i, j) i##j

int main()
{
    int va1=10;
    int va12=20;
    printf("%dn", FUN(va1, 2));
    return 0;
}

 10

 20
 
 30
 40
 

Explanation:
The following program will make you understand about ## (macro concatenation) operator clearly.

14.
What do the following declaration signify?

int *ptr[30];

 ptr is a pointer to an array of 30 integer pointers.


 ptr is a array of 30 pointers to integers.
 
 ptr is a array of 30 integer pointers.
 ptr is a array 30 pointers.
 

15.
What will be the output of the program?

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

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

 9,4
 27,4
 

 27,6
 error
 

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

Step 1: int a, b=3; The variable a and b are declared as an integer type and varaible b id 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 b is incremented by 3. (ie:
b=6)

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

Hence the output of the program is 27, 6.

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

 mello
 hello
 
 hmello
 none of the above
 

17.
What will be the output of the program?

#include<stdio.h>
#define MIN(x, y) (x<y)? x : y;
int main()
{
    int x=3, y=4, z;
    z = MIN(x+y/2, y-1);
    if(z > 0)
        printf("%dn", z);
    return 0;
}

 3
 4
 
 0
 no output
 

Explanation:
The macro MIN(x, y) (x<y)? x : y; returns the smallest value from the given two numbers.

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

Step 2: z = MIN(x+y/2, y-1); becomes,

=> z = (x+y/2 < y-1)? x+y/2 : y - 1;

=> z = (3+4/2 < 4-1)? 3+4/2 : 4 - 1;

=> z = (3+2 < 4-1)? 3+2 : 4 - 1;

=> z = (5 < 3)? 5 : 3;

The macro return the number 3 and it is stored in the variable z.

Step 3: if(z > 0) becomes if(3 > 0) here the if condition is satisfied. It executes the if block statements.

Step 4: printf("%dn", z);. It prints the value of variable z.

Hence the output of the program is 3

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

 5
 10
 

 error
 garbage value
 

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;

19.
What will be the output of the program?

#include<stdio.h>
int get();

int main()
{
    const int x = get();
    printf("%d", x);
    return 0;
}
int get()
{
    return 20;
}

 garbage value
 error
 

 20
 0
 

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

20.
   What will be the output of the program ?

#include

int main()
{
    float arr[] = {12.4, 2.3, 4.5, 6.7};
    printf("%dn", sizeof(arr)/sizeof(arr[0]));
    return 0;
}

 5

 4
 
 7
 8
 

21.
 
What will be the output of the program?

#include<stdio.h>
#define MESS junk

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

 junk

 MESS
 
 error
 nothing will print
 

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

22.
What do the following declaration signify?

int (*pf)();

 pf is a pointer to function.


 pf is a function pointer.
 
 pf is a pointer to a function which return int
 pf is a function of pointer variable.
 

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

 ((((a+i)+j)+k)+l)

 *(*(*(*(a+i)+j)+k)+l)
 
 (((a+i)+j)+k+l)
 ((a+i)+j+k+l)
 

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

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

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

 char *p = (char*)malloc(100);
 char *p = (char *)(malloc*)(100);
 
25.
What does the following declaration mean?
int (*ptr)[10];

 ptr is array of pointers to 10 integers

 ptr is a pointer to an array of 10 integers


 
 ptr is an array of 10 integers
 ptr is an pointer to array
 

26.    What is (void*)0?

 Representation of NULL pointer


 Representation of void pointer
 
 Error
 None of above
 

27.
What will be the output of the program?

#include<stdio.h>
#define MAX(a, b) (a > b ? a : b)

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

 8

 9
 
 4
 7
 

Explanation:
The macro MAX(a, b) (a > b ? a : b) returns the biggest value of the given two numbers.

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

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

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

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

=> x = 9

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

Hence the output of the program is 9.

28.
     What will be the output of the program?

#include
typedef void v;
typedef int i;

int main()
{
    v fun(i, i);
    fun(2, 3);
    return 0;
}
v fun(i a, i b)
{
    i s=2;
    float i;
    printf("%d,", sizeof(i));
    printf(" %d", a*b*s);
}

 2,8
 2,4
 
 4,8

 4,12
 

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

 4,4,4
 3,3,3
 
 2,2,2
 1,1,1
 
30.
What do the following declaration signify?

char *arr[10];

 arr is a array of 10 character pointers.


 arr is a array of function pointer.
 
 arr is a array of characters.
 arr is a pointer to array of characters.
 

C Language Test 6
1.
Declare the following statement?
"A pointer to a function which receives an int pointer and returns float pointer".

 float *(ptr)*int;
 float *(*ptr)(int)
 

 float *(*ptr)(int*)
 float (*ptr)(int)
 

2.
What do the following declaration signify?

void (*cmp)();

 cmp is a pointer to an void function type.


 cmp is a void type pointer function
 
 cmp is a function that return a void pointer.

 cmp is a pointer to a function which returns void .


 

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

 1

 2
 
 4
 6
 

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.


4.
Which of the following statements are correct about an array?
1: The array int num[26]; can store 26 elements.
2: The expression num[1] designates the very first element in the array.
3: It is necessary to initialize the array at the time of declaration.
4: The declaration num[SIZE] is allowed if SIZE is a macro.

 1,2

 1,4
 
 1,5
 1,6
 

5.
What will be the output of the program?

#include<stdio.h>
#define FUN(i, j) i##j

int main()
{
    int va1=10;
    int va12=20;
    printf("%dn", FUN(va1, 2));
    return 0;
}
 10

 20
 
 30
 40
 

Explanation:
The following program will make you understand about ## (macro concatenation) operator clearly.

Walk-in Fresh Graduates @ TATA AIA - Get Rs.2.8 Lakhs salary - Apply
Now Apply Now

6.
What will be the output of the program ?

#include<stdio.h>

int main()
{
    void fun(int, int[]);
    int arr[] = {1, 2, 3, 4};
    int i;
    fun(4, arr);
    for(i=0; i<4; i++)
        printf("%d,", arr[i]);
    return 0;
}
void fun(int n, int arr[])
{
    int *p=0;
    int i=0;
    while(i++ < n)
        p = &arr[i];
    *p=0;
}

 0.1.2.3

 1,2,3,4
 
 1,3,4,5
 1,4,5,6
 

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

 30
 27
 
 9
 3
 
8.
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;
}

 Address of i: Address of j
 10 , 223
 

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

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

 mello
 hello
 
 hmello
 none of the above
 

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

 Value of elements in array


 First element of the array
 

 Base address of the array


 Address of the last element of array
 

Explanation:
The statement 'C' is correct. When we pass an array as a funtion argument, the base address of the array
will be passed.
11.
What does the following declaration mean?
int (*ptr)[10];

 ptr is array of pointers to 10 integers

 ptr is a pointer to an array of 10 integers


 
 ptr is an array of 10 integers
 ptr is an pointer to array
 

12.
What will be the output of the program ?

#include<stdio.h>

int main()
{
    static char *s[] = {"black", "white", "pink", "violet"};
    char **ptr[] = {s+3, s+2, s+1, s}, ***p;
    p = ptr;
    ++p;
    printf("%s", **p+1);
    return 0;
}
 ink
 ack
 
 ite
 let
 

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

 stdio.h
 stddef.h
 

 stdio.h and stddef.h


 math.h
 

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

14.
    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;
}
 2,3,4
 2,2,2
 
 3,3,3
 4,4,4
 

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.

15.

What will be the output of the program?

#include<stdio.h>

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

 2,4,2
 2,4,4
 
 2,5,6
 1,2,5
 

16. The operator used to get value at address stored in a pointer variable is

 *
 &&
 
 !!
 $$
 

17.
What will be the output of the program?

#include<stdio.h>
#define MIN(x, y) (x<y)? x : y;
int main()
{
    int x=3, y=4, z;
    z = MIN(x+y/2, y-1);
    if(z > 0)
        printf("%dn", z);
    return 0;
}

 3
 4
 
 0
 no output
 

Explanation:
The macro MIN(x, y) (x<y)? x : y; returns the smallest value from the given two numbers.

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

Step 2: z = MIN(x+y/2, y-1); becomes,

=> z = (x+y/2 < y-1)? x+y/2 : y - 1;

=> z = (3+4/2 < 4-1)? 3+4/2 : 4 - 1;

=> z = (3+2 < 4-1)? 3+2 : 4 - 1;

=> z = (5 < 3)? 5 : 3;

The macro return the number 3 and it is stored in the variable z.

Step 3: if(z > 0) becomes if(3 > 0) here the if condition is satisfied. It executes the if block statements.

Step 4: printf("%dn", z);. It prints the value of variable z.

Hence the output of the program is 3

18.
What will be the output of the program ?

#include<stdio.h>

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;
}
 6,7,8
 4,5,6
 

 3,2,15
 2,3,4
 

19.
What will be the output of the program?

#include<stdio.h>
#define MAX(a, b) (a > b ? a : b)

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

 8

 9
 
 4
 7
 

Explanation:
The macro MAX(a, b) (a > b ? a : b) returns the biggest value of the given two numbers.

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

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


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

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

=> x = 9

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

Hence the output of the program is 9.

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

 12,6,12
 11,5,11
 
 11,5,garbage
 12,6,garbage
 

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

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

 char *ptr[3]();
 char (*ptr)*[3];
 
 char (*ptr[3])();

 char (*ptr)[3];
 

22.
What will be the output of the program?

#include<stdio.h>
int get();

int main()
{
    const int x = get();
    printf("%d", x);
    return 0;
}
int get()
{
    return 20;
}

 garbage value
 error
 

 20
 0
 

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

23.
In which stage the following code 
#include<stdio.h> 
gets replaced by the contents of the file stdio.h

 During editing
 During linking
 
 During execution

 During preprocessing
 

Explanation:
The preprocessor replaces the line #include <stdio.h> with the system header file of that name. More
precisely, the entire text of the file 'stdio.h' replaces the #include directive.
24.
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;
}

 4,4,4
 3,3,3
 
 2,2,2
 1,1,1
 

25.
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;
}
 Error: RValue required
 Error: cannot convert from 'const int *' to 'int *const'
 
 Error: LValue required in strcpy

 No error
 

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

K 75 0.000000

26.
   What will be the output of the program ?

#include

int main()
{
    char *str;
    str = "%s";
    printf(str, "Kn");
    return 0;
}

 error
 no output
 

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

 In the first statement 6 specifies a particular element, whereas in the second
statement it specifies a type
 In the first statement 6 specifies a array size, whereas in the second statement it

specifies a particular element of array.


 
 In the first statement 6 specifies a particular element, whereas in the second
statement it specifies a array size.
 In both the statement 6 specifies array size.
 

28.
   What will be the output of the program ?

#include

int main()
{
    float arr[] = {12.4, 2.3, 4.5, 6.7};
    printf("%dn", sizeof(arr)/sizeof(arr[0]));
    return 0;
}

 5

 4
 
 7
 8
 
29.
     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;
}

 5
 9
 

 10
 3+7
 

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


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

 char *ptr[3]();

 char *ptr[3];
 
 char (*ptr[3])();
 char **ptr[3];
 

C Language Test 7

1.
What does the following declaration mean?
int (*ptr)[10];

 ptr is array of pointers to 10 integers

 ptr is a pointer to an array of 10 integers


 
 ptr is an array of 10 integers
 ptr is an pointer to array
 

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

#include<stdio.h>
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;
}

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

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


 
 i= FFE2 ptr=20 j=FFE4 ptr=30
 garbage value
 

3.
     What will be the output of the program?

#include
typedef void v;
typedef int i;

int main()
{
    v fun(i, i);
    fun(2, 3);
    return 0;
}
v fun(i a, i b)
{
    i s=2;
    float i;
    printf("%d,", sizeof(i));
    printf(" %d", a*b*s);
}

 2,8
 2,4
 
 4,8
 4,12
 

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

 Address of i: Address of j
 10 , 223
 
 Error: cannot convert parameter 1 from 'const int **' to 'int **'
 Garbage value
 

5.
What do the following declaration signify?

char *arr[10];

 arr is a array of 10 character pointers.


 arr is a array of function pointer.
 
 arr is a array of characters.
 arr is a pointer to array of characters.
 

Walk-in Fresh Graduates @ TATA AIA - Get Rs.2.8 Lakhs salary - Apply
Now Apply Now
6.
What will be the output of the program in Turb C (under DOS)?

#include<stdio.h>

int main()
{
    int arr[5], i=0;
    while(i<5)
        arr[i]=++i;

    for(i=0; i<5; i++)


        printf("%d, ", arr[i]);

    return 0;
}

 1,2,3

 garbage value 1,2,3,4


 
 1,2,3,4,5
 1,4,5
 

7.

What will be the output of the program?

#include<stdio.h>

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

 2,4,2
 2,4,4
 
 2,5,6
 1,2,5
 

8.
   What will be the output of the program ?

#include

int main()
{
    char *str;
    str = "%s";
    printf(str, "Kn");
    return 0;
}

 error
 no output
 

 k
 %s
 

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

 ((((a+i)+j)+k)+l)
 *(*(*(*(a+i)+j)+k)+l)
 
 (((a+i)+j)+k+l)
 ((a+i)+j+k+l)
 

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

 5
 9
 

 10
 3+7
 

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

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

 4,4,4
 3,3,3
 
 2,2,2
 1,1,1
 

12.
What will be the output of the program?

#include<stdio.h>
#define FUN(i, j) i##j

int main()
{
    int va1=10;
    int va12=20;
    printf("%dn", FUN(va1, 2));
    return 0;
}

 10

 20
 
 30
 40
 

Explanation:
The following program will make you understand about ## (macro concatenation) operator clearly.

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

 5
 10
 

 error
 garbage value
 

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;

14.
 
What will be the output of the program?

#include<stdio.h>
#define MESS junk

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

 junk
 MESS
 
 error
 nothing will print
 

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

15.
What will be the output of the program?

#include<stdio.h>
int get();

int main()
{
    const int x = get();
    printf("%d", x);
    return 0;
}
int get()
{
    return 20;
}

 garbage value
 error
 

 20
 0
 

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

16.
What will be the output of the program?

#include<stdio.h>
#define MAX(a, b) (a > b ? a : b)

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

 8

 9
 
 4
 7
 

Explanation:
The macro MAX(a, b) (a > b ? a : b) returns the biggest value of the given two numbers.

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

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

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


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

=> x = 9

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

Hence the output of the program is 9.

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

 stdio.h
 stddef.h
 

 stdio.h and stddef.h


 math.h
 

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

18.
What will be the output of the program?

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

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

 9,4
 27,4
 
 27,6
 error
 

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

Step 1: int a, b=3; The variable a and b are declared as an integer type and varaible b id 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 b is incremented by 3. (ie:
b=6)

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

Hence the output of the program is 27, 6.

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

 Value of elements in array


 First element of the array
 

 Base address of the array


 Address of the last element of array
 

Explanation:
The statement 'C' is correct. When we pass an array as a funtion argument, the base address of the array
will be passed.
20.
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;
}

 1

 2
 
 4
 6
 

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.

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

 65480,65490
 65480,65496
 
 65480,65499
 65480,65489
 

22. The operator used to get value at address stored in a pointer variable is

 *
 &&
 
 !!
 $$
 

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

 .
 #
 
 &

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

 Error: RValue required


 Error: cannot convert from 'const int *' to 'int *const'
 
 Error: LValue required in strcpy

 No error
 

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

K 75 0.000000

25.
What will be the output of the program ?

#include<stdio.h>

int main()
{
    static char *s[] = {"black", "white", "pink", "violet"};
    char **ptr[] = {s+3, s+2, s+1, s}, ***p;
    p = ptr;
    ++p;
    printf("%s", **p+1);
    return 0;
}

 ink
 ack
 
 ite
 let
 

26.
What will be the output of the program ?

#include<stdio.h>

int main()
{
    void fun(int, int[]);
    int arr[] = {1, 2, 3, 4};
    int i;
    fun(4, arr);
    for(i=0; i<4; i++)
        printf("%d,", arr[i]);
    return 0;
}
void fun(int n, int arr[])
{
    int *p=0;
    int i=0;
    while(i++ < n)
        p = &arr[i];
    *p=0;
}

 0.1.2.3

 1,2,3,4
 
 1,3,4,5
 1,4,5,6
 

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

 2,3,4
 2,2,2
 
 3,3,3
 4,4,4
 

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.

28.
What will be the output of the program?

#include<stdio.h>

int main()
{
    const int i=0;
    printf("%dn", i++);
    return 0;
}

 10
 11
 
 no output

 Error: ++needs a value


 

Explanation:
This program will show an error "Cannot modify a const object".

Step 1: const int i=0; The constant variable 'i' is declared as an integer and initialized with value of
'0'(zero).

Step 2: printf("%dn", i++); Here the variable 'i' is increemented by 1(one). This will create an error "Cannot
modify a const object".

Because, we cannot modify a const variable.

29.
Declare the following statement?
"A pointer to a function which receives an int pointer and returns float pointer".

 float *(ptr)*int;
 float *(*ptr)(int)
 
 float *(*ptr)(int*)
 float (*ptr)(int)
 

30.
Which of the following statements are correct about an array?
1: The array int num[26]; can store 26 elements.
2: The expression num[1] designates the very first element in the array.
3: It is necessary to initialize the array at the time of declaration.
4: The declaration num[SIZE] is allowed if SIZE is a macro.

 1,2

 1,4
 
 1,5
 1,6
 
C Language Test 8
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;
}

 Before modification arr[3] = 4 After modification arr[3] = 10


 Error: Invalid parameter
 
 both a and b
 none of these
 

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

 A keyword used to create variables


 A variable that stores address of an instruction
 

 A variable that stores address of other variable


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

 near=2 far=4 huge=4


 near=4 far=8 huge=8
 
 near=2 far=4 huge=8
 near=4 far=4 huge=8
 

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

 1

 2
 
 4
 6
 

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.


Walk-in Fresh Graduates @ TATA AIA - Get Rs.2.8 Lakhs salary - Apply
Now Apply Now

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;
}
 Error: RValue required
 Error: cannot convert from 'const int *' to 'int *const'
 
 Error: LValue required in strcpy

 No error
 

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

 128
 garbage value
 
 error
 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]

 ((((a+i)+j)+k)+l)

 *(*(*(*(a+i)+j)+k)+l)
 
 (((a+i)+j)+k+l)
 ((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;

 In the first statement 6 specifies a particular element, whereas in the


second statement it specifies a type
 In the first statement 6 specifies a array size, whereas in the second

statement it specifies a particular element of array.


 
 In the first statement 6 specifies a particular element, whereas in the
second statement it specifies a array size.
 In both the statement 6 specifies array size.
 

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

 12,6,12
 11,5,11
 
 11,5,garbage
 12,6,garbage
 

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)();

 cmp is a pointer to an void function type.


 cmp is a void type pointer function
 
 cmp is a function that return a void pointer.

 cmp is a pointer to a function which returns void .


 

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

 error
 h
 

 hello
 hel
 

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

 mello
 hello
 
 hmello
 none of the above
 

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

 Address of i: Address of j
 10 , 223
 

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

15.
 
What will be the output of the program?

#include<stdio.h>
#define MESS junk

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

 junk

 MESS
 
 error
 nothing will print
 

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;

 argv is a pointer to pointer.

 argv is a pointer to a char pointer.


 
 argv is a function pointer.
 argv is a member of function pointer.
 
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;
}

 5
 9
 

 10
 3+7
 

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

 30
 27
 
 9
 3
 

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

 65480,65490

 65480,65496
 
 65480,65499
 65480,65489
 

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

 25

 11
 
 error
 garbage value
 

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

 char *ptr[3]();

 char *ptr[3];
 
 char (*ptr[3])();
 char **ptr[3];
 

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;
}
 4,4,4
 3,3,3
 
 2,2,2
 1,1,1
 

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

 2,3,4
 2,2,2
 
 3,3,3
 4,4,4
 

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();

 f is a pointer variable of function type.

 f is a function returning pointer to an int


 
 f is a function pointer.
 f is a simple declaration of pointer variable.
 
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;
}

 5
 10
 
 error
 garbage value
 

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;
}
 It compiles
 Compiles with an warning
 

 Not compile
 Compiles and print nothing
 

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

 1

 10
 
 3
 4
 

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

 char *ptr[3]();
 char (*ptr)*[3];
 
 char (*ptr[3])();

 char (*ptr)[3];
 

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;
}
 4,4,4
 2,4,4
 
 4,2,4
 2,2,2
 

C Language Test 9

1. The operator used to get value at address stored in a pointer variable is

 *
 &&
 
 !!
 $$
 

2.
What does the following declaration mean?
int (*ptr)[10];

 ptr is array of pointers to 10 integers

 ptr is a pointer to an array of 10 integers


 
 ptr is an array of 10 integers
 ptr is an pointer to array
 

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

 near=2 far=4 huge=4


 near=4 far=8 huge=8
 
 near=2 far=4 huge=8
 near=4 far=4 huge=8
 

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

4.
    What do the following declaration signify?

char *scr;
 scr is a pointer to pointer variable.
 scr is a function pointer.
 

 scr is a pointer to char.


 scr is a member of function pointer.
 

5.
What will be the output of the program?

#include<stdio.h>
#define FUN(i, j) i##j

int main()
{
    int va1=10;
    int va12=20;
    printf("%dn", FUN(va1, 2));
    return 0;
}

 10

 20
 
 30
 40
 

Explanation:
The following program will make you understand about ## (macro concatenation) operator clearly.
Walk-in Fresh Graduates @ TATA AIA - Get Rs.2.8 Lakhs salary - Apply
Now Apply Now

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

 char *ptr[3]();

 char *ptr[3];
 
 char (*ptr[3])();
 char **ptr[3];
 

7.
What do the following declaration signify?

int (*pf)();

 pf is a pointer to function.


 pf is a function pointer.
 
 pf is a pointer to a function which return int
 pf is a function of pointer variable.
 

8.
What do the following declaration signify?

int *ptr[30];

 ptr is a pointer to an array of 30 integer pointers.

 ptr is a array of 30 pointers to integers.


 
 ptr is a array of 30 integer pointers.
 ptr is a array 30 pointers.
 

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

 In the first statement 6 specifies a particular element, whereas in the second
statement it specifies a type
 In the first statement 6 specifies a array size, whereas in the second statement it

specifies a particular element of array.


 
 In the first statement 6 specifies a particular element, whereas in the second
statement it specifies a array size.
 In both the statement 6 specifies array size.
 

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

 128
 garbage value
 
 error
 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"


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

#include<stdio.h>
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;
}

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

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


 
 i= FFE2 ptr=20 j=FFE4 ptr=30
 garbage value
 

12.
What do the following declaration signify?
void (*cmp)();

 cmp is a pointer to an void function type.


 cmp is a void type pointer function
 
 cmp is a function that return a void pointer.

 cmp is a pointer to a function which returns void .


 

13.
Which of the following statements are correct about an array?
1: The array int num[26]; can store 26 elements.
2: The expression num[1] designates the very first element in the array.
3: It is necessary to initialize the array at the time of declaration.
4: The declaration num[SIZE] is allowed if SIZE is a macro.

 1,2

 1,4
 
 1,5
 1,6
 

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

 4,4,4
 2,4,4
 
 4,2,4
 2,2,2
 

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

 30
 27
 
 9
 3
 

16. 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?
 .
 #
 
 &

 ->
 

17.
What do the following declaration signify?

char *arr[10];

 arr is a array of 10 character pointers.


 arr is a array of function pointer.
 
 arr is a array of characters.
 arr is a pointer to array of characters.
 

18.
In which stage the following code 
#include<stdio.h> 
gets replaced by the contents of the file stdio.h

 During editing
 During linking
 
 During execution
 During preprocessing
 

Explanation:
The preprocessor replaces the line #include <stdio.h> with the system header file of that name. More
precisely, the entire text of the file 'stdio.h' replaces the #include directive.

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

 Error: RValue required


 Error: cannot convert from 'const int *' to 'int *const'
 
 Error: LValue required in strcpy

 No error
 

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

K 75 0.000000

20.
What do the following declaration signify?

char **argv;

 argv is a pointer to pointer.

 argv is a pointer to a char pointer.


 
 argv is a function pointer.
 argv is a member of function pointer.
 

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

 char *ptr[3]();
 char (*ptr)*[3];
 
 char (*ptr[3])();

 char (*ptr)[3];
 

22.

What will be the output of the program?


#include<stdio.h>

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

 2,4,2

 2,4,4
 
 2,5,6
 1,2,5
 

23.
What will be the output of the program?

#include<stdio.h>

int main()
{
    const int i=0;
    printf("%dn", i++);
    return 0;
}

 10
 11
 
 no output

 Error: ++needs a value


 
Explanation:
This program will show an error "Cannot modify a const object".

Step 1: const int i=0; The constant variable 'i' is declared as an integer and initialized with value of
'0'(zero).

Step 2: printf("%dn", i++); Here the variable 'i' is increemented by 1(one). This will create an error "Cannot
modify a const object".

Because, we cannot modify a const variable.

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

 error
 h
 

 hello
 hel
 

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

25.
What will be the output of the program ?

#include<stdio.h>

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

 1

 10
 
 3
 4
 

26.
     What will be the output of the program?

#include
typedef void v;
typedef int i;

int main()
{
    v fun(i, i);
    fun(2, 3);
    return 0;
}
v fun(i a, i b)
{
    i s=2;
    float i;
    printf("%d,", sizeof(i));
    printf(" %d", a*b*s);
}

 2,8
 2,4
 
 4,8

 4,12
 

27.
   What will be the output of the program ?

#include

int main()
{
    float arr[] = {12.4, 2.3, 4.5, 6.7};
    printf("%dn", sizeof(arr)/sizeof(arr[0]));
    return 0;
}

 5

 4
 
 7
 8
 
28.
What will be the output of the program?

#include<stdio.h>
int get();

int main()
{
    const int x = get();
    printf("%d", x);
    return 0;
}
int get()
{
    return 20;
}

 garbage value
 error
 

 20
 0
 

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

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

 12,6,12
 11,5,11
 
 11,5,garbage
 12,6,garbage
 

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

30.
What do the following declaration signify?

int *f();
 f is a pointer variable of function type.

 f is a function returning pointer to an int


 
 f is a function pointer.
 f is a simple declaration of pointer variable.
 

C Language Test 10

1.
What will be the output of the program in Turb C (under DOS)?

#include<stdio.h>

int main()
{
    int arr[5], i=0;
    while(i<5)
        arr[i]=++i;

    for(i=0; i<5; i++)


        printf("%d, ", arr[i]);

    return 0;
}

 1,2,3

 garbage value 1,2,3,4


 
 1,2,3,4,5
 1,4,5
 

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

 65480,65490

 65480,65496
 
 65480,65499
 65480,65489
 

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

 2,3,4
 2,2,2
 
 3,3,3
 4,4,4
 

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.


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

 error
 h
 

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

5.
What will be the output of the program ?

#include<stdio.h>
void fun(int **p);

int main()
{
    int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0};
    int *ptr;
    ptr = &a[0][0];
    fun(&ptr);
    return 0;
}
void fun(int **p)
{
    printf("%dn", **p);
}

 1
 2
 
 3
 4
 

Walk-in Fresh Graduates @ TATA AIA - Get Rs.2.8 Lakhs salary - Apply
Now Apply Now

6.
What will be the output of the program ?

#include<stdio.h>

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

 1

 10
 
 3
 4
 

7.
What will be the output of the program ?

#include<stdio.h>

int main()
{
    void fun(int, int[]);
    int arr[] = {1, 2, 3, 4};
    int i;
    fun(4, arr);
    for(i=0; i<4; i++)
        printf("%d,", arr[i]);
    return 0;
}
void fun(int n, int arr[])
{
    int *p=0;
    int i=0;
    while(i++ < n)
        p = &arr[i];
    *p=0;
}

 0.1.2.3

 1,2,3,4
 
 1,3,4,5
 1,4,5,6
 

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

 char *ptr[3]();
 char (*ptr)*[3];
 
 char (*ptr[3])();

 char (*ptr)[3];
 
9. What would be the equivalent pointer expression for referring the array element a[i][j][k][l]

 ((((a+i)+j)+k)+l)

 *(*(*(*(a+i)+j)+k)+l)
 
 (((a+i)+j)+k+l)
 ((a+i)+j+k+l)
 

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

 5
 9
 

 10
 3+7
 

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


11.
What do the following declaration signify?

int (*pf)();

 pf is a pointer to function.


 pf is a function pointer.
 

 pf is a pointer to a function which return int


 pf is a function of pointer variable.
 

12.
What will be the output of the program?

#include<stdio.h>
#define MIN(x, y) (x<y)? x : y;
int main()
{
    int x=3, y=4, z;
    z = MIN(x+y/2, y-1);
    if(z > 0)
        printf("%dn", z);
    return 0;
}

 3
 4
 
 0
 no output
 

Explanation:

The macro MIN(x, y) (x<y)? x : y; returns the smallest value from the given two numbers.

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

Step 2: z = MIN(x+y/2, y-1); becomes,

=> z = (x+y/2 < y-1)? x+y/2 : y - 1;

=> z = (3+4/2 < 4-1)? 3+4/2 : 4 - 1;

=> z = (3+2 < 4-1)? 3+2 : 4 - 1;

=> z = (5 < 3)? 5 : 3;

The macro return the number 3 and it is stored in the variable z.

Step 3: if(z > 0) becomes if(3 > 0) here the if condition is satisfied. It executes the if block statements.

Step 4: printf("%dn", z);. It prints the value of variable z.

Hence the output of the program is 3

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

 Before modification arr[3] = 4 After modification arr[3] = 10


 Error: Invalid parameter
 
 both a and b
 none of these
 

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

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

 It compiles
 Compiles with an warning
 

 Not compile
 Compiles and print nothing
 

Explanation:

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

15.

What will be the output of the program?

#include<stdio.h>

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

 2,4,2

 2,4,4
 
 2,5,6
 1,2,5
 
16.
Declare the following statement?
"A pointer to a function which receives an int pointer and returns float pointer".

 float *(ptr)*int;
 float *(*ptr)(int)
 

 float *(*ptr)(int*)
 float (*ptr)(int)
 

17. The operator used to get value at address stored in a pointer variable is

 *
 &&
 
 !!
 $$
 

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

int main()
{
    float arr[] = {12.4, 2.3, 4.5, 6.7};
    printf("%dn", sizeof(arr)/sizeof(arr[0]));
    return 0;
}

 5

 4
 
 7
 8
 

19.      A pointer is

 A keyword used to create variables


 A variable that stores address of an instruction
 

 A variable that stores address of other variable


 All of the above
 
20.
      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;
}

 30
 27
 
 9
 3
 

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

 4,4,4
 3,3,3
 
 2,2,2
 1,1,1
 

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

 5
 10
 
 error
 garbage value
 

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;

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

 Value of elements in array


 First element of the array
 

 Base address of the array


 Address of the last element of array
 
Explanation:

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

24.
What will be the output of the program if the array begins 1200 in memory?

#include<stdio.h>

int main()
{
    int arr[]={2, 3, 4, 1, 6};
    printf("%u, %u, %un", arr, &arr[0], &arr);
    return 0;
}

 1300,1200,1500

 1200,1200,1200
 
 1300,1400,1500
 1200,1200,1300
 

25.
What do the following declaration signify?

int *ptr[30];
 ptr is a pointer to an array of 30 integer pointers.

 ptr is a array of 30 pointers to integers.


 
 ptr is a array of 30 integer pointers.
 ptr is a array 30 pointers.
 

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

 mello
 hello
 
 hmello
 none of the above
 
27. In which header file is the NULL macro defined?

 stdio.h
 stddef.h
 

 stdio.h and stddef.h


 math.h
 

Explanation:

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

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

 1
 2
 
 4
 6
 

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.

29.
What will be the output of the program?

#include<stdio.h>
#define MAX(a, b) (a > b ? a : b)

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

 8
 9
 
 4
 7
 

Explanation:

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

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

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

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

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

=> x = 9

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

Hence the output of the program is 9.

30.
What will be the output of the program ?

#include<stdio.h>

int main()
{
    static char *s[] = {"black", "white", "pink", "violet"};
    char **ptr[] = {s+3, s+2, s+1, s}, ***p;
    p = ptr;
    ++p;
    printf("%s", **p+1);
    return 0;
}

 ink
 ack
 
 ite
 let
 

You might also like