1/115
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No study sessions yet.
To declare a c-string and initialize it to the value of "phonebook",
a. char s1=phonebook;
b. char s1[10]="phonebook";
c. c-string phonebook;
d. char s1[10]=phonebook;
b. char s1[10]="phonebook";
When the extraction operator is used to read data into a string,
a. it skips all white spaces
b. it skips only new lines
c. it reads everything on the line
d. it reads as many characters that will fit into the c-string
a. it skips all white spaces
If you want to read into a c-string, you must ensure that the user does not enter more characters than
a. The size of the c-string
b. The size of the c-string + 1
c. The size of the c-string -1
d. It doesn't matter.
c. The size of the c-string -1
What is wrong with the following attempted c-string declaration and initialization?
char str1[5]={'a', 'b', 'c'};
a. There are only 3 values in the braces
b. The single quotes should be double quotes
c. The values do not constitute a c-string
d. nothing
d. nothing
How can you assign the value "toaster" to a c-string name str of size 10?
a. str="toaster;
b. str=toaster;
c. strcpy(str,"toaster");
d. str.strcpy("toaster");
c. strcpy(str,"toaster");
What is the difference between strcmp and strncmp?
a. No difference
b. they both compare, one expects an integer for the number of characters to compare.
c. one copies, the other compares
d. They are in different libraries
b. they both compare, one expects an integer for the number of characters to compare.
Which assignment statements will copy the value " toaster" into a string variable (str1)?
a. strcpy(str1,"toaster");
b. str1 = "toaster";
c. str1 = toaster;
d. str1 += toaster;
b. str1 = "toaster";
What is the value of str after the following code?
string str;
a. a garbage string
b. the empty string
c. the null character
d. unknown
b. the empty string
Which is the proper way to determine how many characters are in the string variable named str?
a. str.getLength()
b. str.length()
c. length(str)
d. getLength(str)
b. str.length()
If the name of a file to open is in the string variable name fileName, which of the following will correctly open the file for output?
a. out_file.open(fileName);
b. out_file.open("fileName");
c. fileName.open(outfile);
d. None are correct
a. out_file.open(fileName);
Which of the following would correctly read an entire line from an input file stream named fin into a string variable named line.
a. getline(fin, line);
b. fin.getline(line);
c. fin.getline(line,'\n\');
d. fin.getline(line,80);
a. getline(fin, line);
What is the proper way to declare a vector of strings named names?
a. vector strings names;
b. vector
c. vector
d. all of the above
c. vector
What is the value of numbers.size() after the following code?
vector
a. 0
b. 10
c. 100
d. unknown
a. 0
What is the value of numbers.size() after the following code?
vector
a. 0
b. 10
c. 100
d. unknown
c. 100
What is the value of numbers.size() after the following code?
vector
numbers.reserve(100)
a. 0
b. 10
c. 100
d. unknown
a. 0
What is the value of numbers.capacity() after the following code?
vector
numbers.reserve(100)
a. 0
b. 10
c. 100
d. unknown
c. 100
If a vector named numbers has 20 elements in it, what is the result of executing the following statement?
numbers.resize(10);
a. no change
b. the first 10 elements are removed
c. the last 10 elements are removed
d. this causes a run-time error
c. the last 10 elements are removed
Given the following code, what is the correct statement to insert the string str2 into str1, directly after the 'd'?
string str1="abcdefg";
string str2="ABCDE";
a. str1.insert(4,str2);
b. str2.insert(4,str1);
c. insert(str1,4)=str2;
d. insert(str2,4)=str1;
a. str1.insert(4,str2);
In a vector, which of the following statements is true?
a. Indexing vector access is range checked.
b. The range of legal index values for a vector is 0 to the value of v.size()-1
c. To add a value use the member function v.push_front( )
d. To increase or decrease a vector's size v.new_size(newSize);
b. The range of legal index values for a vector is 0 to the value of v.size()-1
A vector called nameList is using a for loop to search for and delete a stored value. Which code snippet will accomplish this?
a. nameList.erase(matchedName);
b. nameList[i] = 0;
c. nameList.erase(nameList.begin() + i);
d. nameList.erase(i);
e. Both C and D
c. nameList.erase(nameList.begin() + i);
Which of the following correctly declare 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;
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 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*;
b. typedef float* floatPtr;
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];
Which of the following statements correctly returns the memory from the dynamic array pointer p1 to the freestore?
a. delete [] p1;
b. delete p1[];
c. delete *p1;
d. delete p1;
a. delete [] p1;
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 arein the array.
b. an array can be assigned the value in a pointer variable
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
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
Given the following structure definitions, what is the correct way to print the person's 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};
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 that allows 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 outside the class.
c. None of the above
d. A and B
b. The data can not be changed or viewed by anyone outside the class.
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, 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
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 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 protected 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. may be accessed by members of the class and any derived classes
e. are considered to be global variables
d. may be accessed by members of the class and any derived classes
In a struct, all members are ____________ by default
a. public
b. private
c. global
d. all of the above
a. public
In a class, all members are ____________ by default
a. public
b. private
c. global
d. all of the above
b. private
A derived class has access to
a. the private functions and variables of its ancestor classes
b. the public functions and variables of its ancestor classes
c. only the functions and variables defined it its class
d. none of the above
b. the public functions and variables of its ancestor classes
A derived class is
a. Less specific than its parent, or base class
b. More specific than its parent, or base class
c. More private than its parent, or base class
d. Smaller than its parent, or base class
b. More specific than its parent, or base class
A derived class named dName is derived from the base class cName. How would this derived class be defined?
a. class cName { class dName{}; };
b. class dName :: cName {};
c. class dName(cName) {};
d. class dName : public cName{};
d. class dName : public cName{};
Who can access private data in a class?
a. members of the class
b. friends of the class
c. everyone
d. B and C
e. no one
a. members of the class
When should you generally pass an object of the class to a friend function as a reference parameter?
a. If the friend function changes the values of the data member(s).
b. If the friend function will not change the values of the data member(s).
c. It is more efficient to pass the object by reference.
d. A and B
e. A and C
e. A and C
What is wrong with the following member function definition given the class below?
class Rational
{
public:
Rational();
Rational(int numer, int denom);
Rational(int whole);
int getNumerator() const;
int getDenominator() const;
friend void display(ostream& out, const Rational& value);
private:
int numerator;
int denominator;
};
int Rational::getNumerator() const{numerator = 0;
return numerator;
}
a. You can not set the numerator to zero
b. The function may not modify numerator, but it can modify denominator
c. The function may not modify any of the private data members
d. nothing
e. A and B
f. A and C
c. The function may not modify any of the private data members
To overload functions with symbolic names (like + - / <<), you must use the keyword_______ before the symbolic name.
a. const
b. operator
c. reference
d. void
b. operator
When overloading an operator, which of the following is true?
a. One of the arguments must be an object of the class
b. The operator can be a friend or a member of the class.
c. The operator does not have to be a friend or a member of the class
d. All of the above
e. None of the above
d. All of the above
How many parameters are there in a binary operator implemented as a friend?
a. 0
b. 1
c. 2
d. as many as you need
c. 2
If you want to be able to compile the following code, Rational r1;
int x;cout << r1 + x << endl;
which overloaded operator(s) do you need?
a. friend Rational operator+( const Rational& left, int right);
b. friend void operator+ (const Rational& left, int right);
c. friend ostream operator << (ostream& out, const Rational& object);
d. friend ostream& operator << (ostream& out, const Rational& object);
e. A and C
f. A and D
f. A and D
In an overloaded insertion or extraction operator, which object should be the first parameter, the stream or the object of the class?
a. the stream
b. the object
c. it doesn't matter
d. none of the above
a. the stream
Which of the following operators can not be overloaded?
a. =
b. ==
c. .
d. [ ]
c. .
Given the following class and array declaration, how would you print out the age of the 10th person in the array?
class personClass
{
public:
void setAge(int newAge);
void setGender( char newGender);
void setSalary(float newSalary);
int getAge();
char getGender();
float getSalary();
private:
int age;
char gender;
float salary;
};
personClass people[100];
a. cout << people[10];
b. cout << people[9];
c. cout << people[9].age;
d. cout << people[9].getAge();
d. cout << people[9].getAge();
Which of the following statements are true?
a. Array elements may be user-defined types (structs or classes)
b. If a function is expecting a variable of the base type of the array, then you can pass an element of the array to the function.
c. All of the above.
d. None of the above
c. All of the above.
When a dynamic array with a class for a base type is declared, which constructor is called?
a. the copy constructor
b. the destructor
c. the default constructor
d. an explicit constructor
c. the default constructor
The destructor for a class is called
a. explicitly from the main program
b. when the class is instantiated
c. when the object of the class goes out of scope
d. Only at the end of main
c. when the object of the class goes out of scope
ADTs should be in separate files because
a. This promotes information hiding
b. This promotes data abstraction
c. This promotes software reusability
d. This results in faster recompilation
e. All of the above
f. None of the above
e. All of the above
The file that contains the main portion of your program is called
a. the implementation file
b. the interface file
c. the application file
d. the specification file
c. the application file
In order to make a user-defined ADT available that is defined in the file myfile.h, youwould
a. #include
b. #include myfile.h
c. #include
d. #include "myfile.h"
d. #include "myfile.h"
Connecting the application and implementation files together to form an executable file is called
a. compiling
b. assembling
c. linking
d. debugging
c. linking
All the code between
#ifndef MYCLASS_Hand
#endf
is ____________ if MYCLASS_H is defined.
a. skipped
b. executed
c. compiled
d. debugged
a. skipped
The identifier used in the #ifndef directive should be
a. the name of the class in upper case letters
b. Your name in upper case letters
c. The file name in uppercase letters (with an __ instead of a .)
d. whatever you want it to be
c. The file name in uppercase letters (with an __ instead of a .)
We use the #ifndef, #define, and #endif
a. to prevent multiple definitions of a class
b. when we use separate files
c. whenever we use a class
d. none of the above
e. A and B
e. A and B
Which file name will end in a .h?
a. Implementation
b. Application
c. All input files
d. Interface File
e. A and B
d. Interface File
Which file name will end in a .cpp?
a. Implementation File
b. Application File
c. All input files
d. Interface File
e. A and B
e. A and B
If you have a class defined in separate files, and change the main program, which files need to be re-compiled?
a. The interface
b. The application
c. The Implementation
d. All files
e. B and C
b. The application
What is the difference between an ADT and a class in C++?
a. In an ADT, the user does not have access to the implementation details
b. In an ADT, the user can change the implementation details
c. There is no difference
d. A class must always be in a separate file
a. In an ADT, the user does not have access to the implementation details
A namespace is
a. a collection of name definitions
b. using std
c. used to distinguish between identical names
d. All of the above
e. A and C
e. A and C
A using directive that appears inside a set of braces applies
a. Only to that block
b. From that point to the end of the file
c. Everywhere
d. Only if the namespace is std
a. Only to that block
In order to create a namespace called student, you use
a. namespace student{//code goes here}
b. {namespace student//code goes here}
c. student namespace{//code goes here}
d. {student namespace//code goes here}
a. namespace student{//code goes here}
If you want to only use cin and cout (but no other names) from the std namespace, you would
a. using std::cin;
using std::cout;
b. not be able to do it.
c. cin and cout are not in the std namespace
d. always have to say std::cin and std::cout
e. Either A or D
e. Either A or D
Why will the following code not compile?
namespace ns1{
void print();
void display1(){};
}
namespace ns2{
void print();
void display2(){};
}
int main(){
using namespace ns1;
using namespace ns2;
display1();
display2();
print();
return 0;
}
a. The call to print is ambiguous
b. We have not included the iostream library
c. We have not used namespace std
d. It will compile
a. The call to print is ambiguous
In order to hide functions that are defined in the implementation file, they should be part of the ______________ namespace.
a. global
b. std
c. class
d. unnamed
d. unnamed
The pointer in a node points to
a. the data part of a node
b. the count part of a node
c. the pointer part of the node
d. the whole node
d. the whole node
If you want to declare a node type struct and typedef a pointer type for that node, in which order must you make these declarations?
a. first the node pointer, then the node, since the node has a pointer in it.
b. first the node, then the typedef.
c. it doesn't matter which order you do them in
d. you cannot do both of them in the same program
b. first the node, then the typedef.
If you have a node pointer named head, which is pointing to a valid node, how do you access the member variable in the node named item?
a. head.item
b. head.itemc. (head).item
d. head->item
e. all of the above
f. A or B
g. C or D
g. C or D
We generally set a pointer variable to nullptr
a. to signify that the pointer does not point to any memory
b. to signify the end of a list
c. because we want all our pointers to always point to nullptr
d. never
e. A and B
f. A and C
e. A and B
As defined in the text, the pointer variable head
a. is the first node in the list
b. points to the first node in the list
c. is always nullptr
d. is undefined
b. points to the first node in the list
If NodeTypePtr is defined to be a pointer type to a node in a linked list, then the declaration NodeTypePtr head;
a. allocates memory for the node that head will point to
b. automatically makes head->link point to NULL
c. allocates only the memory for a pointer variable
d. allocates a linked list
c. allocates only the memory for a pointer variable
Given the following declarations, which statement would put the value of 3 in the item part of the first node in the linked list?
struct Node{
int item;
Node* link;
};
typedef Node* NodePtr;
NodePtr head;head = new Node;
a. head=3;
b. head.item=3;
c. *head.item=3;
d. head->item=3;
d. head->item=3;
What is wrong with the following definition of headInsert?
struct Node{
int item;
Node* link;
};
typedef Node* NodePtr;
void headInsert(NodePtr& head, int data){
NodePtr tmp = new Node;
tmp->item = data;
head->next = tmp;
tmp->next = head->next;
}
NodePtr head;
headInsert(head, 4);
a. head->next is pointing to nullptr
b. if there were any nodes following head they are now lost.
c. nothing is wrong.
d. tmp should be declared to be a Node not a NodePtr
b. if there were any nodes following head they are now lost.
In the following search function for a linked list (using the Node and NodePtr as defined in the text), why is there code to check if here is nullptr?
NodePtr search(NodePtr head, int target){nodePtr here = head;if(here == nullptr){return nullptr;
}else
{
while( here->data != target && here->link != nullptr)
{here = here->link;}
if(here->data == target){
return here;
}else
{
return nullptr;
}}}
a. the list may be empty
b. the list may be full
c. there is no reason for that code to be there
d. A and B
a. the list may be empty
Which of the following loops correctly uses iter as an iterator to move through the nodes of the linked list?
NodePtr iter;
//a pointer to a node
a. while(iter != nullptr)iter++;
b. while(iter != nullptr)iter=iter->link;
c. for(iter= nullptr; iter != nullptr; iter=iter->link)
d. for( iter=head; iter != nullptr; iter=iter->link)
d. for( iter=head; iter != nullptr; iter=iter->link)
What happens if you have two lists (list1, list2), no assignment operator in your class, and attempt to execute the following statement?
list1 = list2;
a. you have a syntax error
b. you get a complete and identical copy of list2 into list1
c. list1 and list2 now name the same list
d. no problem
c. list1 and list2 now name the same list
Which of the following would completely remove all the nodes in a linked list and return them to the freestore?
void free(NodePtr head){
//what goes here?}
a.if(head == nullptr)return;else{free(head->link);delete head;}
b.NodePtr here, prev;while(head->link != nullptr){here=head;prev= head;while(here->link != nullptr){prev=here;here=here->link;}delete here;prev->link= nullptr;}delete head;
c.NodePtr here, prev;while(head ! nullptr{here=head;while(here->link != nullptr){
prev=here;here=here->link;}delete here;prev->link= nullptr;}delete head;
d.NodePtr here, prev;while(head->link != nullptr){here=head;prev= head;while(here->link != nullptr){prev=here;here=here->link;}delete here;prev->link= nullptr;
}
delete head;
e. all of the above
f. A and B
g. B and C
f. A and B
A stack exhibits ______________ behavior
a. last-in/ first-out
b. last-in/last-out
c. first-in/first-out
d. none of the above
a. last-in/ first-out
A queue exhibits ______________ behavior
a. last-in/ first-out
b. last-in/last-out
c. first-in/first-out
d. none of the above
c. first-in/first-out
To remove an item from the stack, we call the ____ function
a. pop
b. top
c. push
d. empty
a. pop