Docsity

Quiz 2: Introduction to Object-Oriented Programming with C++ - Questions and Answers, Quizzes of Computer Science

Toggle sidebar
bg1
Name: Student Id:
Introduction to Software Development and Object-Oriented
Programming
ECS 40 Summer Session II 2003
Quiz 2 (10 points)
Time: 10 mins
August 14, 2003
Please read the code below answer the questions that follow.
The code compiles correctly. The questions are easy.
// -----File A.h begins here-----------
1 #ifndef A_H
2 #define A_H
3
4 #include <iostream>
5
6
7 class A {
8
9 public:
10 A (int x);
11 void print() const;
12 void increment(const int);
13
14 private:
15 int val;
16
17 };
18
19 #endif
20 // ------File A.cpp begins here
21 #include "A.h"
22
23 A :: A(int x)
24 {
25 val = x;
26
27 }
28
29 void A :: print (void ) const
30 {
31 cout << "Value of val is: " << val << endl;
32 }
33
34 void A :: increment(const int i)
35 {
36 val += i;
37
38 }
1
pf2

Partial preview of the text

Download Quiz 2: Introduction to Object-Oriented Programming with C++ - Questions and Answers and more Quizzes Computer Science in PDF only on Docsity!

Name: Student Id:

Introduction to Software Development and Object-Oriented

Programming

ECS 40 Summer Session II 2003

Quiz 2 (10 points)

Time: 10 mins

August 14, 2003

Please read the code below answer the questions that follow.

The code compiles correctly. The questions are easy.

// -----File A.h begins here----------- 1 #ifndef A_H 2 #define A_H 3 4 #include 5 6 7 class A { 8 9 public: 10 A (int x); 11 void print() const; 12 void increment(const int); 13 14 private: 15 int val; 16 17 }; 18 19 #endif 20 // ------File A.cpp begins here 21 #include "A.h" 22 23 A :: A(int x) 24 { 25 val = x; 26 27 } 28 29 void A :: print (void ) const 30 { 31 cout << "Value of val is: " << val << endl; 32 } 33 34 void A :: increment(const int i) 35 { 36 val += i; 37 38 }

39 //-----File main.cpp begins here 40 #include "A.h" 41 42 void test (A&); 43 44 int main() 45 { 46 A a(60); 47 48 A * aPtr = & a; 49 50 a.print(); 51 52 test(a); 53 54 aPtr->print(); 55 56 return 0; 57 } 58 59 void test( A & x) 60 { 61 62 x.increment(2); 63 }

  1. Typical name for lines 1, 2, and 19 is preprocessor directives.
  2. The complete name for the symbol :: on lines 23 and 34 is binary scope resolution operator.
  3. The function print() declared in line 11 has class scope but the function test declared in line 42 has file scope.
  4. The process of creating an object from class (e.g. line 46) is called instantiation.
  5. The function A (int) delcared at line 10 is a constructor for class A.
  6. We are passing reference to an object of class A to function test (line 59) when we make function call test(a) at line 52.
  7. The output of the code is:

Value of val is: 60 Value of val is: 62

  1. True or False Memory allocated for static variable is retained even after the variable goes out of its scope. True