Data Structure in C++ (CS 211), Slides of Data Structures and Algorithms

Answer: d. It's recursive multiplication and test(a+test(a,b-1)) iteratively calculates b times sum of a. Page 6. STEM Success Center. CS 211. Worksheet. 5 ...

Typology: Slides

2022/2023

Uploaded on 05/11/2023

ekasha
ekasha 🇺🇸

4.8

(22)

270 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
STEM Success Center
CS 211
Worksheet
Data Structures using C++
1. What will be the output of the below program?
class Num{
int a;
};
void main()
{
Num n;
cout << n.a;
}
a. Compile error
b. n.a
c. n
d. Garbage value
2. Create a class named ‘Employee’ with an integer variable Employee_ID’ and a string variable
‘Employee_name’. Create an object of the class employee and assign values to Employee_ID and
Employee_name.
3. Which of the following is used to define the class member externally?
a. :
b. ;
c. #
d. ::
pf3
pf4
pf5

Partial preview of the text

Download Data Structure in C++ (CS 211) and more Slides Data Structures and Algorithms in PDF only on Docsity!

CS 211 Data Structures using C++

  1. What will be the output of the below program? class Num{ int a; }; void main() { Num n; cout << n.a; } a. Compile error b. n.a c. n d. Garbage value
  2. Create a class named ‘Employee’ with an integer variable ‘Employee_ID’ and a string variable ‘Employee_name’. Create an object of the class employee and assign values to Employee_ID and Employee_name.
  3. Which of the following is used to define the class member externally? a. : b. ; c. # d. ::

CS 211

  1. What is the value of the test(12,13)? int test(int a, int b) { if (a == 0) return 0; return test(a+test(a,b-1)); } a. 1 b. 26 c. 0 d. 156
  2. What will be the output of the following program? #include using namespace std; int main() { int a = 32, *ptr = &a; char ch = 'P', &cho = ch; cho += a; *ptr += ch; cout << a << ", " << ch << endl; return 0; } a. 32, P b. 32, p c. 144, p d. 144, P

CS 211 Solutions

1. What will be the output of the below program? class Num{ int a; }; void main() { Num n; cout << n.a; } a) Compile error b) n.a c) n d) Garbage value Answer: a Since the default access is private in c++, a is a private member of Num and so it causes a compiler error to access it outside the class. 2. Create a class named ‘Employee’ with an integer variable ‘Employee_ID’ and a string variable ‘Employee_name’. Create an object of the class employee and assign values to Employee_ID and Employee_name. Answer: class Employee { public: int Employee_ID; string Employe_name; }; int main()

CS 211

Employee e; e.Employee_ID=43; e.Employee_name=”Jey”; cout<< e.Employee_ID << e.Employee_name<<endl; return 0; }

3. Which of the following is used to define the class member externally? a. : b. ; c. # d. :: Answer: d :: is used to define members of the class globally. 4. What is the value of the test(12,13)? int test(int a, int b) { if (a == 0) return 0; return test(a+test(a,b-1)); } a. 1 b. 26 c. 0 d. 156 Answer: d It’s recursive multiplication and test(a+test(a,b-1)) iteratively calculates b times sum of a.

CS 211

6. What will be the output of the following program? #include using namespace std; int main() { const int i = 15; const int* const ptr = &i; (ptr)++; int j = 20; ptr = &j; cout << i; return 0; } a. 20 b. 21 c. 15 d. Compile error Answer: d. Compile error Explanation: Here “ptr” has been defined as a constant pointer that holds the address of a constant integer “i”. So, neither can the value of “ptr” be changed, nor can the value held by it can be changed. Thus the lines “(ptr)++” and “ptr=&j” are invalid, as they are trying to modify the variable i’s content and the value of the pointer respectively. This gives an error.