Would you like to submit the test?

# C Language Questions And Answers Sample Test 5

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

## C Language Test 5

You have scored 3 out of 10
You Scored:2
Total Questions:2
Attended Questions:2
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;
}

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

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

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

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

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

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

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

9.
What do the following declaration signify?

int *f();

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

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

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

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

Explanation:

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

14.
What do the following declaration signify?

int *ptr[30];

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

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

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

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

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

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

21.

What will be the output of the program?

#include<stdio.h>
#define MESS junk

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

Explanation:

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

22.
What do the following declaration signify?

int (*pf)();

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

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

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

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

26.    What is (void*)0?

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

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

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

30.
What do the following declaration signify?

char *arr[10];

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