Programing C++ Exam #3

¡Supera tus tareas y exámenes ahora con Quizwiz!

38) Identify the type of function definition used in the code. class AgeCalculator { public: int CalcAge(int age) { ageMonths = age * 12; return ageMonths; } private: int 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

b. Inline function definition

34) What has access to a class's private data members? a. Member functions and class users b. Member functions but not class users c. Class users but not member functions d. Neither class users nor member functions

b. Member functions but not class users

7) Which of the following is true for overloading a class constructor? a. The return types of the constructors should be different. b. The parameter types of the constructors should be different. c. The name of the constructor should be different. d. The access scope of the constructors should be different.

b. The parameter types of the constructors should be different.

9) Which line of code creates an Arcade object correctly? class Arcade { public: Arcade(); Arcade(string name, int r); private: string arcName; int rating;};Arcade:: Arcade() { arcName = "New"; rating = 1;}Arcade:: Arcade(string name, int r) { arcName = name; rating = r;} a. myArc("Games Ablaze", 5) b. Arcade("Games Ablaze", 5); c. Arcade myArc("Games Ablaze", 5); d. Arcade = myArc("Games Ablaze", 5);

c. Arcade myArc("Games Ablaze", 5);

11) What is the value of fordFusion's odometer at the end of main( )? class SimpleCar { public: SimpleCar(); SimpleCar(int miles); void Drive(int miles); private: int odometer;};SimpleCar::SimpleCar() { odometer = 0;}SimpleCar::SimpleCar(int miles) { odometer = miles;}void SimpleCar::Drive(int miles) { odometer = odometer + miles;} int main() { SimpleCar fordFusion; SimpleCar hondaAccord(30); fordFusion.Drive(100); fordFusion.Drive(20); return 0;} a. 20 b. 30 c. 100 d. 120

d. 120

8) Which constructor will be called by the statementHouse myHouse(97373);for the given code? class House { House(); // Constructor A House(int zip, string street); // Constructor B House(int zip, int houseNumber); // Constructor C House(int zip); // Constructor D}; a. Constructor A b. Constructor B c. Constructor C d. Constructor D

d. Constructor D

21) What is the output? int main() { for (int i = 0; i < 3; ++i) { cout << i; } cout << i; return 0;} a. 012 b. 0122 c. 0123 d. Error: The variable i is not declared in the right scope

d. Error: The variable i is not declared in the right scope

12) Convert the following constructor to a constructor initialization list. House::House() { houseNum = 1; price = 1.00;} a. House() : houseNum(1), price(1.00) { } b. House:: House().houseNum(1), price(1.00) { } c. House:: House() :: houseNum(1), price(1.00) { } d. House:: House() : houseNum(1), price(1.00) { }

d. House:: House() : houseNum(1), price(1.00) { }

32) Which XXX completes the following code, assuming a class called Movie 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. Movie myMovie;myMovie.GetDetails(); b. Movie myMovie;myMovie.SetDetails(movieRating, actors); c. Movie newMovie;newMovie.GetDetails(); d. Movie newMovie;newMovie.SetDetails(movieRating, actors);

d. Movie newMovie;newMovie.SetDetails(movieRating, actors);

19) 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;} a. 0 b. 1 c. No output: Call to IsEven() fails due to no return value d. No output: A compiler error occurs due to unknown variable even

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

48) The following program generates an error. Why? #include <iostream>#include <string>#include "Shopping.cpp>"using namespace std;int main() { int num1; int num2; cin >> num1; cin >> num2; if (num1 > num2) cout << num1 << " is greater" << endl; else cout << num2 << " is greater" << endl; return 0;} a. No strings are used in the program, so the include statement for string would generate an error. b. main() must define a Shopping object, since the Shopping class is included. c. The variables num1 and num2 should be declared in a class and accessed through functions. d. Only .h files can be included, hence including Shopping.cpp is invalid.

d. Only .h files can be included, hence including Shopping.cpp is invalid.

47) The following program generates an error. Why? class Currency { public: string toCurrency; private: int amount; Currency Convert(int amt, string toCurr);};Currency Currency::Convert(int amt, string toCurr) { Currency myMoney; if (toCurr == "Euro") { myMoney.amount = amt * .89; } else if (toCurr == "Yen") { myMoney.amount = amt * 108.67; } return myMoney;}int main() { int amountIn; string strCurr; Currency newMoney; cout << "Enter the amount to convert and currency:"; cin >> amountIn; cin >> strCurr; newMoney = newMoney.Convert(amountIn, strCurr); return 0;} a. The class and main should be separated into different files. b. The object newMoney should be created using a vector. c. Only the amount should be passed as an argument to Convert(). d. The function Convert should be made public to be called in main.

d. The function Convert should be made public to be called in main.

18) Which XXX completes 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; } a. (nothing) b. double ConeVolume, ComputeSquare; c. double ConeVolume(); double ComputeSquare(); d. double ConeVolume (double r, double h); double ComputeSquare (double r);

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

33) Mustang is an object of type Car. Which statement invokes drive()? a. Car.drive(150); b. drive(135); c. mustang->drive(115); d. mustang.drive(145);

d. mustang.drive(145);

41) Which XXX completes the code to generate "Enter the student grade:" as the output? #include <iostream>#include <string>#include <vector>using namespace std;class Student { public: void SetDetails(int StuGrade, string name) { studentGrade = StuGrade; studentName = name; } private: int studentGrade; string studentName;};int main() { vector<Student> studList; Student newStudent; int r; string nm; cout << "Enter the student grade: " << endl; cin >> r; XXX return 0;} a. newStudent.pushBack(studList); b. studList.pushBack(newStudent); c. newStudent.push_back(studList); d. studList.push_back(newStudent);

d. studList.push_back(newStudent);

39) Which XXX completes the definition of SetDetails()? class School { public: XXX(int crNum, int grNum, int tNum) { numClassrooms = crNum; numGrades = grNum; numTeachers = tNum; } private: int numClassrooms; int numGrades; int numTeachers;};int main() { School newSchool; int classroomNum; int gradeNum; int teacherNum; cin >> classroomNum; cin >> gradeNum; cin >> teacherNum; newSchool.SetDetails(classroomNum, gradeNum, teacherNum); return 0;} a. School.SetDetails b. int SetDetails c. char SetDetails d. void SetDetails

d. void SetDetails

36) Which XXX is used for the member function Display() definition in the following code? class Student { public: void SetName(string studentName); void SetGrade(int studentGrade); void Display(); private: string name; int grade;};XXX { ...} a. Display() b. void Display() c. void Display::Student() d. void Student::Display()

d. void Student::Display()

Object:

instance of a class

this pointer

is a special implicit parameter generated by the compiler for member functions in class/struct

Pointer

is a variable that stores the memory address of another variable

&

is an address-of (or reference) operator

Using this->

is necessary to distinguish a member variable from a parameter with the same name, but otherwise can be omitted

Dot operator .

is used to access its members for non-pointer variables whereas arrow operator -> is used for pointer variables

Member initialization list

provides a way to initialize member before the body of the constructor

struct:

the C++ command to create a structure

this pointer points to

the object for which the member function is called

Use this->

to explicitly specify that the following is the member

Constructor

- is a special member function that is used to initialize member variables - is called when an object of class or struct is created/instantiated - has the same name as the class name, does NOT have the return type (not even void) - is public for common case

Global variable:

A variable that is accessible anywhere in the program

Variable scope:

The area in a program where a variable is accessible

Private/public

allows the layer of protection

Member:

an element within the struct

Structure object:

an instance of the struct with a unique name

Static variable:

A variable that is initialized once and then retains its last value in between function calls

Local variable:

A variable that is only accessible within a particular function (including the main)

Pass by reference:

An lvalue shared with a function whose value can be modified by the function

constructor overloading

Defining multiple constructors with different data type and/or number of parameters

We call this location memory address

When a variable is declared, a memory is assigned to a specific location;

Filestream:

a buffer that holds data that either was read from a file or is intended to be written to a file - Output / Input File Stream

20) What is the output? #include <iostream>using namespace std;const double LB_PER_KG = 2.2;double KgsToLbs(double kilograms) { double pounds; pounds = kilograms * LB_PER_KG; return pounds;}int main() { double pounds; pounds = KgsToLbs(10); cout << pounds; return 0;} a. 22.0 b. No output: LB_PER_KG causes a compiler error due to being outside any function c. No output: Global variable declared as const causes compiler error d. No output: variable pounds declared into two functions causes compiler error

a. 22.0

25) Which of the following statements describes an abstraction? a. Hiding the implementation and showing the important features b. Hiding the implementation and the important features c. Showing the implementation and the important features d. Showing the implementation and hiding the important features

a. Hiding the implementation and showing the important features

31) What has access to a class's public data members? a. Member functions and class users b. Member functions but not class users c. Class users but not member functions d. Neither class users nor member functions

a. Member functions and class users

16) 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

a. MyFct() cannot be called with a literal

10) What is the final value of myCity's data members? Answer is in the format cityName, cityPopulation. #include <iostream>#include <string>using namespace std;class City { public: City(string name = "NoName", int population = 0); private: string cityName; int cityPopulation;};Country::Country(string name, int population) { cityName = name + " City"; cityPopulation = population;}int main() { City myCity; } a. NoName, 0 b. City, 0 c. Error: Constructor is not defined correctly d. Error: myCity is not assigned a value in main()

a. NoName, 0

13) Convert the following constructor to use a constructor initializer list. Restaurant::Restaurant() { pizza = 10; burger = 25; } a. Restaurant::Restaurant(): pizza(10), burger(25){} b. Restaurant:: Restaurant() :: pizza(10), burger(25) { } c. Restaurant() : pizza(10), burger(25) { } d. Restaurant::pizza(10), burger(25) { }

a. Restaurant::Restaurant(): pizza(10), burger(25){}

Class and struct

are the same except the default access in class is private and in struct it is public by default

37) Which XXX declares a member function called GetAmount() and returns the variable amount? class Bill { public: void SetProduct(string prodName); void SetAmount(int prodAmount); XXX private: string name; double amount;};int main() { Bill myBill; cout << myBill.GetAmount();} a. void GetAmount(); b. double GetAmount(); c. void Bill::GetAmount(); d. double Bill::GetAmount();

b. double GetAmount();

23) What is the output, if userVal is 5? int x;x = 100;if (userVal != 0) { int tmpVal; tmpVal = x / userVal; cout << tmpVal;} a. 0 b. 5 c. 20 d. Error: The compiler generates an error

c. 20

5) What is the output? #include <iostream> using namespace std; class Number { public: int x; }; int main() { Number a; int sum = 0; a.x = 10; Number b = a; sum = a.x + b.x; cout << sum; return 0;} a. 0 b. 10 c. 20 d. 30

c. 20

6) What is the return type for constructors? a. void b. int c. Constructors do not have a return type. d. The return type depends on the constructors' definition.

c. Constructors do not have a return type.

1) For the class Timer, which XXX initializes the variables to 0? class Timer { public: XXX void Print() { cout << minutes << ":" << seconds; }; private: int minutes; int seconds; }; a. Timer { minutes = 0; seconds = 0; } b. Timer::Timer() { minutes = 0; seconds = 0; } c. Timer() = 0; d. Timer() { minutes = 0; seconds = 0; }

d. Timer() { minutes = 0; seconds = 0; }

Pass by value:

A literal, constant, or lvalue shared with a function and whose calling value can not be changed

Data Structure:

a group of elements under one name

14) Which corrects the logic error in the following program? void FindPrevNext (int x, int prev, int next) { prev = x - 1; next = x + 1;}int main() { int x = 10; int y; int z; FindPrevNext (x, y, z); cout << "Previous = " << y << ", Next = " << z; return 0;} a. The variables prev and next should be passed by reference b. The variables y and z should be initialized to 0 c. The function FindPrevNext() should have a return statement for variables prev and next d. prev and next should be initialized in FindPrevNext()

a. The variables prev and next should be passed by reference

28) The _____ construct defines a new type to group data and functions to form an object. a. class b. struct c. array d. library

a. class

24) Consider the customer management system and the inventory management system of a coffee shop. Which items should be a part of the CustomerBillDetails object type? a. orderQuantity b. coffeeStock c. orderFrequency d. minimumOrder

a. orderQuantity

15) What is the output? void Swap(int& x, int y) { int tmp; tmp = x; x = y; y = tmp;} int main() { int p = 4, q = 3; Swap(p, q); cout << "p = " << p << ", q = " << q << endl;} a. p = 3, q = 3 b. p = 4, q = 3 c. p = 3, q = 4 d. Error: Argument names must match parameter names

a. p = 3, q = 3

3) Select the default constructor signature for a Student class. a. public Student( ) b. public void Student( ) c. private void Student( ) d. private Student( )

a. public Student( )

22) What is the output, if n is 3? for (int i = 1; i <= n; ++i) { int factorial = 1; factorial = factorial * i; cout << factorial << " ";} a. 1 1 1 b. 1 2 3 c. 1 2 6 d. Error: Cannot use the loop variable i inside the for loop

b. 1 2 3

29) How many objects of type Car are created? Car mustang;Car prius;int miles;Truck tundra; a. 1 b. 2 c. 3 d. 4

b. 2

17) What is the output? int MyFct(int x) { int y; x = x * 2; y = x + 1; return y;} int main() { int a; a = 5; cout << a << " " << MyFct(a);} a. 5 6 b. 5 11 c. 6 11 d. Error: Cannot assign parameter x

b. 5 11

46) Which of the following is true if a programmer has to create a class Clinic with the following data members and functions? class Clinic { int patientRecNum; string patientName; void SetDetails(int num, string name); string GetDetails();}; a. Data members should be public items and function declarations should be private items. b. Data members should be private items and function declarations should be public items. c. The data member patientName should be a private item and all other members should be public items. d. The function SetDetails should be a public item and all other members should be private items.

b. Data members should be private items and function declarations should be public items.

2) What is the output? #include <iostream>using namespace std;class Greet { public: Greet(); };Greet::Greet() { cout << "Hello"; } int main() { Greet greeting; return 0; } a. No Output b. Hello c. Hello Hello d. Error: Compilation error

b. Hello

43) The Hotel class is defined in two separate files: Hotel.h and Hotel.cpp. Which of the following statements is true? a. Hotel.cpp contains the class definition with data members and function declarations. b. Hotel.cpp contains member function definitions and includes Hotel.h. c. Hotel.h contains the class and member function definitions. d. Hotel.h contains the class definition with only data members.

b. Hotel.cpp contains member function definitions and includes Hotel.h.

30) 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 newStudent; Student currentStudent; Student oldStudent; currentStudent.GetDetails(); return 0;} a. Three Student objects are declared. GetDetails() is invoked for all objects of the class Student. b. Three Student objects are declared. GetDetails() is invoked for the currentStudent object. c. One Student object is declared. GetDetails() is invoked for the Student object. d. One Student object is declared. GetDetails() is invoked for the currentStudent object.

b. Three Student objects are declared. GetDetails() is invoked for the currentStudent object.

4) Which XXX is the proper default constructor? public class Employee { private double mySalary; XXX} a. public Employee(double salary) { mySalary = salary;} b. public Employee( ) { mySalary = 10000;} c. public Employee( ) { double mySalary = 10000;} d. private Employee( ) { mySalary = 10000;}

b. public Employee( ) { mySalary = 10000;}

26) A commuter swipes a public transport card at a kiosk and sees his account balance displayed on the screen. The commuter would be viewing an abstraction if _____. a. the machine is the only place on which the information is stored b. there is other information about his account that is not displayed onscreen c. all the available information is displayed onscreen d. the card is the only place on which the information is stored

b. there is other information about his account that is not displayed onscreen

40) Assuming a class Employee has been defined, which of the following statements is correct for declaring a vector? a. vector[Employee] employeeList; b. vector<Employee> employeeList; c. vector Employee employeeList; d. vector (Employee) employeeList;

b. vector<Employee> employeeList;

42) What is the output with the given input? 85 Lisa76 Tina56 Bell65 Ben-1 class Student { public: void SetScoreAndName(int studentScore, string studentName) { score = studentScore; name = studentName; }; int GetScore() const { return score; }; string GetName() const { return name; }; private: int score; string name; };int main() { vector<Student> studentList; Student currStudent; int currScore; string currName; unsigned int i; cout << "Type Score + Name." << endl << "To end: -1" << endl; cin >> currScore; while (currScore > 0) { cin >> currName; currStudent.SetScoreAndName(currScore, currName); studentList.push_back(currStudent); totalScore += currScore; cin >> currScore; } cout << "Students: "; for(i = 0; i < studentList.size(); i++){ cout << studentList.at(3 - i).GetName() << " "; } return 0;} a. Students: Lisa Bell Ben Tina b. Students: Lisa Tina Ben Bell c. Students: Ben Bell Tina Lisa d. Students: Lisa Tina Bell Ben

c. Students: Ben Bell Tina Lisa

27) A(n) _____ can be used to implement an abstract data type (ADT). a. object b. encapsulation c. class d. inheritance

c. class

35) Consider the class Student below. Which XXX is valid code? class Student { public: void SetName(string studentName); void SetGrade(int studentGrade); void Display(); private: string name; int grade;};int main() { Student student1; XXX ...} a. student1.name = "Harry"; b. myString = student1.name; c. student1.Display(); d. student1.SetGrade("Harry");

c. student1.Display();

Public members

can be accessed by anyone while private members can be accessed by other members only

member variable of type vector

can be constructed with non-zero elements only when (or after) the constructor of the class is called

Stream error function:

can be used to check a filestream for its current state

Dot operator

can be used with public members only and not with private members

44) Given the following class definition in the file ClassRoom.h, which XXX and YYY complete the corresponding .cpp file? #ifndef CLASSROOM_H#define CLASSROOM_Hclass ClassRoom { public: void SetNumber(int n); void Print() const; private: int num;};#endif #include <iostream>using namespace std;XXXvoid 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

d. #include "ClassRoom.h"void ClassRoom::Print() const


Conjuntos de estudio relacionados

6.1-7.5 Geometry PreAP (CUMULATIVE), PreAP Geometry CUMULATIVE (8 & 10), PreAP Geometry CUMULATIVE (11 & 12)

View Set

Theories (post midterm material)

View Set

GEOSC 10 RockOns/Practice Quizzes

View Set

Marketing (mix, positioning, pricing, segmentation)

View Set

Palabras con sufijos (-dad, -ísimo, -mente, -ción, -xión, -sión)

View Set

Basic Appraisal Procedures-Chapter 7-8 -Sales Comparison Approach

View Set

Stock Based Compensation (Chapter 16)

View Set