1. Class-objects store only data; never functions.
T__ F__
2. An instance (non-static) class-member function is called to act
on a specific object.
T__ F__
3. 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 3 B. 1 1 C. 3 1 D. 1 3
4. You can’t declare as const all of class-data-members.
T__ F__
5. The following class implementation is syntactically correct:
class Demo
{
public:
Demo();
~Demo();
};
Demo::Demo()
{ cout < "An object has just been defined, so the constructor"
< " is running.\n";
}
Demo::~Demo()
{ cout < "Now the destructor is running.\n"; }
T__ F__
6. The following implementation is accepted by compiler:
class GradeBook
{
public:
void DisplayMessage(){
cout < "Welcome !" < endl;
}
};
int main()
{
GradeBook myGradeBook;
DisplayMessage();
}
T__ F__
7. The following implementation is syntactically correct:
class DumbBell;
{
int weight;
public:
void SetWeight(int);
};
void SetWeight(int w)
{weight={w};}
int main()
{
DumbBell bar;
bar.SetWeight(200);
}
T__ F__
8. The following class declaration is syntactically correct:
class Circle
{
double centerX;
double centerY;
double radius;
public:
SetCenter(double,double);
SetRadius(double);
};
T__ F__
9. The following class declaration is not syntactically correct:
class Circle
{ public:
void setRadius(double r)
{radius{r};}
private:
double radius;
public:
double calcArea()
{return 3.14 * pow
(radius, 2);}
};
T__ F__
10. A constructor has a header syntactically implemented like for any
other function in C++.
T__ F__
11. The following prototype is correctly declared in class Employee:
int Employee( const char *, const char * );
T__ F__
12. 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__
13. Just as a class can have multiple constructors, it can also have
multipledestructors.
T__ F__
14. What will the following code display on the screen ?
#include <iostream>
using namespace std;
class Tank
{
int gallons;
public:
Tank()
{gallons=50;}
Tank(int gal)
{gallons=gal;}
int GetGallons() {return gallons;}
};
int main()
{
Tank storage1, storage2, storage3{20};
cout < storage1.GetGallons() < " ";
cout < storage2.GetGallons() < " ";
cout < storage3.GetGallons();
}
A. Nothing B. 20 50 C. 50 50 20 D. 50 20 50
15. The following implementation is accepted by compiler:
class Building
{
public:
int x;
};
int main()
{
Building c;
cout < "x = " < c.x < endl;
}
T__ F__
16. Visual Studio accepts a direct initialization of data-members of
a class inside the class, like:
class A
{
public: // or private:
int x{7};
// class code
};
T__ F__
17. 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__
18. The following class implementation is accepted by compiler:
class Box
{
private:
double length, width, height;
public:
mutable int accessCounter;
Box(double l = {10}, double w = {20}, double h = {30}) :
length{l}, width{w}, height{h}
{ }
double Volume() const
{
accessCounter++;
return length * width * height;
}
};
int main()
{
Box b;
b.Volume();
}
T__ F__
19. 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__
20. 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__
- The following implementation is accepted by compiler:
class Automobile
{
public:
void SetPrice(double newPrice);
void SetProfit(double newProfit);
double GetPrice();
private:
double price;
double profit;
double GetProfit();
};
int main()
{
Automobile hyundai, jaguar;
jaguar.SetPrice(50000.99);
hyundai.price{ 4999.99 };
double aProfit{ jaguar.GetPrice()
};
}
T__ F__
22. The following implementation is not syntactically correct:
class Building
{
public:
int x;
};
int main()
{
Building c;
cout < "x = " < c.x < endl;
}
T__ F__
23. The following implementation is not accepted by compiler:
class myClass
{
private:
int x;
public:
int y;
myClass(){x = {}; y = {};}
};
int main()
{
myClass numberOne = myClass();
}
T__ F__
24. The following implementation is not accepted by compiler:
class Change {
private:
int pennies; int nickels; int dimes; int quarters;
Change()
{
pennies = nickels = dimes = quarters = 0;
}
Change(int, int, int, int);
};
void Change::Change(int p, int n, int d, int q)
{
pennies = p;nickels = n;dimes = d;quarters = q;
}
int main()
{
Change obj { 5, 4, 2, 1 };
}
T__ F__
25. The following class implementation is accepted by compiler:
class Box
{
private:
double length, width, height;
public
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__
26. The following code is syntactically correct:
class Box
{
private:
int length, width, height;
public:
Box()
{
length = { 10 }; width = { 20 }; height = { 30 };
}
Box(int l, int w, int h)
{
length = { l };width = { w }; height = { h };
}
};
int main()
{
Box box1{ 78, 24, 18 };
Box box2;
}
T__ F__
27. A ‘struct’ is never accepted as implemented like a ‘class’,
with ‘private’ members and ‘constructors’, like :
struct Box
{
private:
int length;
int width;
int height;
public:
Box(int l, int w, int h)
{
cout < "Constructor called.";
length = { l };
width = { w };
height = { h };
}
Box()
{
cout < endl < "Default constructor called.";
}
};
T__ F__
28. The following code-segment is syntactically incorrect:
struct TwoVals{int a,b;};
void main()
{
TwoVals.a =10;
TwoVals.b =20;
}
T__ F__
29. It is legal to declare a structure inside main(), like:
int main()
{
struct Access
{
int keyNumber;
char key;
};
// Code
}
T__ F__
30. The following code-segment is syntactically incorrect:
struct TwoVals
{
int a = 5;
int b =10;
};
int main()
{
TwoVals v;
cout <v.a <""v.b;
return 0;
}
T__ F__
31. 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__
32.struct AUTO
{
int year;
char make[20];
};
Which statement correctly declares and initializes the
structure variables ?
a. struct AUTO car = 1990, 'F', 'o', 'r', 'd';
b. struct AUTO { 1990, "Ford" } car;
c. struct AUTO car = 1990, strcpy( make, "Ford" );
d. struct AUTO car { 1990, "Ford" };
33. The following implementation is syntactically incorrect:
class A
{
public:
void foo() { }
};
int main(){
A *p, obj;
obj.foo();
p = {&obj};
p->foo(); }
T__ F__
34. The following array definition is valid:
int numbers [10] = {0,0,1,0,0,1,0,0,1,1};
T__ F__
35. The following array definition is invalid:
int matrix [5] = {1,2,3,4,5,6,7};
T__ F__
36. The following array definition is valid:
double radii [10] = {3.2,4.7};
T__ F__
37. The following array declaration is valid:
int blanks[];
T__ F__
38. The following array definition is invalid:
char codes[] = {'A','X','1','2','s'};
T__ F__
39. The following array definition is valid:
string suit [4] = {"Clubs","Diamonds","Hearts","Spades"};
T__ F__
40. 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__ F__
41. /* 1 */ const size_t SIZE = 10;
/* 2 */ int array[ SIZE ];
/* 3 */ int index;
/* 4 */ for (index=0;
/* 5 */ index<=SIZE;
/* 6 */ ++index)
/* 7 */ {
/* 8 */ array[index] = 0;
/* 9 */ }
Which line may cause an error?
a. /* 1 */ b. /* 2 */ c. /* 4 */ d. /* 5 */ e. /* 6 */
42. Suppose an array has been declared as int arr[3];
You cannot use the expression arr++;
T__ F__
43. The following code-segment is syntacticallyincorrect:
const size_t SIZE = 4;
int oldValues[SIZE] = { 10, 20, 30, 40 };
int newValues[SIZE];
newValues = oldValues;
T__ F__
44. char array[10]; Which of the following statements is valid?
a. *array[0] = 'A'; b. array = "ABCD";
c. *(array + 9) = '\0'; d. *array + 9 = 'Z';
45. 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
46. Given the declarations
int arr[30];
int *ptr;
the statement ptr=arr; is correct.
T__ F__
47. Given the declarations
int nums[10]{};
int *ptr = nums;
the statements *(ptr+3) and *(nums+3) are not equivalent.
T__ F__
48. char name[20] = "John Q. Doe";
char *pName = name;
char letter;
Which statement below assigns 'Q' to the variableletter?
a. letter = name[5]; b. letter = name[6];
c. letter = &pName[5]; d. letter = *pName[5];
e. None of the above
49. The following three arrays of characters are all declared as
legalC-type strings:
char name1[] = "Holly";
char name2[] = {'W', 'a', 'r', 'r', 'e', 'n', '\0'};
char name3[] = {'W', 'a', 'r', 'r', 'e', 'n'};
T__ F__
50. 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__
51. 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__
52. The following code-segment is logically incorrect:
char c[10] {"abcd"};
char *cPtr{ c };
cout < "Array address = " < c < endl;
T__ F__
53. 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__
54. The following function header
voidAverage(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__
55. The following C++ array
const int SIZE = 3;
array<int, SIZE> arr = {};
is initialized to 0.
T__ F__
56. The following declaration
array<int> a = { 1, 2, 3 };
is syntactically correct.
T__ F__
57. 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__
58. The following code-segment is syntactically correct
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__
59. The following ‘range for’ can’t be implemented, for the above
array, using ‘auto’:
for (auto i : myarray)
cout < i < "";
T__ F__
60.The following array-allocation is syntactically illegal:
size_t inputNums{20};
int* p = new int [inputNums];
T__ F__
61. To delete the dynamically allocated array, to which ‘p’
points, use the statement
delete [] p;
T__ F__
62. 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__
63. Which statement correctly declares and initializes a C-string?
a. string array[ 13 ] = Chapter nine;
b. char array[]{"Chapter nine"};
c. int array[ 8 ] = "Chapter nine";
d. char array = 'Chapter nine';
e. None of the above
64. The following code-segment displays 4:
char dog[]{"Fido"};
cout < strlen(dog) < endl;
T__ F__
65. The following code-segment is syntactically incorrect:
char string[]{"Stop"};
if (isupper(string) == "STOP")
cout < "true";
T__ F__
66. The following code-segment is syntactically correct:
int numeric;
char x[]{"123"};
numeric =itoa(x);
T__ F__
67. The following statement is syntactically correct:
const char * bird {"wren"};
T__ F__
68. The following code-segment is accepted by compiler:
char x{'a'}, y{'a'};
if (strcmp(x,y) == 0)
cout < "true";
T__ F__
69. 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
70. The following code-segment is legal for C-strings:
char str1[30]{"Many hands"};
char str2[]{" make light work"};
str1 += str2;
T__ F__
71. The following code-segment returns 0:
char str1[30]{"Many hands"};
char str2[]{" make light work"};
cout < strcat_s(str1, str2) < endl;
T__ F__
72. The following code-segment returns 0:
char str1[11];
char str2[]{"I love C++ programming"};
cout < strncpy_s(str1, str2, 15);
T__ F__
73. What is the displayed result from the statement
cout < strcmp( "sun" , "Sun" ) ""
< strcmp( "Sun" , "sun" ) ""
< strcmp( "Sun" , "Sun" )< endl;
a. 0 0 0 b. 1 -1 0
c. -1 1 0 d. None of the above
74. 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__
75.The following statement prints B:
cout < toupper('b');
T__ F__
76.The following code-segment is syntactically correct:
char input;
cout < "Enter a character: ";
cin.get(input);
if( isalpha(input) )
cout < "That is an alpha character.\n";
T__ F__
77. How many elements are there in the following array?
double sales[6][4]{};
a. 6 b. 4 c. 24
78. The following code-segment is syntactically correct:
int a[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
a[ 1, 1 ] = 5;
T__ F__
79. The following code-segment is syntactically incorrect:
char s[][20] { "Robert", "Lassie", "Oliver" };
cout < s[1];
T__ F__
80. 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__
81. Given the array declaration
char ary[][3] = {'a','b','c','d','e','f','g','h','i'};
the statement cout < ary[2][2];
would print the letter
a. e b. i c. f d. None of the above
- The following code-segment is syntactically incorrect:
char str[10];
str = {"Hello"};
T__ F__
83. The following code-segment is syntactically incorrect:
int main()
{
char str[5]{ "Hi" };
int index{};
while (str[index] != '\0')
{
str[index] = 'X';
index++;
}
cout < str < endl;
}
T__ F__
- The following code segment
char str[7]{ "DoBeDo" };
str[6] = { 'Z' };
cout < str < endl;
correctly displays DoBeDo
T__ F__
85.The following initialization is illegal:
string s("C#");
T__ F__
86. But, this one is legal:
string s1; s1{"bees"};
T__ F__
87. The following code-segment is syntactically correct:
char cs[]{"Hello C++ !!!"};
string s{cs};
cout < s < endl;
T__ F__
88. String class concatenation is legal, like:
string str("one"); str += " two";
T__ F__
89. The followingcode-segment is legal:
string str1{"abc"}, str2;
str2 = str1;
T__ F__
90. All following ‘cout’ statements display the same value:
string sentence("This");
cout < sentence.length();
cout < sentence.size();
cout < sentence.capacity();
T__ F__
91. After the statement
sentence.clear();
is added above the previous 'cout’ statements, functions length()
and capacity() will both return 0.
T__ F__
92. The vector template class is able to add or delete elements at
any index.
T__ F__
93. 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__
94.The following code-segment is syntactically illegal:
vector<int>vect;
for (int count = 0; count < 10; count++)
vect.push_back(count);
T__ F__
95.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__
96. Function shrink_to_fit() handles the request to reduce the
capacity() of a vector to its size().
T__ F__
97. The following is a legal template-syntax for inheritance:
class D : public B
{
// new class members
};
T__ F__
98. The creation of a derived class indirectly affects its base
class's source code.
T__ F__
99. It is appropriate to use the protected instead of private access
specifier when abase class shouldprovide a service only to its
derivedclasses.
T__ F__
100. Every object of a derived class can also act as an object of
that class's base class.
T__ F__