Professional Documents
Culture Documents
com
Please read the following instructions carefully before attempting any of the
uestions:
Write the main () to call the - operator and print the result also. This operator should subtract
the items of one object from the other.
1. Instance variables
2. Instance methods
3. Constructors
Create a class named Order. This class has the following attributes/ data members
i) OrderID
ii) Items
iii) PricesOfItems
a) Create the object of this class using parameterized constructor in order to initialize
OrderID and Items data members. Default values of both the data members must be
equal to 1.
b) Write the following member functions of this class;
i) OrderBill ( ), this function will calculate the invoice of an order and display the
result. The formula to calculate the invoice is
invoice=SumPrices();
ii) SumPrices( ), this function will sum the prices of the items entered by the user
from
keyboard in PricesOfItems array and returns the result.
c) Write the setter functions for OrderID, Items, and PricesOfItems. And getter functions
for OrderID and Items.
Write a program that inputs one string of max length of 10. If the string is identical to "Virtual",
output the message "Correct Password"; otherwise, output the first four characters in the
message and the length of the message.
If we open a file stream myfile for reading, what will give us the current position of the file
pointer?
1 tellg()
2 tellp()
3 seekg()
4 seekp()
5 fstream.h
There is a class Student, Which one of the following is a valid destructor for this class.
1 Student();
2 Student(int);
3 ~ Student();
4 int~ Student();
5 ~ Student(int);
Write code that will declare, initialize, and fill in an array of objects of type int. After your code
executes, the array should look as follows.
0 2 4 6 8 10 12 14 16 18
www.vujannat.ning.com
A friend function
o must be having a prototype with no arguments
o must be invoked by the class that declares it a friend
o must be invoked by an object of the class that declares it a friend
o can access the private data of the class that declares it a friend
o cannot access the data members of a class
o OR ( || )
o AND ( &&)
o XOR ( ^ )
o Complement operator ( ~ )
o Insertion operator (>>)
Write a program that uses a function multiple(int,int) that determines for a pair
of integers whether the second integer is a multiple of the first. The function
should take two integer arguments and return 1 (true) if the second is a multiple
of the first and 0 (false) otherwise. Use this function in a program that inputs a
series of pairs of integers.
Write a program that uses a function template called min to determine the
smaller of two arguments. Test the program using integer, character and floating
point number pairs in main ().
b) Write a member function of this class named deposit (), this function will
calculate the current balance for the user's account. In deposit function user will
be prompted to enter the amount to be deposited and displays the incremented
balance.
Also write the getter and setter functions for the data members of this class
If the statements
int j,k;
j = 123;
k= 234;
int* q, * r;
cout<<*q<<' '<<*r;
are executed, what will be displayed?
1. height
2. width
a) Write a parameterized constructor to initialize the data members.
b) Write member functions to Overload the following Operators and Display
the Results.
1. –
2. *
Implement the following checks in operator overloading functions:
i. Check for negative values in subtraction before and after the operation,
change them into absolute values or negate them. (If width = -3 its
absolute value is width=3.)
ii. Check for zero values in multiplication. If any value is zero, displays a
message" Height or width cannot be zero". And exit from the function.
In main () create the objects of the class and assign values to their data
members and then call the overloaded operators
A copy constructor
o takes no arguments
o copies the data of any two constructors in that class
o takes an arbitrary number of arguments
o creates a new object that later may be assigned the data of an
existing object
o creates an object initialized with the same data as an existing object
c) prompt the user for 8 integers and store those integers in the array
StudentID/LoginID: ______________________________
► int[8]
► int[7]
► myArray[8]
► myArray[7]
► an object is initialized with another object data of the same class when it’s
created
► an object is initialized with another object of any class when it’s created
► 9.6
► -9.6
► 9
► -9
b) 01001000 | 10111000 =
c) 01110010 ^ 10101010 =
Write a program that will create a class stat. the class stat has the following data
members.
A float array values of size 10.
The variables sum and avg both of type float.
The class stat has the following member functions.
takedata()
calculate()
show()
The function takedata() is used to read data from the user through keyboard into the
array values. The function calculate() is used to calculate the sum of all values in the
array values and store the value in the variable sum. The function calculate(), then
calculates the average by using the formula sum/10 and stores the result in the variable
avg. The function show() is used to display the value in avg on the screen.
The program will create an object obj1 in the class stat. The program will read data into
obj1 using takedata(), calculate the average using the function calculate() and then
display the result using show() function.
Write a program that will create a class rectangle. The class rectangle has the data
members width and height, both of type float. The class rectangle has one member
function getdata(). The program will create two objects rect1 and rect2 of the class
rectangle. The program will read data into rect1 and rect2 trough the function getdata().
The program will compare both the objects by overloading “==” operator to decide that
rect1 and rect2 are same or not.
If rect1 and rect2 are same then program will display the message “Both rectangles are
same” otherwise the program will display “Both rectangles are not same”.
Note: rectangles are called same if their heights and widths are same.
We have two classes Class1 and Class2 and we want to construct two way friendships
between them. Write the coding sketch to achieve this friendship.
www.vujannat.ning.com
Write a program that will create and display the following series:
65, 44, 27, 14, 5, 0, -1, 2, 9, 20.
Hint:
The series is created by the formula 2x2 – 3x
for x=-5 to 4
The variable x is of type integer.
146
76
8
70
Question No. 4 Marks : 2
Carefully analyze the following lines of code and chose the correct option.
ch1=”a”;
ch2=’a’;
2 bytes
4 bytes
8 bytes
10 bytes
Question No. 6 Marks : 2
an array variable
array declaration
struct
constant pointer
#include <iostream>
int main()
{
unsigned short x = 1;
return 0;
}
b) Program2
#include <iostream.h>
int main()
{
unsigned short x = 1024;
while(x>=1)
{
cout <<x << endl;
x =x >> 1;
}
return 0;
}
Question No. 8 Marks : 6
MIDTERM EXAMINATION
SPRING 2007 Marks: 50
CS201 - INTRODUCTION TO PROGRAMMING Time: 90min
(Session - 1 )
StudentID/LoginID: ______________________________
► Last
► Middle
► Post Design
► First
►
An arithmetic operator
► A logical operator
► A relational operator
► A division operator
In C/C++ language when an array is passed to a function then by default its passing
Mechanism is,
► Call by value
► Call by Reference
int x = 2 * 3 + 4 * 5;
What value will x contain in the sample code above?
► 22
► 26
► 46
► 50
► Rectangle
► Arrow symbol
► Oval
► Circle
Which one of the following will declare a pointer to an integer at address 0x22ff74 in memory?
► int *x;
*x = 0x22ff74;
► int *x = &0x22ff74;
► int *x = *0x22ff74;
► Must
► Optional
► Not necessary
► A syntax error
► 3
► 5
► 7
► 4
i. sqrt();
ii. getline();
iii. printf();
iv. exit();
v. rand();
Write a program which consists of three variables Area, Per, Base, this program should find
and display the area of triangle using the formula,
Write a function BalerAvg which calculate and display the average of a player (Baler), call this
function in main program (function). Take input of runs given and ball delivered from the user in
main funciton.
The average may be calculated by the formula,
StudentID/LoginID: ______________________________
► 1
► 3
► 2
► 4
► #
► {
► (
► ~
► new
► delete
► +=
► sizeof
Analyze the following code and list all the errors and line numbers in the given code.
int i=2;
for(i<=100;i++)
{
if(i%2==0)
{
cout<<i;
cout<<” is even /n’;
}
}
Declare a class student having the following data members and member functions in the
Connecting VU Students
class.
stdno(integer)
stdname(character array of size 20)
phone(long integer)
a. Write the statement that will create an array of 10 objects of the class student and
place the base address of the array in a pointer ptr of the class student given in Question
No. 6.
b. Write the code of the parameterized constructor for the class student given in
Question No. 6.
Write a program that will create a class distance. The class distance consist of two data
members feet and inches, both of types float. The class distance consist of two member
functions, getdata() and show(). The function getdata() is used to read data from the
user through keyboard into feet and inches. The show() function is used to display the
values of feet and inches on the screen.
The program will create two objects dest1 and dest2 of the class distance. The program
will read data into dest1 and dest2 using getdata(). The program will create another
object dest3 of the class distance by overloading the minus “-“operator trough
expression dest3=dest1-dest2. The program will then display dist3 on the screen using
show() function. The program also takes care of the fact that distance can not be
negative. If dest2 is larger then dest1 then dest3 should not be created and program
should display the message “Distance can not be negative”.
StudentID/LoginID: ______________________________
► Iterative function
► Inline function
► Recursive function
► main ()
The reserved words public and private comes under the category
Connecting VU Students
3
► structures
► strings
► accessibility modifiers
► types of functions
Write a program that will read any four digit number from the user through key board and st
number into an Integer variable n. The program then calculates the sum of all the digits in t
number n and display the result on the screen.
Write a program that will create a class book. The class book has the following data
members
bookid( long integer)
author( char array of size 20)
yearpb( integer)
The class book has a parameterized constructor and a member function
show() that is used to display the data in a tabular form. The program creates three
objects b1, b2 and b3 and assigned data trough the parameterized constructor. The
program then display the using the show() function.
Analyze the following code and list all the errors along the line numbers.
class test
{
private:
int a, b;
public:
test( int x, int y)a(x),b(y){};
void show()
{
cout<<endl<<a<<endl<<b;
}
test operator +(test t);
}
test operator+(test t)
{
Connecting VU Students
4
test temp;
temp.x=a+a.t;
temp.b=b+b.t
return(temp);
}
Is it possible to define a two dimensional array of objects in a class? If yes justify your
answer by giving an example program. If no then give reasons.
Write a program that uses a function template divide(). The function divide() will take two
values and return the reminder of these two values. Test the function with two integer
values x=104 and y=49 and two float vales a=6.9 and b=2.7.