C++ Practice

Ace your homework & exams now with Quizwiz!

Suppose I have the following recursive function, what is the return value when call myop(4)? int myop(int n){ if(n <= 2) return 1; return 1 + myop(n-1) + myop(n-2) } Answers: 7 6 8 5

5

Which of the following declare and initialize a character type variable name grade with value 'A'? Answers: char grade = 'A'; char grade = A; char grade = "A" char grade = "A";

char grade = 'A';

Which of the following are best code to continue if the file open fails Answers: exit(1); cout << "Cannot open file. Exit the progam"; exit(1); exit(0); cout << "Cannot open file. Exit the progam"; exit(0);

cout << "Cannot open file. Exit the progam"; exit(1);

To overload functions with symbolic names (like + - / <<), you must use the keyword ________ before the symbolic name. Answers: const operator void reference

operator

Fibonacci sequence is defined as following: f subscript 1 space equals space 1 comma space f subscript 2 equals 1 comma space f subscript n space end subscript equals space f subscript n minus 1 end subscript plus f subscript n minus 2 end subscript space w h e n space n space greater or equal than 3. Write a recursive function, take positive integer n as input, return nth Fibonacci number. What is placed in _____________ int fibonacci(int n){ if(n == 1 || n == 2) _____________ return fibonacci(n-1)+fibonacci(n-2); } Answers: int result = 1; return 0; return 1; return 2;

return 1;

Which of the following access a variable named next in structure *student ? Answers: student-next student student'next student->next

student->next

Which of the following is needed in order to execute the following statement: time_t timer; Answers: #include <time.h> #include <ctime> #include <cstdlib> There is a syntax error

#include <ctime>

Suppose we have the following definition: vector<int> vec, vec1; //use push_back to put 10 values into vec vector<int>::iterator p = vec.begin(); vector<int>::const_iterator q = vec.begin(); Which of the following expressions is NOT legal? Treat the effect of these as non-cumulative. Answers: p = vec.end (); q = vec1.end(); *q = 1; *p = 1;

*q = 1;

By using which operator does point to next element is represent in iterator? Answers: ++ -- + -

++

Suppose that outFile is an ofstream connected to file named "output.txt". What will be output to output.txt file due to following code? int x = 5; outFile.setf(ios::showpos); outFile << x; Answers: 5 +5 -5 5.0

+5

What is the output of the following piece of code? int i = 0; while(++i <= 5) cout << i << ", "; Answers: 0, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, This is an infinity loop, nothing output There is a syntax error 1, 2, 3, 4, 5,

1, 2, 3, 4, 5,

Assume int x = 5; int y = 10; What will be the y value after the execution of following C++ code segment? if(x > y){ y = 8; } else{ if(x > 20); y = 5; } Answers: There is a syntax error 8 5 10

10

Suppose that I have function void myswap(int& x, int& y){ int temp = x; x = y; y = temp; } what will be the output of the following code segment? int x = 5, y = 10; myswap(x, y); cout << x << ", " << y; Answers: 5, 10 There is a syntax error 10, 5 There is a run time error

10, 5

What is the value of numbers.capacity() after the following code? vector<float>numbers; numbers.reserve(100) Answers: 10 100 Unknown 0

100

Suppose the input is 1234. What is the output of the following function? void display(int x){ if(x < 10){ cout << x; return; } display(x/10); cout << x%10 << ", "; } Answers: 1, 2, 3, 4 12, 3, 4, 12, 3, 4 1, 2, 3, 4,

12, 3, 4,

Output of following program? Assume that the size of int is 4 bytes and size of double is 8 bytes, and there is no alignment done by the compiler. #include<iostream> #include<stdlib.h> using namespace std; template<class T, class U, class V=double> class A { T x; U y; V z; static int count; }; int main() { A<int, int> a; A<double, double> b; cout << sizeof(a) << endl; cout << sizeof(b) << endl; return 0; } Answers: 8 16 16 24 There is a syntax error 20 28

16 24

Given three functions: int fun(int x) { return x*x;} double funny(int x) { return x;}; double fun(double x) { return x;} What is the output of the following code: cout << setprecision(2) << funny(2.0); Answers: 2.0 4.0 2 There is a compile error

2

What is the output? Assume that the size of char is 1 byte and size of int is 4 bytes, and there is no alignment done by the compiler. #include<iostream> #include<stdlib.h> using namespace std; template<class T, class U> class A { T x; U y; static int count; }; int main() { A<char, char> a; A<int, int> b; cout << sizeof(a) << endl; cout << sizeof(b) << endl; return 0; } Answers: 6 12 2 8 8 8 There is a syntax error

2 8

What is the output of the following code? vector<string> names; names.push_back("Tom"); names.push_back("Jack"); cout << names.size() << ", " << names[1] << endl; Answers: 2, Jack 2, Tom There is a syntax error 10, Jack

2, Jack

Suppose that I have function void myswap(int x, int y){ int temp = x; x = y; y = temp; } what will be the output of the following code segment? int x = 5, y = 10; myswap(x, y); cout << x << ", " << y; Answers: There is a syntax error 5, 10 There is a run time error 10, 5

5, 10

What is the output? #include <iostream> #include <iterator> #include <list> using namespace std; int main () { list<int> mylist; for (int i = 0; i < 10; i++) mylist.push_back (i * 10); list<int> :: iterator it = mylist.begin(); advance (it, 5); cout << *it << endl; return 0; } Answers: 30 40 50 60

50

What is the output of the following code? int *p = new int[3]; *p = 1; *(p+1) = 2; *(p+2) = 3; int result = 0; int * q = p; for(int i = 0; i < 3; i++){ result += *q; q++; } cout << result; Answers: 6 0 There is a syntax error 1

6

What will be the output of the code? Assume user types 5 on keyboard? int *p, x; p = new int; *p = 10; cin >> x; p = &x; cout << ++*p; Answers: There is a syntax error 5 6 11 10

6

Assume int x = 15; int y = 10; What will be the y value after the execution of following C++ code segment? if(x > y) if(x > 20) y = 6; else y = 7; Answers: 6 There is a syntax error 7 10

7

Copy of What is the output? #include <iostream> using namespace std; class Test { static int count; int id; public: Test() { count++; id = count; cout << "Constructing " << id << endl; if(id == 3) throw 3; } ~Test() { cout << "Destructing " << id << endl; } }; int Test::count = 0; int main() { try { Test array[3]; } catch(int i) { cout << "Caught " << i << endl; } } Answers: Constructing 1 Constructing 2 Constructing 3 Destructing 3 Destructing 2 Destructing 1 Caught 2 Constructing 1 Constructing 2 Constructing 3 Destructing 2 Destructing 1 Caught 3 There is a syntax error Constructing 1 Constructing 2 Constructing 3 Destructing 1 Destructing 2 Caught 3

Constructing 1 Constructing 2 Constructing 3 Destructing 2 Destructing 1 Caught 3

Which of the following is the head of copy constructor of Employee class? Answers: Employee(Employee& other) void Employee() void Employee(const Employee& other) Employee() void Employee(Employee& other) Employee(const Employee& other)

Employee(const Employee& other)

True or false? Regarding the following two rules of when to write Big Three: Copy Constructor, Assignment Operator, and Destructor? 1. Only write the one you will explicitly use in your program. For instance, you may write Copy Constructor but not destructor. 2. Prefer to write these methods even if the implicit ones are correct. Answers: True, True False, True False, False True, False

False, False

Predict output of the following program #include<iostream> using namespace std; class Base { public: virtual void show() { cout<< "I am base \n" ; } }; class Derived: public Base { public: void show() { cout<< "I am derived \n" ; } }; int main( void ) { Base *bp = new Derived; bp->show(); Base &br = *bp; br.show(); return 0; } Answers: I am base I am base I am base I am derived I am derived I am base I am derived I am derived

I am derived I am derived

What is the output? #include <iostream> using namespace std; int main() { try { try { throw 10; } catch (int n) { cout << "Inside Catch\n"; throw; } } catch (int a) { cout << "Outside Catch\n"; } return 0; } Answers: Inside Catch There is a syntax error Inside Catch Outside Catch Outside Catch

Inside Catch Outside Catch

Give the following simplified classes and code, what is the output of the last statement? class Pet{ public: virtual void print(); string name; private: }; class Dog:public Pet{ public: void print(); string breed; }; void Pet::print(){ cout << "My name is " << name; } void Dog::print(){ Pet::print(); cout << "My breed is " << breed << endl; } Dog* dPtr = new Dog; Pet* pPtr = dPtr; dPtr->name = "rover"; dPtr->breed = "Collie"; pPtr->print(); Answers: My name is rover My breed is Collie My name is roverMy breed is Collie My breed is Collie Nothing will be printed

My name is roverMy breed is Collie

Give the following simplified classes and code, what is the output of the last statement? class Pet{ public: virtual void print(); string name; private: }; class Dog:public Pet{ public: void print(); string breed; }; void Pet::print(){ cout << "My name is " << name << endl; } void Dog::print(){ Pet::print(); cout << ", My breed is " << breed << endl; } Dog dPtr; Pet pPtr; dPtr.name = "rover"; dPtr.breed = "Collie"; pPtr = pDog; pPtr->print(); Answers: My name is rover Nothing will be printed. The last statement has syntax error My name is rover, My breed is Collie , My breed is Collie

Nothing will be printed. The last statement has syntax error

Which of the following is true about pure virtual functions? 1) Their implementation is not provided in a class where they are declared. 2) If a class has a pure virtual function, then the class becomes abstract class and an instance of this class cannot be created. Answers: Both 1 and 2 are correct Only 1 is correct Only 2 is correct Neither 1 nor 2 is correct

Only 2 is correct

In the following function template, what must be TRUE in order to use the function with a given data type? template <class T> int smallest( T array[], int size) { int small=0, i; for(i=0;i<size;i++) { if(array[i] < array[small]) small=i; } return small; } Answers: The data type must be a pre-defined data type. The data type must be character based. The data type must have a < operator defined for it. The data type must be numeric.

The data type must have a < operator defined for it.

Which of the following should be virtual if a base class uses dynamic memory allocation? Answers: The destructor The constructor The copy constructor all public functions

The destructor

Suppose that I have function void myswap(int *x, int *y){ int temp = *x; *x = *y; *y = temp; } what will be the output of the following code segment? int x = 5, y = 10; myswap(x, y); cout << x << ", " << y; Answers: 5, 10 There is a syntax error There is a run time error 10, 5

There is a syntax error

What is the output of the following piece of code? for(int i = 0; i < 5; i++); cout << i << ", "; Answers: 6, 0, 1, 2, 3, 4, 5, 4, 0, 1, 2, 3, 4, 5, 6, There is a syntax error

There is a syntax error

What is the output of the following piece of code? int i = 0; do{ cout << i << ", "; }while (++i <= 5) Answers: 1, 2, 3, 4, 5, There is a syntax error 5, This is an infinity loop, nothing output 6,

There is a syntax error

What will be the output after the execution of the following piece of code? char grade = 'a'; switch(grade){ case 'A', case 'a': cout <<"Excellent"; break; case 'B', case 'b': cout << "Good"; break; case 'C', case 'c': cout << "Average"; break; default: cout << "Work harder"; break; } Answers: Excellent Good Average Work harder There is a syntax error

There is a syntax error

Consider the following implementation, what is the correct? int mystrcmp(const char* str1, const char * str2){ while(*str1 != '\0' && *str2 != '\0') if(*str1++ != *str2++) break; return *str1 - *str2; } Answers: This implementation is correct. It will compare two strings as built-in strcmp function does This implementation is incorrect. The two arguments shall not be const char * This implementation is incorrect since there will be infinite loop This implementation is incorrect since there are some syntax error

This implementation is correct. It will compare two strings as built-in strcmp function does

Assume that I have a Student class, and I overloaded the operators >> and << for Student class. If I want to input a Student object from keyboard, which of the following code is correct? Assume the Student object variable name is s. Answers: cin >> s; Cannot do it s >> info; cout << s;

cin >> s

Which statement can never be reach in following code? #include <iostream> using namespace std; int main() { int x = -1; try { cout << "Before throw \n"; if (x < 0) { throw x; cout << "After throw \n"; } } catch (int x ) { cout << "Caught Exception \n"; } cout << "After catch \n"; return 0; } Answers: cout << "Caught Exception \n"; cout << "Before throw \n"; cout << "After catch \n"; cout << "After throw \n";

cout << "After throw \n";

If the name of the input file was in a variable named filename, which of the following is the correct way to open the input stream named inFile and associate it with this file? Answers: inFile.open(filename); inFile = "filename"; inFile = filename; inFile.open("filename");

inFile.open(filename);

Which of the following code calculate the sum of all elements in given array int a[SIZE}; where SIZE is an int constant that has been initialized. Assume a has been initialized. Answers: int result = 0; for(int i = 0; i < SIZE; i++) result *= a[i]; int result = 1; for(int i = 0; i < SIZE; i++) result *= a[i]; int result; for(int i = 0; i < SIZE; i++) result += a[i]; int result = 0; for(int i = 0; i < SIZE; i++) result += a[i];

int result = 0; for(int i = 0; i < SIZE; i++) result += a[i];

Which of the following doesn't have 5 as output? Answers: int x = 5; cout << ++x; cout << 40%7; cout << 40/7; int x = 5; cout << x++;

int x = 5; cout << ++x;

Which of the following mystrlen is the best implementation? Answers: size_t mystrlen(const char * source){ size_t result = 0; while(*source != '\0') result++; return result; } size_t mystrlen(const char * source){ size_t result = 0; while(*source++ != '\0') result++; return result; } size_t mystrlen(char * source){ size_t result = 0; while(*source != '\0'){ result++; source++; } return result; } size_t mystrlen(const char * source){ size_t result = 0; while(*++source != '\0') result++; return result; }

size_t mystrlen(char * source){ size_t result = 0; while(*source != '\0'){ result++; source++; } return result; }

Which assignment statements will copy the value " toaster" into a string variable (str1)? Answers: str1 += toaster; strcpy(str1,"toaster"); str1 = "toaster"; str1 = toaster;

str1 = "toaster";

Assume that char name[80] = "John Doe"; char nameCopy[80]; Whihc of the following assign "John Doe" to nameCopy? Answers: name = nameCopy; nameCopy = name; C strcpy(name, nameCopy); strcpy(nameCopy, name);

strcpy(nameCopy, name);

Which of the following is correct definition for Student structure which is used in LinkedList that stores student's info: id, gpa, name? Answers: structure Student{ int id; double gpa; string name; Student* next; }; struct Student{ int id; double gpa; string name; Student next; }; struct Student{ int id; double gpa; string name; Student* next; } struct Student{ int id; double gpa; string name; Student* next; };

struct Student{ int id; double gpa; string name; Student* next; };

Assume that I have a structure: struct Student{ string name; int id; double gpa; Student* next; }; Student *student = new Student; How to I assign the student gpa as 3.85? Answers: student.gpa = 3.95; student'gpa = 3.95; student-gpa = 3.95; student->gpa = 3.95;

student->gpa = 3.95

Suppose that the base class has a private attribute int id; then which of the following methods the child class will use to manipulate id? Answers: void setId(int _id); void getId() const; int getId() const; int setId(int _id);

void setId(int _id); int getId() const;

Which of the following is the head of destructor of Employee class? Answers: ~Employee(Employee& other) void ~Employee(const Employee& other) void ~Employee() void ~Employee(Employee& other) ~Employee(const Employee& other) ~Employee()

~Employee()


Related study sets

FL 2-15 Chapter 9 Practice Questions

View Set

AP Biology - Unit 1 Progress Check: MCQ

View Set

Chapter 12 (Motivating Employees)

View Set