Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Multiple choi ce Pointers Program fragment #1 What is the output from program fragment #1? * 10 points a. m = 177, n = 43,

Multiple choice

Pointers

Program fragment #1

image text in transcribed

What is the output from program fragment #1? *

10 points

a. m = 177, n = 43, *p = 177

b. m = 91, n = 43, *p = 177

c. m = 5, n = 7, *p = 19

d. m = 5, n = 19, *p = 43

Arrays

Program fragment #2

image text in transcribed

Suppose that in program fragment #2 that Box is a C++ class. Which of the following is true? *

a. the variable ptr points to an array of 2500 Box's

b. an exception is thrown if the system does not have memory for 2500 Box's

c. the default constructor for Box is called 2500 times

d. all of the above are possible.

Constructors

In program fragment #2, suppose that the Box class does not have a default constructor. (Yes, it is possible that you do not have a default constructor at all.) What would happen?

a. When the program is executed, each Box object in the array would be initialized to all zeroes.

b. When the program is executed, the alternate constructor for Box will be called for each Box object in the array.

c. An exception will be thrown when the program reaches the second line.

d. The program will not compile.

Pointer addition

Program fragment #3

image text in transcribed

Suppose that in program fragment #3 that the local variable x is stored in address 4294946208. If the sizeof(int) is 4, what is the value of the pointer ptr after execution of the program fragment? *

a. 4294946212

b. 4294946204

c. 4294946209

d. the value of ptr cannot be determined from the information given.

5. Class definitions

ClassOne & ClassTwo Definitions

image text in transcribed

Suppose that x is a ClassOne object. Which of the following expressions can be used to invoke the member function func1( )? *

10 points

a. x->func1()

b. ClassOne::func1()

c. (*x).func1()

d. x.func1()

Data member names

Continuing with the definitions of ClassOne and ClassTwo from the previous question, each of the data members in these definitions are prefixed by m_ . Why?

a. The g++ compiler requires all data members to start with m_.

b. It is required by the Coding Standard.

c. It is a good idea to distinguish between function members and data members.

d. It is a good idea to distinguish between data members, local variables and parameters.

Constructors

Program fragment #4

image text in transcribed

Using the class definitions for ClassOne and ClassTwo in Question #5, suppose that a ClassTwo pointer ptr is defined as above. Which constructors are invoked? *

a. The default constructor for ClassOne and the default constructor for ClassTwo are invoked.

b. The alternate constructor for ClassOne and the default constructor for ClassTwo are invoked.

c. The default constructor for ClassOne and the alternate constructor for ClassTwo are invoked.

d. The alternate constructor for ClassOne and the alternate constructor for ClassTwo are invoked.

Pointer access

After the code in program fragment #4 is executed, how can we access the ClassOne data member called m_member1 using ptr? *

a. ptr->m_data1->m_member1

b. ptr.m_data1->m_member1

c. ptr->m_data1.m_member1

d. ptr.m_data1.m_member1

. More Constructors

Program fragment #5

image text in transcribed

Consider the ClassOne constructor above. What is the difference between local and m_member1?

a. m_member1 is already defined in ClassOne, but local is added as a member after the constructor is executed.

b. the storage for local goes away after the constructor finishes, but the storage for m_member1 persists for as long as the host object exists.

c. m_member1 is accessible by func1(), but local is not accessible by func1() because it is not public.

d. There is no difference between local and m_member1. They are both data members of the ClassOne object being created by the constructor.

Privacy

Considering the definitions of ClassOne and ClassTwo in Question #5, which of the following statements has access to the private member function func2( )? *

a. any statement in any function that declares an object of ClassOne can access func2( ) through that object.

b. any statement in the implementation of the ClassOne member function func1( ).

c. any statement in the implementation of the ClassTwo alternate constructor.

d. all of the above

delete operator

Program fragment #6

delete [ ] ptr

Let ptr be a pointer to int. When is it appropriate to use the delete operator shown above? *

a. When ptr points to an int that was allocated using the new operator

b. When ptr points to an array of int that was allocated using the new operator

c. When ptr points to an int that was allocated using malloc()

d. When ptr points to an array of int that was allocated using malloc()

Segmentation faults

For programs running under Unix, when might a "segmentation fault" occur? *

a. when an exception is thrown but not caught

b. when a statement in a non-member function tries to access a private data member of a class.

c. when a function calls the delete operator on a NULL pointer.

d. when a program tries to access memory reserved for the operating system.

13. Memory Leaks

When would a memory leak occur?

a. when a dynamically allocated array is too big for the intended purpose.

b. when a dynamically allocated array is too small for the intended purpose.

c. when memory allocated using new never gets deallocated using delete.

d. when the new operator asks for more memory than is available in the system.

Guarded Header Files

Program fragment #7

image text in transcribed

Header files should be guarded using #ifndef, #define and #endif as above. Why?

a. It is required by the CMSC 341 Coding Standards.

b. It prevents this header file from being included twice.

c. It tells the compiler that the ClassOne declarations are in this file.

d. It tells the compiler to include the implementations of the ClassOne member functions after the #endif

Namespaces

Why do we often put "using namespace std ;" at the top of our programs?

10 points

a. It is required by the CMSC 341 Coding Standards

b. It allocates memory for the standard library.

c. It tells the compiler to include the standard library

d. We are too lazy to type std:: all over the place.

int main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intranet And Web Databases For Dummies

Authors: Paul Litwin

1st Edition

0764502212, 9780764502217

More Books

Students also viewed these Databases questions

Question

Why would a firm want to monitor its purchasing performance?

Answered: 1 week ago