CS201 - Introduction To Programmming Solved Subjective Questions From Spring 2010 Final Term Papers
CS201 - Introduction To Programmming Solved Subjective Questions From Spring 2010 Final Term Papers
1/15
CS201 – Introduction to Programmming
Solved Subjective Questions
From spring 2010 Final Term Papers
Answer:
float floatArry[10] = {1.0,2.0,3.0,4.0};
Write the general syntax for the declaration of pre-increment and post-
increment member operator function.
Answer:
Classname operator ++(); ---- pre increment
Classname operator ++(int) ---- post increment
Answer:
template
class myclass { ---} ;
Answer:
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
http://www.vustudents.net
Page. No. 2/15
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.
int input ;
cin >> oct >> input;
cout << hex << input ;
Answer:
53
Rational: it will take 123 as octal and print it in hex form which is 53.
Answer:
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
Answer:
Unary operator takes only one argument like i++ or i— (Post
increment or post decrement operators for integers) or ++i,--i (Pre
increment or pre decrement operators for integers) ,we can not make
Unary operator as binary or binary as Unary operator.
http://www.vustudents.net
Page. No. 3/15
Answer:
Modulus operator:
This operator can only be used with integer operands ONLY
Answer:
The manipulators are like something that can be inserted into stream,
effecting a change in the behavior. For example, if we have a floating
point number, say pi (л), and have written it as float pi = 3.1415926 ;
Now there is need of printing the value of pi up to two decimal places
i.e. 3.14. This is a formatting functionality. For this, we have
a manipulator that tells about width and number of decimal points of a
number being printed.
Write down piece of code that will declare a matrix of 3x3. And
initialize all its locations with 0;
Answer:
int matrix [3] [3] ;
include<iostream.h>
main () {
int matrix [3][3];
int inivalue = 0;
http://www.vustudents.net
Page. No. 4/15
Question No: 11 ( Marks: 2 )
What is the difference between switch statement
and if statement.
Answer:
The “If” statement is used to select among two alternatives. It uses a
Boolean expression to decide which alternative should be executed.
The switch statement is used to select among multiple alternatives. It
uses an int expression to determine which alternative should be
executed.
Answer:
Initializer list is used to initialize the contained objects at the
construction time.
Answer:
Yes, it is possible to overload new and delete operators to customize
memory management. These operators can be overloaded in global
(non-member) scope and in class scope as member operators.
Answer:
template <class T, class U>
T func (T a, U b) {
return (a<b?a:b);
}
calling
int i;
double x;
x = func<int,double> (j,l);
http://www.vustudents.net
Page. No. 5/15
Answer:
Simply: variable of the inner code is use in the inner code block.
Answer:
In references we give the memory address of the object, due to
references we pass values without making the copy. Hence, when we
have many values & we want efficiency we use references to avoid
copy.
Answer:
Date operator >> (date & d1){
cout<<d1.day<<”-”<<d1.month<<”-”<<d1.year;
}
Answer:
Endl is manipulator and it inserts new line character and flushes the
stream.
http://www.vustudents.net
Page. No. 6/15
\n is control character which is used to insert line break.
Answer:
It is process by which we make our code such a way that it improves
the speed of program. By use of optimization we refine program codes
in such a way that it run faster and consume less memory. We do it in
such a way that output quality is not compromised.
Answer:
It will give a compiler error because a,b,c are not declared.
1)
void func1(){
int x = 0;
x++;
cout << x << endl;
}
Answer:
http://www.vustudents.net
Page. No. 7/15
1
1
1
2)
void func2(){
static int x = 0 ;
x++;
cout << x << endl ;
}
Answer:
1
2
3
Answer:
calloc/malloc and new operator return returns a null pointer to indicate
that no memory is available
Answer:
'this' is use to refer the current class member without using the name
of the class.
1) Matrix m1 (m2);
2) Matrix m1, m2;
m1 = m2;
3) Matrix m1 = m2;
http://www.vustudents.net
Page. No. 8/15
Answer:
Answer:
0
The output will zero as 1/5 and its .05 but conversion to int make it
zero
Above is prototype of template class so assume passing an int and
returning an int
Answer:
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 temp;
http://www.vustudents.net
Page. No. 9/15
temp = m;
temp.number= number * number;
return temp.number;
Answer:
friendship relation between classes is a one way relation that is
if one class declare friend another class then the another class
is the friend of first class but not the first class if the friend of
another class.
Answer:
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.
Answer:
Unary operator takes one argument.
a ++ is an example of unary operator
Operator is keyword
http://www.vustudents.net
Page. No. 10/15
Answer 1:
Answer 2:
Code readability: We should use Tab and spaces so codes are easily
readable.
http://www.vustudents.net
Page. No. 11/15
Output should be displayed as given below:
xxxxxx1000
xxxxxx1500
xxxxx20000
xxxxx30000
xxxxx60000
Answer:
#include <iostream.h>
#include <iomanip.h>
main(){
int sal1 =1000;
int sal2 =1500;
int sal3 =20000;
int sal4 =30000;
int sal5 =60000;
class Matrix
{
private:
http://www.vustudents.net
Page. No. 12/15
int Elements[3][3];
};
Answer:
Element operator >> (Element &element){
cout<<element[0][0]<<element[0][1]<<element[0][2];
cout<<element[1][0]<<element[1][1]<<element[1][2];
cout<<element[2][0]<<element[2][1]<<element[2][2];
}
Answer:
Answer:
class frinedclass{
public:
friend int compute(exforsys e1)
};
Int compute(exforsys e1)
{
//Friend Function Definition which has access to private data
return int(e1.a+e2.b)-5;
}
http://www.vustudents.net
Page. No. 13/15
Answer:
Many things can be possible without using templates but it does offer
several clear advantages not offered by any other techniques:
Advantages:
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 helps 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.
Answer:
#include
math
{
mth operator + (obj1,int x)
http://www.vustudents.net
Page. No. 14/15
{
number temp;
temp=obj1.number+x;
return temp.number;
}
}
Answer:
#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;
}
Answer:
void String::operator = ( const String &other )
{ int length ;
length = other.length();
delete buf;
http://www.vustudents.net
Page. No. 15/15
buf = new char [length + 1];
strcpy( buf, other.buf ); }
Read the given below code and explain what task is being performed by
this function
Matrix :: Matrix ( int row , int col )
{
numRows = row ;
numCols = col ;
elements = new ( double * ) [ numRows ] ;
for ( int i = 0 ; i < numRows ; i ++ )
{
elements [ i ] = new double [ numCols ] ;
for ( int j = 0 ; j < numCols ; j ++ )
elements [ i ] [ j ] = 0.0 ;
}
}
Hint : This function belong to a matrix class, having
Number of Rows = numRows
Number of Columns = numCols
Answer:
In the above mentioned code, first of all programmer call the
constructor who have two parameters for the number of rows &
columns in the matrix. Then this constructor also dynamically allocates
the memory for the elements of the matrix & also initializes the value
of the all elements of matrix with 0.0
http://www.vustudents.net