CS201 Solved Subjective Final Term by Junaid
CS201 Solved Subjective Final Term by Junaid
CS201 Solved Subjective Final Term by Junaid
CS201-Introduction to
Programming
(Solved Subjective)
LECTURE FROM
(23 to 45)
[email protected] FOR MORE VISIT JUNAID MALIK
[email protected]
VULMSHELP.COME 0304-1659294
CS201-Introduction to
AL-JUNAID TECH INSTITUT
Programming
Question 1:-
Identify each of the following as system software and application software. (mark 5)
LINUX, DISK CLEANUP, WORD PROCESSOR, WINDOWS, STUDENT
INFORMATION
Answer:-
System software: - Linux, Disk cleanup, windows.
Application software:- Word Processor, Student information
Question 2:-
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 numbers of digits after the decimal number.(5)
Answer:-
#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 3:-
Define static variable also explain life time of static variable? (3)
Answer:
Static variable means maintaining the state of a variable. It exists and lives around
even when we are outside the function. It is created and initialized only once during
the lifetime of the program and therefore it will be destroyed or taken out of
memory only once during the lifetime of the program.
Question 4:-
What do you know about run time error? (3)
Answer:
Run-Time Errors
rules
AL-JUNAID TECH INSTITUT
Example: Accessing a non-existent variable, property, method,
object, etc (e.g. a method name is misspelled)
• Sources of these can be determined by a careful
Question 6:
what is the source and destination of cin?(2)
Answer:
For cin, the source is normally keyboard and the destination can be an ordinary
variable i.e. native-data type variable
Question 7:
Write the general syntax of allocation memory dynamically to an array using new
operator? (2)
Answer:
Following is the syntax:
new data_type [number_of_locations];
Question 8:
What is diffrent between pointer and variable?
Answer:-
normal variable contains tha value of variable either int or float whereas pointer
variable contains the address of another variable
Question 9:
What is difference between Unary and binary operators and how they can be
overloaded?
Answer:-
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
AL-JUNAID TECH INSTITUT
Here is general syntax of overloading
Return-type operator symbol (parameters);
Operator is keyword
Question 10:
How many types of templates?
Answer:-
There are two different types of templates in C++ language i.e.’ function templates and
class templates.
Question 11:
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:
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
Question 12:
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 *operator (math *m)
{
math temp;
temp = m;
AL-JUNAID TECH INSTITUT
temp.number= number * number;
return temp.number;
Question No.13:
Define buffer? Explain its usage? 5 MARKS
Answer:
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.
Question No.14:
Why binary search algorithm is efficient than linear search
algorithm? 5 marks
Answer:
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.
Question No.15:
Operator function ka syntax (3 marks)
Question No.16:
Post increment and pre increment k syntax btana thay (2 marks)
Answer:
Classname operator ++(); ---- pre increment
Classname operator ++(int) ----- post increment
Question No.17:
What is language translator?(2 marks)
Answer:
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
Question No.18:
Write something something about testing in designing program?
3 MARKS
Answer:-
Testing. The programmer should design a test plan and use it to test the program. It
AL-JUNAID TECH INSTITUT
is a good idea, when possible, to have someone else test the program.
Question No.19:
Read the given below code and explain what task is being performed by this function.
5 MARKS
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
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;
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
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.
OR
Answer: 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.
Answer:-
int matrix [3] [3] ;
Answer:-
1) Matrix m1 (m2); copy constructor
2) Matrix m1, m2;
m1 = m2; assignment operator
3) Matrix m1 = m2; assignment operator
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.
}
Question No: 27 ( 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.
Answer:-
#include <iostream>
#include <iomanip>
int main ()
{
double x1 = 12345624.72345
double x2 = 987654.12345
double x3 = 1985.23456
cout << setprecision (3) <<
x1<< endl;
AL-JUNAID TECH INSTITUT
cout << setprecision (4) <<
x2 << endl;
cout << setprecision (5) <<
x3<< endl;
return 0;
}
1)
void func1(){
int x = 0;
x++;
cout << x << endl;
}
Answer:
1
1
1
2)
void func2(){
AL-JUNAID TECH INSTITUT
static int x = 0 ;
x++;
cout << x << endl ;
}
Answer:
1
2
3
Answer:
#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);
void setb(int);
void setc(int);
int geta();
int getb();
int getc();
};
void Person: :seta(int aa)
{
a=aa;
}
b=bb;
AL-JUNAID TECH INSTITUT
}
void Person: :setc (int cc)
{
c=cc;
}
main()
int num;
cin>>num;
}
}
1)
void func1(){
int x = 0;
x++;
cout << x << endl;
}
2)
void func2(){
static int x = 0 ;
x++;
cout << x << endl ;
AL-JUNAID TECH INSTITUT
}
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.
Answer:
#include <stdio.h>
AL-JUNAID TECH INSTITUT
#include <iostream>
#include <cstring>
using namespace std;
class Date
{
public:
int day;
int month;
int year;
public:
Date()
{
day=0;
month=0;
year=0;
void setDay(int);
void setMonth (int);
void setYear(int);
int getDay();
int getMonth();
int getYear();
void showDate();
};
AL-JUNAID TECH INSTITUT
void Date: :setDay(int d)
{
if{d<1 | | d>31)
cout<<"Invalid month Renter it";
cin>>d;
}
day=d;
}
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;
object.setDate();
object.Display();
system("pause");
}