1. Given the C strings, describe the output of each code segment. Each question
ID: 3690351 • Letter: 1
Question
1. Given the C strings, describe the output of each code segment. Each question is independent of the others.
char w1 [10] = “crater”;
char w2 [5] = “hill”;
char w3[5];
a. w3[0] = ‘d’;
w3[1] = ‘o’;
w3[2] = ‘e’;
w3[3] = ‘’;
w3[4] = ‘s’;
cout << w3;
___________________________________________
b. w2[0] = w1[3];
w2[3] = w1[4];
cout << w1 << “ “ << w2; ___________________________________________
c. cout << strlen(w1) << strlen(w2);
___________________________________________
d. strcpy (w3, w2);
strcpy (w1, w3);
cout << w1 << “ “ << w2 << “ “ w3;
___________________________________________
e. if (strcmp (w1, w2) < 0) cout << “low”;
else if (strcmp (w1, w2) == 0)) cout << “same”;
else cout << “high”;
___________________________________________
2. Which statement(s) is true about a dynamic array?
a. It is an array with size known at compile-time.
b. It is accessed from the main program via a pointer variable.
c. It is an array for which run-time index bounds checking has been requested.
d. It is an array which is automatically resized during execution if the programmer attempts to overwrite the end of the array.
e. All statements a) – d) are true about a dynamic array.
3. What is the output of the following code?
int k = 1, m = 2;
int *p1, *p2;
p1 =&k;
p2 = &m;
*p1 = 10;
*p2 = 20;
cout << *p1 << “ “<< *p2 << endl;
p1 = p2;
cout << *p1 << “ “ <<*p2 << endl;
____________________________________________
____________________________________________
4. Write the output of the code on each provided line. Write ? if garbage displays in an array element.
int main ( )
{ int * a = new int [4];
int * p;
int n = 10;
int k;
for (p = a; p < a + 4; p++)
{ *p = n;
n = n * 2;
}
for (int k = 0; k < 4; k++)
cout << a[k] << “ “; __________________________________________
return 0;
}
5. Which of the code segments best accomplishes the task of creating a dynamic double array of size 50 and assigning value 0.0 to each component?
a. double * d = new double; b. double * d = new double[50];
for (int ct = 0; ct < 50; ct++) for (int ct = 0; ct < 50; ct++) { *d = 0.0; *d = 0.0;
d++; }
c. double * d = new double [50]; d. for (int ct = 0; ct < 50; ct++)
for (int ct = 0; ct < 50; ct++) { double d = new double;
d[ct] = 0.0; *d = 0.0; }
6. Which statement is true about the effect of the code below?
int a [4] = { 10, 20, 30, 40 };
int * ptr;
for (ptr = a; ptr < a+4; ptr++)
*ptr = *ptr + 1;
a. The elements of array a change values to 11, 21, 31, and 41.
b. The initial elements of array a are shifted up by one array index.
c. The elements of array a retain their initial values 10, 20, 30, and 40.
d. Array a now holds memory addresses instead of integers 10, 20, 30, and 40.
e. A new array with elements 11, 21, 31, and 41 is created and variable ptr holds its base address.
7. Which of the code segments creates a memory leak? A memory leak occurs if dynamic data remains on the head but the pointer to it has been lost.
a. int * p1 = new int; b. int * p1 = new int;
int * p2 = new int; int * p2 = new int;
*p1 = 5; *p1 = 8;
*p2 = 6; *p2 = 9;
p1 = p2; delete p1;
c. Neither code segment creates a memory leak.
d. Both code segments create a memory leak.
Explanation / Answer
1.
a.output
doe
b.output
crater tile
c.output
64
d.compile time error(expected ';' before "W3" )
to resolve compilation we have to do put "<<" (cout << w1 << “ “ << w2 << “ “<< w3; )
If we put this it will display tile tile tile
e.output
compile time error(expected primary expression ) before)
to resolve compilation error we have to remove ")" ( else if (strcmp (w1, w2) == 0)) )
once we remove the ")" output will be same
2.Ans
d
3.Output
10 20
20 20
4.Output
10 20 40 80
5.Ans
C
6.Ans
a. The elements of array a change values to 11, 21, 31, and 41.
7.Ans
d The pointers never deleted
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.