4361 l2 [This question paper contains l2 printed pages.
Replace ellipses ... with proper code to override Your Roll No..........,..,.
rnethod display speed0 in class Bike and Car.
Use runtime polymorphism to display the contents Sr. No. of Qucstion Paper: 4361 G
of objects of classes Vehicle, Car, and Bike.
Unique Paper Code 32341101
(b) Dilfcrentiatc between call by value and call by Name of the Paper Programming Fundamentals
relerence using an appropriate example. (4) Using C++
Name of the Course B.Sc. (H) Computer Science
1 (a) Write a program in C++ to read a file intb.txt and Scmcster I
display the number of words in the file. (5)
Duratior:3 Hours
(b) Consider the example given below. The weather
department records the rainfall (in cms) for 4 lnstructions for Candidates
wceks in a 2D matrix. Write a code to calculate
Write your Roll No. ou the top immediately on rcc9lpt
thc average rainlall for each week. (5)
of this question paper.
Mon Tue Wed Thur Fri Sat Sun Z. Question Number l is compulsory in Section A.
Weck I 2.2 3 5 I 0.9 1.1 1.2
3. Attempt any 4 questions from Section B.
Weck 2 1.6 2.t 1.7 2.6 5.1 0.8 t.2
Weck 3 1.7 1.9 t.5 3.7 3.9 1.5 4.1 4. Parts of a question should be attempted together
Week 4 0.6 0.5 1.3 3.3 1.5 1.9 2.5
Section A
l. (a) Give the C++ declarations fbr the following
(5)
(1000) P.T.O.
4361 2 436t ll
(i) Array ol type double that holds l0 valucs. class Vehicle {
int max_speed;
(ii) Opening a file val.txt in write mode
public:
(iii) Pointer to character variable x initialised
Vehicle0 {
to 'A', .
:
max speed l0;
(iv) Function prototype that accepts variables
x and y where x is of type double and y is
virtual void display_speed0 {
an array of integer.
cout<<"Maximum speed of vehicle is: "<<max_speed;
(v) Declare a variable t of type integer,
initialised to value l3 and use suitable
class Bike: public Vehicle {
manipulators to display the value in octal
public:
format.
Bike 0 {
(b) Identify all the syntax errors (if any) in the given max_speed = 60;
code and rewrite the correct code: )
(i) for (i=0, i< 10, i++) (2)
{
);
cout<<"Square is: ", i*2, "\n"; class Car: public Vehicle {
public:
Car0 {
(ii) void arel(int length:8, int breadth) (2)
max_speed = 100;
{
return length*breadth; )
)
cout<<"Area of the rectangle is: );
"<<area (8, 20);
P. T. O.
4361 10 4361 3
5 (a) Write a f'unction in C++ that takes a given number (iii) class AA (3 )
{
n and displays the square root if the number is public:
positive otherwise throw an exception with a void print(int x);
message "Negative number found". (4) int AA(int);
private
(b) Explain copy constructor with the help of an int size;
example. (3) int max;
)
(c) Give the output of the following C++ co6s 5rirr.,.
(c) Give the output of the following. Assume all
Assume all the header files have been included
necessary header files have been included. (2)
(3)
int foo(float a) { (i) void main 0 {
returo a/21 int a : 10;
) intb: (ao/o2)2 l0: 100;
int foo(int a) { cout << b;
return a* 2; )
void main0 { ( ii) void mainO { (3)
float a = 3.5; int x: l;
cout<<foo(a); try 1
cout << "lnside try n";
if (x < 0)
I
6 (a) Create the following hierarchy in C++ and throw x;
implement the functions as mentioned below: cout << "A11er throw n";
(6)
)
P.T.O.
4361 4 4361 9
catch (int x) { (b) Consider the following C++ class: (8)
cout << "Exception Caught n";
class location
l
cout << "After catch n"; {
I int latitude, longitude;
)
(iii) char val : 'a'; (3)
switch(val) {
where latitude and longitude are the geographic
case 'a': coordinates of a location. For the above class,
cout<<"case a executed "; provide the definition of the lbllowing member
case 'b': functions:
cout<{"case b executed ";
break; (i) Parameterised Constructor
default:
(ii) Overload the " " Operator to subtract two
cout<<"Default block executed";
Iocations.
)
(iii) Display Function.
(iv) void passing(int &a, int b){ (3)
u= 6+b; Define the main function. Use operator overloading
b= b-a; ) to create two locations and store the difference
void main0 { between these two locations and display the result.
inta=7,b=6; For example, two locations. l0 latitude, l5
passing(a,b);
longitude, and 6 latitude, 9 longitude will give the
cout<<"a = "<<a'
result 4 latitude, 6 longitude when subtracted.
cout<<"b = "<<b'
)
P.T.O
4361 8 4361 5
(b) Explain the difference between function (v) class base { (5)
overloading and function overriding with suitable public:
example. (4)
base0 {
cout<<"In base"<<"\n" ;
3 (a) Give the output with an explanation ibr the
)
fbllowing. Assume all necessary header files have
-base0 {
been included. (5)
cout<<"Base destructor\n";
string strl ("Hello"); )
string str2 ("HEllo"); ];
class derived: public base {
coul.<<str I .compare( st12 )<<"\n";
public:
cout<.str I.substr(3,1 0)<<"\n':
derived 0 {
cout<<"In derived"<<"\n";
cout<<str2.replace( I , 2, "el")<<"\n"; )
-derived$ {
cout<<strl.insert(4, str2);
cout<<" Derived destructor\n" ;
(b) Write a function in C++ using command line )
arguments that adds all the numbers passed as );
command line arguments and display the sum. void main0 {
(s)
derived d; I
4 (a) Explain pure virtual functions by giving an cout<<fi xed<<setprecision(2)<<l 3.4546<<" Vt";
example. (2)
)
P. T. O.
436t 6 4361 7
(d) Give the output with justification. Assume memory Section B
address of val to be 1024, memory address of
ptrl to be 1078 and memory address of ptr2 to be
) (a) Define a class in C++ weightConverter with the
I 154 : (3)
following features: (6)
#include <iostream> lkg = 2.2 pounds
int main0
(i) Declare the Data Members: weightKG,
{
weightPounds of type double
int val :5;
int *ptrl = &val; (ii) Define the parameterised constructor to
int **ptr2 = &ptrl ; initial ize weightKG.
cout<<"*ptr1 = t'<<xp11l <<,'\n";
(iii) Define the member functions with
cout<<,,*ptr2 = "<<*ptr2<<',\n";
appropriate prototypes for the following
: ua1*,r.p62<<"\n"i
"ou,.a,tr,*ptf2 task:
return 0;
l. Converting WeightKc to WeightPounds
)
2. Write an inline function averageWeight0
(e) Convert the following for-loop to equivalent do- that accepts one weight in Kilograms
while loop : (4) and another in Pounds and returns the
average weight of both (in the same
for(int i :0; i<100; i: i*3)
units)
for (int j = l0; j<100; j++) (iv) Define the main function to initialize the
cout<<i*j; object of weightConverter and call the
cout<<"\n"; appropriate member function to convert
i
weight of 50 kgs in pounds.
P.T.O