CS201 Mega Collection of Final Term Papers PDF
CS201 Mega Collection of Final Term Papers PDF
CS201 Mega Collection of Final Term Papers PDF
Collected By Anonymous
Anonymous Jan,2013
CS201-
CS201- Introduction to Programming
Mega Collection
for
Final Term
Only Solved
Paper Year Session
Paper # 01 2012 Unknown
Paper # 02 2011 (session_02)
Paper # 03 2011 (session_03)
Paper # 04 2010 Unknown
Paper # 05 2010 (session_02)
Paper # 06 2010 (session_03)
Paper # 07 2010 (session_05)
Paper # 08 2009 (session_01)
Paper # 09 2009 (session_02)
Paper # 10 2008 Unknown
www.vuhelp.pk Page 1
1st
Collected By Anonymous
Anonymous Jan,2013
Paper # 01
FINALTERM EXAMINATION
FALL (2012)
(2012)
CS201-
CS201- Introduction to Programming
Data
Memory Address
Data Type
Values
#
Question No: 3 ( Marks: 1 ) - Please choose one
Within the statement obj1=obj2; obj1 will call the assignment operator function and obj2 will be
passed as an argument to function.
True
False
www.vuhelp.pk Page 2
1st
Collected By Anonymous
Anonymous Jan,2013
What is the sequence of event(s) when deallocating memory using delete operator?
The second parameter of operator functions for << and >> are objects of the class for which we are
overloading these operators.
True
False
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>
int
short
Float
www.vuhelp.pk Page 3
1st
Collected By Anonymous
Anonymous Jan,2013
Char
True
False
For which values of the integer _value will the following code becomes an infinite loop?
int number=1;
while (true) {
if (number == 3) break;
number += integer_value; }
Only 0
only 1
only 2
True
False
www.vuhelp.pk Page 4
1st
Collected By Anonymous
Anonymous Jan,2013
Signed integer
Un-signed integer
Signed double
Un-signed double
Only Functions
Only variables
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];
Unary operator implemented as member function takes ____ arguments whereas non-member
function takes _____ arguments.
One,zero
Zero, one
www.vuhelp.pk Page 5
1st
Collected By Anonymous
Anonymous Jan,2013
One, two
Two, one
The first parameter of overloaded stream insertion operator is _________ where second parameter is
_______
True
False
True
False
While calling function, the arguments are assigned to the parameters from _____________.
left to right.
right to left
www.vuhelp.pk Page 6
1st
Collected By Anonymous
Anonymous Jan,2013
looped
nested
overloaded
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
Remain constant.
right
left
binary
unary
If text is a pointer of class String then what is meant by the following statement?
www.vuhelp.pk Page 7
1st
Collected By Anonymous
Anonymous Jan,2013
The return type of the operator function for << operator is __________.
Void
implement
design
analysis
Memory allocated at run time is a system resource and it is the responsibility of _____ to de-allocate the
memory.
System
programmer
User of program
true
false
www.vuhelp.pk Page 8
1st
Collected By Anonymous
Anonymous Jan,2013
Answer:
class class-name()
definition of class
};
Answer:
The difference between endl and \n is that endl is use to start a new line for the next row
Answer:
Identify each of the following as function call, function definition and function declaration.
Function call:
www.vuhelp.pk Page 9
1st
Collected By Anonymous
Anonymous Jan,2013
and Num2
Function call:
3. func(5, 6) ;
Function call:
Function call:
Function ; Function definition: Integer; Function declaration: Num1 and Num2 from
user
Consider the following code segment. What will be the output of the following code segment?
class class1{
public:
class class2{
public:
class2(){
};
class1(){
www.vuhelp.pk Page 10
1st
Collected By Anonymous
Anonymous Jan,2013
};
main(){
class1::class2 obj1;
class1 obj2 ;
func(int x, int y)
Answer:
No, we cannot define two functions as func(intx, inty) func(int &x, int&y) because its give an error
function not initializing.
Answer:
When we use new operator to create objects the memory space is allocated for the object
and then its constructor is called. Similarly, when we use delete operator with our objects,
the destructor is called for the object before deallocating the storage to the object.
Answer:
In function overloading, the functions have the same name but differ either by the number of
arguments or the type of the arguments.
www.vuhelp.pk Page 11
1st
Collected By Anonymous
Anonymous Jan,2013
Operator overloading is to allow the same operator to be bound to more than one
implementation, depending on the types of the operands.
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
Read the given below code and explain what task is being performed by this function
numRows = row ;
numCols = col ;
elements [ i ] [ j ] = 0.0 ;
www.vuhelp.pk Page 12
1st
Collected By Anonymous
Anonymous Jan,2013
Answer:
In this code the matrix function is defined, it get the number of rows from the user and create the
row of matrix and then get the columns from the user and create the columns. The New is
showing for creating more array space for the data which user enters. The elements [i][j] will
print the data in matrix form.
Paper # 02
FINALTERM EXAMINATION
FALL 2011(session_02)
2011(session_02)
CS201-
CS201- Introduction to Programming
www.vuhelp.pk Page 13
1st
Collected By Anonymous
Anonymous Jan,2013
The syntax of the prototype of the overloaded operator function is: return-type operator
operator-symbol (parameter-list);
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 ;
www.vuhelp.pk Page 14
1st
Collected By Anonymous
Anonymous Jan,2013
www.vuhelp.pk Page 15
1st
Collected By Anonymous
Anonymous Jan,2013
False
Its not compulsory, only min we have one generic data type but we can have native data
type as well.
www.vuhelp.pk Page 16
1st
Collected By Anonymous
Anonymous Jan,2013
cin>> i;
}
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.
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.
The default value of a parameter is provided inside the function prototype or function
definition.
www.vuhelp.pk Page 17
1st
Collected By Anonymous
Anonymous Jan,2013
www.vuhelp.pk Page 18
1st
Collected By Anonymous
Anonymous Jan,2013
we prefer to use new and delete operators as they are designed to work with classes
and objects.
www.vuhelp.pk Page 19
1st
Collected By Anonymous
Anonymous Jan,2013
manipulator that tells about width and number of decimal points of a number being
printed.
Some manipulators are parameter less. We simply use the name of the manipulator that
works. For example, we have been using endl, which is actually a manipulator, not data.
When we write cout << endl ; a new line is output besides flushing the buffer. Actually, it
manipulates the output stream.
Question No: 30 ( Marks: 2 )
Write down piece of code that will declare a matrix of 3x3. And initialize all its locations
with 0;
Ans:
int matrix [3] [3] ;
matrix [0] [0] = 0;
matrix [0] [1] = 0;
matrix [0] [2] = 0;
matrix [1] [0] = 0;
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
www.vuhelp.pk Page 20
1st
Collected By Anonymous
Anonymous Jan,2013
}
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;
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 ()
{
www.vuhelp.pk Page 21
1st
Collected By Anonymous
Anonymous Jan,2013
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.
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.
www.vuhelp.pk Page 22
1st
Collected By Anonymous
Anonymous Jan,2013
Paper # 03
FINALTERM EXAMINATION
FALL 2011 (session_03)
CS201-
CS201- Introduction to Programming
www.vuhelp.pk Page 23
1st
Collected By Anonymous
Anonymous Jan,2013
www.vuhelp.pk Page 24
1st
Collected By Anonymous
Anonymous Jan,2013
design
analysis
none of the given options
Paper # 04
FINALTERM EXAMINATION
CS201 - Introduction to programming
Final Term Spring 2010
www.vuhelp.pk Page 25
1st
Collected By Anonymous
Anonymous Jan,2013
Classes
Functions
Operators
Reference
Direct Reference
Indirect Reference
Partial Reference
Proper Reference
Which of the following function is used to increase the size of already allocated memory chunk?
malloc
calloc
realloc
free
#error
#define
#line
#ndefine
www.vuhelp.pk Page 26
1st
Collected By Anonymous
Anonymous Jan,2013
list of preprocessors
#include #include filename #define #undef #ifdef #ifndef #if #else #elif
#endif #error #line #pragma #assert
The stream objects cin and cout are included in which header file?
iostream.h
fstream.h
istream.h
ostream.h
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).
When an array of object is created dynamically then there is no way to provide parameterized
constructors for array of objects.
True
False
www.vuhelp.pk Page 27
1st
Collected By Anonymous
Anonymous Jan,2013
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.
True
False
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.
What will be the value of a and b after executing the following statements?
a = 3;
b = a++;
3, 4
www.vuhelp.pk Page 28
1st
Collected By Anonymous
Anonymous Jan,2013
4, 4
3, 3
4, 3
Consider the following code segment. What will be the output of following code?
int b = (*a) + 2;
return b ;
main () {
int x =6 ;
cout << x ;
6,8,6
6,6,8
6,8,8
6,6,6
_______ is used to trace the logic of the program and correct the logical errors.
Compiler
Editor
Linker
Debugger
www.vuhelp.pk Page 29
1st
Collected By Anonymous
Anonymous Jan,2013
new and delete are _____ whereas malloc and free are _____.
Functions, operators
Classes, operators
Operators,Functions
Operators, classes
Like member functions, ______ can also access the private data members of a class.
Non-member functions
Friend functions
The stream insertion and stream extraction operators are already overloaded for ______.
Built-in-data types
www.vuhelp.pk Page 30
1st
Collected By Anonymous
Anonymous Jan,2013
If we define an identifier with the statement #define PI 3.1415926 then during the execution of
the program the value of PI __________.
Remain constant.
right
left
binary
unary
You can assign values to several variables in a single statement. For example, the following
code sets the contents of apples and oranges to the same value:
The assignment operator is right associative, so this statement executes by first storing the
value 10 in oranges and then storing the value in oranges in apples, so it is effectively
Explicitly
Implicitly
www.vuhelp.pk Page 31
1st
Collected By Anonymous
Anonymous Jan,2013
The appropriate data type to store the number of rows and colums of the matrix
is____________.
float
int
char
Which of the following function do NOT initialize the chunk of memory to all zero?
calloc() function
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.
The function free() returns back the allocated memory got thorough calloc and malloc to _____ .
stack
heap
www.vuhelp.pk Page 32
1st
Collected By Anonymous
Anonymous Jan,2013
cin object
cout object
true
false
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.
A Matrix can be composed of ints, floats or doubles as their elements. Best way is to handle this
, _______________
Use templates
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.
template
www.vuhelp.pk Page 33
1st
Collected By Anonymous
Anonymous Jan,2013
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.
What will be the output of following code, if user input a number 123?
int input ;
53
Rational: it will take 123 as octal and print it in hex form which is 53.
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
Class can declare a friend class from inside and someone from outside the class cannot declare
itself friend of a class.
www.vuhelp.pk Page 34
1st
Collected By Anonymous
Anonymous Jan,2013
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
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;
class String
private:
char buf[25];
};
www.vuhelp.pk Page 35
1st
Collected By Anonymous
Anonymous Jan,2013
Write code for assignment (=) operator function which assign one String object to other object.
Your code should also avoid self assignment
Answer:
{ int length ;
length = other.length();
delete buf;
Read the given below code and explain what task is being performed by this function
numRows = row ;
numCols = col ;
elements [ i ] [ j ] = 0.0 ;
www.vuhelp.pk Page 36
1st
Collected By Anonymous
Anonymous Jan,2013
Paper # 05
FINALTERM EXAMINATION
FALL 2010 (session_02)
CS201-
CS201- Introduction to Programming
True
False
True
False
Overloaded new operator function takes parameter of type size_t and returns
void (nothing)
www.vuhelp.pk Page 37
1st
Collected By Anonymous
Anonymous Jan,2013
Void pointer
object pointer
int pointer
True
False
If there is more than one statement in the block of a for loop, which of the
following must be placed at the beginning and the ending of the loop block?
parentheses ( )
Braces { }
brackets [ ]
The return type of a function that do not return any value must be ________
float
int
void
double
JAVA
C
FORTRAN
Like member functions, ______ can also access the private data members of a
class.
www.vuhelp.pk Page 38
1st
Collected By Anonymous
Anonymous Jan,2013
Non-member function
Friend functions
It can be declared anywhere in class as these are not affected by the public and private
keywords.
Functions
Operators
Manipulators
Objects
True
False
www.vuhelp.pk Page 39
1st
Collected By Anonymous
Anonymous Jan,2013
Cast
cost
const
True
False
Which of the following function call is correct for the function prototype?
defaultParameters (5);
True False
www.vuhelp.pk Page 40
1st
Collected By Anonymous
Anonymous Jan,2013
The appropriate data type to store the number of rows and colums of the
matrix is____________.
float
Int
char
Data type
memory referee
value
Decremented
Incremented
Multiplied
NULL value has been defined in ______ and _________ header files.
NULL has been defined in the header files stdlib.h and stddef.h.
www.vuhelp.pk Page 41
1st
Collected By Anonymous
Anonymous Jan,2013
Use templates
Suppose there is a template function func having argument of type U and return type
T. What will be the C++ syntax to call this function, passing a variable x of type
double and returning an int type?
Which variable will be used in inner code block if we have the same names of variable
at outer code block and inner code block?
www.vuhelp.pk Page 42
1st
Collected By Anonymous
Anonymous Jan,2013
Write the C++ code for the declaration of overloaded stream insertion and stream
extraction operator for the object d of type Date.
What will be the output of following functions if we call these functions three times?
1)
void func1(){
int x = 0;
x++;
2)
void func2(){
static int x = 0 ;
x++;
www.vuhelp.pk Page 43
1st
Collected By Anonymous
Anonymous Jan,2013
What is difference between Unary and binary operators and how they can be
overloaded?
class Matrix
private:
int Elements[3][3];
};
Write the operator function of stream extraction operator (>>) for this class.
What is meant by user interface and class interface in C++ ? And what role a class
interface can play in user interface [Marks 5]
Paper # 06
FINALTERM EXAMINATION
FALL 2010 (session_03)
CS201-
CS201- Introduction to Programming
Question No: 1 ( Marks: 1 ) - Please choose one
If it is required to copy an array to another array then,
Both arrays must be of the same size and data type (Page 105)
Both arrays may be of different size
Both arrays may be of different data type
Both arrays may be of different size and type
www.vuhelp.pk Page 44
1st
Collected By Anonymous
Anonymous Jan,2013
False
http://mathbits.com/MathBits/CompSci/Structures/Functions.htm
parentheses ( )
braces { } (Page 37)
brackets [ ]
arrows < >
JAVA
www.vuhelp.pk Page 45
1st
Collected By Anonymous
Anonymous Jan,2013
B
C (Page 12)
FORTRAN
www.vuhelp.pk Page 46
1st
Collected By Anonymous
Anonymous Jan,2013
cost
const
www.vuhelp.pk Page 47
1st
Collected By Anonymous
Anonymous Jan,2013
www.vuhelp.pk Page 48
1st
Collected By Anonymous
Anonymous Jan,2013
Paper # 07
FINALTERM EXAMINATION
Spring 2010 (session-
(session-05)
CS201-
CS201- Introduction to Programming
Marks: 58
Header file: fstream.h includes the definition of the stream classes __________.
www.vuhelp.pk Page 49
1st
Collected By Anonymous
Anonymous Jan,2013
* operator
operator
None of given.
True
False
True
False
Member function
www.vuhelp.pk Page 50
1st
Collected By Anonymous
Anonymous Jan,2013
Non-member function
Private function
Public function
True
File
Disk
Keyboard
RAM
Which of the following is correct way to initialize a variable x of int type with value 10?
int x ; x = 10;
int x = 10;
int x, x = 10;
x = 10;
Consider the following code segment. What will be the output of the following program?
int func(int) ;
www.vuhelp.pk Page 51
1st
Collected By Anonymous
Anonymous Jan,2013
int num = 10 ;
int main(){
int num ;
num = 5 ;
cout num ;
cout func(num) ;
return num ;
5, 5
10, 5
5, 10
10, 10
With template function, the compiler automatically detects the passed data and generates
a new copy of function using passed data.
True
False
What will be the correct syntax to declare two-dimensional array of float data type?
float arr{2}{2} ;
www.vuhelp.pk Page 52
1st
Collected By Anonymous
Anonymous Jan,2013
float arr[2][2] ;
float arr[2,2] ;
float[2][2] arr ;
True
False
While calling function, the arguments are assigned to the parameters from
left to right.
right to left
looped
www.vuhelp.pk Page 53
1st
Collected By Anonymous
Anonymous Jan,2013
nested
overloaded
If we define an identifier with the statement #define PI 3.1415926 then during the
execution of the program the value of PI __________
Remain constant.
Constructor
Destructor
www.vuhelp.pk Page 54
1st
Collected By Anonymous
Anonymous Jan,2013
Zero argument
Databases Management
Networks
When a call to a user-defined function finishes, the variable defined inside the
function is still in existence.
True
False
True
False
www.vuhelp.pk Page 55
1st
Collected By Anonymous
Anonymous Jan,2013
A Matrix can be composed of ints, floats or doubles as their elements. Best way is to
handle this , _______________
Use templates
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.
Write Matrix class as a template class and write code once for all native data types
"delete" operator is used to return memory to free store, which is allocated by the "new"
operator.
True
False
How the data members of a class are initialized with meaningful values?
www.vuhelp.pk Page 56
1st
Collected By Anonymous
Anonymous Jan,2013
What will be the output of following functions if we call these functions three times?
1)
void func1(){
int x = 0;
x++;
cout x endl;
2)
void func2(){
static int x = 0 ;
x++;
cout x endl ;
What is the keyword this and what are the uses of this pointer?
Write the general syntax of a class that has one function as a friend of a class along with
definition of friend function.
www.vuhelp.pk Page 57
1st
Collected By Anonymous
Anonymous Jan,2013
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
xxxxxx1000
xxxxxx1500
xxxxx20000
xxxxx30000
xxxxx60000
Paper # 08
FINALTERM EXAMINATION
Spring 2009
CS201-
CS201- Introduction to Programming (Session - 1)
Time: 120 min
Marks: 75
Question No: 1 ( Marks: 1 ) - Please choose one
www.vuhelp.pk Page 58
1st
Collected By Anonymous
Anonymous Jan,2013
* operator
operator
None of given.
If constructor contains a return statement in its body then compiler will give __________
No error
Syntax error
Logical error
True
False
www.vuhelp.pk Page 59
1st
Collected By Anonymous
Anonymous Jan,2013
When an operator function is define as member function for a Unary operator then the number
of argument it take is/are,
Zero
One
Two
N arguments
operator Class-Name + ( )
Class-Name operator + ( )
Member function
Non-member function
Private function
Public function
True
False
www.vuhelp.pk Page 60
1st
Collected By Anonymous
Anonymous Jan,2013
What is the sequence of event(s) when deallocating memory using delete operator?
Deleting an array of objects without specifying [] brackets may lead to memory leak
True
False
float, int
float, double
int
char
True
False
www.vuhelp.pk Page 61
1st
Collected By Anonymous
Anonymous Jan,2013
With template function, the compiler automatically detects the passed data and generates
a new copy of function using passed data.
True
False
What will be the correct syntax to initialize all elements of two-dimensional array to value
0?
When an operator function is define as member function then operand on the left side of
operator must be an object.
True
False
True
False
return
www.vuhelp.pk Page 62
1st
Collected By Anonymous
Anonymous Jan,2013
break
continue
goto
Every data member or function inside the structure is ___ by default whereas everything
declared inside a class is ____ by default.
private, public
public, private
private, protected
public, protected
Which of the following is true for the C++ statement given below?
obj2 will be passed as an argument to + operator whereas obj2 will drive the + operator
www.vuhelp.pk Page 63
1st
Collected By Anonymous
Anonymous Jan,2013
obj1 will drive the + operator whereas obj2 will be passed as an argument to + operator
Unary operator implemented as member function takes ____ arguments whereas non-
member function takes _____ arguments.
One, zero
Zero, one
One, two
Two, one
The input/output streams cin and cout are ________ therefore have _______.
Structures, function
Functions, objects
True
False
www.vuhelp.pk Page 64
1st
Collected By Anonymous
Anonymous Jan,2013
Constructor
Destructor
Built-in- Function
Operators
class M {
public:
M &operator+(const M &);
...
};
...
Let assume if p and q are class objects then function is implemented as _______
Member function
Non-member function
www.vuhelp.pk Page 65
1st
Collected By Anonymous
Anonymous Jan,2013
Friend function
right
left
binary
unary
True
False
A constructor that will create a new object with a full copy of the other object, is copy is
known as ___________
deep copy
www.vuhelp.pk Page 66
1st
Collected By Anonymous
Anonymous Jan,2013
shallow copy
constructor copy
Ans:
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
What are the main types of operators in terms of number of arguments they take?
Ans:
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.
Ans:
In a C++ program, if you create object A of class X, you can then obtain the address of A by
using the "this" pointer. The address is available as a local variable in the non-static member
functions of X, and its type is const X*. The "this" pointer works because C++ creates instances
of its data members, and it keeps one copy of each member function.
www.vuhelp.pk Page 67
1st
Collected By Anonymous
Anonymous Jan,2013
Ans: 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.
What will be the output of following functions if we call these functions three times?
Ans:
1)
void func1(){
int x = 0;
x++;
cout x endl;
1
1
1
2)
void func2(){
static int x = 0 ;
x++;
cout x endl ;
www.vuhelp.pk Page 68
1st
Collected By Anonymous
Anonymous Jan,2013
1
2
3
If the requested memory is not available in the system then what does calloc/malloc and new
operator return?
Ans:
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.
If we want to send the data by reference and dont want that original data should be affected
then what can we do to prevent any change?
Ans:
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.
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.
www.vuhelp.pk Page 69
1st
Collected By Anonymous
Anonymous Jan,2013
The following code segment has errors. Locate as many as you can and explain briefly.
double centerX;
double centerY;
double radius;
void setRadius(int);
Date class should contain three data members day, month, year and setter and getter function
for these data members. Date class should also contain showdate() member function to display
date.
Person class should contain three data members Name, Address, and Bday, where Name and
Address are char pointer while Bday(Date of birth) is of type Date, Person class should further
contain two member functions Display() and setdate().
In main program Create an object of Class person and call the member functions with it.
www.vuhelp.pk Page 70
1st
Collected By Anonymous
Anonymous Jan,2013
#include <stdio.h>
#include <iostream>
#include <cstring>
class Date
public:
int day;
int month;
int year;
public:
Date()
day=0;
month=0;
year=0;
void setDay(int);
www.vuhelp.pk Page 71
1st
Collected By Anonymous
Anonymous Jan,2013
void setYear(int);
int getDay();
int getMonth();
int getYear();
void showDate();
};
if{d<1 | | d>31)
cin>>d;
day=d;
if(m<1 | | m>12)
www.vuhelp.pk Page 72
1st
Collected By Anonymous
Anonymous Jan,2013
cin>>m;
month=m;
year=y;
return day;
return month:
return year;
www.vuhelp.pk Page 73
1st
Collected By Anonymous
Anonymous Jan,2013
coutday"-"month"-"yearend1;
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()
www.vuhelp.pk Page 74
1st
Collected By Anonymous
Anonymous Jan,2013
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: "Addressend1;
Ad-date.showDate();
};
void main()
Person object;
object.setDate();
www.vuhelp.pk Page 75
1st
Collected By Anonymous
Anonymous Jan,2013
object.Display();
system("pause");
Write a C++ program that contains a class myClass having two data members of type int.
A default constructor which must initialize all the data members to their meaningful
values.
A destructor with no implementation.
Setter member functions to set all data members of class
Getter member functions to get all data members of class
#include <stdio.h>
#include <iostream>
#include <cstring>
www.vuhelp.pk Page 76
1st
Collected By Anonymous
Anonymous Jan,2013
class myclass
public:
int a;
int b;
construct{int,int.int}
void seta(int);
void setb(int);
void setc(int);
int geta();
int getb();
int getc();
};
www.vuhelp.pk Page 77
1st
Collected By Anonymous
Anonymous Jan,2013
a=aa;
b=bb;
c=cc;
main()
int num;
cin>>num;
www.vuhelp.pk Page 78
1st
Collected By Anonymous
Anonymous Jan,2013
Person i_
Paper # 9
FINALTERM EXAMINATION
FALL 2009 (session_02)
CS201-
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 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
www.vuhelp.pk Page 79
1st
Collected By Anonymous
Anonymous Jan,2013
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
The stream insertion and extraction operators are not already overloaded for _______
True
False
The constructors can be overloaded. We can write as many constructors as we require. At
one time, the compiler will call the correct version of the constructor".P# 323 these solutions
are meant to host at vuzs site only
Overloaded new operator function takes parameter of type size_t and returns
void (nothing)
void pointer
object pointer
www.vuhelp.pk Page 80
1st
Collected By Anonymous
Anonymous Jan,2013
int pointer
Also note that the new operator returns a void pointer. Any new operator we write must have
this parameter and return type.
Which of the following is the correct way to declare a variable x of integer type?
x int ;
integer x ;
int x;
x integer
True
False
There are few data types in C language. These data types are reserved words of C language.
The reserve words can not be used as a variable manes. P# 17
Zero
One
Two
Three
The function arguments must contain at least one generic data type. P# 499
True
www.vuhelp.pk Page 81
1st
Collected By Anonymous
Anonymous Jan,2013
False
We can write overloaded template functions as long as there is use of different number or
type of arguments.. P # 503
True
We can also define a variable of user define data type (object) as static.
True
False
operator Class-Name + ( )
Class-Name operator + ( )
Let suppose
int a, b, c, d, e;
a = b = c = d = e = 42;
www.vuhelp.pk Page 82
1st
Collected By Anonymous
Anonymous Jan,2013
a = (b = (c = (d = (e = 42))));
(a = b = (c = (d = (e = 42))));
a = b = (c = (d = (e = 42)));
(a = b) = (c = d) = (e = 42);
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. There
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) ;
Example:
www.vuhelp.pk Page 83
1st
Collected By Anonymous
Anonymous Jan,2013
#include<stdio.h>
#include<conio.h>
void read(int *,int);
void dis(int *,int);
void main()
{
int a[5],b[5],c[5],i;
True
www.vuhelp.pk Page 84
1st
Collected By Anonymous
Anonymous Jan,2013
False
structures do not occupy any memory until it is associated with the structure variable
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
If we want a two-way relationship, OtherClass will have to declare ClassOne as a friend class,
resulting in a complete two-way relationship
It can be declared anywhere in class as these are not affected by the public
and private keywords.
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
Memory Address
www.vuhelp.pk Page 85
1st
Collected By Anonymous
Anonymous Jan,2013
Data values
When memory for a program is allocated at run time then it is called ________
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.
Data encapsulation
Member functions
Utility functions
Constructor
Destructor
www.vuhelp.pk Page 86
1st
Collected By Anonymous
Anonymous Jan,2013
Date operator++(int )
Date Date::operator++()
helpIncrement();
// parameter name.
Date Date::operator++(int)
helpIncrement();
www.vuhelp.pk Page 87
1st
Collected By Anonymous
Anonymous Jan,2013
} // This paper was solved by vuzs Team and meant for hosting
Once the static variables are created, they exist for the life of the program. They do not die.
Be a syntax error
Be a logical error
logical expressions
www.vuhelp.pk Page 88
1st
Collected By Anonymous
Anonymous Jan,2013
arithmetic expressions
Dynamic allocation
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:
( 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.
www.vuhelp.pk Page 89
1st
Collected By Anonymous
Anonymous Jan,2013
( Marks: 2 )
What will be the output of following code, if user input a number 123?
int input ;
( Marks: 2 )
suppose, the heap size is decreased as we had allocated memory from it despite the fact
that it was never utilized. If this step of allocating memory and then destroy the pointer to
this memory carries on then the size of the heap will going 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 )
numRows = m.numRows ;
www.vuhelp.pk Page 90
1st
Collected By Anonymous
Anonymous Jan,2013
numCols = m.numCols ;
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.
( 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-
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.
www.vuhelp.pk Page 91
1st
Collected By Anonymous
Anonymous Jan,2013
( 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 )
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.
Paper # 10
10
FINALTERM EXAMINATION
www.vuhelp.pk Page 92
1st
Collected By Anonymous
Anonymous Jan,2013
FALL (2008)
CS201-
CS201- Introduction to Programming
Time: 120 min
Marks: 75
Question No: 1 ( Marks: 1 ) - Please choose one
Two
Three
Four
Five
Different
Identical
1 byte exactly
1 bit exactly
www.vuhelp.pk Page 93
1st
Collected By Anonymous
Anonymous Jan,2013
True
False
True
False
Local
Global
operator Class-Name + ( )
Class-Name operator + ( )
The compiler does not provide a copy constructor if we do not provide it.
True
False
www.vuhelp.pk Page 94
1st
Collected By Anonymous
Anonymous Jan,2013
What is the functionality of the following syntax to delete an array of 5 objects named arr
allocated using new operator?
delete arr ;
What is the sequence of event(s) when allocating memory using new operator?
What is the sequence of event(s) when deallocating memory using delete operator?
True
False
The operator function of << and >> operators are always the member function of a class.
www.vuhelp.pk Page 95
1st
Collected By Anonymous
Anonymous Jan,2013
True
False
A
template function must have at least ---------- generic data type
Zero
One
Two
Three
If we do not mention any return_value_type with a function, it will return an _____ value.
int
void
double
float
int
void
double
float
Suppose a program contains an array declared as int arr[100]; what will be the size of array?
99
100
www.vuhelp.pk Page 96
1st
Collected By Anonymous
Anonymous Jan,2013
101
True
False
True
False
Which of the following option is true about new operator to dynamically allocate memory to an
object?
new and delete are _____ whereas malloc and free are _____.
Functions, operators
Classes, operators
Operators, functions
Operators, classes
Like member functions, ______ can also access the private data members of a class.
Non-member functions
www.vuhelp.pk Page 97
1st
Collected By Anonymous
Anonymous Jan,2013
Friend functions
Friend function must be declared at the top within class definition. (not sure)
It can be declared anywhere in class as these are not affected by the public
and private keywords. (not sure)
For non-member operator function, object on left side of the operator may be
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.
www.vuhelp.pk Page 98
1st
Collected By Anonymous
Anonymous Jan,2013
Member function
Non-member function
Friend function
at file scope
data type
memory referee
value
www.vuhelp.pk Page 99
1st
Collected By Anonymous
Anonymous Jan,2013
True
False
Heap
Free store
static storage
stack
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.
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.
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.
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.
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.
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 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.
Write a program using getline() member function to inputs a string up to delimiter character
comma (,) and then display the string on the screen.
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.
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.
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.