CS201 All Finalterm Papers Mega File
CS201 All Finalterm Papers Mega File
CS201 All Finalterm Papers Mega File
KKCENTRE MPP
FINALTERM EXAMINATION
CS201- Introduction to Programming
( Marks: 1 ) - Please choose one
Question No: 2
Question No: 3
Within the statement obj1=obj2; obj1 will call the assignment operator function and obj2 will be passed as an argument to
function.
True
False
Prepaid By NAJAM-Ul-HASSAN
Question No: 4
KKCENTRE MPP
( Marks: 1 ) - Please choose one
What is the sequence of event(s) when deallocating memory using delete operator?
Only block of memory is deallocated for objects
Only destructor is called for objects
Memory is deallocated first before calling destructor
Destructor is called first before deallocating memory
Question No: 5
The second parameter of operator functions for << and >> are objects of the class for which we are overloading these
operators.
True
False
Question No: 6
To include code from the library in the program, such as iostream, a directive would be called up using this command.
#include iostream.h
include <iostream.h>
include <iostream.h>
#include <iostream.h>
Prepaid By NAJAM-Ul-HASSAN
Question No: 7
KKCENTRE MPP
( Marks: 1 ) - Please choose one
Question No: 8
Question No: 9
For which values of the integer _value will the following code becomes an infinite loop?
int number=1;
while (true) {
cout << number;
if (number == 3) break;
number += integer_value; }
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
only 1
only 2
Question No: 10
Question No: 11
Question No: 12
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
None of the given options
Question No: 13
Which of the following is the correct C++ syntax to allocate space dynamically for an array of 10 int?
new int(10) ;
new int[10] ;
int new(10) ;
int new[10];
Question No: 14
Unary operator implemented as member function takes ____ arguments whereas non-member function takes _____ arguments.
One, zero
Zero, one
One, two
Two, one
Prepaid By NAJAM-Ul-HASSAN
Question No: 15
KKCENTRE MPP
( Marks: 1 ) - Please choose one
The first parameter of overloaded stream insertion operator is _________ where second parameter is _______
Question No: 16
Question No: 17
Question No: 18
While calling function, the arguments are assigned to the parameters from _____________.
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
left to right.
right to left
no specific order is followed
none of the given options.
Question No: 19
Question No: 20
If we define an identifier with the statement #define PI 3.1415926 then during the execution of the program the value of PI
__________.
cannot be replaced
None of the given options
Remain constant.
can be changed by some operation
Question No: 21
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Assignment operator is -------------------------associative.
right
left
binary
unary
Question No: 22
If text is a pointer of class String then what is meant by the following statement?
text = new String [5];
Creates an array of 5 string objects statically
Question No: 23
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
The return type of the operator function for << operator is __________.
class for which we overload operator
reference of ostream class (ostream&)
reference of istream class (istream&)
void
Question No: 24
Question No: 25
Memory allocated at run time is a system resource and it is the responsibility of _____ to de-allocate the memory.
System
Programmer
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
User of program
None of given options
Question No: 26
Question No: 27
( Marks: 2 )
Question No: 28
( Marks: 2 )
Answer:
The difference between endl and \n is that endl is use to start a new line for the next row
And \n is a new line character.
Question No: 29
( Marks: 2 )
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
What is the this pointer? Give an example of its use.
Answer:
This pointer is use to points to the current object in programming.
Question No: 30
( Marks: 2 )
Identify each of the following as function call, function definition and function declaration.
int func(int num1, int num2);
Function call:
Function ; Function definition: Integer; Function declaration: Num1
and Num2
int func(int, int);
Function call:
Function ; Function definition: Integer; Function declaration: integers
func(5, 6) ;
Function call:
Function ; Function definition: numbers; Function declaration: 5&6
int func(int num1, int num2){}
Function call:
Function ; Function definition: Integer; Function declaration: Num1 and Num2 from
user
Question No: 31
( Marks: 3 )
Consider the following code segment. What will be the output of the following code segment?
class class1{
public:
class class2{
public:
class2(){
cout << Calling default constructor of class2\n ;
}
};
class1(){
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
cout << Calling default constructor of class1\n ;
}
};
main(){
class1::class2 obj1;
class1 obj2 ;
}
Question No: 32
( Marks: 3 )
( Marks: 3 )
Question No: 34
( Marks: 5 )
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Operator overloading is to allow the same operator to be bound to more than one implementation, depending on the types of
the operands.
Question No: 35
( Marks: 5 )
Why the first parameter of operator function for << operator must be passed by reference?
Answer:
Operator<<'s first parameter must be an ostream passed by reference. Its second parameter, the IntList that is printed, does not
have to be passed as a const-reference parameter; however it is more efficient to pass it by reference than by value (since that
avoids a call to the copy constructor), and it should not be modified by operator<<, so it should be a const reference parameter
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 ;
}
}
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
FINALTERM EXAMINATION
CS201- Introduction to Programming
Time: 90 min
Marks: 58
Question No: 1 ( Marks: 1 ) - Please choose one
&& is -------------------- operator.
An arithmetic
Logical
Relational
Unary
Question No: 2
When we use manipulators in our program then which header file should be included?
iostream.h
stdlib.h
stdio.h
iomanip.h
Question No: 6
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
True
False
Question No: 7
Overloaded delete operator function takes the same parameter as an argument returned by new operator function.
True
False
Question No: 13 ( Marks: 1 ) - Please choose one
The second parameter of operator functions for << and >> are objects of the class for which we are overloading these
operators.
True
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
False
Question No: 14
Which character is inserted at the end of string to indicate the end of string?
new line
tab
null
carriage return
Question No: 18
Which of the following function calling mechanism is true for the function prototype given below?
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
float func(float &);
Call by value
Call by reference using pointer
Call by reference using reference variable
None of the given options
Question No: 21
Overloaded delete operator function takes parameter of void pointer and returns ________.
void
void pointer
pointer to an object
pointer of type int
Question No: 23
It is a way of reusing the code when we contain objects of our already written classes into a new class,
True
False
Question No: 25
The functions used for dynamic memory allocation return pointer of type ______
int
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
float
void
double
Question No: 27
( Marks: 2 )
Write a declaration statement for an array of 10 elements of type float. Include an initialization statement of the first four
elements to 1.0, 2.0, 3.0 and 4.0.
Answer:
float floatArry[10] = {1.0,2.0,3.0,4.0};
Question No: 28 ( Marks: 2 )
Write the general syntax for the declaration of pre-increment and post-increment member operator function.
Classname operator ++(); ---- pre increment
Classname operator ++(int) ---- post increment
Question No: 29 ( Marks: 2 )
What is difference between endl and \n?
Question No: 30 ( Marks: 2 )
What does code optimization mean?
Question No: 31 ( Marks: 3 )
How is the following cout statement interpreted by compiler?szuv
cout << a << b << c ;
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? Answer : A
(ii) The destructor of which class will be called first? Answer : B
Question No: 33 ( Marks: 3 )
Define static variable. Also explain life time of static variable?
Question No: 34 ( Marks: 5 )
What is difference between Unary and binary operators and how they can be overloaded?
Question No: 35 ( Marks: 5 )
What steps we must follow to design good program?
Question No: 36 ( Marks: 5 )
Write a program which defines five variables which store the salaries of five employees, using setw and setfill manipulators to
display all these salaries in a column.
Note: Display all data with in a particular width and the empty space should be filled with character x
Output should be displayed as given below:
xxxxxx1000
xxxxxx1500
xxxxx20000
xxxxx30000
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
xxxxx60000
FINALTERM EXAMINATION
CS201- Introduction to Programming (Session - 1)
Question No: 1 ( Marks: 1 ) - Please choose one
To access the data members of
Question No: 2
( Marks: 1 )
( Marks: 1 )
Question No: 4
( Marks: 1 )
of Class Constructor.
True
False
Prepaid By NAJAM-Ul-HASSAN
Question No: 5
KKCENTRE MPP
( Marks: 1 )
define as member function for a Unary operator then the number of argument it take is/are,
Zero
One
Two
N arguments
Question No: 6
( Marks: 1 )
operator function is
( Marks: 1 )
______________ .
Member function
Non-member function
Private function
Public function
Question No: 8
( Marks: 1 )
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
False
Question No: 9
( Marks: 1 )
Question No: 10
( Marks: 1 )
( Marks: 1 )
Question No: 12
( Marks: 1 )
function by default.
True
False
Question No: 13
( Marks: 1 )
( Marks: 1 )
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
int arr[2][3] = {0,0} ;
int arr[2][3] = {{0},{0}} ;
int arr[2][3] = {0},{0} ;
int arr[2][3] = {0} ;
Question No: 15
( Marks: 1 )
define as member function then operand on the left side of operator must be an object.
True
False
Question No: 16
( Marks: 1 )
( Marks: 1 )
( Marks: 1 )
private, public
public, private
private, protected
public, protected
Question No: 19
( Marks: 1 )
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Which of the following is true for
Question No: 20
( Marks: 1 )
obj2 will be passed as an argument to + operator whereas obj2 will drive the + operator
obj1 will drive the + operator whereas obj2 will be passed as an argument to + operator
Both objects (obj1, obj2) will be passed as arguments to the + operator
Any of the objects (obj1, obj2) can drive the + operator
Question No: 21
( Marks: 1 )
One, zero
Zero, one
One, two
Two, one
Question No: 22
( Marks: 1 )
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Structures, function
Objects, member functions
Functions, objects
None of the given options
Question No: 23
( Marks: 1 )
( Marks: 1 )
_______________.
Constructor
Destructor
Both a constructor and a destructor
None of the given options
Question No: 25
( Marks: 1 )
Question No: 26
( Marks: 1 )
segment.
class M {
public:
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
M &operator+(const M &);
...
};
p+q
//code of line implies that p.operator+(q)
...
Let assume if p and q are class objects then function is implemented as _______
Member function
Non-member function
Friend function
None of the given options
Question No: 27
( Marks: 1 )
------------associative.
right
left
binary
unary
Question No: 28
( Marks: 1 )
Question No: 29
( Marks: 1 )
Prepaid By NAJAM-Ul-HASSAN
Question No: 30
KKCENTRE MPP
( Marks: 1 )
new object with a full copy of the other object, is copy is known as ___________
deep copy
shallow copy
constructor copy
none of the options
Question No: 31
( Marks: 1 )
What is the use of reference data
type?
A reference data type is a variable that can contain an address. The reference data types in Java are arrays, classes and
interfaces. You'll hear often say that Java does not have pointers. Yet, you could consider a reference data type to be a pointer
Question No: 32
( Marks: 1 )
What are the main types of
The difference is in the number of arguments used by the function. In the case of binary operator overloading, when the
function is a member function then the number of arguments used by the operator member function is one (see below
example). When the function defined for the binary operator overloading is a friend function, then it uses two arguments.
Question No: 33
( Marks: 2 )
Question No: 34
( Marks: 2 )
What are manipulators? Give one
example.
Manipulators are operators used in C++ for formatting output. The data is manipulated by the programmers choice of
displayed endl manipulator. This manipulator has the same functionality as the \n newline character.
Question No: 35
( Marks: 3 )
What will be the output of
1)
void func1(){
int x = 0;
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
x++;
cout << x << endl;
}
Output will be:
1
1
1
2)
void func2(){
static int x = 0 ;
x++;
cout << x << endl ;
}
Output will be:
1
2
3
Question No: 36
( Marks: 3 )
If the requested memory is not
available in the system then what does calloc/malloc and new operator return?
malloc returns a void pointer to the allocated space or NULL if there is insufficient memory available. To return a pointer to a
type other than void, use a type cast on the return value. The storage space pointed to by the return value is guaranteed to be
suitably aligned for storage of any type of object. If size is 0, malloc allocates a zero-length item in the heap and returns a valid
pointer to that item.
By default, malloc does not call the new handler routine on failure to allocate memory. You can override this default behavior
so that, when malloc fails to allocate memory, malloc calls the new handler routine in the same way that the new operator does
when it fails for the same reason.
Question No: 37
( Marks: 3 )
( Marks: 5 )
Write down the disadvantages of
the templates.
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
They can present seriously confusing syntactical problems esp. when the code is large and spread over several header and
source files.
Then, there are times, when templates can "excellently" produce nearly meaningless compiler errors thus requiring extra care
to enforce syntactical and other design constraints. A common mistake is the angle bracket problem.
Question No: 39
( Marks: 5 )
The following code segment has
Question No: 40
( Marks: 10 )
Write a program which consists of
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Date()
{
day=0;
month=0;
year=0;
}
void setDay(int);
void setMonth (int);
void setYear(int);
int getDay();
int getMonth();
int getYear();
void showDate();
};
void Date: :setDay(int d)
{
if{d<1 | | d>31)
cout<<"Invalid month Renter it";
cin>>d;
}
day=d;
}
void Date: :setMonth (int m)
{
if(m<1 | | m>12)
{
cout<<"Invalid month Renter it";
cin>>m;
}
month=m;
}
void Date: :setYear (int y)
{
year=y;
int Date: :getDay()
{
return day;
}
int Date: :getMonth()
{
return month:
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
}
int Date: :getYear()
{
return year;
}
void Date: :showDate()
{
cout<<day<<"-"<<month<<"-"<<year<<end1;
}
Class Person
{
public:
char *Name;
char *Address
Date Bday;
public:
Student()
{
Name=new char[20];
Address=new char[10];
cin.getline(Name,20);
cout<<"Enter Address:";
cin.getline(Address,10);
}
void setDate()
{
cout<<"Enter Day:";
cin>>Ad_date.day;
cout<<"Enter month:";
cin>>Ad_date.month;
cout<<"Enter Year:";
cin>>Ad_date.year;
}
void Display()
{
cout<<"Name: "<<end1;
cout<<"Address: "<<Address<<end1;
cout<<"Date of Birth: ";
Ad-date.showDate();
}
};
void main()
{
Person object;
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
object.setDate();
object.Display();
system("pause");
}
Question No: 41
( Marks: 10 )
Write a C++ program that
#include <stdio.h>
#include <iostream>
#include <cstring>
using namespace std;
class myclass
public:
int a;
int b;
int *iptr, *sptr;
construct{int,int.int}
void seta(int);
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
void setb(int);
void setc(int);
int geta();
int getb();
int getc();
};
void Person: :seta(int aa)
{
a=aa;
}
void Person: :setb (int bb)
{
b=bb;
}
void Person: :setc (int cc)
{
c=cc;
}
main()
{
int num;
cout<<"Enter the number of objects to be created";
cin>>num;
for (int i =1;i==num;i++)
{
Person i_
}
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
FINALTERM EXAMINATION
CS201- Introduction to Programming
Question No: 1
In C/C++ if we define an array of size eight (8) i.e. int Arr [8]; then the last element of this array will be stored at,
Arr[0]
Arr[8]
Arr[7]
Arr[-1]
Question No: 2
When an array is passed to a function then default way of passing this array is,
By data
By reference
By value
By data type
Question No: 3
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Data Type
Data
Question No: 4
Question No: 5
Question No: 6
What is the sequence of event(s) when allocating memory using new operator?
Only block of memory is allocated for objects
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Only constructor is called for objects
Memory is allocated first before calling constructor
Constructor is called first before allocating memory
Question No: 7
We can delete an array of objects without specifying [] brackets if a class is not doing dynamic memory allocation internally.
True
False
Question No: 8
The second parameter of operator functions for << and >> are objects of the class for which we are overloading these
operators.
True
False
Question No: 9
Which of the following is correct way to initialize a variable x of int type with value 10?
int x ; x = 10 ;
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
int x = 10 ;
int x, x = 10;
x = 10 ;
Question No: 10
Default mechanism of function calling in case of array is _____ and in case of variable is ___.
Call by value, call by reference
Call by referene, call by reference
Call by reference, call by value
Call by value, call by value
Question No: 11
Prepaid By NAJAM-Ul-HASSAN
Question No: 12
KKCENTRE MPP
( Marks: 1 ) - Please choose one
Question No: 13
For which array, the size of the array should be one more than the number of elements in an array?
int
double
float
char
Question No: 14
new and delete are _____ whereas malloc and free are _____.
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Functions, operators
Classes, operators
Operators, functions
Operators, classes
Question No: 15
Member functions
Public member functions
Private member functions
Non-member functions
Question No: 16
The prototype of friend functions must be written ____ the class and its definition must be written ____
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
outside, inside the class
outside, outside the class
Question No: 17
If overloaded plus operator is implemented as non-member function then which of the following statement will be true for the
statement given below?
obj3 = obj1 + obj2 ;
obj2 will be passed as an argument to + operator whereas obj2 will drive the + operator
obj1 will drive the + operator whereas obj2 will be passed as an argument to + operator
Both objects (obj1, obj2) will be passed as arguments to the + operator
Any of the objects (obj1, obj2) can drive the + operator
Question No: 18
Which one of the following is the declaration of overloaded pre-increment operator implemented as member function?
Prepaid By NAJAM-Ul-HASSAN
Question No: 19
KKCENTRE MPP
( Marks: 1 ) - Please choose one
For cin, the source is normally a ________ and destination can be ______.
Question No: 20
Question No: 21
Prepaid By NAJAM-Ul-HASSAN
Question No: 22
KKCENTRE MPP
( Marks: 1 ) - Please choose one
Question No: 23
While calling function, the arguments are assigned to the parameters from _____________.
left to right.
right to left
no specific order is followed
none of the given options.
Question No: 24
When an operator function is defined as member function for a binary Plus (+) operator then the number of argument it take
is/are.
Zero
One
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Two
N arguments
Question No: 25
With user-defined data type variables (Objects), self assignment can produce __________.
Syntax error
Logical error
Link error
Non of the given options
Question No: 26
Assignment operator is used to initialize a newly declared object from existing object.
True
False
Question No: 27
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Constructor of enclosing class will be called first
Constructor of inner object will be called first
Constructor and Destructor will be called simultaneously
None of the given options
Question No: 28
Question No: 29
new operator allocates memory from free store and return _____________.
A pointer
A reference
An integer
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
A float
Question No: 30
Question No: 31
( Marks: 1 )
( Marks: 1 )
What are the two types of conversion for user-defined data types?
Types of conversion
The possible types of conversion are:
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Standard conversion:
This applies to:
Single-byte character sets (SBCS)
Graphic or double-byte character sets (DBCS)
Mixed character sets (containing SBCS and DBCS data)
Multi-byte character sets (MBCS)
By default, to binary data in INTEL format.
No conversion:
This applies to:
Character data encoded as UCS-2 or UTF-8
By default, to binary data in System
Packed decimal data.
Question No: 33
( Marks: 2 )
Is there a way to increase the size of already allocated memory chunk ? Can the same chunk be increased or not?
Ans:
Dynamic memory allocation often makes up a large part of program execution time. Different variants of the best-fit allocator
are implemented and their space and time costs measured and compared.
Reallocating an already allocated chunk uses the realloc() library function call.
Void realloc(void*ptr,size_t size)
Question No: 34
( Marks: 2 )
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
template <class myType>
myType GetMax (myType a, myType b)
{
return (a>b?a:b);
};
Question No: 35
( Marks: 3 )
Question No: 36
( Marks: 3 )
How many arguments does binary member operator function and binary non-member operator function take?
Ans:
A binary operator shall be implemented either by a non-static member function with one parameter or by a non-member
function with two parameters.
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Question No: 37
( Marks: 3 )
Ans:
It is multiplication of two multiplication so here I m presenting a sample code to multiply two matrix a , b and the result will
be stored in matrix C
#include<iostream.h>
void main()
{
int a[3][3] , b[3][3] , c[3][3];
int i , j , k;
cout<<"Enter Matrix A";
for( i = 0 ; i < 3 ; i++)
for( j = 0 ; j < 3 ; j++)
cin>>a[i][j];
cout<<"Enter Matrix B";
for( i = 0 ; i < 3 ; i++)
for( j = 0 ; j < 3 ; j++)
cin>>b[i][j];
for( i = 0 ; i < 3 ; i++)
for( j = 0 ; j < 3 ; j++)
{
c[i][j] = 0;
for( k = 0 ;k < 3 ; k++)
c[i][j] += a[i][k]*b[k][j];
}
cout<<"The resultant matrix is ";
for( i = 0 ; i < 3 ; i++)
{
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
for( j = 0 ; j < 3 ; j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
}
Question No: 38
( Marks: 5 )
Write the C++ syntax for making a class friend of other class.
Ans:
Ans:
Friend class is one which has been declared so (as a friend) inside other class to make it access the private members of the
class which has extended it's friendship.
For Example,
class A
{
private:
.......
public:
..............
friend class B;
};
class B
{
.......
..............
};
As in the above code snippet, class A has extended it's friendship to class B by declaring B as it's friend inside it's area.
Since the Class B has became a friend of A, B can directly access all the private members of A. But the reverse is not possible.
Prepaid By NAJAM-Ul-HASSAN
Question No: 39
KKCENTRE MPP
( Marks: 5 )
What is a template function? Give the general syntax of writing a template function.
Ans:
Function Templates
To perform identical operations for each type of data compactly and conveniently, use function templates. You can write a
single function template definition. Based on the argument types provided in calls to the function, the compiler automatically
instantiates separate object code functions to handle each type of call appropriately. The STL algorithms are implemented as
function templates.
Syntax:
Template<classT>
CLASS class-name()
{
//Definition of class
};
Class name<T>::function name (argument list)
{
//Function body
};
Question No: 40
( Marks: 10 )
Write a program which contains a class student. The class should contain two char pointer variables Name, and department.
The class should further contain constructors, overload the stream insertion operator (<<) for this class.
In main function create two objects and display these objects.
#include<iostream>
#include<conio>
#include <cstdlib>
using namespace std;
class student {
private:
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
char name[30] ;
char department[30] ;
public:
Question No: 41
( Marks: 10 )
Ans:
The C++ STL (Standard Template Library) is a generic collection of class templates and algorithms that allow programmers to
easily implement standard data structures like queues, lists and stacks.
Bottom of Form
STL Containers
The C++ STL provides two kinds of containers:
Sequence Containers
C++ Vectors
C++ Lists
C++ Double-Ended Queues
Associative Containers
C++ Maps
C++ Multimaps
C++ Sets
C++ Multisets
The idea behind the C++ STL is that the hard part of using complex data structures has already been completed. If a
programmer would like to use a stack of integers, all one has to do is use this code:
stack<int> myStack;
With minimal effort, one can now push and pop integers onto this stack. Through the magic of C++ Templates, one could
specify any data type, not just integers. The STL Stack class will provide generic functionality of a stack, regardless of the data
in the stack.
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Advantages of (STL)
Templates are easier to write then writing several versions of your similar code for different types. You create only one generic
version of your class or function instead of manually creating specializations.
Templates can be easier to understand, since they can provide a straightforward way of abstraction type information.
Templates are type-safe. This is because the types that templates act upon are known at compile time, so the compiler can
perform type checking before errors occur.
Templates help in utilizing compiler optimizations to the extreme.
FINALTERM EXAMINATION
CS201- Introduction to Programming
Question No: 1
( Marks: 1 ) - Please choose one
When we define an array of objects then,
Destructor will call once for whole array
Destructor will call for each object of the array
Destructor will never call
Depends on the size of array
Question No: 2
( Marks: 1 ) - Please choose one
We can also create an array of user define data type
True
False
Question No: 3
( Marks: 1 ) - Please choose one
What is the sequence of event(s) when allocating memory using new operator?
Only block of memory is allocated for objects
Only constructor is called for objects
Memory is allocated first before calling constructor
Constructor is called first before allocating memory
Question No: 4
( Marks: 1 ) - Please choose one
We can delete an array of objects without specifying [] brackets if a class is not doing dynamic memory allocation internally
True
False
http://vustudents.ning.com
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Question No: 5
( Marks: 1 ) - Please choose one
The declarator of Plus (+) member operator function is
Class-Name operator + (Class-Name rhs)
Operator Class-Name + ( )
Operator Class-Name + ( rhs)
Class-Name operator + ( )
Question No: 6
( Marks: 1 ) - Please choose one
The second parameter of operator functions for << and >> are objects of the class for which we are overloading these
operators
True
False
Question No: 7
( Marks: 1 ) - Please choose one
Which of the following is correct way to initialize a variable x of int type with
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
r
value 10?
int x ; x = 10 ;
int x = 10 ;
int x, x = 10;
x = 10 ;
Question No: 8
( Marks: 1 ) - Please choose one
Default mechanism of function calling in case of array is
Call by value, call by reference
Call by referene, call by reference
Call by reference, call by value
Call by value, call by value
Question No: 9
( Marks: 1 ) - Please choose one
What does STL stand for?
Source template library
Standard template library
Stream template library
Standard temporary library
Question No: 10
( Marks: 1 ) - Please choose one
Skill(s) that is/are needed by programmers
Paying attention to detail
Think about the reusability
Think about user interface
All of the given options
Question No: 11
( Marks: 1 ) - Please choose one
For which array, the size of the array should be one more than the number of elements in an array?
int
double
float
char
Question No: 12
( Marks: 1 ) - Please choose one
new and delete are
whereas malloc and free are
Functions, operators
Classes, operators
Operators, functions
Operators, classes
http://vustudents.ning.com
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Question No: 13
( Marks: 1 ) - Please choose one
The prototype of friend functions must be written the class and its definition
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
must be written
inside, inside the class
inside, outside the class
outside, inside the class
outside, outside the class
Question No: 14
( Marks: 1 ) - Please choose one
Friend function of a class are
of a class.
Non-member functions
Friend functions
Any function outside class
None of the given options
http://vustudents.ning.com
Question No: 15
( Marks: 1 ) - Please choose one
If overloaded plus operator is implemented as non-member function then which
of the following statement will be true for the statement given below?
obj3 = obj1 + obj2 ;
obj2 will be passed as an argument to + operator whereas obj2 will drive the + operator
obj1 will drive the + operator whereas obj2 will be passed as an argument
to + operator
Both objects (obj1, obj2) will be passed as arguments to the + operator
Any of the objects (obj1, obj2) can drive the + operator
Question No: 16
( Marks: 1 ) - Please choose one
Which one of the following is the declaration of overloaded pre-increment operator implemented as member function?
Class-name operator +() ;
Class-name operator +(int) ;
Class-name operator ++() ;
Class-name operator ++(int) ;
Question No: 17
( Marks: 1 ) - Please choose one
For cin, the source is normally a
and destination can be
File, native data type
Disk, user-define type
Keyboard, variable
File, user-define type
http://vustudents.ning.com
Question No: 18
( Marks: 1 ) - Please choose one
We can do condition compilation with pre processor directives.
True
False
Question No: 19
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
r
Zero
One
Two
Three
Question No: 21
( Marks: 1 ) - Please choose one
The default value of a parameter can be provided inside the
function prototype
function definition
both function prototype or function definition
none of the given options
Question No: 22
( Marks: 1 ) - Please choose one
While calling function, the arguments are assigned to the parameters from
left to right
right to left
no specific order is followed
none of the given options
Question No: 23
( Marks: 1 ) - Please choose one
When an operator function is defined as member function for a binary Plus (+)
operator then the number of argument it take is/are http://vustudents.ning.com
Zero
One
Two
N arguments
Question No: 24
( Marks: 1 ) - Please choose one
new operator allocates memory from free store and return
A pointer
A reference
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
An integer
A float
Question No: 25
With user-defined
self
assignment
can produce
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
r
Syntax error
Logical error
Link error
Non of the given options
Question No: 26
( Marks: 1 ) Write Simple Program
Assignment operator is used to initialize a newly declared object from existing object
True
False http://vustudents.ning.com
Question No: 27
( Marks: 1 ) Briefly define/Justify
When an object of a class is defined inside an other class then,
Constructor of enclosing class will be called first
Constructor of inner object will be called first
Constructor and Destructor will be called simultaneously
None of the given options
Question No: 28
( Marks: 1 ) Brief answer required
In the member initializer list, the data members are initialized,
From left to right
From right to left
In the order in which they are defined within class
None of the given options
Question No: 29
( Marks: 1) - Brief answer required
"new" and "delete" keywords are
in C++ language
Built-in- Function
Operators
Memory Allocation Function
None of the given options
Question No: 30
( Marks: 2 ) - Brief answer required
What are the two types of conversion for user-defined data types?
Question No: 31
( Marks: 2 ) - Brief answer required
Give the general syntax of class template.
Question No: 32
( Marks: 2 ) - Brief answer required
What is a constructor in class? http://vustudents.ning.com
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Question
No:
33
(
Marks: 2
)
Brief answer required
Is there a way to increase the size of already allocated memory chunk ? Can the same chunk be increased or not?
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
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
The same pointer that is returned by the new operator, is passed as an argument to the delete operator. These rules apply to
both, if operators (new and delete) are overloaded as member or non-member operators (as global operators).
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
if we are allocating an array of objects, there is no way to pass arguments to objects constructors. Therefore it is required that
the objects that are stored in such an array have a no-argument constructor.
Question No: 8 ( Marks: 1 ) - Please choose one
C is widely known as development language of _______ operating system.
Linux
Windows
Unix
Mac OS
In the start C became widely known as the development language of the UNIX operating system, and the UNIX operating
system was written by using this C language. The C language is so powerful that the compiler of C and other various operating
systems are written in C.
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
Class templates can have friends. A class or class template, function, or function template can be a friend to a template class.
Friends can also be specializations of a class template or function template, but not partial specializations.
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
a = 3;
b = a++;
3, 4
4, 4
3, 3
4, 3
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
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.
The operator returns a reference.
When an operator function is implemented as a non-member function, the left-most operand may be an object of the operators
class, an object of a different class, or a built-in type
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
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
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
The malloc function differs from calloc in the way that the space allocated by malloc is not initialized and contains any values
initially.
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
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
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
A Matrix can be composed of ints, floats or doubles as their elements. Instead of handling these data types separately, we can
write Matrix class as a template class and write code once for all native data types. While writing this template class, the better
approach to write will be, to go with a simple data type (e.g. double) first to write a Matrix class and then extend it to a
template class later.
Question No: 27 ( Marks: 2 )
Give the general syntax of class template.
template
class myclass { ---} ;
Question No: 28 ( Marks: 2 )
What is a truth Table?
There are some areas where the decision structures become very complicated. Sometimes, we find it difficult to evaluate a
complicated logical expression. Sometimes the logic becomes extremely complicated so that even writing it as a simple syntax
statement in any language. It becomes complicated to determine what will be evaluated in what way. We know the concept of
truth table. The truth tables are very important. These are still a tool available for analyzing logical expressions. We will read
logic design in future, which is actually to do with chips and gates. How we put these things together.
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 ;
53
Rational: it will take 123 as octal and print it in hex form which is 53.
Question No: 30 ( Marks: 2 )
What is principle of friendship in the context of functions and classes?
Class can declare a friend function and someone from outside the class cannot declare itself friend of a class.
A friend function can access the private variables of class just like a member function
Question No: 31 ( Marks: 3 )
What are the limitations of the friendship relation between classes?
Class can declare a friend class from inside and someone from outside the class cannot declare itself friend of a class.
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
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Question No: 33 ( Marks: 3 )
Define static variable. Also explain life time of static variable?
When you declare a static variable (native data type or object) inside a function, it is created and initialized only once during
the lifetime of the 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
#include
main () {
double a = 12.12345;
double b = 13.123456;
double c = 14.1234567;
cout << setprecision (5) << a << endl;
cout << setprecision (2) << a << endl;
cout << setprecision (3) << a << endl;
}
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:
void String::operator = ( const String &other )
{ int length ;
length = other.length();
delete buf;
buf = new char [length + 1];
strcpy( buf, other.buf ); }
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 ++ )
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
elements [ i ] [ j ] = 0.0 ;
}
}
Hint : This function belong to a matrix class, having
Number of Rows = numRows
Number of Columns = numCols
Marks: 58
Question No: 1
( Marks: 1 ) - Please choose one
*.doc is _____________ by type.
.
Sequential File
Random Access File
Data File
Record File
Question No: 2
( Marks: 1 ) - Please choose one
Which of the following is NOT a preprocessor directive?
#error
#define
#line
#ndefine
Question No: 3
( Marks: 1 ) - Please choose one
The return type of operator function must always be void.
True
False
The syntax of the prototype of the overloaded operator function is: return-type operator operator-symbol (parameter-list);
Question No: 4
( Marks: 1 ) - Please choose one
What does (*this) represents?
The current function of the class
The current pointer of the class
The current object of the class
A value of the data member
Whenever an object calls a member function, the function implicitly gets a pointer from the calling object. That pointer is
known as this pointer. this is a key word. We cannot use it as a variable name. this pointer is present in the function,
referring to the calling object. For example, if we have to refer a member, lets say buf, of our String class, we can write it
simply as: buf ;
Question No: 5
( Marks: 1 ) - Please choose one
The statement cin.get (); is used to,
Read a string from keyboard
Read a character from keyboard
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Read a string from file
Read a character from file
Question No: 6
( Marks: 1 ) - Please choose one
When we do dynamic memory allocation in the constructor of a class, then it is necessary to provide a destructor.
True
False
Question No: 7
( Marks: 1 ) - Please choose one
Overloaded new operator function takes parameter of type size_t and returns
void (nothing)
void pointer
object pointer
int pointer
Question No: 8
( Marks: 1 ) - Please choose one
The second parameter of operator functions for << and >> are objects of the class for which we are overloading
these operators.
True
False
The second parameter to operator << is an object of the class that we are overloading the operator for. Similar is the case
for operator >>.
Question No: 9
( Marks: 1 ) - Please choose one
C++ is a case-sensitive language
True
False
Question No: 10
( Marks: 1 ) - Please choose one
To include code from the library in the program, such as iostream, a directive would be called up using this
command.
#include iostream.h
include
include
#include
Question No: 11
( Marks: 1 ) - Please choose one
A template function must have only generic data types.
True
False
Its not compulsory, only min we have one generic data type but we can have native data type as well.
Question No: 12
( Marks: 1 ) - Please choose one
Template class can not have static variables.
True
False
Question No: 13
( Marks: 1 ) - Please choose one
What will be the correct syntax to assign an array named arr of 5 elements to a pointer ptr?
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
*ptr = arr ;
ptr = arr ;
*ptr = arr[5] ;
ptr = arr[5] ;
Question No: 14
( Marks: 1 ) - Please choose one
What will be the correct syntax to access the value of fourth element of an array using pointer ptr?
ptr[3]
(ptr+3)
*(ptr+3)
Both 1and 3
try this demo program to confirm result I wrote for you.
2 option will print the reference rest 1,3 are righ options
#include
#include
// #include
main()
{
int myarr [4]= {0,1,2,3};
int *ptr ;
ptr = myarr;
cout<
cout<<*(ptr+3);
cout<<(ptr+3);
int i = 0;
cin>> i;
}
Question No: 15
( Marks: 1 ) - Please choose one
If most significant bit of un-signed number is 1 then it represents a positive number.
True
False
The most significant bit is used as a sign bit. If this bit is zero, the number is considered positive. However, if it is 1, the
number will be considered negative.
Question No: 16
( Marks: 1 ) - Please choose one
If there is a symbol (& sign) used with the variable name followed by data type then it refers to _____ and if & is
being used with variable name then it refers to _____.
Address of variable, reference variable
Reference variable, value of variable
Reference variable, address of variable
Address of variable, value of variable
we see a data type followed by & sign, its a reference. And when the & sign is being used in the code with a variable
name then it is the address of the variable
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Question No: 17
( Marks: 1 ) - Please choose one
We can also do conditional compilation with preprocessor directives.
True
False
Question No: 18
( Marks: 1 ) - Please choose one
The default value of a parameter can be provided inside the ________________
function prototype
function definition
both function prototype or function definition
none of the given options.
The default value of a parameter is provided inside the function prototype or function definition.
Question No: 19
( Marks: 1 ) - Please choose one
Classes defined inside other classes are called ________ classes
looped
nested
overloaded
none of the given options.
Question No: 20
( Marks: 1 ) - Please choose one
What purpose do classes serve?
Data encapsulation
Providing a convenient way of modeling real-world objects
Simplifying code reuse
All of the given options
Question No: 21
( Marks: 1 ) - Please choose one
vuzs
Every class contains _______________.
Constructor
Destructor
Both a constructor and a destructor
None of the given options
Question No: 22
( Marks: 1 ) - Please choose one
new operator is used to allocate memory from the free store during
Compile Time
Run Time
Link Time
None of the given options
Question No: 23
( Marks: 1 ) - Please choose one
When an object of a class is defined inside another class then,
Destructor of enclosing class will be called first
Destructor of inner object will be called first
Constructor and Destructor will be called simultaneously
None of the given options
Question No: 24
( Marks: 1 ) - Please choose one
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
matrix [1] [2] = 0;
matrix [1] [2] = 0;
matrix [2] [0] = 0;
matrix [2] [1] = 0;
matrix [2] [2] = 0;
we can also do it as given below
int matrix [3][3] = { 0 }; //all elements 0
Question No: 31
( Marks: 3 )
Which one (copy constructor or assignment operator) will be called in each of the following code segment?
1) Matrix m1 (m2);
2) Matrix m1, m2;
m1 = m2;
3) Matrix m1 = m2;
Ans:
1) Matrix m1 (m2); copy constructor
2) Matrix m1, m2;
m1 = m2;
assignment operator
3) Matrix m1 = m2; assignment operator
Question No: 32
( Marks: 3 )
What will be the output of following function if we call this function by passing int 5?
template T reciprocal(T x) {return (1/x); }
Ans:
1/5
Question No: 33
( Marks: 3 )
Identify the errors in the following member operator function and also correct them.
math * operator(math m);
math * operator (math m)
{
math temp;
temp.number= number * number;
return number;
}
ANS:
The errors are in the arguments of the member operation function and also in the body of operator member
function.
Correct function should be
math *operator(math *m);
math *operator (math *m)
{
math temp;
temp = m;
temp.number= number * number;
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
return temp.number;
}
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.
Ans:
#include
#include
int main ()
{
double x1 = 12345624.72345
double x2 = 987654.12345
double x3 = 1985.23456
cout << setprecision (3) << x1<< endl;
cout << setprecision (4) << x2 << endl;
cout << setprecision (5) << x3<< endl;
return 0;
}
Question No: 35
( Marks: 5 )
What are the advantages and disadvantages of using templates?
Ans:
Many thing can be possible without using templates but it do offer several clear advantages not offered by any
other techniques:
Advanatages:
Templates are easier to write than writing several versions of your similar code for different types. You create
only one generic version of your class or function instead of manually creating specializations.
Templates are type-safe. This is because the types that templates act upon are known at compile time, so the
compiler can perform type checking before errors occur.
Templates can be easier to understand, since they can provide a straightforward way of abstracting type
information.
It help in utilizing compiler optimizations to the extreme. Then of course there is room for misuse of the
templates. On one hand they provide an excellent mechanism to create specific type-safe classes from a generic
definition with little overhead.
Disadvantages:
On the other hand, if misused
Templates can make code difficult to read and follow depending upon coding style.
They can present seriously confusing syntactical problems esp. when the code is large and spread over several
header and source files.
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Then, there are times, when templates can "excellently" produce nearly meaningless compiler errors thus
requiring extra care to enforce syntactical and other design constraints. A common mistake is the angle bracket
problem.
Question No: 36
( Marks: 5 )
Suppose a program has a math class having only one data member number.
Write the declaration and definition of operator function to overload + operator for the statements of main
function.
math obj1, obj2;
obj2= 10 + obj1 ;
Ans:
#include
math
{
mth operator + (obj1,obj2)
mth operator + (obj1,obj2)
{
mth operator + (obj1,obj2)
mth operator + (obj1,obj2)
}
}
FINALTERM EXAMINATION
CS201- Introduction to Programming
Time: 120 min
Question No: 1
types of software
Two
Three
Four
Five
Question No: 2
Marks: 75
_________________ .
Different
Identical
Two names of same function
None of the above
Question No: 3
( Marks: 1 ) - Please choose one
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
When a pointer is incremented, it
are manipulators.
True
False
Question No: 6
( Marks: 1 ) - Please choose one
In functions that return reference, use __________variables.
Local
Global
Global or static
None of the given option
Question No: 7
( Marks: 1 ) - Please choose one
The declarator of Plus (+) member
operator function is
Class-Name operator + (Class-Name rhs)
operator Class-Name + ( )
operator Class-Name + ( rhs)
Class-Name operator + ( )
Question No: 8
( Marks: 1 ) - Please choose one
The compiler does not provide a copy constructor if we do not provide it.
True
False
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Question No: 9
( Marks: 1 ) - Please choose one
What is the functionality of the following syntax to delete an array of 5 objects named arr allocated using new operator?
delete arr ;
Deletes all the objects of array
Deletes one object of array
Do not delete any object
Results into syntax error
Question No: 10
( Marks: 1 ) - Please choose one
What is the sequence of event(s) when allocating memory using new operator?
Only block of memory is allocated for objects
Only constructor is called for objects
Memory is allocated first before calling constructor
Constructor is called first before allocating memory
Question No: 11
( Marks: 1 ) - Please choose one
What is the sequence of event(s) when deallocating memory using delete operator?
Only block of memory is deallocated for objects
Only destructor is called for objects
Memory is deallocated first before calling destructor
Destructor is called first before deallocating memory
Question No: 12
( Marks: 1 ) - Please choose one
new and delete operators cannot be overloaded as member functions.
True
False
Question No: 13
( Marks: 1 ) - Please choose one
The operator function of << and >> operators are always the member function of a class.
True
False
Question No: 14
( Marks: 1 ) - Please choose one
A template function must have at
least ---------- generic data type
Zero
One
Two
Three
Question No: 15
( Marks: 1 ) - Please choose one
If we do not mention any return_value_type with a function, it will return an _____ value.
int
void
double
float
Such function which do not return any value are called:
int
void
double
float
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Question No: 16
( Marks: 1 ) - Please choose one
Suppose a program contains an array declared as int arr[100]; what will be the size of array?
0
99
100
101
Question No: 17
( Marks: 1 ) - Please choose one
The name of an array represents address of first location of array element.
True
False
Question No: 18
( Marks: 1 ) - Please choose one
Reusing the variables in program helps to save the memory
True
False
Question No: 19
( Marks: 1 ) - Please choose one
Which of the following option is true about new operator to dynamically allocate memory to an object?
The new operator determines the size of an object
Allocates memory to object and returns pointer of valid type
Creates an object and calls the constructor to initialize the object
All of the given options
Question No: 20
( Marks: 1 ) - Please choose one
new and delete are _____ whereas malloc and free are _____.
Functions, operators
Classes, operators
Operators, functions
Operators, classes
Question No: 21
( 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: 22
( Marks: 1 ) - Please choose one
Which of the following statement is best regarding declaration of friend function?
Friend function must be declared after public keyword.
Friend function must be declared after private keyword.
Friend function must be declared at the top within class definition.
It can be declared anywhere in class as these are not affected by the public and private keywords.
Question No: 23
( Marks: 1 ) - Please choose one
The operator function overloaded for an Assignment operator (=) must be
Non-member function of class
Member function of class
Friend function of class
None of the given options
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Question No: 24
( Marks: 1 ) - Please choose one
For non-member operator function, object on left side of the operator may be
Object of operator class
Object of different class
Built-in data type
All of the given options
Question No: 25
( Marks: 1 ) - Please choose one
The operator function will be implemented as _____, if obj1 drive the - operator whereas obj2 is passed as arguments to operator in the statement given below.
obj3 = obj1 - obj2;
Member function
Non-member function
Friend function
None of the given options
Question No: 26
( Marks: 1 ) - Please choose one
Which one of the following is the declaration of overloaded pre-increment operator implemented as member function?
Class-name operator +() ;
Class-name operator +(int) ;
Class-name operator ++() ;
Class-name operator ++(int) ;
Question No: 27
( Marks: 1 ) - Please choose one
The static data members of a class are initialized _______
at file scope
within class definition
within member function
within main function
Question No: 28
( Marks: 1 ) - Please choose one
Class is a user defined___________.
data type
memory referee
value
none of the given options.
Question No: 29
( Marks: 1 ) - Please choose one
We can also define a user-defines manipulators.
True
False
Question No: 30
( Marks: 1 ) - Please choose one
Automatic variable are created on ________.
Heap
Free store
static storage
stack
Question No: 31
( Marks: 1 )
How do we provide the default values of function parameters?
Answer: The default value of a parameter is provided inside the function prototype or function definition. For example, we
could declare the default function arguments for a function while declaring or defining it.
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Question No: 32
( Marks: 1 )
Why do java consider pointer as dangerous
Answer:
The concept of pointers is very important but quite limited to C and C++. The modern languages, for example JAVA, describe
pointers as dangerous. We can go anywhere in the memory and can change a value. There is another problem with pointers,
which is that these could be pointing to nowhere.
Question No: 33
( Marks: 2 )
What is memory leak?
Answer: size. When there is no memory on heap, the computer will stop running and there may be a system crash. This
situation is called a memory leak.
Question No: 34
( Marks: 2 )
What does optimization the of code means?
Answer:
Header file is a nice mechanism to put function prototypes and define constants (global constants) in a single file. That file can
be included simply with a single line of code.
Question No: 35
( Marks: 3 )
What is the difference between structure and class?
Answer:
In structures, some data variables are gathered, grouped and named as a single entity. Class and structure are very closely
related. In classes, we group some data variables and functions. These functions normally manipulate these variables. Before
going ahead, it is better to understand what a class is: A class includes both data members as well as functions to manipulate
that data
Question No: 36
( Marks: 3 )
See the following code segment.
template <class T>
class myclass {
private:
T x;
public:
myclass (T a) {
x = a;
}
};
Write the main function which creates two objects of class for int and double data types.
Question No: 37
( Marks: 3 )
Is it possible to define two
functions as given below? Justify your answer.
func(int x, int y)
func(int &x, int &y)
Solution:
No, it is impossible to define two functions as in the main function the way to call both functions is same. How does the
compiler know that which functions is being called? There is no way for the compiler to find out. Therefore there is an
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
ambiguity and that is not allowed. The only thing to realize is the side effect. Side effects are critical to take care of whenever
you are doing call by reference.
Question No: 38
( Marks: 5 )
Write a program using getline() member function to inputs a string up to delimiter character comma (,) and then display the
string on the screen.
Question No: 39
( Marks: 5 )
Do you think that friend functions violate encapsulation? Justify your answer.
Answer:
The friend functions of a class have access to the private data members of class. Despite being a good thing, there is possibility
of vulnerability. We are opening our thoughts, inside view for somebody else. Without having 100% trust, it will be risky to
make our thoughts and feelings public. We want that our private data is accessible to someone outside, not public for
everybody. Otherwise, the data encapsulation and data-hiding concept will be violated. We keep the data members private and
declare some specific functions that are not member of the class but friend of the class. As friends, they have access to the
inside data structure of the class despite not being members.
Question No: 40
(Marks: 10 )
Write a simple program using the get() member function of cin object reading a text of 30 characters from the keyboard, store
them in an array and then using put() member function of cout object to display them on the screen.
Question No: 41
(Marks: 10 )
Write a small program which defines two user-defined manipulators named octal and hexadecimal. These
manipulators should display the decimal numbers into octal and hexadecimal.
In the main function, input a decimal number from the user and then display this decimal number into octal and
hexadecimal using user-define manipulators named octal and hexadecimal.
FINALTERM EXAMINATION
Fall 2009
CS201- Introduction to Programming
Time: 120 min
Marks: 75
If we write a statement like s2 = s1; ___ will be the calling object and ____ will be passed to the = operator as an
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
argument.
s1, s1
s1, s2
s2, s1
s2, s2
If we write a statement like s2 = s1; s2 will be the calling object and s1 will be passed to the = operator as an argument.
P# 397
cout << setfill(0) << setw(7) << 128 ;
0000128
0128128
1280000
0012800
default alignment is from left due to this it first prints 4 Zeros(setw=7, digit=3 i.e 1-2-8,) 7-3=4 Zeros vuzs
The stream insertion and extraction operators are not already overloaded for _______
Built-in data types
User-defined data types
Both built-in and user-defined types
None of the given options
Overloaded new operator function takes parameter of type size_t and returns
void (nothing)
void pointer
object pointer
int pointer
Also note that the new operator returns a void pointer. Any new operator we write must have this parameter and return
type.
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Which of the following is the correct way to declare a variable x of integer type?
x int ;
integer x ;
int x;
x integer
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Exits from switch statement
None of the given options
We can also define a variable of user define data type (object) as static.
True
False
Let suppose
int a, b, c, d, e;
a = b = c = d = e = 42;
This can be interpreted by the complier as:
a = (b = (c = (d = (e = 42))));
(a = b = (c = (d = (e = 42))));
a = b = (c = (d = (e = 42)));
(a = b) = (c = d) = (e = 42);
a = (b = (c = (d = (e = 42) ) ) );
What will be the range of numbers generated by function rand () % 9?
0 to 9
1 to 9
0 to 8
1 to 8
When 6 divides any number, the remainder will always be less than 6. Ther
result will be between therefore we will add 1. 1 + rand ( ) % 6;
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Which of the following is the correct function call having array named student of 10 elements as a parameter.
addRecord(student[]) ;
addRecord(student) ;
addRecord(student[10]) ;
addRecord(*student) ;
when we pass array we dont give limit of array
Example:
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Identifier is a name that can be given to variables, labels and functions.
True
False
An 'Identifier' means any name that the user creates in his/her program. These names can be of variables, functions and
labels
If a class A declares itself a friend of class B and a class B declares itself a friend of class C then
Class A is also a friend of class C.
Class B is also a friend of class A.
Class A is also a friend of class C if A declares C as its friend.
Class A is also a friend of class C if C declares A as its friend.
If we want a two-way relationship, OtherClass will have to declare ClassOne as a friend class, resulting in a complete twoway relationship
Friend is a very strong statement. It is too strong to be affected by public or private we can put it anywhere in the class
A pointer is a special type of variable that contain ___________
Memory Address
Data values
Both Values and Memory
None of given of options
Pointer is a special type of variable that contains a memory address.
When memory for a program is allocated at run time then it is called ________
static memory allocation
dynamic memory allocation
stack memory allocation
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
virtual memory allocation
When we create an object of the class at run time, it will allocate memory according to our requirement. So there is no
waste of memory and the situations in which we want to store large data in small memory or vice versa are prevented.
So we do dynamic memory allocation inside these classes.
What purpose do classes serve?
Data encapsulation
Providing a convenient way of modeling real-world objects
Simplifying code reuse
All of the given options
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
// return non-incremented, saved, temporary object
return temp;
// value return; not a reference return
} // This paper was solved by vuzs Team and meant for hosting
at vuzs otherwise its stolen contents
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
Static memory allocation is also known as ____________
Dynamic allocation
Compile time allocation
Run time allocation
None of the given options
This type of memory static allocation. It is also known as compile time allocation.
( Marks: 1 )
Another member function of cin is getline(). It reads a complete buffer i.e. the number of character specified up to a
delimiter we specify. We can write something like:
cin.getline(char *buffer, int buff_size, char delimiter = \n)
( Marks: 1 )
When memory is allocated dynamically using new operator within the constructor of class then what is an
appropriate place to de-allocate the memory?
Whenever we allocate memory with the new operator, it is our responsibility to de-allocate this memory after the
termination of the program. To do this de-allocation, we have an operator delete. To de-allocate the memory, allocated
with p = new int ; we will write delete
(p) ;
It will not delete the p rather, it will send the memory gotten and pointed by p back to the free store.
( Marks: 2 )
What will be the output of following code, if user input a number 123?
int input ;
cin >> oct >> input;
cout << hex << input ;
( Marks: 2
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
on to decrease. It may become of zero size. When there is no memory on heap, the computer will stop running and
there may be a system crash. This situation is called a memory leak
( Marks: 3 )
When we call calloc function to allocate memory and its return a NULL pointer what does it mean?
Calloc function takes two arguments. The first argument is the required space in terms of numbers while the second one
is the size of the space
Now we have to see what happens when either we ask for too much memory at a time of non-availability of enough
memory on the heap or we ask for memory that is available on the heap , but not available as a single chunk?. In this
case, the call to calloc will fail. When a call to memory allocation functions fails, it returns a NULL pointer.
( Marks: 3 )
Read the given code and explain code functionality.
Matrix :: Matrix ( const Matrix & m )
{
numRows = m.numRows ;
numCols = m.numCols ;
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 ] = m.elements [ i ] [ j ] ;
}
}
( Marks: 3 )
What is the keyword this and what are the uses of this pointer?
Whenever an object calls a member function, the function implicitly gets a pointer from the calling object. That pointer is
known as this pointer. this is a key word. We cannot use it as a variable name. this pointer is present in the function,
referring to the calling object.
this pointer points to the current object.
( Marks: 5 )
What do you mean by garbage collection and how it works in JAVA and C++ ?
JAVA gives the concept of garbage collection with the use of references. Due to this garbage collection, we are free from
the headache of de- allocating the memory. We allocate and use the memory. When it is no longer in use, JAVA
automatically deletes (frees) it through garbage collection But in C and C++ languages, we have to take care of de-
Prepaid By NAJAM-Ul-HASSAN
KKCENTRE MPP
allocating the memory. In classes where we use dynamic memory, we have to provide destructors to free this memory.
The languages keep evolving, new constructs will keep evolving in existing or new languages.
( Marks: 5 )
Explain the concept of separation of interface from the implementation in the context of classes, using a real world
example.
( Marks: 10 )
Write a simple program using the get() member function of cin object reading a text of 30 characters from the
keyboard, store them in an array and then using put() member function of cout object to display them on the
screen.
( Marks: 10 )
Overload the Binary Assignment (=) Operator.
Write a program which has a class List, This class should have Two data members, an array of integers list[] and an
integer variable length (i.e. number of elements in the list).The class should further contain a default constructor, a
Print() function which display the list and a Function insert() which insert an element in the list and Assignment (=
) Operator function, which contain code for the assignment of one object to other. .
In main function define two objects list1 and list2 and use the statement list2 = list1; and use (call ) print function
with both objects
Prepaid By NAJAM-Ul-HASSAN
Prepaid By NAJAM-Ul-HASSAN