CS2250 C++ Midterm Review

Ace your homework & exams now with Quizwiz!

Which of the following statements must you include in your program if you want to input data from the keyboard or output data to the screen? Give the best answer according to the ISO C++ Standard. #include <cin_cout> #include <iostrem.h> #include <iostream> #include <fstream.h> #include <iostream.h>

#include <iostream>

Assuming that the user types in the following " ab" (that's a space space a b). Also assume that the following code is executed: char input; cin.get(input); Which of the following is stored in the value input? The values in parentheses explain each possibility but should not be considered part of the answer. a (space) b ab

(space)

consider the following statements: int *p; int i, k; i = 142; k = i; p = &i; Which of the following statements changes the value of i to 143? *k = 143; k = 143; p = 143; *p = 143;

*p = 143;

What is the output of the following code? class Test{ int value; public: Test (int v = 0) { value = v; } int getValue() const { return value;} }; int main() { const Test t; cout << t.getValue(); return 0; } compiler error garbage value 0

0

Consider the following code: void swap (int *a, int *b) { int *temp; temp = a; a = b; b = temp; } int main() { int i = 0, j = 1; swap (&i, &j); cout << i << " " << j << endl; } 1 1 1 0 0 0 Nothing Compiler error 0 1

0 1

What is the output of the following code: int x = 10; void fun() { int x = 2; { int x = 1; cout << ::x << endl; } } int main () { fun(); return 0; } 1 2 compiler error 10

10 If a Scope Resolution Operator is placed before a variable name then the global variable is referenced.

What is the output of the following program? int main() { int num[5]; int* p; p = num; *p = 10; p++; *p = 20; p = &num[2]; *p = 30; p = num +3; *p = 40; p = num; *(p+4) = 50; for (int i = 0; i < 5; i++) cout << num[i] << " , "; return 0; } compile error 10, 20, 30, 40, 50 50, 50, 50, 50, 50, 10, 20, 30, 40, 50, RunTime error

10, 20, 30, 40, 50, The values are being assigned to the array, and dereferenced to print them

What does the following code print? int foobar( int& a ) { a += 4; return a; } int main () { int a = 7; foobar(a) cout << a << endl; return 0; } 4 7 Nothing, it throws a runtime error 11 Nothing, it does not compile

11

Assume all necessary libraries are included. What does the following code print? int foobar( int& a, int b ) { a += b; b+= a; return a + b; } int main () { int a = 3; int b = 5; foobar(a, b) if (foobar( a, b ) > a + b) cout << a << endl; else cout << b << endl; return 0; } 13 34 3 5 8

13

What is the output of the following program? int main() { int ar[] = {4, 5, 6, 7}; int* p = (arr + 1); cout << *arr + 10; return 0; } 12 runtime error 15 14 compiler error

14 'arr' works as a pointer to the array. So 'arr' points to the first location in the array. Similarly 'arr+1' points to the second location in the array, and so on. Therefore, *arr accesses the first value in the array which is 4 and asdds it to 10. This gives 14 as output.

Consider this piece of code: void mysterious(int i, int &k) { i = 1; k = 2; } int main () { int x = 0; mysterious (x, x); cout << x << endl; return 0; } What is the value of x that gets printed by the main? 0 2 1 None of these

2

Assume all necessary libraries are included. What does the following code print? int foobar(int a, int b) { if (a < b) return a; else return b; } int main() { int a = 3; int b = 5; cout << foobar(b, a) << endl; return 0; } 3 5 Nothing, there is a compiler error Nothing, there is a run-time error 8

3

How many blank lines are output by the following statement? cout << "Happy Holiday" << " January 1!" << endl << endl << endl << endl;

3

What does the following code print? int foobar( int a, int b ) { if ( a < b ) return a; else return b; } int main () { int a = 3; int b = 5; foobar(a) cout << foobar(b, a) << endl; return 0; } 8 5 Nothing, compiler error 3 Nothing, run-time error

3

int count = 0; cin >> value; while (value < 0 && count < 5) { cout << "Value must be non negative" << "enter the value again." << endl; cin >> value; count++; } cout << "Count: " << count << endl; What is the value of count displayed after the following loop completes? Assume the user types in the following values: -1 -2 -1 0 3

3

What does the following code print? int foobar( int& a, int b ) { a += b; b += a; reutrn a + b; } int main() { int a = 3; int b = 5; foobar (a, b); cout << foobar(b , a) << endl; return 0; } 19 Nothing, it does not compile 34 10 6

34

What is the output of the following code? int* foo(int*); int main() { int x[5] = { 1, 2, 3, 4, 5 }; int i, *p; p = foo(x); for (i = 0; i < 5; i++) cout << *(p+i) << " "; return 0; } int* foo(int* p) { int i; for (i = 0; i < 2; i++) { int temp = *(p+i); *(p+i) = *(p+4-i); *(p+4-1) = temp; } return p; } 5 4 3 2 1 1 2 3 4 5 runtime error compiler error

5 4 3 2 1

What is the output of the following? int main() { int x[5] = { 1, 2, 3, 4, 5 }; int* p = x; int i; for (i = 0; i < 2; i++) { int temp = *(p+i); *(p+i) = *(p+4-i); *(p+4-1) = temp; } for (i = 0; 0 < 5; i++) cout << x[i] << " "; return 0; } compile error 1 2 3 4 5 5 4 3 2 1 Address of the elements Runtime error

5 4 3 2 1

What does the following code print? int foobar( int a ) { a += 4; return a; } int main () { int a = 7; foobar(a) cout << a << endl; return 0; } 4 7 Nothing, it throws a runtime error 11 Nothing, it does not compile

7

What is the output of the following code? class X { private: static const int a = 76; public: static int getA() {return a;} }; int main() { cout << X::getA() << endl; return 0; } compile error 76 garbage value

76 Generally, it is not allowed to initialize data members in C++ class declaration, but static const integral members are treated differently and can be initialized with declaration.

If CountedQue is derived from QueType, in the following code snippet, what operator goes in the blank? void CountedQue::Dequeue(ItemType& item) { length--; QueType________________Dequeue(item); -> a space only, no operator is necessary :: : .

::

Which of the following operators is the stream insertion operator? >> = <> << //

<<

Assume that we have a function Foo() defined as follows (what is does is irrelevant): void Foo(T a) { //code } In main, Foo is invoked as follows: Foo(b); Which of the following are true about the relationship between the type of the formal parameter a and the type of the actual parameter b? The formal parameter may be an object from a derived class of the actual parameter. Actual and corresponding formal parameter must be identical Actual parameter may be an object from a derived class of the formal parameter. All of the statements are true None of the statements are true.

Actual parameter may be an object from a derived class of the formal parameter.

Suppose that a C++ class D is derived from a base class B. Class B has a public member function Func() that is NOT declared to be virtual, and class D redefines its own version of Func(). At execution time, suppose that a D object is passed to the following function as it's parameter: void DoSomething (B& x) { x.Func(); } Within DoSomething function, whos version of Func() is called? D's version neither B's nor D's version both B's and D's version B's version

B's version

What is the result of the following code? class Test { int value; public: Test(int v = 0); ); Test::Test(int v) { value = v; ) int main(){ Test t[100]; return 0; } Compiler error Builds a Test item whose value is set to 100 Builds 100 items of type Test

Builds 100 items of type Test

What is the output of the following program? class Point { Point() { cout << "Constructor called"; } }; int main () { Point t1; return 0; } Nothing Runtime Error Compiler Error Constructor called

Compiler Error

What is the output of the following code? Class Test { public: Test(Test &t) { } Test() { } }; Test fun() { cout << "fun() called"; Test t; return t; } int main() { Test t1; Test t2 = fun(); return 0; } Nothing Compiler Error: Because copy constructor argument is non-const fun() called

Compiler Error: Because copy constructor argument is non-const

What is the output of the following code? Class Point{ int x; public: Point(int x) { this->x = x;} Point(const Point p) { x = p.x;} int getX() {return x;} }; int main() { Point p1(10); Point p2 = p1; cout << p2.getX(); return 0; ) 10 none of the above garbage value compiler error

Compiler Error: P must be passed by reference Objects must be passed by reference in copy constructors. Compiler checks for this and produces compiler error if not passed by reference.

What is the output of the following program? int main() { int a = 10. *pa, &ra; pa = &a; ra = a; cout << "a=" << ra; retrun 0; } runtime error 10 A memory address compiler error

Compiler error Reference variables are different from pointers. Pointers can be initialized as well as assigned, but references can only be initialized. Here, 'ra' reference variable is being assigned the address of 'a' instead of initializing it when it is declared. This throws a compiler error - 'ra' declared as a reference but not initialized.

What is output by the following code? class P { public: virtual void show ( cout << "Hello" << endl; ) = 0; ); class Q: public P { int x; }; int main (void) { Q q; return 0; } Nothing Compiler Error Hello

Compiler error We get the error because we can't create objects of abstract classes. P is an abstract class as it has a pure virtual mehtod. Class Q also becomes bastract because it is derived from P and it doesn't implement show().

What is the output of the following code? class Point { int x, y; public: Point(int i = 0, int j = 0) {int x = i; y = j; } int getX() const { retrun x;} int getY() { return y; } }; int main() { const Point t; cout << t.getX() << " "; cout << t.gety(); return 0; } garbage values 0 0 Compiler error in line cout << t.getX() << " "; Compiler error in line cout << t.gety();

Compiler error in line cout << t.gety(); A const object can only call const functions

What is the output of the following program? Class Point { public: Point() { cout << "Constructor Called"; } }; int main() { Point t1, *t2; return 0; } Constructor called Nothing runtime error Constructor called Constructor called compiler eror

Constructor called

Which of the following are NOT provided by the compiler by default? Copy Destructor Destructor Copy Constructor Zero-argument Constructor

Copy Destructor

Suppose that a C++ class D is derived from a base class B. Class B has a public member function Func() that is declared to be virtual, and class D redefines its own version of Func(). At execution time, suppose that a D object is passed to the following function as it's parameter: void DoSomething (B& x) { x.Func(); } Within DoSomething function, whos version of Func() is called? D's version neither B's nor D's version both B's and D's version B's version

D's version

Which of the following concept of oops allows complier to insert arguments in a function call if it is not specified? Call by pointer Call by value Default parameters Call by reference

Default parameters

Every ________ gets a ___________

Every new gets a delete

T/F A copy constructor is automatically built into any class that needs it.

False

T/F A destructor is a special operation that is explicitly called by the client program.

False

T/F A struct differs from an array in that its elements need not occupy a contiguous block of memory.

False

T/F Both static and dynamic arrays in C++ have a built-in data member named size that keeps track of the number of elements stored in the array.

False

T/F C++ provides a way to sequentially iterate through the fields in a struct.

False

T/F If currPtr to a node in adynamic linked list, the operation currPtr++ advances to the next node in the list.

False

T/F In C++, a derived class's constructor is executed before the base class constructor is executed.

False

T/F The at() methods in the string and the vector classes do not check the array bounds to ensure the item being accessed is part of the string or vector

False

T/F The capacity method of the vector class represents the number of items stored in the vector.

False

T/F The members of a class are public by default.

False

T/F The members of a struct are private by default

False

Consider the following class declaration: class Foobar { public: Foobar(); int a (); void b(int foobar); private: int foobar; }; Which of the following will correctly create an object of type Foobar? Foobar* f3 = new Foobar; Foobar f1; Foobar f2(); Foobar* f4 = new Foobar();

Foobar* f3 = new Foobar; Foobar f1; Foobar* f4 = new Foobar();

What is the output of the following code? Class Test { public: Test() { cout << "Hello from Test() ";} }; Test a; int main() { cout << "Main Started "; return 0; } Main Started Hello From Test() Hello from Test() Main Started Main Started Compiler ERrror; Global Objects are not allowed

Hello from Test() Main Started. There is a global object 'a' which is constructed before the main functions starts, so the constructor for a is called first, then main() execution begins

What is the output of the following code? class P { public: void print() { cout << "Inside P::"; } }; class Q : public P { public: void print() { cout << "Inside Q"; } }; int main(void { R r; r.print(); return 0; } Inside Q Inside P:: Inside P::Inside Q Compiler error

Inside Q The print function is not defined in class R. So it looked up in the inheritance hierarchy. print() is present in both classes P and Q, which of them should be called? The idea is, if there is multilvel inheritance, then function is linearly searched up in the inheritance heirarchy until a matching function is found.

What is cout? It is a class It is an operator It is a reserved word (C++ keyword) It is an object (class instance) it is a function

It is an object (class instance)

In C++, what is the order of execution of the operators? Relational operators first followed by Boolean operators NOT operator first, followed by relational operators, followed by other Boolean operators Depends on the compiler Boolean operators first followed by relational operators

NOT operator first, followed by relational operators, followed by other Boolean operators

Which of the following also known as an instance of a class? Friend Functions Member Variables Object Member Functions

Object

Which of the following is also known as an instance of a class? Member Functions Friend Functions Object Member Variables

Object

In C++, const qualifier can be applied to (select zero or more): Reference variables Member functions of a class Function arguments To a class data member which is declared as static

Reference variables Member functions of a class Function arguments To a class data member which is declared as static When a function is declared as const, it cannot modify data members of its class. When we don't want to modify an argument and pass it as reference or pointer, we use const qualifier so that the argument is not accidentally modified in the function. Class data members can be declared as both const and static for class wide constanats. Reference variabels can be const when they refer a const location

What is output by the following code? Class Test { public: Test(); } Test::Test() { cout << "Constructor Called" << endl; } int main() { cout << "Start" << endl; Test t1(); cout << "End" << endl; return 0; } Start End Compiler Error Start End Constructor Called Constructor Called Start End

Start End Note that the line "Test t1();" is not a constructor call. Compiler considers this line a s declaration of function t1 that does not receive any parameter and returns the object of type Test.

What is the output of the following program? int main() { int track[] = { 10, 20, 30, 40 }, *stricker; stricker = track; track[1] += 30; cout << "Striker>" << *striker << " "; *striker -= 10; striker++; cout << "Next@ << *striker << " "; striker += 2; cout << "Last@" << *striker << " "; cout << "Reset To" << track[0] << " "; retrun 0; } Compiler Error Stricker> Next@ Last@ Reset To 10, 20, 30, 40 Striker>10 Next@40 Last@50 Reset To0 Striker>10 Next@50 Last@40 Reset To0

Striker>10 Next@50 Last@40 Reset To0 The array track contains 4 elements { 10, 20, 30, 40} and the pointer striker holds the base address of the array track i.e, address of track[0]. 1) *striker holds the data value of track[0] ie 10. Decrement in *striker by 10 makes track[0] = 0. 2) incrementing pointer striker gives the next location of track ie 1. Now *striker gives the data value of track[1] 3. Again by incrementing by 2, striker reaches to the 4 address of the array 4. At last print the value of track[0] which is 0

In a Function, passing a parameter by value does not necessarily protect the contents of the caller's data structures from being affected by the function under which of the following conditions? Value parameters always protect the caller's data The value parameters are arrays The function is recursive The value parameters are pointer variables The value parameters are integers

The value parameters are pointers

T/F A constructor cannot be explicitly called by the client program

True

T/F A copy constructor must be called explicitly by the client program.

True

T/F A programmer, using inheritaance to specialize a class X, does not need access to the source code for X's implementation.

True

T/F A programmer, using inheritance to specialize a class X, does not need access to the source code for X's implementation.

True

T/F Dynamic binding is accomplished using the virtual keyword on a method in the base or parent class.

True

T/F If a copy constructor is not defined by the developer of a class, a default version is provided and results in a shallow copy.

True

T/F In C++, if class X is a base class of class Y, then Y cannot directly access X's private data.

True

T/F The implicit return type of a class constructor is the class type itself.

True

T/F The members of a struct are public by default.

True

Assume that a class called Weapon exists that has a default constructor. Create a pointer to a Weapon (named myWeapon) and the Weapon associated with it.

Weapon* myWeapon = new Weapon();

In the object-oriented design of an airline passenger reservation program, suppose that "airplane" has been identified as one object and "airplane seat" has been identified as another object. Focusing specifically on the airplane object, how does it relate to the airplane seat object? an is-a relationship an independent and equal relationship a has-a relationship both a has-a and an is-a relationship none of the above

a has-a relationship

What is the term for an interface that simplifies the use of an object. For example, driving a car only requires understanding how to use a steering wheel, gear shift, and pedals, it is unnecessary to understand the internal workings of the car engine.

abstraction

A class should provide a copy constructor in which of the following cases? At least one data member is a long integer At least one data member is an array a copy constructor is never required a copy constructor should always be provided member functions are defined at least one data member is a pointer

at least one data member is a pointer

Which of the following represents functions that are in the cstdlib library? pow atoi rand atof abs

atoi rand atof abs

Which of the following return true if it's argument is an odd integer? bool IsOdd (int x) { return (x % 2 == 1); } bool IsOdd (int x) { return (x /2 == 1); } bool IsOdd (int x) { if (x % 2 == 1) return true; else return false; }

bool IsOdd (int x) { return (x % 2 == 1); } and bool IsOdd (int x) { if (x % 2 == 1) return true; else return false; }

What is the role of the expression : QueType in the following constructor? CountedQue::CountedQue(max) : QueType(max) { length = 0; } doesn't mean anything different from the standard constructor behavior causes the invocation of the non-default base-class constructor forces the execution of the derived-class constructor, then the base-class constructor causes the invocation of the non-default derived-class constructor forces the execution of the base-class constructor, then the derived-class constructor

causes the invocation of the non-default base-class constructor

Assume that there is an integer named "a". Read a value from the console into variable a.

cin >> a;

What is the output of the following program? int main() { const int i = 20; const int* const ptr = &i; (*ptr)++; int j = 15; ptr = &j; cout << i; return 0; ) 15 compiler error 35 20 21

compile error Here "ptr" has been defined as a constant pointer that holds address of a constant integer "i". So, neither can the value of "ptr" be changed, nor can the value held by it can be changed. Thus, the lines "(*ptr)++" and "ptr=&j" are invalid, as they are trying to modify the variable i's content and the value of the pointer respectively. This gives an error

What is the result of the following code? class Test { int value; public: Test(int v); ); Test::Test(int v) { value = v; ) int main(){ Test t[100]; return 0; } compiler error builds 100 items of type test builds a test item whose value is set to 100

compiler error The class Test has one user defined cosntructor "Test(int v)" that expects one argument. It doesn't have a cosntructor without any argument as the compiler doesn't create the default constructor if user defines a constructor

Which of the following statements declares a real constant named PI with the value 3.1459? const float PI = 3.1459; const float = 3.1459; PI : REAL; float PI = 3.1459; const PI float = 3.1459;

const float PI = 3.1459;

Assume a string variable called 'name' has already been defined, display the name to console in a single line of code followed by an end of line.

correct options: cout << name + "\n"; cout << name << "\n"; cout << name << endl;

Assume that there is a string variable named "title" with some phrase of length 10 or more. Display the 7th character to the console and nothing else.

correct options: cout << title.at(6); cout << title[7]; cout << title.at(7); cout << title[6]; cout >> title.at(6); cout >> title[6];

Display the phrase "Doctor Who" to the console followed by an end of line (return).

cout << "Doctor Who << endl;

Using the same gpaPtr variable from the previous question, display the value of the pointee to the console and NOTHING ELSE

cout << *gpaPtr;

Examine the following class definition: class DataType { public: void Initialize (int day, int month, int year); int GetYear() const; int GetMonth() const; int GetDay() const; private: int year; int month; int day; }; Which of the following statements in a client program correctly prints out the year of the variable day1 of type DateType? The year cannot be printed by a client program. cout << day1.GetYear(); cout << GetYear(day1); cout << GetYear.day1; cout << day1.GetYear;

cout << day1.GetYear();

Function Prototypes are... not legal in C++ can be either declarations definitions

declarations

Assume that there is an integer variable called 'size' and that we have already asked the user to provide a value for the size of a dynamic array. Assume that we have created a dynamic array of strings called 'names' that has space for 'size' number of strings. Delete the 'names' array.

delete [] names;

Using the same double pointer gpaPtr, delete it's memory

delete gpaPtr;

Which of the following gets called when an object goes out of scope? main constructor virtual function destructor

destructor

In a single line of code, declare a pointer to a double value called gpaPTr and initialize its value to 3.4

double* gpaPtr = new double(3.4);

Consider the following code: class Foobar { public: Foobar(); int a(); void b(int foobar); private: int foobar; ); Assume that pointer named foobar is created that points to an object of type Foobar. Which of the following will correctly call method a() on foobar? foobar->a() a->foobar() foobar.a() a.foobar() a(foobar)

foobar->a()

The class declaration belongs in the ____________ file for the class stub client implementation super header

header

Assume that a pointer to a Weapon object exists named "myWeapon." Call the fire method on myWeapon. Assume the fire method does not have any parameters. Ignore any return values from the Fire method.

myWeapon->Fire();

Constructors have ______ return type. int void char no

no

Copy constructor must receive its arguments by _________________ only pass-by-reference only pass by address either pass-by-value or pass-by-reference only pass-by-value

only pass-by-reference

The cmath library contains all of the following functions except for: abs tan log rand pow

rand

Assume that there is an integer variable called 'size' and that we have already asked the user to provide a value for the size of a dynamic array. Which of the following correctly creates a dynamic array of strings called 'names' that has space for 'size' number of strings. string *names = string[size]; string* names = new string[size]; string[] names = new string[size]; string*names = new string(size);

string* names = new string[size];

Select all of the phrases that accurately complete the following phrase: A destructor should be included in a class if ____________________ the class has a destructor one of the class members is a pointer the class overloads the assignment operator the class has a non-default constructor

the class has a destructor one of the class members is a pointer the class overloads the assignment operator

Consider the following program: int main() { string str = "This*is^a.45min test."; int i; for ( i = 0; i < str.length(); i++) { if (ispunct(str[i])) str[i] = ' '; // a blank str[i] = tolower (str[i]}); } cout << str; } What is printed by the last line of the code? thisisa45mintest this*is^a.45min test. this is a 45min test this is a 45min test.

this is a 45min test

T/F A deep copy copies one class object to another including any pointed-to data

true

A destructor is automatically invoked (vs directly invoked) when an instance of the class goes out of scope when a pointer tot hat object is deleted. never when one fo the data members is a pointer

when an instance of the class goes out of scope


Related study sets

Quiz: Parallel Structure and Misplaced and Dangling Modifiers

View Set

Chapter 12, 13, and 14 Revel Questions

View Set

Chapter 12: International Bond Market

View Set

Sociology- Qualitative and Quantitative research

View Set

Necessary and sufficient Practice

View Set

Bio 103 Final Review (Chapters 7-13)

View Set