C Programming :: Dynamic Memory Allocation

NA
SHSTTON
6
Solv. Corr.
10
Solv. In. Corr.
16
Attempted
0 M:0 S
Avg. Time

1 / 18

What will print out?

#include
void main()
{
char *p1="name";
char *p2;
p2=(char*)malloc(20);
memset (p2, 0, 20);
while(*p2++ = *p1++);
printf("%s\n",p2);
}


Aname

Bempty string

Cnull values

DNone of these

 View Answer |  Discuss in Forum |  Workspace | Asked In BOA (Bank of America)Societe Generale |

Answer: Option B

Explanation:

In the given question expression while(*p2++ = *p1++); loop is evaluating the expression: *p2++ = *p1++. The while loop expression *p2 = *p1 is evaluated using the result of *p1. However, this value is still assigned to *p2 even if the expression evaluates as false or (0).


As in each step p1 and p2 address is incremented, when while loop terminated y that time p1 and p2 will point to null. so that when p2 printed blank printed. even p1 will too print blank (empty string)



Workspace

NA
SHSTTON
6
Solv. Corr.
2
Solv. In. Corr.
8
Attempted
0 M:0 S
Avg. Time

2 / 18

What is the output of the following C Program?

#include
#include
void main()
{
int *mptr, *cptr;
mptr = (int*)malloc(sizeof(int));
printf("%d",*mptr);
cptr = (int*)calloc(sizeof(int),1);
printf("%d",*cptr);
}


A0 0

Bgarbage-value 0

C0 garbge value

DCompilation Error

Answer: Option B

Explanation:

The memory space allocated by malloc is uninitialized,
whereas calloc returns the allocated memory space initialized to zeros.

Workspace

NA
SHSTTON
1
Solv. Corr.
5
Solv. In. Corr.
6
Attempted
0 M:0 S
Avg. Time

3 / 18

What is the output of the following 'C' program ?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct
{
char *name;
} Addr;
int main()
{
Addr *s;
char comm[10];
char *str = "Hello"; 
s = (Addr *)malloc(sizeof(Addr)); 
printf("Enter a name"); 
fgets(comm, 10,stdin);
s->name = (char*)malloc(sizeof(char[strlen(comm)]));
strcpy(s->name, comm); 
strcat(str,s->name); 
printf("%s", str);
}

// for the input: india

Aindia

BHello india

CSegmentation fault

DCompilation Error

ENone of these

Answer: Option C

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
0
Solv. Corr.
6
Solv. In. Corr.
6
Attempted
0 M:0 S
Avg. Time

4 / 18

What is the output of the following 'C' program ?
#define MAX 50 
int fun1()
{
char *s1;
s1=(char *) malloc (MAX*sizeof(char));
s1="hello";
printf("%d",s1);
free(s1);
}
int fun2() 
{
char *s;
s=(char *) malloc (MAX*sizeof(char));
strcpy(s,"hello");
printf("%d",s);
free(s);
}
int main ()
{
fun1();
fun2();
return 0;
}

ACompilation Error

Bhello hello

CSegmentation fault

Dhello Segmentation fault

 View Answer |  Discuss in Forum |  Workspace | Asked In Societe Generale |

Answer: Option D

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
9
Solv. Corr.
38
Solv. In. Corr.
47
Attempted
0 M:0 S
Avg. Time

5 / 18

What will be the output of the program?

#include
#include

int main()
{
int *p;
p = (int *)malloc(20); /* Assume p has address of 1314 */
free(p);
printf("%u", p);
return 0;
}


A1314

BGarbage value

C1316

DRandom address

Answer: Option D

Explanation:

the answer is 12157648 ie random values


Workspace

NA
SHSTTON
1
Solv. Corr.
5
Solv. In. Corr.
6
Attempted
0 M:0 S
Avg. Time

6 / 18

What is the output of the following 'C' program ?

#include
#include
int main()
{
int *p;
p = (int *)malloc(20);
printf("%d\n", sizeof(p));
free(p);
return 0;
}


A4

B2

C8

DGarbage value

Answer: Option B

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
1
Solv. Corr.
4
Solv. In. Corr.
5
Attempted
0 M:0 S
Avg. Time

7 / 18

What is the output of the following C Program?

#include
#include
#define MAXROW 3
#define MAXCOL 4

int main()
{
int (*p)[MAXCOL];
p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p));
return 0;
}


A56 bytes

B128 bytes

C24 bytes

D12 bytes

 View Answer |  Discuss in Forum |  Workspace | Asked In Societe Generale |

Answer: Option C

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
5
Solv. Corr.
42
Solv. In. Corr.
47
Attempted
0 M:20 S
Avg. Time

8 / 18

What is the output of the program on a 32 bit Operating System?

#include
#include
void main () {
double val = 12.34;
double *dp = &val;
char *cp;
cp = (char*)malloc(sizeof(char) * 20);
sprintf (cp, "hello world");
printf("%d %d %d", sizeof(cp), sizeof(dp), sizeof(val));
}


A4 4 8

B20 8 8

C11 8 8

D20 4 8

Answer: Option A

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
6
Solv. Corr.
8
Solv. In. Corr.
14
Attempted
0 M:0 S
Avg. Time

9 / 18

What is the output of the program?

#include
#include
void main()
{
char *pl= "word";
char *p2;
p2=(char*)malloc(10);

memset(p2, 'A', 10);
while(*p2++ = *pl++)
printf("%s ", p2);
}


AAAAAAworda AAAAAwordA AAAAwordA AAwordA

BAAAAAAAAA AAAAAAAA AAAAAAA AAAAAA

CAAAAAword AAAAAwor AAAAAwo AAAAAw

DwordAAAAA wordAAAa wordAAA wordAA

Answer: Option B

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
1
Solv. Corr.
3
Solv. In. Corr.
4
Attempted
0 M:0 S
Avg. Time

10 / 18

What will be output of the following "c" code?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
char *p = "Hello";
char *q = "world";
int l1 = strlen (p);
int l2 = strlen (q);
char *l = (char*) malloc (sizeof (l1 + l2)+1);
while (*p) *l++ = *p++;
while (*q) *l++ = *q++;
*l = '\0'; 
}

ASegmentation Fault

BCompilation Error

CNo output No Error

DNone of these

Answer: Option C

Explanation:

Here is no explanation for this answer

Workspace