FINALTERM EXAMINATION
CS201 - Introduction to programming
Final Term Spring 2010
Operator overloading can be performed through__________________.
► Classes
► Functions
► Operators
► Reference
Question No: 2 ( Marks: 1 ) - Please choose one
When a value is referred by a normal variable then it is known as,
► Direct Reference
► Indirect Reference
► Partial Reference
► Proper Reference
Question No: 3 ( Marks: 1 ) - Please choose one
Which of the following function is used to increase the size of already allocated memory chunk?
► malloc
► calloc
► realloc
► free
Question No: 4 ( Marks: 1 ) - Please choose one
Which of the following is NOT a preprocessor directive?
► #error
► #define
► #line
► #ndefine
Question No: 5 ( Marks: 1 ) - Please choose one
The stream objects cin and cout are included in which header file?
► iostream.h
► fstream.h
► istream.h
► ostream.h
Question No: 6 ( Marks: 1 ) - Please choose one
Overloaded delete operator function takes the same parameter as an argument returned by new operator function.
► True
► False
Question No: 7 ( Marks: 1 ) - Please choose one
When an array of object is created dynamically then there is no way to provide parameterized constructors for array of objects.
► True
► False
Question No: 8 ( Marks: 1 ) - Please choose one
C is widely known as development language of _______ operating system.
► Linux
► Windows
► Unix
► Mac OS
Question No: 9 ( Marks: 1 ) - Please choose one
Computer can understand only machine language code.
► True
► False
Question No: 10 ( Marks: 1 ) - Please choose one
We can not define a function as a friend of a Template class.
► True
► False
Question No: 11 ( Marks: 1 ) - Please choose one
What will be the value of 'a' and 'b' after executing the following statements?
a = 3;
b = a++;
► 3, 4
► 4, 4
► 3, 3
► 4, 3
Question No: 12 ( Marks: 1 ) - Please choose one
Consider the following code segment. What will be the output of following code?
int addValue (int *a){
int b = (*a) + 2;
return b ;
}
main () {
int x =6 ;
cout << x << "," ;
cout << addValue(&x) << "," ;
cout << x ;
}
► 6,8,6
► 6,6,8
► 6,8,8
► 6,6,6
Question No: 13 ( Marks: 1 ) - Please choose one
_______ is used to trace the logic of the program and correct the logical errors.
► Compiler
► Editor
► Linker
► Debugger
Question No: 14 ( Marks: 1 ) - Please choose one
new and delete are _____ whereas malloc and free are _____.
► Functions, operators
► Classes, operators
► Operators, functions
► Operators, classes
Question No: 15 ( Marks: 1 ) - Please choose one
Like member functions, ______ can also access the private data members of a class.
► Non-member functions
► Friend functions
► Any function outside class
► None of the given options
Question No: 16 ( Marks: 1 ) - Please choose one
Which situation would require the use of a non-member overloaded operator?
► The overloaded operator is an Assignment operator.
► The left most operand is an object of a class.
► The left operand is built-in data type. ..................100% checked
► The operator returns a reference.
Question No: 17 ( Marks: 1 ) - Please choose one
The stream insertion and stream extraction operators are already overloaded for ______.
► User-defined data types
► Built-in data types
► User-defined and built-in data types
► None of the given options
Question No: 18 ( Marks: 1 ) - Please choose one
If we define an identifier with the statement #define PI 3.1415926 then during the execution of the program the value of PI __________.
► can not be replaced
► None of the given options
► Remain constant.
► can be changed by some operation
Question No: 19 ( Marks: 1 ) - Please choose one vuzs
Assignment operator is -------------------------associative.
► right
► left
► binary
► unary
Question No: 20 ( Marks: 1 ) - Please choose one
When ever dynamic memory allocation is made in C/C++, it is freed_____________.
► Explicitly
► Implicitly
► Both explicitly and implicitly
► None of the given options
Question No: 21 ( Marks: 1 ) - Please choose one
The appropriate data type to store the number of rows and colums of the matrix is____________.
► float
► int
► char
► none of the given options.
Question No: 22 ( Marks: 1 ) - Please choose one
Which of the following function do NOT initialize the chunk of memory to all zero?
► calloc() function
► Both malloc() and calloc()
► None of the above
► malloc() function
Question No: 23 ( Marks: 1 ) - Please choose one
The function free() returns back the allocated memory got thorough calloc and malloc to _____ .
► stack
► heap
► stack and heap
► None of the given options
Question No: 24 ( Marks: 1 ) - Please choose one
width() is member function of _____________
► cin object
► cout object
► Both cin and cout object
► None of the given option
Question No: 25 ( Marks: 1 ) - Please choose one
Templates are not type safe.
► true
► false
Question No: 26 ( Marks: 1 ) - Please choose one
A Matrix can be composed of ints, floats or doubles as their elements. Best way is to handle this , _______________
► Write a separate class to handle each
► Use templates
► Use strings to store all types
► None of the given options
Question No: 27 ( Marks: 2 )
Give the general syntax of class template.
Answer:
template <class T> // where T is generic data type
class myclassname()
{
//class definition;
} ;
Question No: 28 ( Marks: 2 )
What is a truth Table?
Answer:
A truth table is mathematical tool used in logic design, can be used to simplify (minimization of logic). It is used to solve the complex logic by entering data for each combination of inputs and evaluating output for each. The resultant logic can be implemented using universal gates easily. True state is normally represented by T or 1 and false is represented F or 0. We can select all zeros or ones from output for implementation using AND or OR gates.
Question No: 29 ( Marks: 2 )
What will be the output of following code, if user input a number 123?
int input ;
cin >> oct >> input;
cout << hex << input ;
Answer:53
Question No: 30 ( Marks: 2 )
What is principle of friendship in the context of functions and classes?
Answer:
- A class can declare friend function and someone from the outside cannot declare itself friend of class.
- A friend function can access private data members of class as member function can access
Question No: 31 ( Marks: 3 )
What are the limitations of the friendship relation between classes?
Answer:
Now let's talk about the limitations of this friendship business. Firstly, there is no transitive dependency in friend declarations. Suppose I say student A is my friend and being a friend he knows my thoughts and ideas. Now the student A says "student B is my friend" i.e. student B knows thoughts and ideas of student A. Does it mean that student B is also my friend? Does student B knows my thoughts and ideas? The answer is no. As I have not declared student B a friend of mine, so he (student B) does not know about my thoughts and ideas. The same applies to the friend definition for classes. The friendship is not transitive. It is not like 'A is a friend of B and B is a friend of C, therefore A is a friend of C'. It does not work. A has to specifically declare 'B is my friend and C is my friend' to make B and C friends of him. There is no transitive dependency in friend declarations.
Secondly, I can declare you to be my friend. This means I have unveiled my thoughts and ideas to you. But I cannot get your thoughts and ideas unless you declare me a friend of yours. So there is no association, which means A saying B is my friend does not imply in any way that A is a friend of B. Here B is a friend of A. But B has to declare 'A' its friend. Thus the friend keyword produces one-way relationship.
Question No: 32 ( Marks: 3 )
Suppose an object of class A is declared as data member of class B.
(i) The constructor of which class will be called first?................a
(ii) The destructor of which class will be called first? ................b
Question No: 33 ( Marks: 3 )
Define static variable. Also explain life time of static variable?
- A variable declared statically, initialized only once in life time of program.
- Its life extends over life time of program
Question No: 34 ( Marks: 5 )
Write a program which defines three variables of type double which store three different values including decimal points, using setprecision manipulators to print all these values with different number of digits after the decimal number.
#include <stdio.h>
#include<iomanip.h>
main()
{
double first=3.1454454;
double second=23412.213123;
double third=37482374.23432489234;
cout <<fixed << setprecision(3) << first << endl;
cout <<flush;
cout << setprecision(5) << first << endl;
cout << scientific << setprecision(5) << first << endl;
cout << setprecision(5) << first << endl;
system("pause");
}
Question No: 35 ( Marks: 5 )
Let we have a class,
class String
{
private:
char buf[25];
};
Write code for assignment (=) operator function which assign one String object to other object. Your code should also avoid self assignment
Answer:
#include<iostream.h>
#include<string.h>
class String
{
private:
char buf[25];
public:
void displaybuf();
void inputbuf();
String& operator = ( const String &other );
};
void String::displaybuf()
{
cout << buf;
}
void String::inputbuf()
{
cin >> buf;
}
String& String::operator = ( const String &other )
{
if (this != &other)
{
strcpy(buf,other.buf);
return *this;
}
}
main()
{
String a,b,c;
a.inputbuf();
c=b=a;
b.displaybuf();
c.displaybuf();
system("pause");
}
Question No: 36 ( Marks: 5 )
Read the given below code and explain what task is being performed by this function
Matrix :: Matrix ( int row , int col )
{
numRows = row ;
numCols = col ;
elements = new ( double * ) [ numRows ] ;
for ( int i = 0 ; i < numRows ; i ++ )
{
elements [ i ] = new double [ numCols ] ;
for ( int j = 0 ; j < numCols ; j ++ )
elements [ i ] [ j ] = 0.0 ;
}
}
Hint : This function belong to a matrix class, having
Number of Rows = numRows
Number of Columns = numCols
No comments:
Post a Comment