0% found this document useful (0 votes)
100 views21 pages

CS201 Solved SUBJECTIVE Final Term by JUNAID

Cs201

Uploaded by

danishbizinis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views21 pages

CS201 Solved SUBJECTIVE Final Term by JUNAID

Cs201

Uploaded by

danishbizinis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

CS201-Intruduction to Programming

Solved SUBJECTIVE for Final terms papers


Solved by JUNAID MALIK and Team
AL-JUNAID INTITUE GROUP
1. How can we overcome the issue of dangling reference in a program?
Ans:
We can avoid the dangling pointer errors by initialize pointer to NULL, after
de-allocating memory, so that pointer will be no longer dangling. Assigning
NULL value means pointer is not pointing to any memory location.
Pointers
The Pointer in C is a variable that stores address of another variable. A
pointer can also be used to refer to another pointer function.

2. // what will be output of the program below?


Ans:

#include <iostream>

using namespace std;

int main ()

int i =2;

int j =3;

int k=2*++i*j++;

cout<<k;

}
AL-JUNAID INTITUE GROUP

3. What is overloading declaration for a stream extraction (>>) for an


integer.
Ans:
C++ is able to input and output the built-in data types using the stream
extraction operator >> and the stream insertion operator <<. The stream
insertion and stream extraction operators also can be overloaded to perform
input and output for user-defined types like an object.

4. Suppose an object of class A is declared as data member of class B.


Ans:
 The constructer of which class will be called first?
 The distracter of which class will be declared first?
 A
 B
5. //identify and correct error(s) in given code.
while (c<=5);
{
sum + =c;
c ++;
}
Ans:
AL-JUNAID INTITUE GROUP
Logical error

6. Identify each of the following as system software and application


software.
Ans:
LINUX, DISK CLEANUP, WORD PROCESSOR, WINDOWS, STUDENT
INFORMATION

System software: - Linux, Disk cleanup, windows.


Application software: - Word Processor, Student information

7. //what will be the output of the following code snippet?


Ans:
{
int x=10;
int &y=x;
y+=5;
cout<<x;

8. Write a function named sum which accepts an integer in the function use
a static variable to add the passed to it as arguments with each function
call in the end, display the sum of values passed as arguments?
Ans:
#include <iostream>
using namespace std;
int main(){
AL-JUNAID INTITUE GROUP
//Declaring two integer variables
int num1, num2;
/* cout displays the string provided in the
* double quotes as it is on the screen
*/
cout<<"Enter first integer number: ";
/* cin is used to capture the user input
* and assign it to the variable.
*/
cin>>num1;

cout<<"Enter second integer number: ";


cin>>num2;
cout<<"Sum of entered numbers is: "<<(num1+num2);
return 0;
}
9. Define static variable also explain life time of static variable?
Ans:
Static variables refer to maintaining the state of a variable. It exists and
exists even when we lose function. It is created and initialized only once
during the life cycle of the program, so it will only be destroyed or deleted
from memory once during the life cycle of the program.

10.What do you know about run time error?


Ans:
Runtime errors occur when the program is running and trying to do
something that violates the rules.
Example: accessing variables, properties, methods, objects, etc.

11.What is limitation of the friendship between classes?


Ans:
The friendship relationship between classes is a one-way relationship, that is,
if one class declares friends to another class, then the other class is a friend of
the first class, but if it is a friend of another class, it is not the first class.
AL-JUNAID INTITUE GROUP
12.Write a program with defines a template function name Add() which
adds two variable and then returns the sum . Define two variables of
type int main function and then Add() function on these variables .
Program output should look like this:
Ans:
#include <iostream>
using namespace std;

int main()
{
int x;
int y;
int sum;
cout<<"Enter the value of x = ";
cin>>x;
cout<<"Enter the Value of y = ";
cin>>y;
sum=x+y;
cout<<"sum of two Number = "<<sum;
}

13.program output should look like this Enter two integer values to be
added
Ans:
Enter First value:
12
Enter Second value:
25
Addition of two variable of different data type
Sum =37
SOLUTION:
#include <iostream>
using namespace std;

int main()
AL-JUNAID INTITUE GROUP
{
int x;
int y;
int sum;
cout<<"Enter the value of x = ";
cin>>x;
cout<<"Enter the Value of y = ";
cin>>y;
sum=x+y;
cout<<"sum of two Number = "<<sum;
}
14.Construct a logical expression to represent each following condition:
Ans:
a) Score is greater than or equal to 80 butt less than 90;
b) Answer is either N or n;
c) Nis even butt not 8;
d) Number is a multiple of both 4 and 6

class Outer{

int i = 60;

class Inner{

void display(){

System.out.println(i);

public static void main(String args[]){

Inner o = new Inner();

o.display();
AL-JUNAID INTITUE GROUP
}

15.In a C++ program how would you identify whether a declared variable is
a “reference variable ” or an “address variable”?
Ans:
A reference variable's address cannot be changed once set, but when I run the
command, how is my reference's value changed, but the address is the same?
And since the address is the same as, shouldn't the value of ref still be 3, or
maybe shouldn't a's value change into 5, because how can one address have
two values
16.What will be the value of the given expression after evaluation if x=3 and
y=2?
Ans:
(++X*++y)-2
We can rewrite the given expression as follows –

x = x – (x++ - ++y)

Let us break down the expression and compute the results for better
understanding

x++ - ++y

=3–8

= -5

Since we performed post increment with the variable x, after execution of


that line, we have x=4

Therefore,

x – (-5)
AL-JUNAID INTITUE GROUP
= 4 + 5 (- * - = +)

=9

Finally, we have x = 9
17.When memory is allocated dynamically using new operator within the
constructor of class then what is an appropriate place to de-allocate this
memory?
Ans:
The new operator denotes a request for memory allocation on the Free Store.
If sufficient memory is available, new operator initializes the memory and
returns the address of the newly allocated and initialized memory to the
pointer variable

18.How can we initialize an object which is declared as constant data


member in another class?
Ans:
There is no object being introduced. If you have a data member such as int x;
, no int object is created until you actually create an object of the type of the
class.
19.What happens when we use new and data operators?
Ans:
Use of the new operator signifies a request for the memory allocation on the
heap. If the sufficient memory is available, it initializes the memory and
returns its address to the pointer variable. The new operator should only be
used if the data object should remain in memory until delete is called.
Otherwise if the new operator is not used, the object is automatically
destroyed when it goes out of scope. In other words, the objects using new
are cleaned up manually while other objects are automatically cleaned when
they go out of scope.

20.Write a program which defines three variables of type double which


store three different values including decimal points, using set precision
AL-JUNAID INTITUE GROUP
manipulators to print all these values with different numbers of digits
after the decimal number.
Ans:

#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;
}
21.What is limitation of the friendship between classes?
Ans:
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.

22.What is the source and destination of cin?


Ans:
For cin, the source is normally keyboard and the destination can be an
ordinary variable i.e. native-data type variable

23.Write the general syntax of allocation memory dynamically to an array using


new operator?
Ans:
Following is the syntax:
new data_type [number_of_locations];

24.What is different between pointer and variable?


Ans:
AL-JUNAID INTITUE GROUP
Normal variable contains the value of variable either int or float whereas
pointer variable contains the address of another variable

25.What is difference between Unary and binary operators and how they
can be overloaded?
Ans:
 Unary operator takes one argument.
 a ++ is an example of unary operator
 Binary take two operators
 +,-,* are example of binary operators
 Overloaded binary operator may return any type
 Here is general syntax of overloading Return-type operator symbol
(parameters);
 Operator is keyword

26.How many types of templates?


Ans:
There are two different types of templates in C++ language i.e.’ function
templates and class templates.

27.What will be the output of following function if we call this function by


passing int 5?

Ans:

Template T reciprocal (T x) {return (1/x); }

0
The output will zero as 1/5 and its .05 but conversion to int makes it
zero
Above is prototype of template class so assume passing an int and returning
an int

28.Identify the errors in the following member operator function and also
correct them.
math * operator(math m);
AL-JUNAID INTITUE GROUP
math * operator (math m)
{
math temp;
temp.number= number * number;
2 return number;

Answers

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;

29.Define buffer? Explain its usage?


Ans:
A program that writes the output data to the disc, it will be nice to collect the
output data (numbers) and write it on the disc in one write operation instead
of writing the numbers one by one. The area where we gather the numbers is
known as buffer.

30.Why binary search algorithm is efficient than linear search algorithm?


Ans:
Binary search algorithm is more efficient than liner algorithm because the
arrays are sorted in ascending or descending order and we use “divide and
conquer” technique. In binary search each iteration reduces the search by the
factor of two but in the linear we have the same number of searches as we
have the number of elements. E.g. if we have array of 1000 elements the
linear search will take 1000 iterations however binary search will take max
10.
AL-JUNAID INTITUE GROUP
31.Post increment and pre increment k syntax ?
Ans:
Classname operator ++(); ---- pre increment
Classname operator ++(int) ---- post increment

32.What is language translator?


Ans:
So we need a translator which translates the code of our program into
machine language. There are two kinds of translators which are known as
Interpreter and Compilers. These translators translate our program which is
written in C-Language into Machine language.

33.Write something something about testing in designing program?


Ans:
Testing the programmer should design a test plan and use it to test the
program. It is a good idea, when possible, to have someone else test the
program.

34.Read the given below code and explain what task is being performed by
this function?

Ans:

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 ] ;


AL-JUNAID INTITUE GROUP
for ( int j = 0 ; j < numCols ; j ++ )

elements [ I ] [ j ] = 0.0 ;

Answers

In this code the matrix function is defined, it gets 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.

35.How many arguments a Unary Operator take? Can we make a binary


operator as unary operator?
Ans:
Unary operator takes only one arguments like i++ or i—(Post increment or
post decrement operators for integers) or ++i,--i (Pre increment or pre
decrement operators for integers) ,we cannot make Unary operator as binary
or binary as Unary operator.

36.Which arithmetic operators cannot have a floating point operand?


Ans:
Modulus operator
This operator can only be used with integer operands ONLY

37.What are manipulators? Give one example.


Ans:
Manipulators are operators used in C++ for formatting output. The data is
manipulated by the programmer’s choice of displayed. Endl manipulator:
This manipulator has the same functionality as the „\n newline character.

38.Write down piece of code that will declare a matrix of 3x3. And initialize
all its locations with 0;
Ans:
AL-JUNAID INTITUE GROUP
We can also do it as given below int matrix [3][3] = { 0 }; //all elements 0

39.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;

Answers
 Matrix m1 (m2); copy constructor
 Matrix m1, m2;
o m1 = m2; assignment operator
 Matrix m1 = m2; assignment operator
40.What will be the output of following function if we call this function by
passing int 5?

Ans:

template <class T>

T reciprocal(T x)

return (1/x);

Answers

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
AL-JUNAID INTITUE GROUP
41.Identify the errors in the following member operator function and also
correct them.
Ans:
math * operator(math m);
math * operator (math m)
{
math temp;
temp.number= number * number;
return number;

Answers

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;}
42.Write a program which defines three variables of type double which
store three different values including decimal points, using set precision
manipulators to print all these values with different number of digits
after the decimal number.
Ans:
Answer
#include <iostream>
#include <iomanip>
int main ()
{
AL-JUNAID INTITUE GROUP
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;
}

43.What are the advantages and disadvantages of using templates?


Ans:
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.

Disadvantage

 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.
AL-JUNAID INTITUE GROUP

44.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.
Ans:

Math obj1, obj2;

obj2= 10 + obj1 ;

Answer
#include <iostream>
math
{
mth operator + (obj1,obj2)
mth operator + (obj1,obj2)
{
mth operator + (obj1,obj2)
mth operator + (obj1,obj2)
}
}
45.Is it possible to define two functions as given below? Justify your
Answer.
func(int x, int y)
func(int &x, int &y)
Answer
No, we cannot define two functions as func(intx, inty) func(int &x, int&y)
because it’s give an error function not initializing.

46.What happens when we use new and delete operator?


Ans:
AL-JUNAID INTITUE GROUP
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.

47.What is the difference between function overloading and operator


overloading?
Ans:
Difference b/w function overloading and operator overloading is: In function
overloading, the functions have the same name but differ either by the
number of arguments or the type of the arguments.
Operator overloading is to allow the same operator to be bound to more than
one implementation, depending on the types of the operands.

48.Why the first parameter of operator function for << operator must be
passed by reference?
Ans:
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.

49.What is the difference between switch statement and if statement?


Ans:
 if statement is used when we have to check two conditions while
switch is a multi-conditional control statement.
 SWITCH statement can be executed with all cases if the “break”
statement is not used whereas IF statement has to be true to be
executed further.

50.How can we initialize data members of contained object at construction


time?
Ans:
AL-JUNAID INTITUE GROUP
Initializer list is used to initialize the contained objects at the construction
time.

51.How the data members of a class are initialized with meaningful values?
Ans:
The C++ compiler generates a default constructor for a class if the
programmer does not provide it. But the default constructor does not perform
any data members initialization. Therefore, it is good practice that whenever
you write a class, use a constructor function to initialize the data members to
some meaningful values.

52.Can we overload new and delete operators?


Ans:
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.

53.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;
}
Answer:
1
1
1

2)
void func2(){
AL-JUNAID INTITUE GROUP
static int x = 0 ;
x++;
cout << x << endl ;
}
Answer:
1
2
3

You might also like