Exam 2 Practice(eCampus_part2)

The _____ operator is used to inherit one class from another

  1. .

  2. :

  3. ->

  4. ::

---------------------------------------------------------------------------------------------------------------------

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 specific 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()

--------------------------------------------------------------------------------------------------------------------- Which is true about arrays and functions?

  1. Arrays cannot be passed to functions

  2. Passing an array to a function creates a copy of that array within the function

  3. An array is automatically passed to a function as a pointer

  4. A programmer must first make a copy of an array to pass the array to a function.

--------------------------------------------------------------------------------------------------------------------- Which XXX will avoid a memory leak?

#include <iostream>
using namespace std;
 class MyClass {
 public:
 MyClass();
 int value;
 private:
 int * ptr;
 };
 MyClass::MyClass () {
 ptr = new int;
 *ptr = 0;
 delete ptr;
 }
 int main() {
 int value = 100;
 int *ptr1 = new int;
 //XXX
 ptr1 = &value;
  1. delete value;

  2. delete MyClass;

  3. delete ptr1;

  4. delete ptr;

--------------------------------------------------------------------------------------------------------------------- How does the given function improve the code versus if no function was present?

double FahrenheitToCelsius(double fahrenheit) {
 return (fahrenheit - 32.0) * 5.0 / 9.0;
}
int main() {
 double fahrenheit;
 cin >> fahrenheit;
 int c1;
 int c2;
 int c3;
 c1 = FahrenheitToCelsius(fahrenheit);
 c2 = FahrenheitToCelsius(32.0);
 c3 = FahrenheitToCelsius(72.0);
}
  1. The use of the function makes main() run faster

  2. The use of function decreases redundant code

  3. The use of the function reduces the number of variables in the code

  4. The function does not improve the code.

---------------------------------------------------------------------------------------------------------------------

Which is a valid definition for a function that passes two integers (a, b) as arguments, and returns an integer?

  1. myFunction(int a, int b)

  2. int myFunction(a, b)

  3. void myFunction(a, b)

  4. int myFunction(int a, int b)

---------------------------------------------------------------------------------------------------------------------

What is output?

#include <string>
#include <iostream>
using namespace std;
class Student {
 public:
 void SetName(string studentName);
 void SetScore(int studentScore);
 string GetGrade() const;
 private:
 string name;
 int score;
};
void Student::SetScore(int studentScore) {
score = studentScore;
}
string Student::GetGrade() const {
 string grade;
 if (score < 40) {
 grade = "C";
 }
 else if (score < 70) {
 grade = "B";
 }
 else {
 grade = "A";
 }
 return grade;
}
int main() {
 Student stu1;
 stu1.SetScore(80);
 cout << stu1.GetGrade();
 return 0;
}
  1. 80

  2. C

  3. B

  4. A

---------------------------------------------------------------------------------------------------------------------

What is output?

#include <iostream>
using namespace std;
class Line {
 public:
 Line () {
  cout << "No Such line"; //如果有,
 }
};
int main() {
 Line L1, *L2;
 return 0;
}

  1. Compiler Error

  2. L1 *L2

  3. No output

  4. No Such Line

---------------------------------------------------------------------------------------------------------------------

Which of the following statements is true about public member functions of a class?

  1. A class user can call the functions to operate on the object

  2. A class user needs to know how the class ‘functions are implemented

  3. A class user can declare a variable of the class type to create a new function

  4. A class user need not understand how each public member function behaves.

--------------------------------------------------------------------------------------------------------------------- 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
}

(sample1)->show_data();

--------------------------------------------------------------------------------------------------------------------- Consider a class Student . The following code is used to create objects and access its member functions. Which statement is true about the code?

int main() {
 Student newStud;
 Student stud;
 Student oldStud;
 stud.GetDetails();
 return 0;
}

There are three objects created of Student class and they each have their own memory locations. The GetDetails() function is invoked for only the stud object of the class Student

--------------------------------------------------------------------------------------------------------------------- Given a function with one vector parameter scores. How should the parameter be defined if scores may be very large, and the function will modify the parameter?

  1. Constant, and pass by reference

  2. Constant, and pass by value

  3. Not constant, and pass by reference

  4. Not Constant, and pass by value

--------------------------------------------------------------------------------------------------------------------- What is the output?

Void IsEven(int num) {
 int even;
 if (num % 2 == 0) {
 even = 1;
 }
 else {
 even = 0;
 }
}
int main() {
 IsEven(7);
 cout << even;
 return 0;
}
  1. 0

  2. 1

  3. No output. Call to isEven() falls due to no return value

  4. No output: A compiler error occurs due to unknown variable even

--------------------------------------------------------------------------------------------------------------------- The _______ member access operator is used to access a member using the this pointer.

->

---------------------------------------------------------------------------------------------------------------------

Which statement properly frees the dynamically allocated array below?

Musketeer* musketeers = new Musketeer[8];

delete [] musketeers;

---------------------------------------------------------------------------------------------------------------------

Choose the correct syntax for calling the constructor with arguments.

class Sample {
 public:
 Sample(double xValue = 0, double yValue = 0);
 void Print();
 double X;
 double Y;
};
void Sample::Print() {
 cout << "(" << X << ", ";
 cout << Y << ")" << endl;
}

Sample* point2 = new Sample(8, 9);

--------------------------------------------------------------------------------------------------------------------- Which XXX complete the program to calculate the volume of a cone?

XXX
int main () {
 double coneVol;
 coneVol = ConeVolume(2, 4);
 cout << coneVol;
 return 0;
}
 double ConeVolume (double r, double h) {
 return (0.33) * 3.14 * ComputeSquare(r) * h;
}
double ComputeSquare (double r) {
 return r * r;
}
  1. (nothing)

  2. double ConeVolume, ComputerSquare;

  3. double ConeVolume();

double ComputerSquare();

4. double ConeVolume (double r, double h); double ComputeSquare (double r);

---------------------------------------------------------------------------------------------------------------------

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;
};

SetName, SetAge, PrintDetails, pName, and pAge

---------------------------------------------------------------------------------------------------------------------

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();
}

stu1.Grade();

--------------------------------------------------------------------------------------------------------------------- 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;
}

The number is 20

---------------------------------------------------------------------------------------------------------------------

Which of the following code snippets result in a memory leak?

void StudentClass() {
 int * ptr = new int(3);
}

//answer
int main() {
 StudentClass();
 return 0;
}

--------------------------------------------------------------------------------------------------------------------- Given a function with one vector parameter ages. How should the parameter be defined if ages may be very large, and the function will not modify the parameter?

Constant, and pass by reference

---------------------------------------------------------------------------------------------------------------------

If the function's vector parameter is 0 3 9 7 7, what does the function return?

int MyFct(const vector<int>& v) {
 int i;
 int x;
 x = v.at(0);
 for (i = 0; i < v.size(); ++i) {
 if (v.at(i) > x) {
 x = v.at(i);
 }
 }
 return x;
}

9

---------------------------------------------------------------------------------------------------------------------

Three classes are defined as follows. Choose the correct syntax to include all three classes in the main file.

//File ClassA.h
#ifndef ClassA_H
#define ClassA_H
class ClassA {

};
#endif
//File ClassB.h
#ifndef ClassB_H
#define ClassB_H
#include “ClassA.h”
class ClassB {

};
#endif
//File ClassC.h
#ifndef ClassC_H
#define ClassC_H
#include “ClassB.h”
class ClassC {

};
#endif

Answer: #include “ClassC.h”

Last updated

Was this helpful?