Professional Documents
Culture Documents
TEXTBOOK SOLUTIONS
EXPERT Q&A
Sign in
More
Search
home / study / engineering / computer science / computer science questions and
answers / c++ 1. which statement initializes (allocates) a pointer to point to a
memory address in the ...
Question: C++ 1. Which statement initializes (allocates) a pointer to point to a
memory address in the heap...
c++
1.
2.
void goo(int* x)
{
*x = 1;
}
Suppose that a is an int* variable pointing to some integer, and *a is equal to
zero. What is printed if you cout << a; after the function call goo(a)?
0
The address of a
1
The address of x
3.
Suppose bag is a template class, what is the syntax for declaring a bag b of
doubles?
bag of doubles b;
double bag b;
bag b;
bag<double> b;
4.
class bag
{
public:
static const int CAPACITY = 30;
bag( ) { used = 0; }
int erase(int& target);
bool erase_one(int& target);
void insert(int& entry);
void operator +=(bag& addend);
int size( ) const { return used; }
int count(const int& target);
private:
int data[CAPACITY]; // The array to store items
int used; // How much of array is used
};
Which steps would be necessary for changing this bag class to hold double values
instead of integers?
Change the array declaration from int data[CAPACITY] to double data[CAPACITY] and
recompile.
This is a template class, so there is no need to recompile to change the data type.
Round each double value to an integer before putting it in the bag.
Change the int to double for the array and for all return values and parameters
related to the array, then recompile.
PreviousNext
5.
With a single template class, we can implement a container class that can be
instantiated with a different data type for each object.
True
False
6.
When you write a template class, where does the template prefix occur? (Select all
that apply)
7.
line 1
None of the above. There are no errors in these lines.
line 2
8.
9.
10.
class bag
{
public:
static const int CAPACITY = 30;
bag( ) { used = 0; }
int erase(int& target);
bool erase_one(int& target);
void insert(int& entry);
void operator +=(bag& addend);
int size( ) const { return used; }
int count(const int& target);
private:
int* data[CAPACITY];
int used;
};
What is data? (Select the most likely answer)
An integer variable
11.
What kind of a container is an object of type list from the Standard Template
Library?
Sequential
Associative
12.
vector<int> a_list;
vector<int>::iterator iter;
iter = min_element(a_list.begin(), a_list.end());
cout << *iter << "\n";
This code block declares a vector of integers and an iterator for a vector of
integers. The iterator called iter is set to point to the first element in the
vector and that value is displayed.
This code block declares a vector of iterators. The iterator with the smallest
memory location is located and that memory location is displayed.
This code block declares a vector of integers and an iterator for a vector of
integers. The iterator called iter is set to point to the smallest element in the
vector and that element is removed.
This code block declares a vector of integers and an iterator for a vector of
integers. The iterator called iter is set to point to the smallest element in the
vector and that value is displayed.
13.
14.
15.
Which keyword can be used to allow a non-member function to see the private
variables of another class?
protected
grant
friend
overload
16.
int main()
{
Time t(2, 50);
t.setHour(5);
t.setMinute(20);
cout << "The time is " << t << endl;
}
What is necessary in the Time class to make this main function compile?
17.
In which of the following ways can you write an operator overload? (Select all that
apply)
18.
int p1 = 3;
int &p2 = p1;
p2 = p1;
cout << p1 << endl;
The address in the stack of p1
An error occurs
The address in the stack of both p1 and p2
3
19.
Suppose cursor is a pointer that points to a penguin node in a linked list using a
penguin class that includes a member function get_link(). You wish to move cursor
to point to the next node (to the next penguin) in the list. Which statement do you
use?
cursor++;
cursor = cursor->get_link( );
cursor = get_link();
cursor.get_link();
20.
Suppose cursor points to a penguin node in a linked list using a penguin class,
which includes functions called get_data() and get_link(). What Boolean expression
should be true when that penguin node is the ONLY node in the list?
cursor->get_data( ) == 0.0
cursor->get_data( ) == NULL
cursor->get_link( ) == NULL
cursor == NULL
21.
The following steps have been performed for deleting a node from a linked list. The
nodes have the member accessor function get_link(). The head_ptr points to the head
node of a list.
Step 1: Set up a local variable called remove_ptr and set it equal to head_ptr
Step 2: Set head_ptr equal to the head_ptr->get_link()
Step 3: Delete remove_ptr
Which of the following situations should use these steps?
22.
Which operation is better for storing items in an array than storing items on a
linked list?
23.
You are working with a node class named node. Suppose that f is a non-member
function with a prototype like this with a missing data type in the parameter list:
void f(________ head_ptr);
// Precondition: head_ptr is a head pointer for a linked list.
// Postcondition: The function f has done some computation with
// the linked list, but the list itself is unchanged.
What is the best data type for head_ptr in the parameter list of this function?
node* &
node*
node
node&
24.
A virtual function provides you with the ability to have several different
functions with the same name in a parent and children classes and have C++ figure
out which one to call at run-time.
True
False
25.
Suppose cursor points to a node in a linked list (using the node definition with
member functions called get_data and get_link). What Boolean expression will be
true when cursor points to the tail node (last node) of the list?
cursor == NULL
cursor->get_link( ) == NULL
None of the above
cursor->get_data( ) == NULL
26.
Consider the following class:
27.
polymorphism
encapsulation
function closure support
inheritance
29.
class base {
public:
void vfunc() {
cout << "This is base's vfunc()." << endl;
}
};
int main()
{
base *p, b;
derived1 d1;
p = &b;
p -> vfunc();
// remember, this is equivalent to (*p).vfunc();
p = &d1;
p -> vfunc();
}
What is the output of this program?
30.
class base {
public:
virtual void vfunc() {
cout << "This is base's vfunc()." << endl;
}
};
int main()
{
base *p, b;
derived1 d1;
p = &b;
p -> vfunc();
// remember, this is equivalent to (*p).vfunc();
p = &d1;
p -> vfunc();
}
What is the output of this program?
You wish to convert a container class to a template container class. Which of the
following should you consider?
Make the class visible to the client. One way to do this is to include the main
function in the header file.
Make sure operations on container data are not type specific. For example, don't
use string functions on container data.
Make implementation visible. One way to do this is to include both interface and
implementation in the header file.
Make sure all return statements that return container dataonly return container
data. If a return statement returns container data under certain conditions, but
another return statement returns something that is not container data under other
conditions, there could be a data type conflict when a different data type is used
for this template.
32.
string
vector
sort
list
33.
string str2(str1);
Assuming that this code has the using namespace std directive and the appropriate
#include statements, what would you expect the output to be?
aaa
aa
he
are
he
aaa
aaa
aaa
aaa
aa
he
are
aaa
aaa
he
are
RESOURCES
Site Map
Mobile
Publishers
Join Our Affiliate Program
Advertising Choices
TEXTBOOK LINKS
Return Your Books
Textbook Rental
eTextbooks
Used Textbooks
Cheap Textbooks
College Textbooks
Sell Textbooks
STUDENT SERVICES
Chegg Play
Study 101
Chegg Coupon
Scholarships
Career Search
Internships
College Search
College Majors
Scholarship Redemption
COMPANY
Jobs
Customer Service
Give Us Feedback
Chegg For Good
Become a Tutor
LEARNING SERVICES
Online Tutoring
Chegg Study Help
Solutions Manual
Tutors by City
GPA Calculator
Test Prep
PayPal BBB Chegg Plants Trees
© 2003-2018 Chegg Inc. All rights reserved.