100% found this document useful (2 votes)
187 views

05 Preprocessor Directives and Dynamic Memory AllocationWithAnswers Updated 1

The document contains 16 questions related to preprocessor directives and dynamic memory allocation in C programming. Some key concepts covered include: - The use of preprocessor directives like #define, #ifdef, #include to define macros and conditionally compile code. - Using the __LINE__ predefined macro to print the current line number. - Dynamic memory allocation functions like malloc(), calloc(), realloc() to allocate and manage memory for pointers. - Potential issues with returning pointers to local variables from functions or using uninitialized pointers. The questions test understanding of how preprocessor directives work, how to use dynamic memory functions, and identify code snippets that may cause errors or undefined behavior.

Uploaded by

Tejas Rane
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
187 views

05 Preprocessor Directives and Dynamic Memory AllocationWithAnswers Updated 1

The document contains 16 questions related to preprocessor directives and dynamic memory allocation in C programming. Some key concepts covered include: - The use of preprocessor directives like #define, #ifdef, #include to define macros and conditionally compile code. - Using the __LINE__ predefined macro to print the current line number. - Dynamic memory allocation functions like malloc(), calloc(), realloc() to allocate and manage memory for pointers. - Potential issues with returning pointers to local variables from functions or using uninitialized pointers. The questions test understanding of how preprocessor directives work, how to use dynamic memory functions, and identify code snippets that may cause errors or undefined behavior.

Uploaded by

Tejas Rane
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Preprocessor Directives

Dynamic MemoryAllocation
1.
#include <stdio.h>
void fun();
int main( void )
{
fun();
return 0;
}
void fun()
{
#ifndef value
#define value 100
#undef value
#else
#undef value
#define value 200
#endif
#define Value 300 printf("Value : %d",Value);
return ;
}
A. Value : 100
B. Value : 200
C. no output
D. Compile time error
E. Value : 300
Answer: C

2.
#include<stdio.h>
#line 100
int main( void )
{
printf("\n line = %d ",__LINE__ );
#line 0
printf(" line = %d ",__LINE__ );
#line 100
printf(" line = %d ",__LINE__ );
return 0;
}

Augest 2019 – December 2019 1


Preprocessor Directives
Dynamic MemoryAllocation
A. run time error
B. compile time error
C. line = 102 line = 0 line = 100
D. line = 103 line = 0 line = 102

Answer: C

3.

#include<stdio.h>
#include<string.h>
int main( void )
{
#define SUNBEAM "SUNBEAM IT PARK \n"
#define Sunbeam "SUNBEAM MARKETYARD\n"
#ifdef SUNBEAM
printf(SUNBEAM);
#endif
#ifdef SUNBEAM
printf(Sunbeam);
#endif
printf("\'Sunbeam\'\n");
printf("\"SUNBEAM\" \n");
return 0;
}

A. SUNBEAM IT PARK
B. SUNBEAM IT PARK
SUNBEAM MARKETYARD
Sunbeam
SUNBEAM
C. SUNBEAM IT PARK
SUNBEAM MARKETYARD
'Sunbeam'
"SUNBEAM"
D. SUNBEAM MARKETYARD

Answer: C

Augest 2019 – December 2019 2


Preprocessor Directives
Dynamic MemoryAllocation
3.
#include<stdio.h>
#define A(x)((x)*(x))
int main( void )
{
int a, b=3;
a = 75 / (b* A((b+2)));
printf("%d\n", a);
return 0;
}

A. 1
B. 625
C. 75
D. 225

Answer: A

4.

#include <stdio.h>
#define MACRO(n, i, a, m) m##a##i##n
#define MAIN MACRO(n, i, a, m)

#define SUNMEAM sunbeam


int MAIN(void )
{

printf("\"SUNBEAM\"");
return 0;
}

A. "SUNBEAM"
B. SUNBEAM
C. sunbeam
D. compile time error

Answer: A

Augest 2019 – December 2019 3


Preprocessor Directives
Dynamic MemoryAllocation
5.
#include<stdio.h>
#define SWAP(a, b) {b ^= b; a ^= a; b ^= b ;}
int main( void )
{
int x = 10;
int y = 20;
x=x*y; y=x/y; x=x/y;
SWAP(x, y);
x=x+y; y=x-y; x=x-y;
printf("X=%d,Y=%d",x,y);
return 0;
}

A. X=0,Y=0
B. X=10,Y=20
C. X=20,Y=10
D. Compile time error

Answer: A

6.
#include<stdio.h>
#define exp(a) a+a * 5 / a*a
int main( void )
{
int x = exp(3+2) * 5;
printf("Value of X=%d",x);
return 0;
}

A. Value of X=27
B. Value of X=32
C. Value of X=20
D. compile time error

Answer: A

Augest 2019 – December 2019 4


Preprocessor Directives
Dynamic MemoryAllocation
7.
#include<stdio.h>
#define int char
int main( void )
{
int* i = 65, j=56;
printf("sizeof(i)=%d sizeof(j)=%d",sizeof(i),sizeof(j));
return 0;
}

A. sizeof(i)=8 sizeof(j)=1
B. sizeof(i)=4 sizeof(j)=1
C. sizeof(i)=8 sizeof(j)=8
D. sizeof(i)=4 sizeof(j)=4

Answer: B

8.
#include <stdio.h>
#define INCREMENT(x) --x
#define DECREMENT(x) ++x
int main( void )
{
char *ptr = "SunbeamKarad";
int x = 10;
printf("%s", DECREMENT(ptr));
printf("%3d\t",DECREMENT(x));
printf("%s", INCREMENT(ptr));
printf("%3d", INCREMENT(x));
return 0;
}

A. unbeamKarad 11 SunbeamKarad 10
B. SunbeamKarad 11 unbeamKarad 10
C. garbage value SunbeamKarad 11 SunbeamKarad 10
D. garbagevalue SunbeamKarad 9 SunbeamKarad 10

Answer: A

Augest 2019 – December 2019 5


Preprocessor Directives
Dynamic MemoryAllocation
9.
#include<stdio.h>
#define f1(para1,para2) para1##para2
#define f2(para2,para1) para1##para2
int main(void)
{
char var_='A';
printf("%c ",++f1(var,_));
printf(" %d",--f2(_,var));
return 0;
}
A. B 65
B. A 65
C. B 66
D. B 65
Answer: A

10.
#include<stdio.h>
#include<string.h>
int main(void)
{
char joining1[10]="",joining2[10]="",joining3[10]="";
strcat(joining1,"TIME");
strcat( joining1,"_"); strcat( joining1,"_");
strcat( joining3,joining1);
strcat( joining2,"_"); strcat( joining2,"_");
strcpy( joining1,joining2);
strcat( joining1,joining3);
printf("\n time= %s", joining1);
return 0;
}
A. time= __TIME__
b. time= current time will print
c. compile time error
D. run time error

Answer: A

Augest 2019 – December 2019 6


Preprocessor Directives
Dynamic MemoryAllocation
11.
#include<stdio.h>
#include<stdlib.h>
int main ( void )
{
char *title=NULL;
title = (char *) malloc(15);
strcpy(title, "C Programming");
printf("String = %c", *title);
free(title); title=0;
strcpy(title, "C++");
printf(" %s", title);
return 0;
}
A. String = C Programming
B. Complile time error
C. String = C C++
D. exit value -1

Answer: D

12. Consider 32 bit compilation.


#include<stdio.h>
#include<stdlib.h>
int main(void)
{
char *ptr=NULL;
ptr = (char *)calloc(20,sizeof(char));
printf("%d bytes\n", sizeof(ptr));
free(ptr); ptr=NULL;
return 0;
}
A. 4 bytes
B. 1 bytes
C. 8 bytes
D. 20 bytes

Answer: A

Augest 2019 – December 2019 7


Preprocessor Directives
Dynamic MemoryAllocation
13.
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
char *ptr=NULL;
ptr = (char *)calloc(1,10);
strcpy(ptr, "Sunbeam");
ptr = (char *)realloc(ptr,20);
strcat(ptr," IT PARK");
printf("%c",
(ptr[0]>=65 && ptr[0]<=90)? ptr[14]+32 : ptr[0]-32);
free(ptr);
ptr=NULL;
return 0;
}

A. K
B. k
C. s
D. S

Answer: B

14.

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
char *ptr=NULL;
strcpy(ptr , "Demo1");
strcpy(ptr , "Demo2");
free(ptr);
printf("%s\n",ptr);
return 0;
}

Augest 2019 – December 2019 8


Preprocessor Directives
Dynamic MemoryAllocation
A. Demo1
B. Demo2
C. Demo1Demo2
D. exit value -1

Answer: D

15.
what type of data u can store in this block of memory?
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
void *ptr=NULL;
ptr = malloc(10);
return 0;
}
A. int
B. char
C. float
D. all of above data types

Answer: D

16.
Which of the above three functions are likely to cause
problems with pointers?
int * fun1 (void)
{
int x= 10;
return (&x);
}
int * fun2 (void)
{
int * px;
*px= 10;
return px;
}

Augest 2019 – December 2019 9


Preprocessor Directives
Dynamic MemoryAllocation
int *fun3 (void)
{
int *px;
px = (int *) malloc (sizeof(int));
*px= 10;
return px;
}

A. function fun1 and fun2


B. function fun2 and fun3
C. function fun1 , fun2 and fun3
D. function fun3

Answer: A

Augest 2019 – December 2019 10

You might also like