You are on page 1of 11

Skip Navigation

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.

Which statement initializes (allocates) a pointer to point to a memory address in


the heap?

int *p = new int;


int p = new int;
int& p = new int;
int* p;

2.

Here is a function declaration:

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.

Consider the following bag class:

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)

Before each member function implementation.


Before each object declaration.
Before the main function
Before the template class definition

7.

Consider this prototype for a template function:

template <class Item> //line 1


void foo(Item<x>); //line 2
Something is wrong with this prototype. Which line has the error?

line 1
None of the above. There are no errors in these lines.
line 2

8.

int *p1 = new int;


int *p2 = p1;
*p1 = 3;
*p2 = *p1;
cout << p1 << endl;
4
3
Unpredictable
The address of p1

9.

What is wrong with this block of code?

int *p1 = new int; //line 1


int *p2; //line 2
*p2 = 4; //line 3
p1 = p2; //line 4
cout << p1 << p2 << endl; //line 5
The p2 pointer is not instantiated with the new operator before accessing its
memory location on line 3.
A pointer assignment to set the address of a pointer cannot be done with an integer
value on line 3.
The p1 pointer is incorrectly instantiated with the new operator before accessing
its memory location on line 1. Parentheses are missing from this statement.
None of the above. There are no errors in this block of code.
PreviousNext

10.

Consider the following bag class:

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)

A pointer to an object of the Standard Template Library class

A dynamic array of integers


A pointer to an vector that holds int values

An integer variable

11.

What kind of a container is an object of type list from the Standard Template
Library?

Sequential
Associative
12.

What does the following code block do?

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.

What is the delete keyword used for?

To remove a pointer address from the stack


To delete both the pointer and to deallocate the space in the heap that is pointed
to.
To deallocate the space in the heap pointed to by a pointer.
To delete the value stored in the heap pointed to by a pointer

14.

Which of the following declarations is legal (will not generate an error)?

int q = new *int;


int q = new int;
int q* = new int;
int *q;

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.

Consider the following main function:

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?

None of the above.


The Time class must have the data member t defined in the public area of the class.
The << operator must be overloaded for the Time class
A friend function called t is needed.

17.

In which of the following ways can you write an operator overload? (Select all that
apply)

in a class member function


in a nonmember function
in the main function
in a friend function

18.

What is printed by these statements?

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?

To delete ALL nodes from a list


To delete a node from the front of a list
To delete a node from the end of a list
To delete a node from the middle (neither front nor end) of a list.

22.

Which operation is better for storing items in an array than storing items on a
linked list?

retrieve a copy of the item at location i.


inserting an item at the beginning (position 0).
deleting an item at the beginning (position 0).
Growing the size of the container.

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:

class hourlyEmp: public employee {


public:
hourlyEmp();
hourlyEmp(const string& newName, const string& newSsn,
double newPayRate, double newHours);
double getHours() const;
void setHours(double newHours);
void giveRaise(double amount);
void printCheck() const;
private:
double hours;
double payRate;
};
How could you create an object of this class (Select all that apply)?

employee arnold("Arnold Jones","23456664",13,20);


hourlyEmp arnold();
employee::hourlyEmp arnold("Arnold Jones");
hourlyEmp arnold("Arnold Jones","23456664",13,20);

27.

Which of the following is a pure abstract function?

virtual double area() {area= 0};


virtual double area() = 0;
pure virtual double area() = {};
abstract double area();
28.

A programming language is required to provide which things in order for it to be


considered an object oriented programming language?

polymorphism
encapsulation
function closure support
inheritance

29.

Consider the following:

class base {
public:
void vfunc() {
cout << "This is base's vfunc()." << endl;
}
};

class derived1 : public base {


public:
void vfunc() {
cout << "This is derived1'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?

This is base's vfunc().


This is base's vfunc().
This is base's vfunc().
This is derived1's vfunc().
This is derived1's vfunc().
This is derived1's vfunc().
Error. Ambiguous reference.

30.

Consider the following:

class base {
public:
virtual void vfunc() {
cout << "This is base's vfunc()." << endl;
}
};

class derived1 : public base {


public:
void vfunc() {
cout << "This is derived1'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?

This is base's vfunc().


This is derived1's vfunc().
This is base's vfunc().
This is base's vfunc().
This is derived1's vfunc().
This is derived1's vfunc().
31.

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.

Which of the following is a container class in the Standard Template Library?


(Select all that apply)

string
vector
sort
list

33.

Consider the following code.

string str1(3, 'a');

cout << str1 << endl;

string str2(str1);

cout << str2 << endl;

string str3("hello", 2);

cout << str3 << endl;

string str5 = "How are you?";


string str6(str5, 4, 3);

cout << str6 << endl;

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

Expert Answer 100% (2 ratings)


As per Chegg rules when more than one question is posted in the same questions then
the expert is allowed to answer only the first question or for multiple choice
question first 4 or 5. If there is any violation the expert a... view the full
answer
Get this answer with Chegg Study View this answer
Need an extra hand? Browse hundreds of Computer Science tutors.
ABOUT CHEGG
Media Center
College Marketing
Privacy Policy
Your CA Privacy Rights
Terms of Use
General Policies
Intellectual Property Rights
Investor Relations
Corporate Development
Enrollment Services

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.

You might also like