1/233
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No study sessions yet.
If p1 is an integer pointer variable with the value of 1000, p1++ changes P1 to point to the memory location 1001.
FALSE
When you return a dynamic array to the freestore, you must include the number of elements in the array.
False
You can assign an array to a pointer variable.
TRUE
The size of dynamic arrays must be declared at compile time.
FALSE
int *p1;
declares a static variable.
FALSE
Dynamically created variables have no name.
TRUE
If p1 and p2 are both pointers that point to integers in memory, the condition p1==p2 will be true if the values that are in those memory locations are the same.
FALSE
Even though pointers point to addresses which are integers, you can not assign an integer to a pointer variable.
TRUE
In the following statement, all the variables are pointers.
int* p1, p2;
FALSE
A pointer can be stored in an integer variable.
FALSE
A ___________ is the memory address of a variable.
pointer
Declare a pointer variable named ptr to an integer.
int *ptr;
In the statement cout << p1;, the is called the ________________
dereferencing operator
The & operator is called the _______________
address of operator
Write the code that assigns to p1 (an integer pointer variable) the pointer to a dynamically created integer.
p1 = new int;
Dynamic variables are created at __________
run time
Dynamic variables are created from the ___________.
freestore or heap
Write the code to return the dynamic memory pointed to by p1 to the freestore.
delete p1;
The size of a dynamic array is defined at ___________
run-time
Write the code to declare a dynamic array of strings (use the string pointer variable p1) that has as many elements as the variable arraySize.
p1 = new string[arraySize];
Which of the following correctly declares 3 integer pointers?
a. int* p1, p2, p3;
b. int *p1, p2, p3;
c. int p1, p2, *p3;
d. all of the above.
c. int p1, p2, *p3;
Which of the following assigns to p1 the pointer to the address of value?
a. *p1=&value;
b. p1=value;
c. p1=&value;
d. &p1 = *value;
c. p1=&value;
What is the output of the following code fragment?
int v1=2, v2=-1, p1, p2;
p1 = & v1;
p2= & v2;
p2=p1;
cout << *p2 << endl;
a. 2
b. -1
c. -2
d. 1
a. 2
Which of the following statements correctly prints out the value that is in the memory address that the pointer p1 is pointing to?
a. cout << &p1;
b. cout << p1;
c. cout << int* p1;
d. cout << *p1;
d. cout << *p1;
Given that p1 is a pointer variable of the string class, which of the following are legal statements?
a. p1 = new int;
b. cout << *p1;
c. p1 = new char[10];
d. *p1 = new string;
e. B and D
b. cout << *p1;
What is the output of the following code fragment?
float *p1;
p1 = new float(3);
cout << *p1;
a. 3.0
b. unknown, the address p1 points to is not initialized
c. unknown, the code is illegal, p1 points to a dynamic array
d. 0.0
a. 3.0
What is the output of the following code?
int p1, p2;
p1 = new int;
p2 = new int;
*p1=11;
*p2=0;
p2=p1;
cout << p1 <<" " << p2 << endl;
a. 11 0
b. 0 11
c. 11 11
d. 0 0
c. 11 11
What is wrong with the following code fragment?
int p1, p2;
p1 = new int;
p2 = new int;
*p1=11;
*p2=0;
p2=p1;
cout << p1 <<" " << p2 << endl;
delete p1;
delete p2;
a. nothing
b. p1 and p2 both have the same value, so the delete p2 will cause an error
c. You have a memory leak.
d. B and C
d. B and C
Which of the following correctly declares a user-defined data type that defines a pointer to a float?
a. float* floatPtr ;
b. typedef float* floatPtr;
c. typedef floatPtr *float;
d. typedef floatPtr* float
b. typedef float* floatPtr;
Given that a typedef for IntPtr defines a pointer to an integer, what would be the correct declaration for a function that expects a reference to an integer pointer?
a. void f1 (IntPtr& ptr);
b. void f1 (IntPtr&* ptr);
c. void f1 (IntPtr*& ptr);
d. All of the above
a. void f1 (IntPtr& ptr);
Which of the following correctly declares a dynamic array of strings?
a. p1 = new string(13);
b. p1 = new string[];
c. p1 = new string[13];
d. p1 = new stringArray(13);
c. p1 = new string[13];
Given that p1 is an integer pointer variable, and a1 is an integer array, which of the following statements are not legal code?
a. p1= a1;
b. cout << p1[0];
c. cin >> p1[0];
d. a1 = p1;
d. a1 = p1;
Assuming that the pointer variable p1 is of the correct type and size is an integer with some value > 1, which of the following declarations are legal?
a. p1 = new string[size];
b. p1 = new ifstream[size];
c. p1 = new char[size*size];
d. A and B
e. A, B and C
e. A, B and C
Which of the following statements correctly returns the memory from the dynamic array pointer to by p1 to the freestore?
a. delete [] p1;
b. delete p1[];
c. delete *p1;
d. delete p1;
a. delete [] p1;
If p1 is an integer pointer that is pointing to memory location 1001, and an integer takes 4 bytes, then (p1+1) evaluates to:
a. 1002
b. 1004
c. 1005
d. Unknown
c. 1005
Given that p1 is a pointer, p1++
a. always causes a run time error
b. advances p1 by one unit of the type of variable to which p1 points
c. adds 1 to whatever p1 is pointing to
d. adds one element to the array that p1 is pointing to
b. advances p1 by one unit of the type of variable to which p1 points
If a program requires a dynamically allocate two-dimensional array, you would allocate the memory by using
a. p1 = new int*[numRows];
for(int i=0; i < numRows; i++)
p1[i] = new int[numColumns];
b. p1 = new int*[numRows][numColumns];
c. p1 = new[numRows][numColumns]int;
d. none of the above
a. p1 = new int*[numRows];
for(int i=0; i < numRows; i++)
p1[i] = new int[numColumns];
If two pointer variables point to the same memory location, what happens when one of the pointers is freed?
a. The other pointer should be considered to be un-initialized
b. The other pointer still points to a valid memory address
c. If you attempt to free the other pointer a run-time error will occur.
d. All of the above
e. A and C
e. A and C
Which of the following is not true?
a. a pointer can be assigned the address of an array
b. an array can be assigned the value in a pointer variable
c. if a pointer points to an array, it can be used in place of the array name
d. if a pointer points to an array, the pointer does not know how many elements are in the array
b. an array can be assigned the value in a pointer variable
In which case would you consider using a dynamic array?
a. If the array is small, and the size is known before the program runs.
b. If the program needs to get the size of the array from the user
c. If the array size is big, but known at compile time
d. You should always use a dynamic array
b. If the program needs to get the size of the array from the user
This code returns a reference to an object. It should compile, but something is wrong. What is wrong?
Foo& someFunction(int i)
{
Foo f;
f.doSomethingIncredible(i);
return f;
}
a. It returns a reference to a local variable that is destroyed (goes out of scope)when the function exits
b. Variable i should be passed by pointer
c. You cannot return a variable by reference
a. It returns a reference to a local variable that is destroyed (goes out of scope)when the function exits
A struct variable is declared differently from a predefined type such as an int.
FALSE
Two different structure definitions may have the same member names.
TRUE
A structure can only be passed to a function as a call-by-value parameter
FALSE
A function may return a structure.
TRUE
Different class may not have member functions with the same name.
FALSE
A class member function may be private.
TRUE
Class data members are almost always public.
FALSE
It is possible to have multiple private labels in a class definition.
TRUE
The assignment operator may not be used with objects of a class.
FALSE
All constructors for a class must be private.
FALSE
A derived class is more specific than its parent, or base class.
TRUE
The keyword ________ defines a structure type definition.
struct
A structure definition ends with the closing brace and a _________.
semicolon
A structure variable is a collection of smaller values called ____________ values
member
When a structure contains another structure variable as one of its members, it isknown as a ___________________.
hierarchical structure
When several items (variables or variables and functions) are grouped togetherinto a single package, that is known as ______________.
(data) encapsulation
The double colon (::) is known as the __________ operator.
scope resolution operator
Who can access private members in a class?
only other members of the class
A member function that allows the user of the class to find out the value of aprivate data type is called a ___________________.
accessor function
A member function that allows the user of the class to change the value of aprivate data type is called a ___________________
mutator function
If you have a class with a member function called display(ostream& out), that will send the values in the class to the parameter stream, and you need to call that function from within another member function, how would you call it to print the data to the screen? ___________________________
display(cout);
What can a constructor return? _______________
nothing
The name of a constructor is _____________
the name of the class
The constructor of a class that does not have any parameters is called a__________ constructor
default
In the following class constructor definition, the part of the header starting with asingle colon is called the ________________.
BankAccount::BankAccount(): balance(0), interest(0.0)
initialization section
A class in which modifications to the implementation appear to be invisible to theuser of the class is known as _________________
an Abstract Data Type (ADT)
A member function that gets called automatically when an object of the class isdeclared is called a _______________
constructor
If class A is derived from class B, then B is a _______ of A
parent
C++11 allows you to directly set the member variables to initial values in the definition of the class. This feature is called __________________
member initializers
C++11 allows you to have a constructor call another constructor. This feature iscalled _________________________
constructor delegation
In a structure definition, the identifiers declared in the braces are called
a. classes
b. structs
c. member names
d. variables
c. member names
You specify an individual member of a struct by using
a. the assignment operator
b. an ampersand
c. an underscore
d. The dot operator
d. The dot operator
To assign values to a structure variable, you use the
a. equals operator
b. assignment operator
c. extraction operator
d. less than operator
b. assignment operator
What is wrong with the following structure definition?
struct MyStruct{int size;
float weight;
}
a. Nothing
b. Can not have mixed data types in a structure
c. missing semicolon
d. Braces are not needed
c. missing semicolon
Given the following structure definitions, what is the correct way to print the person's birth year?
struct DateType
{
int day;
int month;
int year;
}
struct PersonType
{
int age;
float weight;
DateType birthday;
}
PersonType person;
a. cout << person.birthday.year;
b. cout << year;
c. cout << birthday.year;
d. cout << peson.year;
a. cout << person.birthday.year;
Given the following structure definition, what is the correct way to initialize a variable called today?
struct DateType
{
int day;
int month;
int year;
}
a. DateType today(1,1,2000);
b. DateType today = (1,1,2000);
c. DateType today = {1,1,2000);
d. DateType today = {1,1,2000,0);
c. DateType today = {1,1,2000);
When defining a class, the class should be composed of the kind of values a variable of the class can contain, and
a. member functions for that class
b. the keyword private
c. other class definitions
d. nothing else
a. member functions for that class
Which of the following is the correct function definition header for the getAge function which is a member of the Person class?
a. int getAge();
b. int getAge()
c. int Person:getAge()
d. int Person::getAge()
d. int Person::getAge()
Given the following class definition and the following member function header,which is the correct way to output the private data?
class Person
{
public:
void outputPerson(ostream& out);
private:
int age;
float weight;
int id;
};
void Person::outputPerson(ostream& out)
{
//what goes here?
}
a. out << person.age << person.weight << person.id;
b. out << person;
c. out << age << weight << id;
d. outputPerson(person);
c. out << age << weight << id;
Why do you want to usually make data members private in a class?
a. so that no one can use the class.
b. ensure data integrity
c. provide data abstraction.
d. provide information hiding.
e. B and D
f. B, C and D
f. B, C and D
A member function of a class should be made private
a. always
b. only if it will never be used
c. if it will only be used by other members of the class
d. never, it is illegal to make a member function private
c. if it will only be used by other members of the class
A member function that allow the user of the class to change the value in a data member is known as
a. a mutator function
b. a mutation
c. a manipulator function
d. an accessor function
a. a mutator function
A Member function that allows the user of the class to see the value in a data member is known as
a. a mutator function
b. a mutation
c. a manipulator function
d. an accessor function
d. an accessor function
If you design a class with private data members, and do not provide mutators and accessors, then
a. The private data members can still be accessed from outside the class by using the & operator
b. The data can not be changed or viewed by anyone.
c. None of the above
d. A and B
b. The data can not be changed or viewed by anyone.
A class member function that automatically initializes the data members of a class is called
a. the init function
b. an operator
c. a constructor
d. a cast
c. a constructor
If you have a class named myPersonClass, which of the following correctly declare a constructor in the class definition?
a. myPersonClass::myPersonClass();
b. myPersonClass();
c. init();
d. cast();
b. myPersonClass();
given the following class definition, how could you use the constructor to assign values to an object of this class?class CDAccount{
public:
CDAccount();
CDAccount(float interest, float newBalance);
float getBalance();
float getRate();
void setRate(float interest);
void setBalance(float newBalance);
private:
float balance, rate;
};
and the following object declarationCDAccount myAccount;
a. myAccount = CDAccount(float myRate, float myBalance);
b. myAccount = CDAccount {myRate, myBalance};
c. myAccount = CDAccount[myRate, myBalance];
d. myAccount = CDAccount(myRate, myBalance);
d. myAccount = CDAccount(myRate, myBalance);
Given the following class definition, what is missing?
class ItemClass
{
public:
ItemClass(int newSize, float newCost);
int getSize();
float getCost();
void setSize(int newSize);
void setCost(float newCost);
private:
int size;
float cost;
};
a. nothing
b. a default constructor
c. accessor functions
d. mutator functions
b. a default constructor
Given the following class definition, how would you declare an object of the class, so that the object automatically called the default constructor?
class ItemClass
{
public:
ItemClass();
ItemClass(int newSize, float newCost);
int getSize();
float getCost();
void setSize(int newSize);
void setCost(float newCost);
private:
int size;
float cost;
};
a. ItemClass() myItem;
b. ItemClass myItem(1, 0.0);
c. ItemClass myItem;
d. ItemClass myItem();
e. You can not do this
c. ItemClass myItem;
A data type consisting of data members and operations on those members which can be used by a programmer without knowing the implementation details of the data type is called
a. an abstract definition type
b. an available data type
c. an abstract data type
d. a primitive data type
c. an abstract data type
Which part of the ADT tells the programmer using it how to use it?
a. the implementation
b. the interface
c. the abstractness
d. the scope resolution
b. the interface
If you are designing a class for an ADT, you can tell if the class is an ADT if
a. when you change the implementation of the class, none of the rest of the program needs to change.
b. when you change the interface of the class, nothing else needs to change.
c. you change the private part and the rest of the program using the ADT does not compile.
d. everything must be changed
a. when you change the implementation of the class, none of the rest of the program needs to change.
Developing an ADT means that the user of your class does not have to know the details about how the class is implemented. This is known as
a. interface
b. implementation
c. testing and debugging
d. information hiding
d. information hiding
Given the following class, what would be the best declaration for a mutator function that allows the user of the class to change the age?
class Wine
{
public:
Wine();
int getAge();
float getCost();
private:
int age;
float cost;
}
a. int getAge(int newAge);
b. Wine();
c. void setAge();
d. void setAge(int newAge);
d. void setAge(int newAge);
Given the following class, what would be the best declaration for a constructor that would allow the user to initialize the object with an initial age and cost?
class Wine
{
public:
Wine();
int getAge();
float getCost();
private:
int age;
float cost;
}
a. int getAge(int newAge);
b. Wine();
c. Wine(int age);
d. Wine(int newAge, float newCost);
d. Wine(int newAge, float newCost);
Given the following class and object declaration, how would you print out the age and cost of a bottle of wine?
class Wine
{
public:
Wine();
int getAge();
float getCost();
private:
int age;
float cost;
}
Wine bottle;
a. cout << bottle;
b. cout << Wine.age, Wine.cost;
c. cout << bottle.getAge() << bottle.getCost();
d. cout << bottle.getAge << bottle.getCost;
e. cout << bottle.age << bottle.cost;
c. cout << bottle.getAge() << bottle.getCost();
Data members or member functions of a class that are declared to be private may
a. only be accessed by the main program
b. only be accessed by members of the class
c. not be accessed by the class
d. are considered to be global variables
b. only be accessed by members of the class
Member functions of a class
a. may not be in the private section
b. must be in the private section
c. may be in either section
d. can not be called in the main program
c. may be in either section
In a struct, all members are ____________ by default
a. public
b. private
c. global
d. all of the above
a. public