0% found this document useful (0 votes)
10 views

Assignment 7

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Assignment 7

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

MET 111 Computer and Programing Dr.

Abdelhady Mostafa

MET 111 Assignment #7 Objects and classes

1. What does a class in C++ holds?


a) data
b) functions
c) both data & functions
d) arrays

2. Which is used to define the member of a class externally?


a) :
b) ::
c) #
d) .

3. Which of the following is a valid class declaration?


a) class A { int x; };
b) class B { }
c) public class A { }
d) object A { int x; };

4. The data members and functions of a class in C++ are by default ____________
a) public
b) private
c) None of the above

5. Constructors are used to ____________


a) initialize the objects
b) construct the data members
c) both initialize the objects & construct the data members
d) delete the objects

6. Which of the following gets called when an object is being created?


A. Constuctor
B. Virtual Function
C. Destructors
D. Main
MET 111 Computer and Programing Dr. Abdelhady Mostafa

7. State whether the following statements about the constructor are True or False.
i) constructors should be declared in the private section.
ii) constructors are invoked automatically when the objects are created.
A. True,True
B. True,False
C. False,True
D. False,False

8. Which contructor function is designed to copy object of same class type?


A. Copy constructor
B. Create constructor
C. Object constructor
D. Dynamic constructor

9. When struct is used instead of the keyword class means, what will happen in the
program?
a) access is public by default
b) access is private by default
c) None of the above

10. What is the correct syntax of accessing a static member of a Class?

---------------------------
Example class:
class A
{
public:
static int value;
}
---------------------------

a) A.value
b) A::value
c) A->value
d) A^value

11. Pick the incorrect statement about inline functions in C++?


a) They reduce function call overheads
MET 111 Computer and Programing Dr. Abdelhady Mostafa

b) These functions are inserted/substituted at the point of call


c) Saves overhead of a return call from a function
d) They are generally very large and complicated function

12. Which functions of a class are called inline functions?


a) All the functions containing declared inside the class
b) All functions defined inside or with the inline keyword
c) All the functions accessing static members of the class
d) All the functions that are defined outside the class

13. What will be the output of the following C++ code?


14. #include <iostream>
15. using namespace std;
16. class rect
17. {
18. int x, y;
19. public:
20. void val (int, int);
21. int area ()
22. {
23. return (x * y);
24. }
25. }; a) rect area: 24
26. void rect::val (int a, int b) b) rect area: 12
27. { c) compile error
28. x = a; d) rect area: 56
29. y = b;
30. }
31. int main ()
32. {
33. rect rectangular;
34. rectangular.val (3, 4);
35. cout << "rect area: " <<
rectangular.area();
36. return 0;
37. }

#include <iostream>
a) 5
#include <string>
b) Garbage value
using namespace std;
c) Error
d) None of the above
class A
MET 111 Computer and Programing Dr. Abdelhady Mostafa

{
static int a;

public:
void change(int i)
{
a = i;
}
void value_of_a()
{
cout<<a;
}
};

int main()
{
A a1 ;
a1.change(5);
a1.value_of_a();
return 0;
}

#include <iostream>
#include <string>
using namespace std;
class A
{
static int a;
public:
void change(int i){
a = i;
a) 1055
}
b) 555
void value_of_a(){
c) 101010
cout<<a;
d) 51010
}
};

int A::a = 5;

int main()
{
A a1 ;
A a2 ;
MET 111 Computer and Programing Dr. Abdelhady Mostafa

A a3 ;
a1.change(10);
a1.value_of_a();
a2.value_of_a();
a3.value_of_a();
return 0;
}

#include <iostream>
using namespace std;

class LFC
{
static int x;
public:
static void Set(int xx)
{
x = xx;
A. The program will print 0.
}
void Display() B. The program will print 33.
{ C. The program will report error.
cout<< x ;
}
};
int LFC::x = 0;
int main()
{
LFC.Set(33);
LFC.Display();
return 0;
}
#include <iostream>
using namespace std;

class Example {
public:
Example() {
cout << "Constructor Called!" << endl;
}
void display() {
cout << "Display Function Called!" <<
endl;
}
};

int main() {
Example obj;
MET 111 Computer and Programing Dr. Abdelhady Mostafa

obj.display();
return 0;
}
#include <iostream>
using namespace std;

class Test {
private:
int x = 10;
public:
void show() {
cout << "Value of x: " << x << endl;
}
};

int main() {
Test obj;
obj.show();
return 0;
}
#include <iostream>
using namespace std;

class Person {
private:
string name;
public:
Person(string n) {
name = n;
}
void introduce() {
cout << "Hello, my name is " << name
<< "." << endl;
}
};

int main() {
Person p1("Alice");
p1.introduce();
return 0;
}
#include <iostream>
using namespace std;

class Counter {
private:
static int count;
public:
MET 111 Computer and Programing Dr. Abdelhady Mostafa

Counter() {
count++;
}
static void showCount() {
cout << "Count: " << count << endl;
}
};

int Counter::count = 0;

int main() {
Counter c1, c2, c3;
Counter::showCount();
return 0;
}
#include <iostream>
using namespace std;

class Calculator {
public:
int add(int a, int b = 5) {
return a + b;
}
};

int main() {
Calculator calc;
cout << calc.add(10) << endl;
cout << calc.add(10, 20) << endl;
return 0;
}
#include <iostream>
using namespace std;

class Sample {
private:
int x;
public:
Sample(int val) : x(val) {}
Sample(const Sample &obj) {
x = obj.x;
cout << "Copy Constructor Called!" <<
endl;
}
void display() {
cout << "Value: " << x << endl;
}
};
MET 111 Computer and Programing Dr. Abdelhady Mostafa

int main() {
Sample obj1(10);
Sample obj2 = obj1; // Copy constructor
will be called
obj2.display();
return 0;
}

You might also like