Data Structures - 2250

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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 ? None of these 1 2 0

2

What is the output of the following code? #include <iostream> using namespace std; int* foo(int*); int main() { int x[5] = { 1, 2, 3, 4, 5 }; int i, *p; // exchange values p = foo(x); // output the array x for (i = 0; i < 5; i++) cout << *(p + i) << " "; return 0; } int* foo(int* p) { int i; // exchange values for (i = 0; i < 2; i++) { int temp = *(p + i); *(p + i) = *(p + 4 - i); *(p + 4 - i) = temp; } return p; } 5 4 3 2 1 Runtime error Compiler error 1 2 3 4 5

5 4 3 2 1

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; a += foobar(b, a); b += foobar(a, b); cout << foobar(b, a) << endl; return 0; } 10 Nothing, there is a run-time error. 8 3 5 6 Nothing, there is a compiler error.

6

Assume all necessary libraries are included. 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;} Nothing, it does not compile. 7 Nothing, it throws a run-time error. 4 11

7

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

All

Which of the followings is/are automatically added to every class, if we do not write our own. Copy Constructor Assignment Operator A constructor without any parameter (default constructor) All of the above

All of the above

Which of the followings is/are automatically added to every class, if we do not write our own. Copy Constructor Assignment Operator All of the above A constructor without any parameter (default constructor)

All of the above

Select all of the phrases that accurately complete the following phrase: An overloaded assignment operator should be included in a class if ___________________ the class overloads the assignment operator one of the class members is a pointer the class has a destructor

All options

What is the output of the following code? #include<iostream> #include<stdio.h> using namespace std; class Base { public: Base() { fun(); //note: fun() is virtual } virtual void fun() { cout << "Base Function" << endl; } }; class Derived: public Base { public: Derived(){} virtual void fun() { cout << "Derived Function" << endl; } }; int main() { Base* pBase = new Derived(); delete pBase; return 0; } Base Function Derived Function Base Function Base Function Derived Function Compiler error Derived Function

Base Function

What is the output of the following code? #include<iostream> using namespace std; class Test{public:Test();}; Test::Test() {cout << " Constructor Called. ";} void fun() {static Test t1;} int main() {cout << " Before fun() called. "; fun(); fun(); cout << " After fun() called. "; return 0;} Constructor Called. Constructor Called. After fun() called.Before fun() called. Before fun() called. Constructor Called. After fun() called. Before fun() called. Constructor Called. Constructor Called. After fun() called. Constructor Called. Before fun() called. After fun() called.

Before fun() called. Constructor Called. After fun() called.

Which of the following return true if its argument is an odd integer? 1. bool IsOdd (int x) {if (x % 2 == 1)return true;elsereturn false;} 2. bool IsOdd (int x) {return (x / 2 == 1);} 3. bool IsOdd (int x) {return (x % 2 == 1);}

Both 1 and 2 return true.

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

Builds 100 items of type Test

What is the output of the following program? #include <iostream> using namespace std; int main() { const int i = 20; const int* const ptr = &i; (*ptr)++; int j = 15; ptr = &j; cout << i; return 0; } 20 Compile error 35 15 21

Compile Error

What is the output of the following program? #include <iostream> using namespace std; int main() { int a = 10, *pa, &ra; pa = &a; ra = a; cout << "a=" << ra; return 0; } A memory address 10 Compiler error Runtime error

Compile Error

What is the output of the following code? #include<iostream>using namespace std;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;} Garbage value None of the above 10 Compiler Error: p must be passed by reference

Compiler Error: p must be passed by reference

Look at the following class heading: class CountedQue : public QueType What does the phrase : public QueType mean? None of these CountedQue is being derived from QueType QueType is being derived from CountedQue QueType is being substituted for CountedQue CountedQue is being substituted for QueType

CountedQue is being derived from QueType

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

Default parameters

Every gets a

Every new get a delete

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

False

The members of a class are public by default. True False

False

The members of a struct are private by default. True False

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 f2(); Foobar* f3 = new Foobar; Foobar* f4 = new Foobar(); Foobar f1;

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

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

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

Assume that a linked list is defined with a pointer to the head (first item) in the list and a pointer to the tail (last item) in the list. Which of the following represents the efficiency of adding a new item to the back of the list? O(logN) O(N) O(N2) O(1) O(NlogN)

O(1)

Assume that a linked list is defined with only a pointer to the head (first item) in the list. Which of the following represents the efficiency of searching for a specific item in the list? O(NlogN) O(logN) O(N2) O(1) O(N)

O(N)

What is the output of the following program? #include <iostream> using namespace std; int main() { int track[] = { 10, 20, 30, 40 }, *striker; striker = track; track[1] += 30; cout << "Striker>" << *striker << " "; *striker -= 10; striker++; cout << "Next@" << *striker << " "; striker += 2; cout << "Last@" << *striker << " "; cout << "Reset To" << track[0] << " "; return 0; } Striker> Next@ Last@ Reset To 10, 20, 30, 40 Striker>10 Next@40 Last@50 Reset To0 Striker>10 Next@50 Last@40 Reset To0 Compiler error

Striker>10 Next@50 Last@40 Reset To0

Choose the statement that most accurately describes the following code segment (assuming it was embedded within a correct program)? if count < 10 cout << count else cout << "Count is equal to 10" << endl; 1. The code segment compiles but always prints the value of count. 2. The code segment would not compile; it contains two errors. 3. You Answered The code segment compiles and prints either the value of count or "Count is equal to 10". 4. The code segment would not compile; it contains one error.

The code segment would not compile; it contains two errors.

The implicit return type of a class constructor is the class type itself. True False

True

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

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

const float PI = 3.1459;

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

cout << day1.GetDay();

What should go in the blank if the loop is reading and storing values into an array int data[LIMIT]? for (index = 0; index < LIMIT; index++) { cin >>_________; } data[index - 1] You Answered data[index + 1] data[LIMIT - 1] data[index]

data[index]

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, from the previous question, delete its memory

delete gpaPtr;

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

destructor

A copy constructor is automatically built into any class that needs it. True False

false

All the relational operators can be overloaded except for equality. True False

false

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. True False

false

C++ does not allow the overloading of operators. True False

false

It is not possible to use a list without knowing how it is implemented. True False

false

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. True False

false

The capacity method of the vector class represents the number of items stored in the vector. True False

false

Which of the following can be overloaded? Functions Object Operators

functions and operators

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

header

The file containing the definition of class DateType is called the ______ file. implementation client prototype header

implementation

Constructors have _____ return type. char void int no

no

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

rand

All arrays in C++ are passed as ____________ parameters. value or reference, the compiler chooses reference value or reference, the developer chooses value

reference

What is the output of the following code? #include <stdio.h> int main() { int arri[] = { 1, 2, 3 }; int* ptri = arri; char arrc[] = { 1, 2, 3 }; char* ptrc = arrc; printf("sizeof arri[] = %d ", sizeof(arri)); printf("sizeof ptri = %d ", sizeof(ptri)); printf("sizeof arrc[] = %d ", sizeof(arrc)); printf("sizeof ptrc = %d ", sizeof(ptrc)); return 0; } sizeof arri[] = 12 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 1 sizeof arri[] = 3 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 1 sizeof arri[] = 12 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 4 sizeof arri[] = 3 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 4

sizeof arri[] = 12 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 4

The syntax for overloading an operator in C++ is the operator to be overloaded followed by the word overload. the word overload followed by the symbol to be overloaded. the word operator followed by the symbol to be overloaded. C++ doesn't let you overload operators. the operator to be overloaded followed by the word operator.

the word operator followed by the symbol to be overloaded.

Consider the following program: #include <iostream>#include <string>using namespace std;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 blankstr[i] = tolower (str[i]);}cout << str;} What is printed by the last line of the code ? this*is^a.45min test. this is a 45min test. thisisa45mintest this is a 45min test

this is a 45min test

Consider the following program: #include <iostream>#include <string>using namespace std;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 blankstr[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

A copy constructor must be called explicitly by the client program. True False

true

A destructor is a special operation that is implicitly called when a class object goes out of scope. True False

true

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

true

Dynamic binding is accomplished using the virtual keyword on a method in the base or parent class. True False

true

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 False

true

Reading components into an initially empty list is faster if the list is represented directly as an array rather than a linked list. True False

true

Virtual functions are the C++ mechanism for dynamic binding of operations to objects. True False

true

When implementing a list, insertions and deletions at the front of the list are faster with a linked list representation than with an array representation. True False

true

A destructor is automatically invoked (versus directly invoked) when an instance of the class goes out of scope. when one of the data members is a pointer. when a pointer to that object is deleted. never.

when an instance of the class goes out of scope.

What is the output of the following code? #include <iostream>using namespace std;class Point{int x, y;public:Point(int i = 0, int j = 0) { x = i; y = j; }int getX() { return x; }int getY() { return y; }};int main(){Point p1;Point p2 = p1;cout << "x = " << p2.getX() << " y = " << p2.getY();return 0;} x = garbage value y = garbage value x = 0 y = 0 Compiler Error

x = 0 y = 0

Neither client code (main, etc.) nor derived class code can directly access the private members of the base class. True False

true

Assuming that the user types in the following " ab" (that's 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 variable input? The values in parentheses explain each possibility but should not be considered part of the answer. ab (space space a b) None of these, a run-time exception is generated and the program crashes. (space) a b ab (a b) (space space)

(space)

A struct is declared as follows: struct NameType{ char first[10]; char last[20]; int age;} The first letter in the first name is stored in location 1000. Each letter takes one location and an integer takes 2 locations. What is the address of last[2]?

1,012

What is the output of the following code: #include<iostream> using namespace std; int x = 10; void fun() { int x = 2; { int x = 1; cout << ::x << endl; } } int main() { fun(); return 0; } 10 1 Compiler error 2

10

What is the output of the following program? #include <iostream> using namespace std; 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, Runtime error 10, 20, 30, 40, 50 50, 50, 50, 50, 50,

10, 20, 30, 40, 50,

Assume all necessary libraries are included. 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; } Nothing, it throws a run-time error. Nothing, it does not compile. 7 11 4

11

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 ? None of these 0 2 1

2

What is the output of the following code? #include <stdio.h> void fun(int x) { x = 30; } int main() { int y = 20; fun(y); printf("%d", y); return 0; } 20 10 Runtime Error 30 Compiler Error

20

What is the output of the following code? #include<iostream> using namespace std; class Test { int &t; public: Test (int &x):t(x) { } int getT() { return t; } }; int main() { int x = 20; Test t1(x); cout << t1.getT() << " "; x = 30; cout << t1.getT() << endl; return 0; } 30 30 Compiler Error 20 20 30 20 20 30

20 30

A one dimensional array data is declared as follows: char* data = new char[4]; The base address is 2222 and each value takes up one location in memory. What is the address of the fourth element? 2218 2225 Unknown, the items in the array could have been allocated anywhere in memory. 2226 2219

2225

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

3

What is the output of the following program? #include <iostream> #include <stdlib.h> using namespace std; int main() { float x = 5.999; float* y, *z; y = &x; z = y; cout << x << ", " << *(&x) << ", " << *y << ", " << *z << "\n"; return 0; } 5.999, 5.999, 5.999, 5.999 Compiler error Runtime error 5.999, 5.9, 5.000, 5.900 Address of the elements

5.999, 5.999, 5.999, 5.999

What is the output of the following code? #include <iostream> using namespace std; class X { private: static const int a = 76; public: static int getA() { return a; } }; int main() { cout << X::getA() << endl; return 0; } Garbage value Compiler error 76

76

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

::

What is the insertion operator?

<<

extraction operator

>>

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.

Any of these ways: cout << title[6]; cout << title.at(7); cout << title[7]; cout >> title.at(6); cout << title.at(6); cout >> title[6];

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 its parameter: void DoSomething( B& x ){ x.Func();} Within the DoSomething function, whose version of Func() is called? B's version neither B's nor D's version D's version both B's and D's version

B's version

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

Copy Destructor

Which of the following statements are true about the default constructor? It has no return type Its return type is the type of the class The programmer can define it, but the C++ language doesn't require this It is sometimes, but not always, defined by C++ if it isn't provided by the programmer The programmer must define it It is always defined by C++ if it isn't provided by the programmer

It has no return type The programmer can define it, but the C++ language doesn't require this It is sometimes, but not always, defined by C++ if it isn't provided by the programmer

What is cout ? 1. It is a function 2. It is a class 3. It is an operator 4. It is an object (class instance) 5. It is a reserved word (C++ keyword)

It is an object

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

Object


Ensembles d'études connexes

Business Law Chapter 12 Questions

View Set

Chapter 6- Life insurance Underwriting and Policy issue

View Set

CH 6- DYNAMIC STUDY MODULES - BMD

View Set