Sei sulla pagina 1di 9

Answer key for 1st year 1st monthly Question(Group- A)

PART - A

1. Declaring a member function with the const keyword specifies that the
function is a "read-only" function that does not modify the object for which it is called. To declare a constant member function, place the const keyword after the closing parenthesis of the argument list. The const keyword is required in both the declaration and the definition. A constant member function cannot modify any data members or call any member functions that aren't constant. Example: void display() const { cout<<Bye...} 2. A =0 A=7 B =6 A=7 B=6; because of a is a static variable and b is an ordinary variable. Static variable not depending on any object. 3. t1,t2 & t4 for these objects constructors are must but in given code those constructors are missing. This is an error. Correct Program: class Test { public: Test(){} {cout<<x+y+z; } Test(int x){cout<<x;} { Test(int x,int y, int z)

Test(int x,int y) void main() { 4. private

cout<<x+y; } };

Test t1,t2(30),t3(10,20),t4(1,3,5); }

5. It is on infinite loop. Instead if i==3 their given i=3. So, every time 3 will be assigned to i. 6. Answer : 12 Priority 1 : pre increment/decrement Priority 2: arithmetic operators Priority 3: Assignment Operator increment/decrement Priority 4: Post

the given expression 3 pre increment. After evaluate all the 3 pre increment, the value of a=3. Next step Arithmetic operators ( 3+3*3+3-3). Now this expression returns 12. Next step assignment operator(so, 12 is assign to x). Now the value of x is 12. Next step the post increment. At the end the value of a=5 & x=12. 7. Output is only right justified. Because of setiosflags not given with cout(correct format: cout<<setiosflags(ios::left)). 8. Any 2 difference

This is the usual method to call a function in which only the value of the variable is passed as an argument. In this method, the address of the variable is passed as an argument. Any alternation in the value of the argument passed is local to the function and is not accepted in the calling program. Any alternation in the value of the argument passed is accepted in the calling program. Memory location occupied by formal and actual arguments is different Memory location occupied by formal and an actual argument is same and there is a saving of memory location. Since a new location is created, this method is slow. Since the existing memory location is used through its address, this method is fast. There is no possibility of wrong data manipulation since the arguments are directly used in an application. There is a possibility of wrong data manipulation since the addresses are used in an expression. A good skill of programming is required here

9. The :: operator is known as Scope Resolution operator in C++ and it used for three purpose. 1.To access the Global variables when same variable name exist in Local scope.In this case the the local variable can be accessed normally but when we want to access global variable we have to use :: (Scope Resolution) operator. 2. To define the member functions outside the class. In this case when we want to define the member function whose prototype is declared inside the class 3.The third use of scope resolution operator is used with static data members and static member functions. 10.Answer :1 !(1 && (!(0||1) =>!(1)=>0) )=>!(0) =>1

PART - B
11. #include <iostream.h> #include <conio.h> #include <iomanip.h> class Student { public: char name[20]; int reg_no,m1,m2,m3; Student() { reg_no=m1=m2=m3=0; } void get() { cout<<"\n Enter the value for name,reg no,m1,m2 & m3\n"; cin>>name>>reg_no>>m1>>m2>>m3; } void display(Student *s2,int n); }; void Student :: display(Student *s2,int n) { int res1=0,res2=0,res3=0; cout<<setiosflags(ios::left); cout<<setw(25)<<"Student Name"<<setw(10)<<"Reg No"<<setw(8)<<"Mark1"<<setw(8)<<"Mark2"<<setw(8)<<"Mark3"<<endl; for(int i=0;i<n;i++) {

cout<<setw(25)<<s2[i].name<<setw(10)<<s2[i].reg_no<<setw(8)<<s2[i].m1 <<setw(8)<<s2[i].m2<<setw(8)<<s2[i].m3<<endl; res1=res1+s2[i].m1; res2=res2+s2[i].m2; res3=res3+s2[i].m3; }

cout<<"\n Subject wise average :"; cout<<"\n Mark1 Average :"<<res1/(float)n<<"\n Mark2 Average :"<<res2/(float)n<<"\n Mark3 Average :"<<res3/(float)n; } void main() {

Student s[3],s1; clrscr();int n; cout<<"\n Enter the number of students :"; cin>>n; for(int i=0;i<n;i++) { s[i].get(); } s1.display(s,i); getch(); } 12. Concepts of constructor & destructor, its type (Default, Parameterized & Copy) with example.

13. #include <iostream.h> #include <conio.h> class Date { public: int day,month,year; Date() { day=month=year=0; } Date(int d,int m, int y) { day=d; month=m; year=y; }

Date compare(Date t1) { Date t2; /* This is for sample. Do the proper calculation*/ t2.day=day-t1.day; t2.month=month-t1.month; t2.year=year-t1.year; return t2; }

void display() const { cout<<"\n"<<year <<" years "<<month <<" months "<<day<<" days"; } }; void main() { Date d1,d2(29,1,2013),d3(27,1,1993);

clrscr(); d1=d2.compare(d3); d1.display(); getch(); } 14. #include <iostream.h> #include <conio.h> class BankAccount { public: char cus_name[30]; int acc_no; float balance; BankAccount() { acc_no=0; balance=500.0; } void deposit(int d)

{ balance=balance+d; cout<<"Succ..."; cout<<"Balance :"<<balance; } void withDraw(int w) { if(w<balance) { balance=balance-w; cout<<"Withdrawn successfully...."; cout<<"\nBalance Amount :"<<balance; } else { cout<<"\n Sorry... insufficient balance"; } } void display() const { cout<<"\n Balnace Amount in Account :"<<balance; } ~BankAccount() { cout<<"\n Object Destroyed.."; }

};

void main() { BankAccount ba; clrscr(); int c,n,t=1; char ch; do { cout<<"\n Press d -> Deposit w-> Withdrawn Esc-> exit from menu"; c=getch(); switch(c) { case 'd': cout<<"\n Enter the amount to deposit:"; cin>>n; ba.deposit(n); break; case 'w': cout<<"\n Enter the amount to withdraw:"; cin>>n; ba.withDraw(n); break; case 27 : ba.display(); t=0; break; default : cout<<"\nSorry, Invalid Choice :";

} if(t==1) { cout<<"\n press y to continue or press other key quit...."; ch=getch(); } else { ch='n'; }

}while(ch=='y'); cout<<"\n Thank you..."; getch();

Potrebbero piacerti anche