You are on page 1of 24

C++ Final Exam

==============
Return your answers on a SCAN-TRON FORM Tuesday May 24
Use #2 pencil only
1. Class-objects store only data; never functions.
T__

F__

2. private keyword must be always used inside a class to declare the


private members.
T__

F__

3. An instance (non-static) member function is called to act on a


specific object.
T__

F__

4. If three objects of a class are defined, how many copies of that


instance-data are stored in memory ?
And how many copies of its member functions ?
A. 3

B. 1

C. 1

D. 3

5. You can declare as const all of class-data-members.


T__

F__

6. The following class declaration is syntactically correct:


class Circle:
{
private
double centerX;
double centerY;
double radius;
public
SetCenter(double,double);
SetRadius(double); }
T__
F__
7. The following implementation is accepted by compiler:

class GradeBook
{
public:
void DisplayMessage(){
cout << "Welcome !" << endl;
}
}
int main()
{
GradeBook myGradeBook;
DisplayMessage();
}
T__
8.

A constructor has a header syntactically implemented like for


any other function in C++.
T__

9.

F__

F__

The following implementation is accepted by compiler:


class Building
{
public:
int x;
};
int main()
{
Building c;
cout << "x = " << c.x << endl;
}
T__

F__

10. Visual Studio 2015 accepts a direct initialization of data-

members of a class, inside the class, like:


class A
{
public:
int x{7};

// or private:

// class code
};
T__

F__

11. The parameter-instance-constructor may be also implemented


by using a member-initialize-list, with a syntax like:
GradeBook(string name) : courseName(name)
{}
T__

F__

12. The following class implementation is accepted by compiler:


class GradeBook
{
public:
GradeBook( int, string );
void SetCourseName( string );
string GetCourseName();
~GradeBook(int, string);
private:
string courseName;
};
T__

F__

13. The following class implementation is accepted by compiler:

class Box
{
private:
double length;
double width;
double height;
public:
mutable int accessCounter;
Box(double l = {10}, double w = {20}, double h = {30}) :
length{l}, width{w}, height{h}
{ }

};
T__

double Volume() const


{
accessCounter++;
return length * width * height;
}
F__

14. The following prototype is correctly declared in class Employee:


int Employee( const char *, const char * );
T__

F__

15. The following function call agrees with its prototype:


swap(&x, &y);

// x and y are of int type

void swap(int * p, int * q);


T__

F__

16. The following class implementation is accepted by compiler:

class Box
{
private:
double length, width, height;
Box(double l, double w, double h)
{
length = { l };
width = { w };
height = { h };
}
public:
double Volume() { return length * width * height; }
double BoxSurface()
{
return 2.0 * (length * width + length * height +
height * width);
}
};
int main()
{
Box b { 20, 10, 5};
cout << b.BoxSurface() << endl;
}
T__
F__
17. The following code-segment is syntactically correct:
struct TwoVals{int a, b;};
void main()
{
TwoVals.a = 10;
TwoVals.b = 20;
}
T__
F__
18. The following class definition is accepted by compiler:

class Example
{
public:
Example( int y )
{
data = y;
}
int getIncrementedData() const
{
return data++;
}

};

static int getCount()


{
cout << "Data is " << data << endl;
return count;
}
private:
int data;
static int count;

T__

F__

19. It is legal to declare a structure inside main(), like:


int main()
{
struct Access
{
int keyNumber;
char key;
};
// Code
}
T__
F__
20. The following code-segment is syntactically correct:

struct TwoVals
{
int a = 5;
int b = 10;
};
int main()
{
TwoVals v;
cout << v.a << "" << v.b;
return 0;
}
T__

F__

21. The following code-segment is syntactically correct:


struct Names{string first, last;};
int main()
{
Names customer ("Orley", "Smith");
cout << Names.first << endl;
cout << Names.last << endl;
}
T__

F__

22. The following code-segment is syntactically correct:


int* p{nullptr};
int i{10};
p={&i};
double f;
p = {&f};
T__

F__

23. struct AUTO


{

int year;
char make[20];
};
Which statement correctly declares and initializes the
structure variables ?
a.
b.
c.
d.

struct
struct
struct
struct

AUTO
AUTO
AUTO
AUTO

car = 1990, 'F', 'o', 'r', 'd';


{ 1990, "Ford" } car;
car = 1990, strcpy( make, "Ford" );
car { 1990, "Ford" };

24. In the expression "float *fptr", what has type float ?


a. The address of fptr
c. The variable fptr

b. The variable pointed to by fptr


d. None of the above

25. The following code-segment is syntactically correct:


int i; double f;
void *ptr{&i};
// some code
ptr = {&f};
T__

F__

26. The following code-segment is syntactically correct:


int v{22};
int* const ptr = {&v};
*ptr = {13};
int val{33};
ptr = {&val};
T__

F__

27. The following code-segment is syntactically correct:

int i{24};
const int *p = {&i};
*p = {30};
T__

F__

28. The following implementation is syntactically correct:


class A
{
public:
void foo() { }
};
int main(){
A *p, obj;
obj.foo();
p = {&obj};
p->foo(); }
T__
F__
29. The following array definition is valid:
int numbers [10] = {0,0,1,0,0,1,0,0,1,1};
T__

F__

30. The following array definition is valid:


int matrix [5] = {1,2,3,4,5,6,7};
T__

F__

31. The following array definition is valid:


double radii [10] = {3.2, 4.7};

T__
F__
32. The following array declaration is valid:
int blanks[];

T__

F__

33. The following array definition is valid:


char codes[] = {'A','X','1','2','s'};

T__
F__
34. The following array definition is valid:
string suit [4] = {"Clubs","Diamonds","Hearts","Spades"};
T__

F__

35. Reading an element outside the array bounds is a logic error,


not a syntax error:
char codes [] = {'A','X','1','2','s'};
cout << codes[8];
T__
36. /*
/*
/*
/*
/*
/*
/*
/*
/*

F__
1
2
3
4
5
6
7
8
9

*/
*/
*/
*/
*/
*/
*/
*/
*/

const size_t SIZE = 10;


int array[ SIZE ];
int index;
for (index=0;
index<=SIZE;
++index)
{
array[index] = 0;
}

Which line may cause an error?


a. /* 1 */

b. /* 2 */

c. /* 4 */

37. Suppose an array has been declared as


You cannot use the expression arr++;

T__
F__

d. /* 5 */
int arr[3];

e. /* 6 */

38. The following code-segment is syntactically correct:


const size_t SIZE = 4;
int oldValues[SIZE] = { 10, 20, 30, 40 };
int newValues[SIZE];
newValues = oldValues;
T__

F__

39. char array[10];

Which of the following statements is valid?

a. *array[0] = 'A';
c. *(array + 9) = '\0';

b. array = "ABCD";
d. *array + 9 = 'Z';

40. Assuming that arry[5] is an array of type double, which of the


following refers to the value of the third element in the array?
a. arry+3;

b. *(arry+2);

c. *(arry+3);

d. arry+2;

41. Given the declarations


int arr[30];
int *ptr;
the statement
T__

ptr=arr;

is correct.

F__

42. Given the declarations


int nums[10]{};
int *ptr = nums;
the statements

*(ptr+3)

and

*(nums+3)

are equivalent.

T__
F__
43. char name[20] = "John Q. Doe";
char *pName = name;
char letter;
Which statement below assigns 'Q' to the variable letter?

a. letter = name[5];
c. letter = &pName[5];
e. None of the above

b. letter = name[6];
d. letter = *pName[5];

44. The following three arrays of characters are all declared as


legal C-type strings:
char name1[] = "Holly";
char name2[] = {'W', 'a', 'r', 'r', 'e', 'n', '\0'};
char name3[] = {'W', 'a', 'r', 'r', 'e', 'n'};
T__

F__

45. The following code-segment is accepted with no error:


int b[5];
int *bPtr = b;
cout << b << " " << bPtr << " "
<< bPtr + 1 << " " << &bPtr[1];
bPtr++;
cout << *(b++) << endl;
T__

F__

46. The following code-segment is accepted with no error:


int set[] {5, 10, 15};
int *nums{ &set[3] };
while (nums > set)
{
nums--;
cout << *nums << " ";
}
T__
F__
47. The following code-segment is logically correct:
char c[10] {"abcd"};
char *cPtr{ c };
cout << "Array address = " << c << endl;

T__

F__

48. The following code-segment is accepted with no syntax-error:


int b[] {5, 10, 15};
for(auto i = begin(b); i != end(b); ++i)
cout << " " << *i;
T__

F__

49. The following code-segment is accepted with no error:


const size_t SIZE{3};
array<int, SIZE>arr = {11, 22, 33};
cout << *(arr.begin() + 1) << " "
<< *(arr.end() - 1) << endl;
T__

F__

50. The following function header


void Average(double* arr, int count)
does not agree with the function call that passes a C-array
called values, of type double:
Average(values, sizeof(values));
T__

F__

51. The following C++ array


const int SIZE = 3;
array<int, SIZE> arr = {};

is initialized to 0.
T__

F__

52. The following declaration


array<int> a = { 1, 2, 3 };
is syntactically incorrect.
T__

F__

53. C++ class-arrays cannot be compared using the relational and


equality operators.
T__

F__

54. The following code-segment is syntactically correct:


const int SIZE = 3;
array<int, SIZE> arr1 = {9, 8, 7};
array<int, SIZE> arr2 ;
arr2 = arr1;
T__

F__

55. The following code-segment is syntactically correct


(#include <array> to be included):
array<int, 5> myarray = { 2, 16, 77, 34, 50 };
cout << "myarray contains:";
for ( auto i = begin( myarray ); i != end( myarray ); ++i )
cout << " " << *i;
T__

F__

56. The following range for cant be implemented, for the above
array, using auto:
for (auto i : myarray)
cout << i << " ";

T__

F__

57. The following array-allocation is syntactically legal:


size_t inputNums{20};
int* p = new int [inputNums];
T__

F__

58. To delete the dynamically allocated array, to which p


points, use the statement
delete [] p;
T__

F__

59. The bubble sort algorithm, like the selection sort, performs only
one exchange per-pass.
T__

F__

60. The binary search is more efficient than the linear search
because the values in the array are not required to be in order,
sorted.
T__

F__

61. Which statement correctly declares and initializes a C-string?


a.
b.
c.
d.
e.

string array[ 13 ] = Chapter nine;


char array[]{"Chapter nine"};
int array[ 8 ] = "Chapter nine";
char array = 'Chapter nine';
None of the above

62. The following code-segment displays 4:


char dog[]{"Fido"};
cout << strlen(dog) << endl;

T__

F__

63. The following code-segment is syntactically correct:


char string[]{"Stop"};
if (isupper(string) == "STOP")
cout << "true";
T__

F__

64. The following code-segment is syntactically correct:


int numeric;
char x[]{"123"};
numeric = itoa(x);
T__

F__

65. The following statement is syntactically correct:


const char * bird {"wren"};
T__

F__

66. The following code-segment is not accepted by compiler:


char x{'a'}, y{'a'};
if (strcmp(x,y) == 0)
cout << "true";
T__

F__

67. The number of characters seen in the following array:


char sample[]{"This is a test."};
a. Can be determined with strlen(sample)

b. Can be determined with sizeof(sample)


c. All of the above
d. None of the above
68. The following code-segment is legal for C-strings:
char str1[30]{"Many hands"};
char str2[]{" make light work"};
str1 += str2;
T__

F__

69. The following code-segment returns 0:


char str1[30]{"Many hands"};
char str2[]{" make light work"};
cout << strcat_s(str1, str2) << endl;
T__

F__

70. The following code-segment returns 0:


char str1[11];
char str2[]{"I love C++ programming"};
cout << strncpy_s(str1, str2, 15);
T__

F__

71. What is the displayed


cout << strcmp( "sun"
<< strcmp( "Sun"
<< strcmp( "Sun"

result from the statement


, "Sun" ) << " "
, "sun" ) << " "
, "Sun" ) << endl;

a.
0
0
0
b. 1 -1
0
c. -1
1
0
d. None of the above
72. The following code-segment returns a run-time exception:
const char *s1{"Happy New Year"};
const char *s2{"Happy new year"};
cout << _strcmpi(s1, s2);

T__

F__

73. The following statement prints B:


cout << toupper('b');
T__

F__

74. The following code-segment is syntactically incorrect:


char input;
cout << "Enter a character: ";
cin.get(input);
if( isalpha(input) )
cout << "That is an alpha character.\n";
T__

F__

75. How many elements are there in the following array?


double sales[6][4];
a. 6

b. 4

c. 24

76. The following code-segment is syntactically correct:


int a[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
a[ 1, 1 ] = 5;
T__

F__

77. The following code-segment is syntactically correct:


char s[][20] { "Robert", "Lassie", "Oliver" };
cout << s[1];
T__
F__
78. The following code-segment is syntactically incorrect:
const size_t ROWS {2};
const size_t COLS {3};
int table[][COLS] {{1, 2, 3}, {5, 6, 7}};

void ShowArray(int array[][COLS], int rows){};


int main() { ShowArray(table, ROWS);}
T__

F__

79. Given the array declaration


char ary[][3] = {'a','b','c','d','e','f','g','h','i'};
the statement
a. e

cout << ary[2][2];

b. i

c. f

would print the letter

d. None of the above

80. The following initialization is legal:


string s("C#");
T__

F__

81. But, this one is illegal:


string s1;
T__

s1{"bees"};

F__

82. The following code-segment is syntactically correct:


char cs[]{"Hello C++ !!! "};
string s{cs};
cout << s << endl;
T__

F__

83. String class concatenation is legal, like:


string str("one"); str += " two";
T__
F__
84. The following code-segment is illegal:
string str1{"abc"}, str2;
str2 = str1;

T__

F__

85. All following cout statements display the same value:


string sentence("This");
cout << sentence.length();
cout << sentence.size();
cout << sentence.capacity();
T__

F__

86. After the statement


sentence.clear();
following the above statements, functions length() and
capacity() will both return 0.
T__

F__

87. The vector template class is able to add or delete elements at


any index.
T__

F__

88. The following code-segment is syntactically legal:


vector < int > v(5, -1);
for (auto i = cbegin(v); i != cend(v); ++i)
cout << *i << " ";
T__

F__

89. The following code-segment is syntactically legal:


vector<int>vect;
for (int count = 0; count < 10; count++)
vect.push_back(count);

T__

F__

90. The following code-segment displays false:


vector < int > v(5, -1);
size_t s = v.size();
for (size_t count = 0; count < s; count++)
v.pop_back();
cout << v.empty() << endl;
T__

F__

91. Function shrink_to_fit() handles the request to reduce the


capacity() of a vector to its size().
T__

F__

92.Tocreateanoutputstream,youmustdeclareastreamobjectto
beofclassifstream.
T__

F__

93. The following code-segment is syntactically legal:


#include <iostream> #include <fstream> using namespace std;
int main()
{
ofstream outputFile("c:\\demofile.txt");

if(outputFile.is_open())
outputFile << "Bach \n" << "Beethoven \n" <<"Mozart \n";
outputFile.close();

T__
F__
94. The following statement is syntactically illegal because of
using \\ instead of \ for file-folder specifier:
ifstreaminputFile("c:\\demofile.txt");

T__

F__

95. The following code-segment is syntactically incorrect:


#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string name;
ifstream inputFile("c:\\demofile.txt");
if(!inputFile)
{
cout << "File is not open \n";
exit(EXIT_FAILURE);
}

}
T__

while(inputFile >> name)


cout << name << endl;
F__

96. The following is a legal template-syntax for inheritance:


class D : public B
{
// new class members
};
T__
F__
97. The creation of a derived class indirectly affects its base
class's source code.
T__

F__

98. It is appropriate to use the protected instead of private access


specifier when a base class should provide a service only to its
derived classes.
T__

F__

99. The following code-segment is syntactically incorrect:


#include <iostream>
using namespace std;
class B
{
protected:
int i, j;
public:
void Set(int a, int b) { i={a}; j={b}; }
void Show() { cout << i << " " << j << "\n"; }
};
class D : public B
{
private:
int k;
public:
D(int x) { k={x}; }
void Showk() { cout << k << "\n"; }
void Setk() { k={i*j}; }
};
int main()
{
D ob{3};
ob.Set(1, 2); ob.Show(); ob.Setk(); ob.Showk();
}
T__
F__
100. Every object of a derived class can also act as an object of
that class's base class.
T__

F__

You might also like