Sunteți pe pagina 1din 10

CS 2255 Final Exam Sample

Total Time: 90 minutes Total Points: 60


Write your name clearly. Answer all the questions.
First, read all the questions. Start with the ones that you find easier and then move on to the
tougher ones. You have sufficient space; if you need more space, use the other side of your
question sheet.
Name:______________________________ Date:_________________

Question 1. True/False [8]

1. A linked list is called "linked" because each node in the series has a pointer that points
to the next node in the list.
2. A linked list can grow and shrink as a program runs
3. If an exception is not caught, it is stored for later use.
4. The try/catch/throw construct is able to handle only one type of exception in a try block.
5. An alternative to using the open member function is to use the file stream object
declaration itself to open the file.
6. File output may be formatted the same way as console screen output.
7. When data is read from a file, it is automatically stored in a variable.
8. By default, files are opened in binary mode.

Question 2. What’s Printed?. [6]

Show what would be printed for each of the cout statements shown below.

char string1[30] = "In the Garden";


char string2[15] = "of Eden";
strcat(string1,string2);
cout << string2;

int num[7] = {10,3,4};


int *ptr = &num[2];
cout<< ++*ptr++<<endl;
cout << --*ptr++;

enum {poodle, boxer, terrier} ;

cout << poodle < terrier << " "<<


boxer << " "<< endl;
Question 3. What would the following program print [4]

class test {
public:
static int a;
static int b;
test(int x) {
b=x;
a=b+1;
}
};
int test::a = 0;
int test::b=10;
int main(void) {
test b1(5);
test b2(12);
cout << "b1.a is " << b1.a << endl;
cout << "b1.b is " << b1.b << endl;
cout << "b2.a is " << b2.a << endl;
cout << "b2.b is " << b2.b << endl;
}
Question 4. Objective questions [4]

1. Data types that are created by the 3. Which of the following statements opens
programmer are known as: the file info.txt for both input and output?
a. variables a. dataFile.open("info.txt", ios::in &&
ios::out);
b. abstract data types (ADT)
b. dataFile.open("info.txt", ios::in , ios::out);
c. functions
c. dataFile.open("info.txt", input || output);
d. parameters
d. dataFile.open("info.txt", ios::in | ios::out);
e. None of these

4. The _________ marker is the character


2. Before a structure can be used, it that marks the end of a file, and is
must be automatically written when the file is
a. declared closed.

b. deallocated a. End of File (EOF)

c. initialized b. No More Data (NMD)

d. All of these c. Data Stream Close (DSC)

e. None of these d. Data Read Stop (DRS)


e. None of these
Question 5. Write a swap function that accepts two char arrays only. Function should
swap the values stored in the arrays. See the example below: If the two character
arrays are A: “suman” and B: “Ben”, the new value of A and B should A = “Ben”, and
B: “Kumar”. Do not use any string function. [8]
Question 6. Review the following class [6]

class complex{
int real, Imaginary;

public: // include your methods (constructors, overload +, overload -


. here)
};

Provide the following:

a. 1. constructor with no argument and initialize the real and Imaginary to 0


2. constructor with two arguments and initialize real and imaginary to these arguments
b. overload +
c. overload - operator
Question 7. (a) Write the definition of a class named "computer" which includes the
attribute "name" (a string, the name of the computer) and a print method that prints out
this name. [2]

(b) Also write the definition of a class named "Macintosh" that is derived from the
class "computer" (computer is the base class) and includes the attribute "color" (a
string) and a method named print that prints out the computer name and color. [4]

Here is main function that will test these objects, it uses a pointer to treat a Macintosh
object as a computer object, and calls the print method.

Code:

int main() {

computer mycomputer;

computer *p;

Macintosh imac("Joe's IMAC","Blue");

p = &imac;

p->print();

I want the printout of this test program to be something like:

Name: Joe's IMAC

Color: Blue

That it, I want the color printed out even though the computer class does not have any
knowledge of the color attribute. (in other words - make sure your system is
polymorphic).

that it, I want the color printed out even though the computer class does not have any
knowledge of the color attribute. (in other words - make sure your system is
polymorphic).
(c) How would you turn computer class into an abstract class? Show your change.

Also, after this change, which line in the above code will throw an error if executed [2]
Question 8. Write a recursive function called Fibonacci(n) that returns nth number in
Fibonacci series. In fibonnaci series each number after the first number is the
summation of last two numbers. For example following sequence is Fibonacci series:

1,1,2,3,5,8,13,21,34... That means Fibonacci(6) should be 8 because


the 6th Fibonacci number is 8 in that series [5]

[hint: You must have a termination condition and also, express the Fibonacci number
in recursive equation form based on the above definition]
Question 9. Write a base class called Rectangle, which has two private member
variables length and width. Write a constructor functions to set the values of width and
length of the rectangle and also a function called area, where you calculate the area of
the rectangle. Next derive a class called Square from Rectangle. Note that square
has no member variable rather it uses the member variable from base class
rectangle. Write constructor for this class and also provide a function called
areaSquare to calculate area of square [6]
Question 10. Write template for two functions Average and Maximum. The Average
function should accept two arguments: an array and its size and return the average of
all the elements in the array. The maximum function should also accept two arguments:
an array and its size and return the largest value among all the elements in the array.
Design a simple program that demonstrates the templates with various data types
(int, float, double). [2+2+1]
Bonus Question Define and differentiate between following [6]

1. Procedural programming and Object oriented programming

2. string and char data-types

3. throw block and catch block in exception handling

4. Doubly linked list and Circular linked list

5. Recursion and Iteration

6. Stack and Queue data structure

S-ar putea să vă placă și