0% found this document useful (0 votes)
29 views4 pages

ct2 Sol

The document provides instructions and questions for a class test on software engineering. Question 1 asks about output of code snippets and errors from compiling code. Question 2 defines a Polynomial class and asks to provide a function to copy coefficients for the copy constructor.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views4 pages

ct2 Sol

The document provides instructions and questions for a class test on software engineering. Question 1 asks about output of code snippets and errors from compiling code. Question 2 defines a Polynomial class and asks to provide a function to copy coefficients for the copy constructor.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Indian Institute of Technology Kharagpur

Class Test II 2023-24

Date of Examination: 06 Feb, 2024 Duration: 45 Minutes


Subject No.: CS20006/CS20202 Subject: Software Engineering
Department/Center/School: Computer Science Credits: 3 Full marks: 20

Name:
Roll Number:

Instructions
i. Please write your name and roll number above before attempting any solution.
ii. Write your answers in this question paper itself. It has been given a booklet form for this
purpose.
iii. Use of electronic calculators only is permitted. No extra resources viz. graph papers, log-tables,
trigonometric tables would be required.
iv. All questions are compulsory. Be brief and precise. Mysterious or unsupported answers
will not receive full marks.
v. A few extra blank sheets are provided at the end. Please use them, if for any question,
you need extra space.

Question: 1 2 Total
Points: 10 10 20
Score:
CS20006/CS20202 Class Test II - Page 2 of 4 06 Feb, 2024

1. (a) (3 points) What will be the output for the following program:
1 #include <iostream>
2 #include <string>
3 using namespace std;
4 void Test(string text){
5 cout << "got string '" << text << "'\n";
6 }
7 void Test(bool trueFalse){
8 cout << "got bool '" << (trueFalse?"true":"false") << "'\n";
9 }
10 int main() {
11 string s1 = "abc";
12 char * s2 = "def";
13 Test(s1); Test(s2); Test(true);
14 }

Solution:
got string 'abc'
got bool 'true'
got bool 'true'
(b) (3 points) Write the output/errors while compiling and executing the following program:
1 #include <iostream>
2 using namespace std;
3 void f1() {
4 const char * str = "Bat";
5 char * const str2 = "Rat";
6 cout << str << endl;
7 str2[0] = 'C';
8 cout << str2 << endl;
9 }
10 int main() {
11 f1();
12 }

Solution:
Bat
bus error / segmentation fault
(c) (4 points) Write the output/errors while compiling and executing the following program:
1 #include<iostream>
2 #include<stdlib.h>
3 using namespace std;
4 void * operator new[](size_t size) {
5 cout << "New operator overloading " << endl;
6 void * p = malloc(size);
7 return p;
CS20006/CS20202 Class Test II - Page 3 of 4 06 Feb, 2024

8 }
9 void operator delete[](void * p) {
10 cout << "Delete operator overloading " << endl;
11 free(p);
12 }
13 int main() {
14 int * p = new int[1];
15 *p=9;
16 cout << *p<<endl;
17 delete[] p;
18 }

Solution:
New operator overloading
9
Delete operator overloading

2. (a) (8 points) Consider the following definition of class Polynomial.


1 class Polynomial
2 {
3 int degree; // stores the degree, -1 implies no polynomial
4 double *p; // list of coefficients starting with constant
5 public:
6 Polynomial(int deg=-1) : degree(deg), p((deg!=-1) ? new double[deg
+1] : NULL) {}
7 ˜Polynomial() {delete[] p;}
8 Polynomial(const Polynomial& a) : degree(a.degree), p(coeffcopy(a))
{}
9 };
Provide an appropriate definition of the function coeffcopy below:

Solution:
1 double *coeffcopy(const Polynomial& a) {
2 if(a.degree!=-1) {
3 double *retp=new double[a.degree+1];
4 for(int i=0;i<=a.degree;i++) retp[i]=a.p[i];
5 return retp;
6 }
7 return NULL;
8 }
CS20006/CS20202 Class Test II - Page 4 of 4 06 Feb, 2024

(b) (2 points) In the above problem, should the coeffcopy function be a member function
or a global function? Why?

Solution: It should be member function in order to access the private elements.

Rough work

You might also like