Documente Academic
Documente Profesional
Documente Cultură
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.
Show what would be printed for each of the cout statements shown below.
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
class complex{
int real, Imaginary;
(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;
p = &imac;
p->print();
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:
[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]