PROMOTE MY BLOG: JUST CLICK BELOW BUTTON

Search Any Paper On This Blog

Thursday, March 3, 2011

CS201 Solved Subjectives # 4

By ADEEL ABBAS, Bhakkar. AdeelAbbasbk@gmail.com

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;
for (int a=0;a<3;a++)
{ for (int b = 0;b<3;b++)
{ matrix[a][b]= inivalue;
cout<<matrix[a][b]<<endl;}}
}

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.

How can we initialize data members of contained object at
construction time?
Answer:
Initializer list is used to initialize the contained objects at the
construction time.

Can we overload new and delete operators?
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.

What is the benefit of reference and where can we use it?
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.

What is difference between endl and \n?
Answer:
Endl is manipulator and it inserts new line character and flushes the
stream.
\n is control character which is used to insert line break.

What does code optimization mean?
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.

How is the following cout statement interpreted by compiler?
cout << a << b << c ;
Answer:
It will give a compiler error because a,b,c are not declared.

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

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;
}
Answer:
1
1
1
2)
void func2(){
static int x = 0 ;
x++;

cout << x << endl ;
}
Answer:
1
2
3

If is not available in the system then what
does calloc/malloc and new operator return?
Answer:
calloc/malloc and new operator return returns a null pointer to indicate
that no memory is available




What is the keyword ‘this’ and what are the uses of
‘this’ pointer?
Answer:
'this' is use to refer the current class member without using the name
of the class.

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); }
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

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;
}
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;
temp = m;
temp.number= number * number;
return temp.number;
}

What are the limitations of the friendship relation between
classes?
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.

Define static variable. Also explain life time of static variable?
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.

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

Answer:
#include <iostream.h>
#include <iomanip.h>
main()
{
int sal1 =1000;
int sal2 =1500;
int sal3 =20000;
int sal4 =30000;
int sal5 =60000;
cout << setfill ('x') << setw (10);
cout<< sal1<<endl;
cout << setfill ('x') << setw (10);
cout<< sal2<<endl;
cout << setfill ('x') << setw (10);
cout<< sal3<<endl;
cout << setfill ('x') << setw (10);
cout<< sal4<<endl;
cout << setfill ('x') << setw (10);
cout<< sal5<<endl;
int i=0;
cin>>i; // to stop the screen to show the output
}

No comments:

Post a Comment

PLEASE COMMENT ABOUT YOUR VISIT AND MY SITE

Note: Only a member of this blog may post a comment.