MIDTERM QUESTIONS

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

TRUE/FALSE Unlike arrays, vectors do not store the elements in contiguous memory locations.

FALSE they do store elements contiguously

TRUE/FALSE When you declare a vector, you must set the size of the vector immediately.

FALSE this is the entire point of a vector

Consider the following snippet of code: char *str = "Fall Semester"; What is printed when the following instruction is executed? printf("%s\n", str);

Fall Semester

Given the following declarations and initializations for the following questions: int x = 3; int y = 6; int *ptr1 = &x; int *ptr2 = &y; What is the output of the following program fragment? printf("%d %d\n", x, *ptr1); printf("%d %d\n", y, *ptr2); A. 3 3 6 6 B. 3 &x 6 &y C. 6 6 3 3

A. 3 3 6 6

An objects private member variables can be accessed from outside the object by: A. Public member functions B. Any function C. The dot operator D. The scope resolution operator

A. Public member functions

Consider the following structure: struct Rectangle { int length; int width; }; Below is a pointer of type Rectangle called rptr: Rectangle *rptr; Assume the values of length and width have been set. Now consider the following print statements. Which two of these are the correct ways to print the values of length and width. A. cout << rptr->length << " " << rptr->width << endl; B. cout << *rptr->length << " " << *rptr->width << endl; C. cout << *rptr.length << " " << *rptr.width << endl; D. cout << (*rptr).length << " " << (*rptr).width << endl; E. cout << rptr.(*length) << " " << rptr.(*width) << endl;

A. cout << rptr->length << " " << rptr->width << endl; AND D. cout << (*rptr).length << " " << (*rptr).width << endl;

Suppose we dynamically allocate memory in the following way: char *p; p = new char; Assuming that the value of the pointer variable p has not changed (so it still points to the same dynamic memory), we are finished with this variable and are ready to give the memory back to the operating system to reuse. In C++ which of the following will return the allocated memory to the OS. A. delete char; B. delete p; C. free (p); D. free char;

B. delete p;

Consider the following code. #include <stdio.h> int main() { int a = 112; int b = -1; float c = 3.14; int *d = &a; float *e = &c; char arr[] = "Yvon"; //int *ptr; //*ptr = 12; printf("%i, %i, %f, %f, %s\n", a, *d, c, *e, arr); return 0; } The two lines in the above code that have been commented out can (and did) cause this program to have a segfault, bus error, or memory fault error (uncommented). Briefly, explain why this code could cause this problem.

Because the pointer was not immediately initialized when declared.

Given the following declarations and initializations for the following questions: int x = 3; int y = 6; int *ptr1 = &x; int *ptr2 = &y; Based on the original initializations, what is the output of the following program fragment? ptr2 = ptr1; printf("%d %d\n", x, *ptr1); printf("%d %d\n", y, *ptr2); A. 6 6 6 6 B. 3 3 3 3 C. 3 3 6 3 D. 3 6 3 3

C. 3 3 6 3

Given the following declarations and initializations for the following questions: int x = 3; int y = 6; int *ptr1 = &x; int *ptr2 = &y; Based on the original initializations, what is the output of the following program fragment? *ptr1 = *ptr2; printf("%d %d\n", x, *ptr1); printf("%d %d\n", y, *ptr2); A. 6 6 3 3 B. 3 6 6 6 C. 6 6 6 6 D. 3 3 3 3

C. 6 6 6 6

Use the following structure declaration to answer the question below: struct Rectangle { int length, width; } Suppose I want to create a function that will print the values of a variable of type Rectangle. Since this function is only printing the values I want to make sure the function can not change the member values of the Rectangle structure. Which of the following is the appropriate prototype for this function. A. void showRect(Rectangle r) const; B. const showRect(Rectangle r); C. void showRect(const Rectangle r); D. None of the above

C. void showRect(const Rectangle r);

left or right

Causes the output to all go to one side or another.

Consider the following class declaration: class Widget { private: double price; int quantity; public: //Default Constructor Widget(); //Parameterized Constructor Widget(double, int); //Destructors ~Widget( ); //Setters void setPrice(double); void setQuantity(int); //Getters double getPrice() const; int getQuantity() const; //Member Functions (Methods) void printInfo(); }; Write the Default Constructor, Parameterized Constructor, and Destructor. Write the Setters, Getters, and Member Function.

Widget::Widget( ) { price = 0.0; quantity = 0; } Widget::Widget(double price, int quantity) { this->price = price; this->quantity = quantity; } Widget::~Widget() { cout << "the destructor is being called" << endl; } void Widget::setPrice(double price) { this->price = price; } void Widget::setQunatity(int quantity) { this->quantity = quantity; } double Widget::getPrice() { return(price); } int Widget::getQuantity() { return(quantity); } void Widget::printInfo() { cout << quantity << " " << price; }

In "C" we use fclose(fp) to close a file that was previously opened. Write the necessary code to close a file that has been opened for: a. reading (input) b. writing (output)

a. inputFile.close( ) b. outputFile.close( )

TRUE/FALSE Consider the following array declaration: int arr[ ]; If this is a valid declaration of an array mark true and false if it is invalid.

FALSE needs to be initialized to values or declare a size

Consider the following program: #include <stdio.h> int main() { int x = 1; int y = 2; double result; result = (double)x/y; printf("x is %d, y is %d, result is %f\n", x, y, result); return 0; } This program will print the following: x is 1, y is 2, result is 0.500000 Rewrite the program using C++. Make sure you use the C++ style type casting. Explain why the type cast is necessary.

#include <iostream> #include <iomaip> using namespace std; int main() { int x = 1; int y = 2; double result; cout << showpoint; result = static_cast <double> (x)/y; cout << "x is: " << x << ", "<< "y is: " << y << ", "<< "result is: " << result << endl; return 0; } Result is cast as a double. To produce a type double answer, you must type cast at least one of the int variables.

Consider the following snippet of code and answer the question: #include <stdio.h> int main() { int num ; //This is the "C" version of asking and receiving user input. printf("Please enter an integer between 1 and 100"); scanf("%d", &num); return 0; } Rewrite the above code using C++.

#include <iostream> using namespace std; int main() { int num; cout << "Please enter an integer between 1 and 100" << endl; cin >> num; return 0; }

What denotes the address location of a pointer?

&ptr return the address, not the value of what pointer is pointing to

Describe what * and & do regarding pointers in C.

* dereferences and must be used when you want to access or change the VALUE the pointer is pointing to & initializes and must be used when you want to access or change the ADDRESS the pointer is pointing to

Consider the following program: #include <stdio.h> int main() { int i = 5; int j = 6; int k = 7; int *ip1 = &i; int *ip2 = &j; int **ipp = &ip1; printf("**ipp is:%d\n",**ipp); *ipp = ip2; printf("**ipp is: %d\n", **ipp); *ipp = &k; printf("*ipp = &k is %d\n", **ipp); return 0; } What is the output?

**ipp is: 5 **ipp is: 6 *ipp = &k is 7

When using double pointer **pptr, how would you assign the fourth element of an array to it?

*pptr = array[3];

int values[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int *ptr = values; printf("*ptr + 3 = %i\n", *ptr + 3); What is the output and why?

*ptr + 3 = 4 *ptr dereferences the value at array[0], then adds 3

When and why should you pass const variables to functions?

- when you don't want the value to be changed by the function - because it prevents value from being overwritten

What are the two different ways to dereference pointers to class objects or structs?

1) (*ptr).classObject = 10; 2) ptr -> classObject = 10;

TRUE/FALSE Consider the following array and determine if the definition and initialization is valid. int matrix[5] = {1,8,9,1,5,4}; Mark true if this is valid and false if it is invalid.

FALSE there are 6 elements, it can only hold 5

In class I discussed several reasons you would want to use pointers. In no more than 2 sentences each, describe 2 of these reasons.

1) Gives the ability to work with specific memory locations 2) Necessary for programming microcontrollers 3) In C we must use pointers with concepts such as arrays and strings 4) Allows us to effectively represent complex data structures 5) Change values passed as arguments to functions 6) Optimization of programs - pass pointer to a function does not have to duplicate the data

Consider the following code: Class A { public: int add(int a, int b); }; Explain the two ways to make the above function called add an inline function.

1) implement the function inside the class declaration 2) implement the function outside the class declaration using the keyword inline

What are the two access specifiers of OOP?

1) private 2) public

Consider the following code. #include <stdio.h> int main() { int a = 112; int b = -1; float c = 3.14; int *d = &a; float *e = &c; char arr[] = "Yvon"; //int *ptr; //*ptr = 12; printf("%i, %i, %f, %f, %s\n", a, *d, c, *e, arr); return 0; } Write the output of this program.

112, 112, 3.140000, 3.140000, Yvon

What does the code after the following constructor do and what is it called? Date::Date(int m, int d, int y) : month(m), day(d), year(y) { }

It is an initialization list and it initializes the class variables to the instance variables at the same time the constructor is called.

TRUE/FALSE Consider the following array and determine if the definition and initialization is valid. int matrix[10] = { 0,0,1,0,0,1,0,0,1,1}; Mark true if this is a valid declaration and initialization of an array and false if it is not valid.

TRUE

TRUE/FALSE If you add to a vector that is already full, the vector will automatically increase its size to accommodate the new data value.

TRUE

TRUE/FALSE Objects are instances of a class. They are created with a definition statement after the class has been declared.

TRUE

Consider the following code. What is the output. // This program demonstrates the vector size, // push_back, and pop_back member functions. #include <iostream> #include <vector> using namespace std; int main() { vector<int> values; values.push_back(1); values.push_back(2); values.push_back(3); cout<< "The size of values is " << values.size() << endl; cout << "Popping a value from the vector...\n"; values.pop_back(); cout << "The size of values is now " << values.size() << endl; cout << "Popping a value from the vector...\n"; values.pop_back(); cout << "The size of values is now " << values.size() << endl; cout << "Popping a value from the vector...\n"; values.pop_back(); cout << "The size of values is now " << values.size() << endl; return 0; }

The size of values is 3 Popping a value from the vector... The size of values is now 2 Popping a value from the vector... The size of values is now 1 Popping a value from the vector... The size of values is now 0

Which of the following represent valid declarations of a 2D array? a. int abc[2][2] = {2,4,6,8}; b. int abc[ ][2] = {1,2,3,5}; c. int abc[ ][ ] = {3,5,7,9}; d. int abc[2][ ] = {5,10,15,20};

a. int abc[2][2] = {2,4,6,8}; AND b. int abc[ ][2] = {1,2,3,5};

When working with pointers, if I want to get the value of what a pointer is pointing to I need to use the * . This process is called ______________.

dereferencing

A memory leak occurs if no-longer-needed dynamic memory is not freed. The memory is unavailable for reuse within the program. What can you do to correct this problem?

free all dynamically allocated memory by calling delete delete [ ] arrayptr;

In C++ write a snippet of code using getline( ) to receive a string from a user and store in a type string variable named var.

getline(cin, var);

Write code that verifies a file opened successfully, if it fails inform the user.

if(!inputFile) { cerr << "Error opening data file" << endl; }

What C++ IO stream class do you use for input and output of files?

ifstream ofstream

In "C" we use FILE *fp = fopen("file.txt", "r") to open a file to read. (input) Write the necessary code to open an input file for reading in C++.

ifstream inputFile; inputFile.open("file.txt");

Declare and dynamically allocate memory for an integer array, then release the allocated memory.

int *array; array = new int[20]; delete [ ] array;

Create a pointer of type int called arrayptr.

int *arrayptr;

Create a pointer of type int called arrayptr and dynamically allocate memory for 100 integers

int *arrayptr; arrayptr = new int[100];

Declare an integer pointer initialized to point at the first element in an array.

int *ptr = &array[0];

Consider this snippet of code: int *arrayptr; arrayptr = new int[100]; Write the code to set the value of the memory allocated above to consecutive odd numbers starting with 1. This is not an entire program just a loop that sets the value of memory pointed to by arrayptr. The values should be 1, 3, 5, 7, ..... etc

int neg = 1; for (int i = 0; i < 100; i++) { arrayptr[ i ] = neg; neg = neg + 2; }

Consider the following snippet of code: int num; printf("Enter a number that will represent the number of ints you want to malloc \n"); scanf("%d", &num); Write the code necessary to malloc the amount of memory needed for num. Once you are done with the memory, you should return it to the OS. Write the code needed to return the memory to the OS.

int ptr = (int *) malloc(num * sizeof(int)); free(ptr);

Consider the following snippet of code: char *str = "Fall Semester"; Given: str = str + 7; What is printed when the following instruction is executed? printf("%s\n", str);

mester

What is the code for calling a vector member function?

nameVector.push_back( );

In "C" we use FILE * fp = fopen("file.txt", "w") to open a file to write to. (output) Write the necessary code to open an output file for writing in C++.

ofstream outputFile; outputFile("file.txt");

int *ptr = array; Make the pointer point to the third element in the array.

ptr = &array[2];

int values[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int *ptr = values; printf("ptr = %i\n", *ptr); What is the output and why?

ptr = 1 *ptr dereferences (says return value at the address pointed to)

int values[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int *ptr = values; int *ptr2 = ptr + 3; printf("ptr2 = %i\n", *ptr2); What is the output and why?

ptr2 = 4 ptr2 was initialized to address contained in ptr (no * to dereference value) + 3. ptr points to element 0, so add 3 and you have array[3], which is 4

A pointer is dangling if it contains the address of memory that has been freed by a call to delete. In the notes, I listed the solution to a dangling pointer. What is the solution?

set the pointer to 0 or NULL ptr = 0;

Define a struct called Date, with three int variables named month, day, and year. Created two instances of type struct called today and tomorrow. Then assign today's date as October 14, 2016 and tomorrow's date as the next day.

struct Date { int month; int day; int year; } Date today, tomorrow; today.month = 10; today.day = 14; today.year = 2016; tomorrow.month = 10; tomorrow.day = 15; tomorrow.year = 2016;

int values[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int *ptr = values; What is is specific element is ptr pointing to?

values[0]

What is the code for initializing an array?

vector <type> name(20, 0); (20 elements initialized to 0)


Kaugnay na mga set ng pag-aaral

Unit 1: Texas Contract Law Questions

View Set

Chapter 1 - The Aging Population

View Set

AP European History Final Study Guide

View Set

AP Biology Review 20 DNA Technology & Genomics BIOTECH

View Set