Exam 2 Practice(eCampus_part1)
For the following function, which is a valid function call? Assume maxValue is an integer.
int Max(int x, int y) {
if (x > y){
return x;
} else {
return y;
}
}
a. maxValue = Max(15 25);
b. maxValue = Max();
c. maxValue = Max(15, Max(35, 25));
d. Max = maxValue(15, 25);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
What code for XXX, YYY, ZZZ returns true if the strings has no periods? Choices are listed as XXX / YYY / ZZZ.
bool HasNoPeriods(string s) {
int i;
bool noPeriods XXX;
for (i = 0; i < s.length(); ++i) {
if (s.at(i) == '.') {
YYY;
} else {
ZZZ;
}
}
return noPeriods;
}
a. = false / noPeriods = false / (nothing)
b. = true / noPeriods = false / (nothing)
c. (nothing) / noPeriods = false / noPeriods = true
d. (nothing) / noPeriods = true / noPeriods = false
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
The new operator____.
a. Returns a pointer to the allocated memory
b. Returns an int
c. Allocates memory after calling the class’s constructor
d. Deallocates memory
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Which of these is a dynamically allocated array?
a. An array whose size can change during compile time
b. An array whose address can change during compile time
c. An array whose address can change during runtime
d. An array whose size can change during runtime
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Which of the following statements is true about inline member functions of a class?
a. An inline function is defined in a single line
b. An inline function is typically defined for lengthy functions
c. An inline function is typically short function definition with compact code
d. An inline function should be declared as void and public
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Consider that there is a class Team for soccer team members under Soccer namespace and another class Team for basket team members under the Basketball namespace. What is the correct syntax that will help the compiler identify the correct Team?
Soccer::Team SoccerTeamMembers;
BasketBall::Team BasketballTeamMembers;
Soccer:Team SoccerTeamMembers;
BasketBall:Team BasketballTeamMembers;
Soccer, Basketball::Team(SoccerTeamMembers, BasketballTeamMembers);
Team(SoccerTeamMembers, BasketballTeamMembers);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Declaring a member as ____ in the base class provides access to that member in the derived classes but not to anyone else
a. public
b. protected
c. protected
d. constant
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
The following code generates an error. Why?
class School {
public:
void PrintNames();
protected:
void SetNames();
string names;
private:
string address;
};
class NewSchool : School {
public:
string GetNames();
};
int main() {
NewSchool myObject;
String str;
myObject.SetNames();
str = myObject.GetNames();
return 0;
}
a. In class NewSchool: School, the specifier is not mentioned
b. The function myObject.SetNames() is not accessible by main().
c. The variable address is accessible by the class NewSchool
d. The variable name is accessible by main()
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
What is output?
#include <iostream>
using namespace std;
class Base {
int value;
public:
Base(int value):value(value) {
cout << "The number is " << value;
}
};
int main( ) {
Base il(20);
return 0;
}
a. The number is value
b. the number is 20
c. The number is 0
d. The number is
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Identify the type of function definition used in the code.
#include <iostream>
using namespace std;
class AgeCalculator {
int ageMonths;
public:
int CalcAge(int age) {
ageMonths = age * 12;
return ageMonths;
}
};
int main() {
AgeCalculator newAge;
newAge.CalcAge(5);
return 0;
}
a. Private function definition
b. Inline function definition
c. Overloaded function definition
d. Nested function definition
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Which of the following statements is true about destructors?
a. destructors are used to reallocate the memory that has been allocated for the object
b. destructors are used to deallocate the memory that has been allocated for the object
c. destructors have parameters and a return value
d. destructors are declared using the ‘&’ symbol
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Which XXX is valid for the following code?
int CalcSum(int a, int b) {
return a + b;
}
int main() {
int y;
XXX
return 0;
}
a. y = CalcSum();
b. y = CalcSum(4, 5);
c. y = CalcSum(4+5);
d. CalcSum(y, 4, 5);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Which statement properly frees the dynamically allocated array below?
Musketeer* musketeers = new Musketeer[8];
a. delete musketeers;
b. delete[] mustketeers;
c. musketeers->delete;
d. musketeers*musketeers = delete musketeers;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Identify the correct statement for defining the public function.
#include <iostream>
using namespace std;
class School {
public:
XXX {
numClassrooms = crNum;
numGrades = grNum;
numTeachers = tNum;
}
private:
int numClassrooms;
int numGrades;
int numTeachers;
};
int main() {
School newSchool;
int classroomNum;
int gradeNum;
int teacherNum;
cout << "Enter the number of classrooms in the new school:" << endl;
cin >> classroomNum;
cout << "Enter the number of grades in the new school:" << endl;
cin >> gradeNum;
cout << "Enter the number of teachers the new school:" << endl;
cin >> teacherNum;
newSchool.SetDetails(classroomNum, gradeNum, teacherNum);
return 0;
}
a. School.SetDetails(int crNum, int grNum, int tNum)
b. void SetDetails(int crNum, int grNum, int tNum)
c. char SetDetails(int crNum, int grNum, int tNum)
d. int SetDetails(int crNum, int grNum, int tNum)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Which XXX completes the following code, assuming a class called Movies has been defined with public member functions GetDetails() and SetDetails()?
int main() {
double movieRating;
string actors;
XXX
cout << “The movie’s rating and its actors are: “ << newMovie.GetDetails() << endl;
return 0;
}
a. Movies myMovie;
myMovie.SetDetails();
b. Movies myMovie;
myMovie.GetDetails();
c. Movies newMovie;
newMovie.GetDetails();
d. Movies newMovie;
newMovie.SetDetails();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Which members of the base class Players are inherited by the class SoccerPlayers?
class Players {
public:
void SetName(string newName) { … }
void SetAge(int newAge) { … }
void PrintDetails() { … }
string pName;
int pAge;
};
class SoccerPlayers: public Players {
public:
void SetDetails(string newName) { … }
string GetLeague() { … }
private:
string team;
};
a. SetDetails, GetLeague, and team
b. SetName, SetAge, PrintDetails, SetDetails, and GetLeague
c. SetDetails, GetLeague, pName, pAge, and team
d. SetName, SetAge, PrintDetails, pName, and pAge
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Which of the following statements is true for the following code?
class MyParent {
…
}
class MyChild : protected MyParent {
…
}
a. Public, private and protected members of MyParent are accessible as protected members of MyChild.
b. Public and protected members of MyParent are accessible as protected members of MyChild.
c. Public, private and protected members of MyParent are accessible as public members of MyChild.
d. Public and protected members of MyParent are accessible as public members of MyChild.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
In the given code, which statement can used instead of Statement 1?
class Sample {
public:
Sample(int xValue = 0, int yValue = 0);
void show_data();
};
int main() {
Sample* sample1 = new Sample;
(*sample1).show_data(); // Statement 1
}
a. sample1.show_data();
b. (sample1)->show_data();
c. (&sample)->show_data();
d. show_data.sample1();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
A linked list’s head node stores ______ in the list.
the count of items
the value of the first item
a pointer to the first item node
a pointer to all the nodes
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
The following program results in a compiler error. Why?
int FindMin(int a, int b) {
int min;
if (a < b) {
min = a;
}
else {
min = b;
}
return min;
}
int main() {
int x;
int y;
cin >> x;
cin >> y;
min = FindMin(x,y);
cout << "The smaller number is: " << min << endl;
return 0;
}
a. FIndMin() shoule be defined after main(), not before.
b. The variable min is declared in FindMin(), but is used in main()
c. The variable min is not initialized;
d. The variable min should be a parameter
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
What is the error in this code?
void MyFct(int& x) {
x = (x * x) + 1;
}
int main() {
cout << MyFct(9);
}
a. MyFct() cannot be called with a literal
b. MyFct() is missing a return statement
c. MyFct() cannot both read and assign x
d. No error exists; the program will output 82
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
The following code generates an error. Why?
class Players {
public:
void SetName(string newName) { … }
private:
void SetAge(int newAge) { … }
};
class SoccerPlayers: public Players {
public:
void SetDetails(string newName) { … }
string GetLeague() { … }
};
String leagueName;
SoccerPlayers playerObject;
playerObject.SetName("Tim Allen");
playerObject.SetAge(21);
leagueName = playerObject.GetLeague();
return 0;
}
leagueName = playerObject.GetLeague() will generate an error as the derived class should not call its own members
playerObject.SetName(“Tim Allen”) will generate an error as SetName is not declared in class SoccerPlayers
playerObject.SetAge(21) will generate an error as SetAge is private member which is not inherited by the SoccerPlayers class.
SoccerPlayers playerObject will generate an error as an object of derived class cannot be created
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
What is the output?
void WaterTemperatureForCoffee(int temp) {
if (temp < 195) {
cout << "Too cold.";
}
else if ((temp >= 195) && (temp <= 205)) {
cout << "Perfect temperature.";
}
else if (temp > 205) {
cout << "Too hot.";
}
}
int main() {
WaterTemperatureForCoffee(205);
WaterTemperatureForCoffee(190);
return 0;
}
a. Too cold.
b. Perfect temperature
c. Perfect temperature
Too cold.
d. Perfect temperature.Too cold.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Which is a valid definition for a function that passes two integers (a, b) as arguments, and returns an integer?
a. myFunction(int a, int b)
b. int myFunction(a, b)
c. void myFunction(a, b)
d. int myFunction(int a, int b)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Given the following class definition in the file ClassRoom.h, which XXX and YYY complete the corresponding .cpp file?
#ifndef CLASSROOM_H
#define CLASSROOM_H
class ClassRoom {
public:
void SetNumber(int n);
void Print() const;
private:
int num;
};
#endif
#include <iostream>
using namespace std;
XXX
void ClassRoom::SetNumber(int n) {
num = n;
}
YYY {
cout << "Number of Class Rooms: " << num << endl;
}
a. #include “ClassRoom.cpp”
void ClassRoom.Print() const
b. #include <ClassRoom.h>
void Print() const
c. #include <ClassRoom.cpp>
void CLassRoom::Print()
d. #include "ClassRoom.h"
void ClassRoom::Print() const
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Identify the statement that will throw an error in the following code?
class Student {
public:
void SetName(string studentName);
void Display();
private:
string name;
char grade;
int Grade();
};
void Student::Display() {
grade = Grade();
…
}
int Student::Grade(){
…
}
void SetName(string studentName) {
name = studentName;
}
int main() {
Student stu1;
stu1.Display();
…
stu1.Grade();
}
a. stu1.Grade();
b. stu1.Display();
c. int Student::Grades()
d.
void Student::Display(){
grade = Grade();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Which of the following is the LinkedList class destructor?
a. void ~LinkedList(delete);
b. ~LinkedList(int);
c. void ~LinkedList();
d. ~LinkedList();
Last updated
Was this helpful?