COMSC Midterm 1

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Suppose a program has the following array defined: int arr[12]; Which of the options below uses pointer notation to store 99 in arr[6[? Group of answer choices &(arr+6) = 99 &arr+6 = 99; *(arr+6) = 99; *arr+6 = 99;

*(arr+6) = 99;

In a UML diagram, which of the options below 1) depicts a function that can be accessed ONLY inside of the class code and 2) returns an integer? Group of answer choices +some_func(): int -some_func(): int -some_func(some_number: int) +some_func(some_number: int)

-some_func(): int

Suppose we have the following stored in RAM (memory): Where for each row in the table, the address column lists a hexadecimal memory address, and the value column lists the value that is stored at that memory location. For example, the value 55 is stored at memory location 0X1234ABCD. What is the output of the following cout statements? cout<<&ptr<<endl; cout<<ptr<<endl; Group of answer choices 55 0X1234ABCD 0XFABC7890 55 0XFABC7890 0X1234ABCD 0X1234ABCD 0XFABC7890

0XFABC7890 0X1234ABCD

1. The unary operator _ determines the size of any data type 2. This operator returns the number of _ that the data type takes up in memory Group of answer choices 1. size 2. bytes 1. size 2. bits 1-. sizeof 2-. bytes 1. sizeof 2. bits

1-. sizeof 2-. bytes

#include <iostream> #include "Tree.h" using namespace std; void tree_func(); Tree A; Tree B; int main() { { tree_func(); static Tree C; } Tree D; return 0; } void tree_func() { static Tree E; { Tree F; } } What is the order in which the objects are destroyed? _ is destroyed first. _ is destroyed second. _ is destroyed third. _ is destroyed fourth. _ is destroyed fifth. _ is destroyed sixth. 1. F 2. E 3. D 4. C 5. B 6. A 1. F 2. D 3. C 4. E 5. B 6. A 1. E 2. F 3. C 4. D 5. A 6. B 1. D 2. C 3. F 4. E 5. B 6. A

1. F 2. D 3. C 4. E 5. B 6. A

he & operator is used to dereference a pointer The * operator is the address operator Group of answer choices 1. True 2. False 1. False 2. False 1. True 2. True 1. False 2. True

1. False 2. False

Suppose a program has the following College class: class College { std::string college_name; College(std::string c); }; How would the constructor be represented in a UML diagram? Group of answer choices College +<<constructor>>(c: string) +<<constructor>> College(c: string <<constructor>> +College(c: string) +College <<constructor>>(c: string)

<<constructor>> +College(c: string)

class Car { Public: Car(const string &ma, const string &mo, int ye) { make = ma; model = mo; year = ye; cout<<"CAR CON\n"; } ~Car() { cout<<"CAR DES\n"; } private: string make; string model; int year; }; class Person { Person(const string &n, int a, const Car&c) { name = n; age = ag; car= c; cout<<"PERSON CON\n"; } ~Person() { cout<<"PERSON DES\n"; } string name; int age; Car car; }; int main() { Car c("Toyota", "Corolla", 2017); Person p("James Bond", 45, c); } What is the output of this program? Correct Answer A-) CAR CON PERSON CON PERSON DES CAR DES CAR DES D-) CAR CON PERSON CON PERSON DES CAR DES C-) CAR CON CAR CON PERSON CON PERSON DES CAR DES CAR DES B-) CAR CON CAR CON PERSON CON PERSON DES CAR DES

A-) CAR CON PERSON CON PERSON DES CAR DES CAR DES

All of the following options below are valid arithmetic operations that can be performed on pointers EXCEPT: Group of answer choices Add an integer to a pointer (ptr + i) Subtract an integer from a pointer (ptr - i) Add one pointer to another pointer (ptr1 + ptr2) Subtract one pointer from another pointer (ptr1 - ptr2)

Add one pointer to another pointer (ptr1 + ptr2)

Which of the following options below correctly describes the principle of least privilege? All member variables and all member functions of a class should have private visibility unless it can be proven that the member needs public visibility. All member variables and all member functions of a class should have public visibility unless it can be proven that the member needs private visibility. All member variables of a class should have private visibility and all member functions of a class should have public visibility. All member variables and all member functions of a class should have private visibility.

All member variables and all member functions of a class should have private visibility unless it can be proven that the member needs public visibility.

A variable that is declared inside of a class definition (but NOT inside of any function) is known as a/an what? Local variable Object Attribute Parameter variable

Attribute

The destructors for all objects are invoked when the main function goes out of scope. While the destructor is running the memory held by the object is released. C-) 1. True 2. False B-) 1. False 2. False D- 1. False 2. True A-) 1. True 2. True

B-) 1. False 2. False

Which of the options below is equivalent to this->y? A-) *(this.y) B-) (*this).y C-) (&this).y D-) &(this.y)

B-) (*this).y

Constructors are called for _ objects first. A-) constant D-) local B-) global C-) static

B-) global

What is the name of a constructor that calls another constructor? C-) Delegating constructor B-) Default constructor D-) Explicit constructor A-) Overloaded constructor

C-) Delegating constructor

Which of the following is like a blueprint or template that you can use to create things from? Function Class Constructor Object

Class

Suppose there is a class named College. The prototypes for functions and constructors would usually be stored in which file? Group of answer choices College.h College.exe College.cpp College.obj

College.h

Suppose there is a class named Computer, which can be used to create objects that store information about a particular computer. This class is developed by the class developer. A different person, the application developer, needs to use the Computer class in their application. However, as described above, the application developer did NOT develop the class - they are just using it. The application code is in a file named Main.cpp. In this scenario, the application developer would normally have access to all of the following files EXCEPT: Group of answer choices Computer.cpp Main.exe Computer.h Main.obj

Computer.cpp

Which of the options below grants the minimum (least) access? Group of answer choices Nonconstant pointer to constant data Constant pointer to constant data Nonconstant pointer to nonconstant data Constant pointer to nonconstant data

Constant pointer to constant data

Suppose we have the following declaration: int arr[5] = {1, 2, 3, 4, 5}; In class we discussed the fact that the name of an array (in this case arr) is actually a special kind of pointer. What type of pointer is arr? Group of answer choices Constant pointer to constant data Nonconstant pointer to constant data Constant pointer to nonconstant data Nonconstant pointer to nonconstant data

Constant pointer to nonconstant data

int x = 7; int * const ptr = &x; Which of the options below accurately describes ptr? Group of answer choices Constant pointer to nonconstant data Constant pointer to constant data Nonconstant pointer to constant data Nonconstant pointer to nonconstant data

Constant pointer to nonconstant data

Which of the following can use a member initializer list? Group of answer choices Get function Set function Constructor Object

Constructor

Another name for a get function in a class is what? Mutator Accessor Constructor Destructor

Correct Answer Accessor

What is the default access specifier in a class? Public Global Private Local

Correct Answer private

Suppose a program has the following class and main function: NOTE: Access specifiers have been omitted here because there is another question on this quiz about them. class College { string name; string get_name() { return name; } void set_name(string n) { name = n; } }; int main() { College college_obj; return 0; } On which line in the above program would it be appropriate to insert the word "const" ? string name; College college_obj; void set_name(string n) string get_name()

Correct Answer string get_name()

Suppose a program has the following Course class: class Course { private: string instructor; string textbook; }: And suppose there is only going to be ONE constructor for this class to handle all initializations. Which of the following options below is the correct prototype for the constructor of this class? Course(string = "", string = "", string = ""); Course(); Course(string = ""); Course(string = "", string = "");

Course(string = "", string = "");

Suppose a program has the following class: class Date { private: int day; int month; int year; public: void set_date(int, int, int); void set_day(int); void set_month(int); void set_year(int); }; void Date::set_date(int d, int m, int y) { set_day(d); set_month(m); set_year(y); } void Date::set_day(int d) { if(d >=1 && d<=31) day = d; else throw invalid_argument("invalid day"); } void Date::set_month(int m) { if(m >=1 && m<=12) month = m; else throw invalid_argument("invalid month"); } void Date::set_year(int y) { if(y >=1 && y<=2017) year = y; else throw invalid_argument("invalid year"); } And the following driver program: int main() { Date d; try { d.set_date(32, 13, 2018); } catch (invalid_argument &e) { cout<<"EXCEPTION: "<<e.what() << endl; } } What is the output of this program? EXCEPTION: invalid day All of the above EXCEPTION: invalid month EXCEPTION: invalid year

EXCEPTION: invalid day

One of the concepts we learned about early in chapter 3 is data hiding. Which of the terms below refers to data hiding? Instantiation Encapsulation Polymorphism Inheritance

Encapsulation

int x=5, *ptr = nullptr; ptr = &x; ptr = 100; // Store 100 in x cout << x << endl; What is/are the error(s) in the code shown above? Group of answer choices Line 2 should be *ptr = &x; Line 3 should be *ptr = 100; Line 2 should be *ptr = &x; Line 2 should be *ptr = *x; Line 3 should be *ptr = 100; Line 3 should be *ptr = 100;

Line 3 should be *ptr = 100;

All of the following statements below about local variables are true EXCEPT: A local variable cannot be accessed outside the function in which it's declared. When a function terminates, the values of its local variables are lost. Local variables are declared in the header of a function, inside the parenthesis. A local variable must be declared before it can be used in a function.

Local variables are declared in the header of a function, inside the parenthesis.

1.Logically (i.e., as a concept), objects consist of BLANK 2.Physically (i.e., in memory), objects consist of BLANK Group of answer choices Both member variables and member functions Only member variables Both member variables and member functions Both member variables and member functions Only member variables Both member variables and member functions Only member variables Only member variables

Only member variables Only member variables

Which of the options below shows the correct function prototype AND corresponding function call for a function that is passed a pointer? Group of answer choices Prototype: int some_func(int *); call: some_func(&num); Prototype: int some_func(int &); call: some_func(*num); Prototype: int some_func(int *); call: some_func(*num); Prototype: int some_func(int &); call: some_func(&num);

Prototype: int some_func(int *); call: some_func(&num);

Which of the following can be used to initialize an object when it is declared in an application program? Group of answer choices Public Member Function Private Member Function Public Constructor Private Constructor

Public Constructor

Quiz 4 start Suppose there are two pointers, ptr1 and ptr2. Both are pointers to integers. if(ptr1 == ptr2) cout<<"Same A"<<endl; else cout<<"Different A"<<endl; if(*ptr1 == *ptr2) cout<<"Same B"<<endl; else cout<<"Different B"<<endl; All of the following combinations of output messages are possible EXCEPT: Group of answer choices Different A Same B Same A Same B Same A Different B Different A Different B

Same A Different B

What is the relationship between throw, try, and catch? Group of answer choices Throw handles an exception Try contains code that could possibly cause an exception Catch signals that an exception has occurred Throw signals that an exception has occurred Try contains code that could possibly cause an exception Catch handles an exception Throw handles an exception Try signals that an exception has occurred Catch contains code that could possibly cause an exception Throw signals that an exception has occurred Try handles an exception Catch contains code that could possibly cause an exception

Throw signals that an exception has occurred Try contains code that could possibly cause an exception Catch handles an exception

A UML diagram for a class consists of three parts: the top part, the middle part, and the bottom part. What are shown in the middle part of a UML diagram? UML Diagram Top Part Middle Part Bottom Part Objects Constructors Variables Functions

Variables

The size of an object depends on the amount of memory required to store the class's what? Group of answer choices Variables Constructors All of the above Functions

Variables

Suppose there is a Class named Car, which has a member function named set_make that can be used to set the make of a car object to a particular value (e.g. "Toyota", Ford", "Hyundai", etc.): NOTE: Access specifiers have been omitted here because there is another question on this quiz about them. class Car { string make; void set_make(string m) { make = m; } string get_make() { return make; } }; Then in main we have the following code: int main { string value = "Toyota"; Car car_obj; ................ return 0; } Which line of code could be inserted in main below the line Car car_obj; to call the set_make function for the car_obj object with the argument "Toyota"? car_obj.set_make(value); set_make(car_obj->value); set_make(car_obj.value); car_obj->set_make(value);

car_obj.set_make(value);

Suppose a program has the following class: class Computer { private: string gpu; public: void set_gpu(string g) { gpu = g; } }; And the following driver program: int main { Computer comp; Computer &compref = comp; Computer *compptr = &comp; } For which of the options below are BOTH statements 1 AND 2 correct for setting the gpu member variable to the string "NVIDIA GeForce GTX 1080" for the comp object? Group of answer choices compref->set_gpu("NVIDIA GeForce GTX 1080"); compptr.set_gpu("NVIDIA GeForce GTX 1080"); compref->set_gpu("NVIDIA GeForce GTX 1080"); compptr->set_gpu("NVIDIA GeForce GTX 1080"); compref.set_gpu("NVIDIA GeForce GTX 1080"); compptr->set_gpu("NVIDIA GeForce GTX 1080"); compref.set_gpu("NVIDIA GeForce GTX 1080"); compptr.set_gpu("NVIDIA GeForce GTX 1080");

compref.set_gpu("NVIDIA GeForce GTX 1080"); compptr->set_gpu("NVIDIA GeForce GTX 1080");

In a UML diagram, which of the options below 1) Describes a function that can be accessed anywhere - both inside of the class and outside of the class (such as in main) and 2) Returns an integer? +some_name(): int -some_name(some_value: int) -some_name(): int +some_name(some_value: int)

correct answer +some_name(): int

double val = 55.2; double * ptr = &val; For which of the options below will BOTH cout statements print out the ADDRESS of val? Group of answer choices cout<<ptr<<endl; cout<<*val<<endl; cout<<*ptr<<endl; cout<<*val<<endl; cout<<ptr<<endl; cout<<&val<<endl; cout<<*ptr<<endl; cout<<&val<<endl;

cout<<ptr<<endl; cout<<&val<<endl;

Which of the following options below will print out the addresses of all of the elements of an array? Group of answer choices for(int i=0; i<SIZE; i++) cout<<*(arr+i)<<endl; for(int i=0; i<SIZE; i++) cout<<&arr[i]<<endl; for(int i=0; i<SIZE; i++) cout<<*arr[i]<<endl; for(int i=0; i<SIZE; i++) cout<<&(arr+i)<<endl;

for(int i=0; i<SIZE; i++) cout<<&arr[i]<<endl;

Which of the options below will print out all of the values in the array (1, 2, 3, 4 and 5)? const int SIZE = 5; int arr [5] = {1, 2, 3, 4, 5} Group of answer choices int * ptr = &arr; for(int i=0; i<SIZE; i++) { cout<<*ptr<<endl; (*ptr)++; } int * ptr = &arr; for(int i=0; i<SIZE; i++) { cout<<*ptr<<endl; ptr++; } int * ptr = arr; for(int i=0; i<SIZE; i++) { cout<<*ptr<<endl; (*ptr)++; } int * ptr = arr; for(int i=0; i<SIZE; i++) { cout<<*ptr<<endl; ptr++; }

int * ptr = arr; for(int i=0; i<SIZE; i++) { cout<<*ptr<<endl; ptr++; }

Quiz 5 start int arr[5] = {10, 15, 20, 25, 30}; int *ptr = arr; Which of the options below uses pointer subscript notation? Group of answer choices ptr[2] = 1337; *(ptr+2) = 1337; arr[2] = 1337; *(arr+2) = 1337

ptr[2] = 1337;

Suppose a program has the following College class: class College { std::string college_name; void set_college_name(std::string c); std::string get_college_name(); }; The functions will be implemented in another file outside of the class definition. Which of the options below is the correct way to implement the set_college_name function? Group of answer choices void::College set_college_name(string c) { college_name = c; } void set_college_name::College(string c) { college_name = c; } void::set_college_name College(string c) { college_name = c; } void College::set_college_name(string c) { college_name = c; }

void College::set_college_name(string c) { college_name = c; }

The doubleVal function is supposed to be passed a pointer to an integer, and it doubles the value of the number. Which of the options below is the correct implementation of the doubleVal function? Group of answer choices void doubleVal(int *ptr) { *ptr *= 2; } void doubleVal(int *ptr) { &ptr *= 2; } void doubleVal(int &ptr) { &ptr *= 2; void doubleVal(int &ptr) { *ptr *= 2; }

void doubleVal(int *ptr) { *ptr *= 2; }

Suppose a program has the following House class (implementation details are omitted): class House { int num_of_bedrooms; House(); House(int b); void set_num_of_bedrooms(int b); int get_num_of_bedrooms(); }; And for each House object that is created, it needs to be guaranteed that the num_of_bedrooms is at least 1. In other words, input validation logic is needed to make sure that num_of_bedrooms is always at least 1. If code in the application program (i.e., in main) attempts to set num_of_bedrooms to less than 1, the input validation logic will detect this and simply set num_of_bedrooms to 1, and display an error message to the user that the number of bedrooms must be at least 1. Where in the House class would it be appropriate to place this input validation logic? Group of answer choices House(int b) int get_num_of_bedrooms() House() void set_num_of_bedrooms(int b)

void set_num_of_bedrooms(int b)

A swap function works as follows: It takes in two pointers as arguments. Both pointers point to integers. The values of the two pointers are swapped. For example if num1 is initially 5 and num2 is initially 10, then num1=10 and num2=5 after the swap function has finished executing. NOTE: Assume the following code is in main: int num1=10, int num2=5; swap(&num1, &num2); Which of the options below is the correct implementation of the swap function? Group of answer choices void swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } void swap(int *x, int *y) { int temp; temp = &x *x = &y; *y = temp; } void swap(int *x, int *y) { int *temp = nullptr; *temp = *x; *x = *y; *y = *temp; } void swap(int *x, int *y) { int *temp = nullptr; *temp = &x *x = &y; *y = &temp; }

void swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; }

Quiz 3 Start The default constructor takes _ argument(s) Group of answer choices zero (0) three (3) two (2) one (1)

zero (0)


Set pelajaran terkait