470 likes | 823 Views
c s150. Review for Exam_2 Thursday, April 18 th. Exam 2 – next Tuesday, You will need a blue scantron . 8 - arrays 9 - structs 10 - classes 12 - pointers 14 - exceptions 16 - searching/sorting/vectors 17 - linked lists . Given the following declaration: int j; int sum;
E N D
cs150 Review for Exam_2 Thursday, April 18th
Exam 2 – next Tuesday, You will need a blue scantron. 8 - arrays 9 - structs 10 - classes 12 - pointers 14 - exceptions 16 - searching/sorting/vectors 17 - linked lists
Given the following declaration: int j; int sum; double sale[10][7]; which of the following correctly finds the sum of the elements of the fourth column of sale? a. sum = 0; for (j = 0; j < 7; j++) sum = sum + sale[j][3]; b. sum = 0; for (j = 0; j < 7; j++) sum = sum + sale[j][4]; c. sum = 0; for (j = 0; j < 10; j++) sum = sum + sale[j][4]; d. sum = 0; for (j = 0; j < 10; j++) sum = sum + sale[j][3];
8 - arrays [ 0 ] - first [ 1 ] - second item [ 2 ] - third item [ 3 ] - fourth item ….. ……….. [ n ] - (size – 1) item
A collection of a fixed number of elements (called components) arranged in n dimensions (n>=1) is called a(n) ____. a. matrix c. n-dimensional array b. vector d. parallel array
In row order form, the ____. a. first row is stored first c. first column is stored first b. first row is stored last d. first column is stored last
Consider the following statement: double alpha[10][5];. The number of components of alpha is ____. a. 15 c. 100 b. 50 d. 150
Consider the following declaration: char str[15]; Which of the following statements stores "Blue Sky" into str? a. str= "Blue Sky"; b. str[15] = "Blue Sky"; c. strcpy(str, "Blue Sky"); strcpy("Blue Sky");
Which of the following correctly declares name to be a character array and stores "William" in it? a. char name[6] = "William"; b. char name[7] = "William"; c. char name[8] = "William"; d. char name[8] = 'William';
All components of an array are of the same data type. The array index can be any integer less than the array size. The statement int list[25]; declares list to be an array of 26 components, since the array index starts at 0. Given the declaration int list[20]; the statement list[12] = list[5] + list[7]; updates the content of the twelfth component of the array list. If an array index goes out of bounds, the program always terminates in an error.
Suppose list is a one dimensional array of size 25, wherein each component is of type int. Further, suppose that sum is an int variable. The following for loop correctly finds the sum of the elements of list. sum = 0; for (int i = 0; i < 25; i++) sum = sum + list;
What statement declares alpha to be an array of 25 components of the type int? a.intalpha[25]; c.int alpha[2][5]; b.int array alpha[25]; d.int array alpha[25][25]; OR a.int alpha[5]; c.intalpha[5][5]; b.int array alpha[25]; d.int array alpha[25][25];
What is the output of the following C++ code? int list[5] = {0, 5, 10, 15, 20}; int j; for (j = 0; j < 3; j++) cout << list[j] << " "; cout << endl;
A class is an example of a structured data type. In C++ terminology, a class object is the same as a class instance. The public members of a class must be declared before the private members. As parameters to a function, class objects can be passed by reference only.
The components of a class are called the ____ of the class. a. elements c. objects b. members d. properties
Is the following class definition correct in C++? class studentType { public void setData(string, double, int); private string name; };
Is the following class definition correct in C++? class studentType { public: void setData(string, double, int); void print() const; private: string name; double gpa; }
Is the following class definition correct in C++? studentType class { public: void setData(string, double, int); private: string name; };
Is the following class definition correct in C++? class studentType { public: void setData(string, double, int); private: string name; };
If a member of a class is ____, you cannot access it outside the class. a. public c. private b. automatic d. static A class and its members can be described graphically using a notation known as the ____ notation. a. OON c. UML b. OOD d. OOP
The word ____ at the end of the member functions in the above class clockTypespecifies that these functions cannot modify the member variables of a clockType object. a. static c. automatic b. const d. private
What is the name of the class? How many private members are in the class? What indicates private members in the class? What indicates public members in the class? What indicates protected members in the class?
In C++, the ____ is an operator called the member access operator. a. . c. :: b. , d. # clockType::print();
A class object can be ____. That is, it is created each time the control reaches its declaration, and destroyed when the control exits the surrounding block. a. static c. local b. automatic d. public
A class object can be ____. That is, it can be created once, when the control reaches its declaration, and destroyed when the program terminates. a. static c. local b. automatic d. public In C++, you can pass a variable by reference and still prevent the function from changing its value by using the keyword ____ in the formal parameter declaration. a. automatic c. static b. private d. const
Which of the followingis the correct prototype for a templatefunction (from lecture). template <class T> T maxThree(T x, T y, T z); template-class <functionT> T maxThree(T x, T y, T z); class <templateT> T maxThree(T x, T y, T z);
It’s Thursday Questions? More Questions!
A member function of a class that only accesses the value(s) of the data member(s) is called a(n) ____ function. a. accessor c. constructor b. mutator d. destructor OR a. getter c. safety b.setter d. member function
To guarantee that the member variables of a class are initialized, you use ____. initiators c. constructors b. mutators d. destructors • True or False: • The only built-in operations on classes are assignment and member selection
How many constructors are present in the class definition above? a. none c. two b. one d. three
true or false: Destructors are functions without any type. The destructor name is preceded by the character ‘ ~ ' A class can have only two destructors The destructor always has one parameter. Automatically executes when class object goes out of scope. Automatically executes when class constructor executes.
Abstraction - true or false,.. …. separates design details from usage …. Combines logical properties &implementation details … can NOT be applied to data … we implemented ADTs with structsAND classes. • …Classes &structsare abstract data types with the same capabilities …Abstraction is rarely used and covered for historical purposes only
ADTs,…structs,…classes,.. (true or false) If all member variables are public & there are no member functions - use what? You cannot replace a class with a struct, and get the same capability. In C++, a struct cannot include member functions, constructors, and destructors By default, members of a struct are public
Information Hiding True or False: Information hiding involves hiding the details of the operations on the data An interface fileextension is __________ ? An interface file is also called a __________ ? An interface file contains __________ ? the specification details An interface file contains __________ ? pre and/or postconditions imp details An implementation file contains __________ ? An implementation file extension is __________ ?
Information Hiding - true or false Implementation file must include header file via include statement In include statement, user-defined header files are enclosed in single quotes In include statement, system-provided header files are enclosed between angular brackets Preconditions specify what must be true before the function is called. (what about postconditions?)
Exception Handling true or false When an exception is thrown, the function can - do nothing? - can partially process the exception and throw the same exception. - can partially process the exception and throw a new exception - can throw a new exception - must terminate the program immediately
Exception Handling • Which of the following are true statements? • Exception: a desirable event detectable during program execution • You can create your own exception classes • Catch block specifies type of exception it can catch and contains an exception handler • If no exceptions are thrown in a try block, all catch blocks for that try block are ignored • Data type of catch block parameter specifies type of exception that catch block can catch
Pointers,..ect Pointer variables contain __________ Declare a pointer variable with an asterisk, *, between the data type and the variable Address of operator (&)returns the address of its operand Unary operator * is the dereferencing operator, also known as the indirection operator. The member access operator (->) accesses the object component pointed to by a pointer
Pointers,..ect Which of the following is not a true statement? Which of the following isa true statement? Dynamic variables: - created during execution - created using new operator - deallocatedusing delete operator - created using pointers in c++
Pointers,..ect Which of the following aretrue statements? A dynamic variable cannot be accessed directly, because it is unnamed The value of one pointer variable can be assigned to another Two pointer variables of same type can be compared for equality
Pointers,..ect Which of the following aretrue statements? Value of one pointer variable can be subtracted from another pointer variable Integer values can be added and subtracted from a pointer variable C++ makes pointer arithmetic foolproof. Pointer arithmetic can be very dangerous.
Searching, Sorting, Vectors,..ect Sequential search algorithm: It is very efficient for large lists On average, number of key comparisons is equal to half the size of the list Does not assume that the list is sorted Searches by repeatedly dividing the list in half. It is not very efficient for large lists
Searching, Sorting, Vectors,..ect Binary search algorithm: Much faster than a sequential search Less efficient than sequential search algorithm List must be sorted Also called “Divide and conquer” Searches by repeatedly dividing the list in half.
Searching, Sorting, Vectors,..ect Vector Type: Implements a list Increase/decrease in size during program execution Must specify the type of object the vector stores Instances of the vector object can be created.
Linked Lists Helps address the following problems with an array Array size is fixed Unsorted array: searching for an item is slow Sorted array: insertion and deletion is slow because it requires data movement
More on Tuesday! (in the form of an exam) Questions?