CSCI 675 - C++ Practice Exam

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

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 Response Feedback: For this questions, students need to know C libraries, including: , <cctype>, <ctime>, <cmath>, <cstdlib>, and C++ headers, including: <iomanip>, <iostream>

#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 = 1; *q = 1; p = vec.end (); q = vec1.end(); Response Feedback: Students shall know how to use iterators. the item pointed by constant iterator cannot be changed

*q = 1;

By using which operator does point to next element is represent in iterator? Answers: - + ++ -- Response Feedback: Student shall understand STL and iterator in each class in STL

++

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.0 5 Response Feedback: students shall know how to use iomanip library. The ofstream can use setf with argument ios::fixed, ios::showpos, ios::showpoint. Also know that setprecision function and precision member functon

+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, This is an infinity loop, nothing output There is a syntax error 1, 2, 3, 4, 5, 6, Response Feedback: Students shall know the syntax and semantics of for loop, while loop, and do while loop.

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; Answers: 10, 5 5, 10 There is a run time error There is a syntax error Response Feedback: Students shall be able to write and use functions with pointer argument and can write and call functions that pass by reference

10, 5

What is the value of numbers.capacity() after the following code? Answers: Unknown 0 10 100 Response Feedback: Students shall know vector class in C++. May study http://www.cplusplus.com/reference/vector/vector/ and the functions in it

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: 12, 3, 4 12, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 Response Feedback: Students shall be able to write and trace recursive algorithm

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 There is a syntax error 20 28 16 24 Response Feedback: Student shall know how to write and use template functions and classes For more study, visit http://www.geeksforgeeks.org/c-plus-plus-gq/templates-gq/

16

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: 4.0 2.0 2 There is a compile error Response Feedback: Students shall know how to overload functions, operators, and how to use them. The funny function is not overloaded. It only take int as argument. However when you pass an double value to it, it will not show compile error. The double value will be implicitly casted to int value. You will lose the precision. So the answer will be 2.

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 Response Feedback: Students shall know how to write and use template. Also know the size of each data type.

2

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: There is a syntax error 10, Jack 2, Jack 2, Tom Response Feedback: Students shall know string and vector and their member functions. Study them at http://www.cplusplus.com/reference/

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; } Answers: 10 5 8 There is a syntax error Response Feedback: Students shall pay attention to the syntax of if, if-else, if-esel if -else, and switch structure

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) } Answers: 6 5 7 8 Response Feedback: students shall be able to write and trace the recursive function

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; Answers: There is a syntax error 10, 5 There is a run time error 5, 10 Response Feedback: Students shall be able to write and use functions with pointer argument and can write and call functions that pass by reference. The function call by value so the actual parameters will not be affected

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 Response Feedback: Students shall know the functions in iterator class. May study http://www.cplusplus.com/reference/iterator/

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: 0 1 6 There is a syntax error Response Feedback: Students shall know dynamic array. How to declare and initialize it.

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 11 5 6 10 Response Feedback: Student shall know how to declare a pointer variable. Allocate memory, How to initialize the value pointed by it and How to use the pointer variable.

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 10 7 Response Feedback: Students shall pay attention to the syntax of if, if-else, if-esel if -else, and switch structure else shall pair with closest if.

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 Response Feedback: Students shall know how exception is handled and also know the order constructor and destructor are called

Constructing 1

Which of the following is the head of copy constructor of Employee class? Answers: Employee() void Employee() Employee(Employee& other) void Employee(const Employee& other) Employee(const Employee& other) void Employee(Employee& other) Response Feedback: Students are required to know how to write big three: Copy constructor, Destructor, and Assignment Operator for a class

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, False False, True True, False Response Feedback: Students are required to understand the Big Three: Copy constructor, Destructor, and Assignment Operator. You may read more information from https://www.cs.umd.edu/class/spring2002/cmsc214/Projects/P1/copy-dest.html

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 Response Feedback: Students shall understand virtual function, polymorphism, and inheritance

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: Outside Catch Inside Catch There is a syntax error Inside Catch Outside Catch Response Feedback: Students shall know how exception is handled. For extra study, may visit http://www.geeksforgeeks.org/c-plus-plus-gq/exception-handling-gq/

Inside 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 breed is Collie My name is roverMy breed is Collie Nothing will be printed Response Feedback: The student shall understand the polymorphism

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: Nothing will be printed. The last statement has syntax error , My breed is Collie My name is rover My name is rover, My breed is Collie Response Feedback: Student shall understand the polymorphism and sliced problem. The variable type is not pointer, cannot use ->

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 Response Feedback: Students shall understand virtual function, pure virtual function, and abstract class. May study https://en.wikipedia.org/wiki/Virtual_function

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 character based. The data type must be numeric. The data type must have a < operator defined for it. The data type must be a pre-defined data type. Response Feedback: Students shall understand how to write and use template functions

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: all public functions The copy constructor The constructor The destructor Response Feedback: Students shall understand the destructor, constructor, and the call order when create an object and when destroy an object

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: There is a run time error 10, 5 5, 10 There is a syntax error Response Feedback: Students shall be able to write and use functions with pointer argument and can write and call functions that pass by reference

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: 4, There is a syntax error 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, Response Feedback: Students shall know syntax and semantics of for loop, while loop, and do while loop

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: This is an infinity loop, nothing output 1, 2, 3, 4, 5, 6, There is a syntax error 5, Response Feedback: Students shall know the syntax and semantics of for loop, while loop, and do while loop.

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 Response Feedback: Students shall pay attention to the syntax of if, if-else, if-esel if -else, and switch structure For switch, pay attention to missing break or : Know the consequence if these two are missing Shall be case 'A':case 'a': change other , to : too

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 Response Feedback: students shall understand built-in C-string functions and know how to implement them

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

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"; Response Feedback: The students shall know how to correctly declare and initialize variables of type int, char, double, and bool

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. Answers: Cannot do it cout << s; cin >> s; s >> info; Response Feedback: The student shall know how to oeverload operators +, -, *, /, >>, and <<. if it is needed

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 << "Before throw \n"; cout << "After catch \n"; cout << "Caught Exception \n"; cout << "After throw \n"; Response Feedback: Students shall understand how exception is handled

cout << "After throw \n";

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); Response Feedback: Students need to know how to open a file to read or write and handle the situation that file open fails

cout << "Cannot open file. Exit the progam";

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 = "filename"; inFile.open(filename); inFile = filename; inFile.open("filename"); Response Feedback: Student shall know how to open file to read or write and handle the situation that file cannot be open successfully.

inFile.open(filename);

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); Response Feedback: Students shall know how to write Child class. Understand the virtual methods, and the accessibility of parent class' methods and attributes

int getId() const;

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 = 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]; Response Feedback: Student shall know array declaration, initialization, and the algorithm related to array, for instance, find max, min, sum, and product of array elements. be able to search and sort array

int result = 0;

Which of the following doesn't have 5 as output? Answers: int x = 5; cout << ++x; int x = 5; cout << x++; cout << 40%7; cout << 40/7; Response Feedback: The students shall know operators +, -, *, /, %, ++, --, and their precedence.

int x = 5;

To overload functions with symbolic names (like + - / <<), you must use the keyword ________ before the symbolic name. Answers: const operator void reference Response Feedback: The student shall know how to oeverload operators +, -, *, /, >>, and <<. if it is needed

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; Response Feedback: Students shall be able to write and trace recursive algorithm

return 1;

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; } Response Feedback: Students shall understand the built in C-string functions such as strlen, strcat, strcpy, strcmp, strncpy, strncat, and their implementations

size_t mystrlen(const char * source){

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

str1 = "toaster";

Assume that char name[80] = "John Doe"; char nameCopy[80]; Whihc of the following assign "John Doe" to nameCopy? Answers: nameCopy = name; strcpy(name, nameCopy); strcpy(nameCopy, name); name = nameCopy; Response Feedback: Students shall know how to do C-string assignment, copy, comparison, query length, and concatenate.

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; }; Response Feedback: The students are require to know how to write and use struct

struct Student{

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; Response Feedback: Know how to write and use struct

student->gpa = 3.95;

Which of the following access a variable named next in structure *student ? Answers: student-next student'next student->next student Response Feedback: know how to write and use struct

student->next

Which of the following is the head of destructor of Employee class? Answers: void ~Employee() ~Employee(const Employee& other) void ~Employee(const Employee& other) ~Employee(Employee& other) void ~Employee(Employee& other) ~Employee() Response Feedback: Students are required to know how to write big three: Copy constructor, Destructor, and Assignment Operator for a class

~Employee()


Set pelajaran terkait

15.1 Fertilization and Embryonic Development

View Set

Dynamic Quizzes | BP Integration (ATI)

View Set

EMERGENCY ACTION PLANS AND FIRE PROTECTION quiz part 3 (1-1)

View Set

Logical Reasoning Question types

View Set

Chapter 3- Policy Riders, Provisions, Options and Exclusions

View Set