C++ Practice Exam

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

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;

+5

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

There is a syntax error

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?

inFile.open(filename);

Which of the following is needed in order to execute the following statement: time_t timer; #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.

*q = 1;

By using which operator does point to next element is represent in iterator?

++

What is the output of the following piece of code? int i = 0; while(++i <= 5) cout << i << ", ";

1, 2, 3, 4, 5,

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;

10, 5

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

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 << ", "; }

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; }

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);

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; }

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;

2, Jack

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; } 5 8 10 There is a syntax error

5

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); }

5

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;

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; }

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; 0 1 6 There is a syntax error

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;

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; 6: 7 10 There is a syntax error

7

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; } }

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? Employee() Employee(const Employee& other) void Employee() Employee(Employee& other) void Employee(Employee& other) void 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. True, True True, False False, True False, 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; }

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; }

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();

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 = dPtr; pPtr->print();

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.

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; }

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?

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;

There is a syntax error

What is the output of the following piece of code? for(int i = 0; i < 5; i++); cout << i << ", ";

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; }

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; }

This implementation is incorrect. When the subtraction happens, str1 and str2 are not pointing to the first different chars.

Which of the following declare and initialize a character type variable name grade with value 'A'?

char grade = 'A';

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.

cin >> s;

Which statement can NEVER be reached in the 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; }

cout << "After throw \n";

Which of the following are best code to continue if the file open fails

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

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?

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

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. 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 = 1; 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?

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

To overload functions with symbolic names (like + - / <<), you must use the keyword ________ before the symbolic name.

operator

Fibonacci sequence is defined as following: (...) 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); }

return 1;

Which of the following mystrlen is the best implementation?

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

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

str1 = "toaster";

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

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?

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?

student->gpa = 3.95;

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

student->next

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

~Employee()


Kaugnay na mga set ng pag-aaral

Chapter 29 assessing childbearing women

View Set

#4 OB EAQ Intrapartum Complications

View Set

Google Certification Level 1 (Units 10-13)

View Set

Tom, Baymax, Cara homework 9.10.2021 by Robert - MC033

View Set

life span and development final Exam

View Set