0% found this document useful (0 votes)
85 views

Fie Urmatorul Program

1. The document presents several C++ code snippets and questions about them. The first code defines two classes A and B, with B inheriting from A. It then creates objects of types A and B and calls methods on them using pointers, checking which calls are incorrect. 2. The second question presents a similar code snippet and asks which method call is correct. 3. The third question repeats this pattern, presenting code and asking which method call is correct. 4. The fourth question does the same, but notes that the private method becomes accessible through dynamic binding. It asks which call is incorrect. 5. The remaining questions follow the same format of presenting C++ code snippets with inheritance,

Uploaded by

antrenoru
Copyright
© Attribution Non-Commercial (BY-NC)
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)
85 views

Fie Urmatorul Program

1. The document presents several C++ code snippets and questions about them. The first code defines two classes A and B, with B inheriting from A. It then creates objects of types A and B and calls methods on them using pointers, checking which calls are incorrect. 2. The second question presents a similar code snippet and asks which method call is correct. 3. The third question repeats this pattern, presenting code and asking which method call is correct. 4. The fourth question does the same, but notes that the private method becomes accessible through dynamic binding. It asks which call is incorrect. 5. The remaining questions follow the same format of presenting C++ code snippets with inheritance,

Uploaded by

antrenoru
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 17

1.

Fie urmatorul program:


// Public redeclarat private
// #include <iostream.h>
class A{
public:
void m(){cout<<"A:m()"<<endl;};
virtual void v(){cout<<"A:v()"<<endl;};
};
class B: public A{
private:
void m(){cout<<"B:m()"<<endl;};
virtual void v(){cout<<"B:v()"<<endl;};
};
void main(){
A a,*p;
B b;
b.m();
b.v();
// obiect de tip A
p=&a; p->m(); p->v();
// obiect de tip B
p=&b; p->m(); p->v();
}
Care expresie este incorecta:
a. b.v();
b. p=&a;
c. p->m();
d. p->v();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2. Fie urmatorul program:
// Public redeclarat private
#include <iostream.h>
class A{
public:
void m(){cout<<"A:m()"<<endl;};
virtual void v(){cout<<"A:v()"<<endl;};
};
class B: public A{
private:
void m(){cout<<"B:m()"<<endl;};
virtual void v(){cout<<"B:v()"<<endl;};
};
void main(){
A a,*p;
B b;
b.m();
b.v();
// obiect de tip A
p=&a; p->m(); p->v();
// obiect de tip B
p=&b; p->m(); p->v();
}
Care expresie este corecta:
a. b.m();
b. b.v();
c. p->f();
d. p->m();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3. Fie urmatorul program:
// Public redeclarat private
#include <iostream.h>
class A{
public:
void m(){cout<<"A:m()"<<endl;};
virtual void v(){cout<<"A:v()"<<endl;};
};
class B: public A{
private:
void m(){cout<<"B:m()"<<endl;};
virtual void v(){cout<<"B:v()"<<endl;};
};
void main(){
A a,*p;
B b;
b.m();
b.v();
// obiect de tip A
p=&a; p->m(); p->v();
// obiect de tip B
p=&b; p->m(); p->v();
}
Care expresie este corecta:
a. b.m();
b. b.v();
c. p->f();
d. p->v();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
4. Fie urmatorul program:
// Public redeclarat private
// Metoda PRIVATE DEVINE ACCESIBILA PRIN MECANISMUL DE LEGARE DINAMICA
#include <iostream.h>
class A{
public:
void m(){cout<<"A:m()"<<endl;};
virtual void v(){cout<<"A:v()"<<endl;};
};
class B: public A{
private:
void m(){cout<<"B:m()"<<endl;};
virtual void v(){cout<<"B:v()"<<endl;};
};
void main(){
A a,*p;
B b;
b.m();
b.v();
// obiect de tip A
p=&a; p->m(); p->v();
// obiect de tip B
p=&b; p->m(); p->v();
}
Care expresie este incorecta:
a. b.m();
b. p=&a;
c. p->m();
d. p->v();
5. Fie urmatorul program:
#include <iostream.h>
class Cerc{
public:
Cerc(float r):raza(r){}
float getRaza(){return raza;}
Cerc operator++(){raza++;return *this;}// return by value
Cerc& operator--(){raza--;return *this;}// return by reference

private:
float raza;
};
void main(){
Cerc c(1.0);
cout<<(++(++c)).getRaza()<<” ”;
cout<<c.getRaza()<<” ”;
cout<<(--(--c)).getRaza()<<” ”;
cout<<c.getRaza()<<” ”;
}
Programul afiseaza:
a. 3300
b. 3201
c. 3200
d. 3211
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
6. Fie programul:
#include <iostream.h>
class Cerc{
public:
Cerc(float r):raza(r){}
float getRaza(){return raza;}
Cerc operator++(){raza++;return *this;}// return by value
Cerc operator--(){raza--;return *this;}// return by value
private:
float raza;
};

void main(){
Cerc c(1.0);
cout<<(++(++c)).getRaza()<<” ”;
cout<<c.getRaza()<<” ”;
cout<<(--(--c)).getRaza()<<” ”;
cout<<c.getRaza()<<” ”;
}
Programul afiseaza :
a. 3201
b. 3200
c. 3301
d. 3211

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7. Fie programul:

// singleton
// constructor private
#include <iostream.h>
#include <conio.h>
class S{
public:
static S* create()
{
if(i==0){
refS=new S(); i++;
}
return refS;
}
void setName(char *s){name=s;}
char* getName(){return name;}
static int getNr(){return nr;}

private:
static S* refS;
static int i;
static int nr;
S(){nr++;}
char *name;

};

int S::i=0;
int S::nr=0;
S* S::refS=0;
void main(){
S *o1, *o2;
o1=S::create();
o2=S::create();
o1->setName("Matematica");
o2->setName("Informatica");
cout<<o1->getName()<<endl;
cout<<o2->getName()<<endl;
cout<<S::getNr()<<endl;
getch();
}
Programul afiseaza:
a. Matematica Informatica 1
b. Informatica Informatica 1
c. Informatica Informatica 2
d. Matematica Informatica 2
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
8. Se considera clasa:
#include "iostream.h"
template <class T>
class C{
public:
C(int nrmax=0);
void put(T);
T get();
void remove();
private:
int nrmax;
int nrelem;
int first;
int free;
T* support;
};

template <class T>C<T>::C(int nrmax):nrmax(nrmax){


first=free=nrelem=0;
support = new T [nrmax-1];
}
template <class T> void C<T>::put(T e){
support[free]=e; free= ++free % nrmax; nrelem++;
}
template <class T> T C<T>::get(){
return support[first];
}
template <class T> void C<T>::remove(){
first= ++first % nrmax;
--nrelem;
}
a. Clasa C defineste o structura de tip coada (FIFO)
b. Clasa C defineste o structura de tip stiva (LIFO)
c. Clasa C este un vector
d. Clasa C este abstracta

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

9. Fie urmatorul program, in care se utilizeaza clase abstracte:


// abselem.h
#ifndef ABSTRACTELEM_H
#define ABSTRACTELEM_H
class AbstractElem{
public:
virtual void display()=0;
virtual void process()=0;
};
#endif
//LIST.h
#ifndef LIST_H
#define LIST_H
#include "abselem.h"

class LIST{
public:
LIST(int nrmax=0);
void put(AbstractElem *);
AbstractElem* get();
void remove();
private:
int nrmax;
int nrelem;
int first;
int free;
AbstractElem* *support;
};
#endif
//elements.h
#include "abselem.h"
#include "iostream.h"
class Person: public AbstractElem{
public:
Person(char *name);
virtual void display();
virtual void process();
private:
char *name;

};
class Car: public AbstractElem{
public:
Car(char *name);
virtual void display();
virtual void process();
private:
char *name;
};
//driver.cpp
#include "elements.h"
#include "LIST.h"
LIST x(3);
void main(){
x.put(new Person("Tudor"));
x.put(new Person("Andrei"));
x.put(new Car("B-39-TDR"));
x.get()->process(); x.remove();
x.get()->process(); x.remove();
x.get()->process(); x.remove();
}
//LIST.cpp
#include "LIST.h"
LIST::LIST(int nrmax):nrmax(nrmax){
first=free=nrelem=0;
support = new AbstractElem* [nrmax-1];
}
void LIST::put(AbstractElem * pe){
support[free]=pe; free= ++free % nrmax; nrelem++;
}
AbstractElem* LIST::get(){
return support[first];
}
void LIST::remove(){
first= ++first % nrmax;
--nrelem;
}
//elements.cpp
#include "elements.h"
Person::Person(char *name):name(name){};
void Person::display(){cout<<name<<endl;}
void Person::process(){
cout<<"A Person: ";
display();
}
Car::Car(char *name):name(name){};
void Car::display(){cout<<name<<endl;}
void Car::process(){
cout<<"A Car:";
display();
}

Programul afiseaza datele in ordinea:


a. Tudor Andrei B-39-TDR
b. B-39-TDR Andrei Tudor
c. Andrei B-39-TDR Tudor
d. B-39-TDR Tudor Andrei
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

10. Fie programul:

// destructor static,
#include <iostream.h>
class B{
public:
~B(){cout<<"~B()"<<endl;}
};
class D: public B{
public:
~D(){cout<<"~D()"<<endl;}
};
void main(){
clrscr();
B *pb;
D *pd;
pd= new D();
pb=new D();
delete pb;
delete pd;
}
Programul afiseaza :
a. ~ B() ~B() ~D()
b. ~ B() ~D()
c. ~ B()~D()~B() ~D()
d. ~ B() ~D() ~B()
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
11. Fie urmatorul program:
// Constructor cu semantica prin referinta
// Operatorul = este cel implicit

#include <iostream.h>
class C{
public:
C(int n=0, int v[]);
void set(int i, int val){pi[i]=val;}

friend ostream& operator<<(ostream &o, const C&);


private:
int dim;
int *pi;
};
C::C(int n, int v[]) {
dim=n;
pi= v;
}
ostream& operator<<(ostream &o, const C &m){
for(int j=0; j<m.dim; j++)o<< m.pi[j]<<" ";
return o;
}
void main(){
int a[]={1,2,3}, b[]={10,20};
C x(3,a),y(2, b);
x=y;
y.set(0,-1000);
cout<<x<<endl;
}
Programul afiseaza:
a. -100 20
b. -1000 20
c. 100 2 3
d. 1 20 30
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
12. Fie programul:

#include <iostream.h>
class B{
public:
void st(){cout<< "static method st() of B"<<endl;}
virtual void v(){cout<< "virtual method v() of B"<<endl;}
};
class D: public B{
public:
void st(){cout<< "static method st() of D"<<endl;}
virtual void v(){cout<< " virtual method v() of D"<<endl;}
};
int main(){
B *pb;
D d;
// pointing to a subclass
pb=&d;
((D*)pb)->v();
((D*)pb)->st();
return 0;
}
Programul afiseaza:
a. virtual method v() of D
static method st() of D
b. virtual method v() of B
static method st() of D
c. virtual method v() of D
static method st() of B
d. virtual method v() of B
static method st() of B
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
13. Fie programul:
#include <iostream.h>
class B{
public:
void st(){cout<< "static method st() of B"<<endl;}
virtual void v(){cout<< "virtual method v() of B"<<endl;}
};
class D: public B{
public:
void st(){cout<< "static method st() of D"<<endl;}
virtual void v(){cout<< " virtual method v() of D"<<endl;}
};
int main(){
B b, *pb;
D d, *pd;
pb=&d;
pb->st();
pb->v();
return 0;
}
Programul afiseaza:
a. static method s() of D
virtual method v() of D
b. static method s() of B
virtual method v() of B
c. static method s() of D
virtual method v() of B
d. static method s() of B
virtual method v() of D
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

14. Fie programul


#include <string.h>
#include <iostream.h>
class Person{
public: Person(char *p){nr=0; name=new char[strlen(p)+1]; strcpy(name,p); nr++; cout<<nr<<endl; }
~Person(){delete[] name;}private: char *name; long nr;};void f(){Person *p = new Person("Balanescu"); delete

p;}void main(){ for (int i=0; i<5; i++)f();}


Programul afiseaza:
11111
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
15. Fie programul :
// constructor de copiere in clasa de baza; dar absent in clasa derivata
// compilatorul creeaza un constructor de copiere implicit
// care apeleaza constr de copiere al clasei de baza pentru copierea atributelor din clasa de baza
// si copiaza membru cu membru atributele din D
#include <iostream.h>
class B{
public:
B(){cout<<"B()"<<endl;}
B(B &b){cout<<"B(B &b)"<<endl;}
};
class D: public B{
public:
D(){cout<<"D()"<<endl;}
};
void main(){
B b; // apel B()
B b1(b); // apel B(B & ); nu se mai utilizeaza B()!
D d; // apel B();D()
D d1(d);
B bd(d); // conversie implicita la clasa de baza;
getch();
}
Programul afiseaza :
a. B() B() B(B&b) B() D() B(B &b) B(B &b)
b. B() B() B(B&b) B() D() B(B &b) D() B(B &b)
c. B() B(B&b) B() D() B(B &b) B(B &b)
d. B() B(B&b) D() B(B &b) D() B(B &b)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

16. Fie programul:


//destructor explicit
#include <string.h>
#include <iostream.h>
class Person{
public:
Person(char *p){name=new char[strlen(p)+1]; strcpy(name,p);}
~Person(){delete[] name;}
private:
char *name;
};
void f(){Person *p =
new Person("Balanescu");
delete p;
// fara aceasta instructiune, obiectele se acumuleaza
// in heap si blocheaza executarea, spre deosebire de Java
}
void main(){
while(1)f();
}
a. La iesirea din functia f(), spatiul alocat pentru sirul de caractere “Balanescu” nu este eliberat si memoria va
fi epuizata
b. La iesirea din functia f(), spatiul alocat pentru sirul de caractere “Balanescu” este eliberat
c. Programul se termina datorita epuizarii memoriei
d. Programul se termina dupa primul apel al functiei f()
17. Fie urmatorul program:
#include <iostream.h>
class A{
public:
void s(){cout<<"void A::s()"<<endl;}
void s(int i){i++;cout<<"void A::s(int)"<<endl; }
virtual void v(){cout<<"virtual void A::v()"<<endl;}
virtual void v(int i){i++;cout<<"virtual void A::v(int)"<<endl;}
};
Care afirmatie este corecta:
a. Definitia virtual void v()supraincarca definitia virtual void v(int i)
b. Definitia virtual void v(int i) este incorecta deoarece exista o definitie pentru metoda v.
c. void v() este metoda ce va fi legata static.
d. in expresia p->v(); metoda v este legata static
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
18. Fie urmatorul program:
// Constructor cu semantica prin referinta
// Operatorul = este cel implicit
t#include <iostream.h>
class C{
public:
C(int n=0, int v[]);friend ostream& operator<<(ostream &o, const C&);private: int dim; int *pi;};C::C(int n, int v[]) {
dim=n; pi= v; }ostream& operator<<(ostream &o, const C &m){ for(int j=0; j<m.dim; j++)o<< m.pi[j]<<" "; return o;}
void main(){ int a[]={1,2,3}, b[]={10,20}; C x(3,a),y(2, b); a[0]=-100; cout<<(x=y)<<endl; }
Programul afiseaza:
AFIZEAZA 10, 20, NU ESTE NICI UN X
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
19. Fie programul :
#include <iostream.h>
class Cerc{
public:
Cerc(float r):raza(r){}
float getRaza(){return raza;}
void operator++(){raza++;}
private:
float raza;
};
class Cilindru: public Cerc{
public:
Cilindru(float raza, float inaltime):Cerc(raza), inaltime(inaltime){};
void operator++(){inaltime++;}
float getInaltime(){return inaltime;}
private:
float inaltime;
};
void main(){
Cerc *pc;
Cilindru c(1,5);
pc=&c;
++ *pc;
cout<<pc->getRaza()<<" "<<c.getInaltime()<<endl;
getch();
}
Programul afiseaza :
a. 1 6
b. 2 5
c. 2 6
d. 1 5
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
20. Fie programul:
#include <string.h>
#include <iostream.h>
class Person{
public:
Person(char *p){name=new char[strlen(p)+1]; strcpy(name,p);
nr++; cout<<nr<<endl;
}
private:
char *name;
static long nr;
};
long Person::nr=0;
void f(){Person *p =
new Person("Balanescu");
delete p;
}
void main(){
while(1)f();
}
Care afirmatie este adevarata:
a. Programul afiseaza un sir finit de numere 1 2 3 4...etc. pana memoria este epuizata
b. Programul afiseaza secventa infinita 1 2 3 4...etc. deoarece memoria nu este epuizata
c. Programul nu afiseaza nimic
d. Programul afiseaza 1 apoi se opreste
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

21. Fie programul


//static atribut
#include <conio.h>
#include <string.h>
#include <iostream.h>
class Person{
public:
Person(char *p){name=new char[strlen(p)+1]; strcpy(name,p);
nr++; cout<<nr<<endl;
}
~Person(){nr--; delete[] name;}
private:
char *name;
static long nr;
};
long Person::nr=0;
void f(){Person *p =
new Person("Balanescu");
}
void main(){
for (int i=0; i<5; i++)f();
getch();
}
Programul afiseaza:
a. 1 2 3 4 5
b. 1 1 1 1 1
c. 0 0 0 0 0
d. 0 1 2 3 4
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
22. Fie următorul program C++:
#include <iostream.h>
class Punct{
public:
Punct(int=0, int=0); //constructor
protected:
int x,y;
friend ostream& operator<<(ostream&, const Punct&);
};
class Cerc: public Punct{
public:
Cerc(double r=0.0, int x=0, int y=0);// constructor
protected:
float raza;
friend ostream& operator<<(ostream&, const Cerc&);
};
//implementare Punct
Punct::Punct(int a, int b):x(a),y(b){} // constructor
ostream& operator<<(ostream& output, const Punct& p){
output<<"Punct"<<'['<<p.x<<", "<<p.y<< ']';
return output;
}
//implementare Cerc
Cerc::Cerc(double r, int a, int b):Punct(a,b), raza(r){}
ostream& operator<<(ostream& output, const Cerc& c){
output <<"Centru= "<< (Punct)(c)
<<"; Raza= "<<c.raza;
return output;
};
void main(){
Punct *punctPtr=0, p(30,50);
Cerc *cercPtr=0, c(2.7,120,89);
cout<<p<<endl;
cout<<c<<endl;
// Cerc tratat ca Punct (prin pointer la clasa de baza):
punctPtr=&c;
cout<< *punctPtr << endl;
/* Cerc tratat ca Cerc (prin pointer la clasa de baza, dar
cu conversie explicita de la Punct la clasa derivata Cerc
*/
cercPtr= (Cerc *)(punctPtr);
cout<< *cercPtr<<endl;
/*Punct tratat ca Cerc: programatorul isi asuma responsabilitatea
unor erori: anumite atribute sunt nedefinite
*/
punctPtr= &p; // punctPtr refera un Punct
//Urmeaza conversie asumata de programator,
//de la clasa de baza la clasa derivata
cercPtr=(Cerc *)(punctPtr);
// cercPtr refera p ca pe un cerc
//dar acest asa-zis cerc are raza nedefinita
cout<< *cercPtr <<endl;
}
Prin xx este desemnată o valoare nedefinită.
Care din afirmatiile următoare sunt adevărate:
a. Programul afisează:
Punct[30,50]
Centru=Punct[30,50]; Raza=2.7
Punct[120,89]
Centru=Punct[120,89]; Raza=2.7
Centru=Punct[30,50]; Raza=xx

b. Programul afisează:
Punct[30,50]
Centru=Punct[120,89]; Raza=2.7
Punct[120,89]
Centru=Punct[120,89];
Centru=Punct[30,50]; Raza=xx

c. Programul afisează:
Punct[30,50]
Centru=Punct[30,50];
Punct[120,89]
Centru=Punct[120,89]; Raza=2.7
Centru=Punct[30,50]; Raza=xx

d. Programul afisează:
Punct[30,50]
Centru=Punct[120,89]; Raza=2.7
Punct[120,89]
Centru=Punct[120,89]; Raza=2.7
Centru=Punct[30,50]; Raza=xx
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
23. Fie următorul program C++:
#include <iostream.h>
class B{
public:
B(int i):i(i){}
protected:
int i;
};
class D1: public B{
public:
D1(int i):B(i){}
void inc(){i++;}
};
class D2: public B{
public:
D2(int i):B(i){}
void afisare(){cout<<”i=”<<i<<endl;}
};
class D: public D1, public D2{
public:
D(int i):D1(i),D2(i){}
};
void main(){
D d(0);
d.inc();
d.afisare();
}
Care din afirmatiile următoare sunt adevărate:
a. Programul afisează i=-1
b. Programul afisează i=2
c. Programul afisează i=0
d. Programul afisează i=1
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
24. Fie următorul program C++:
#include <iostream.h>
class B{
public:
B(int i=-1):valB(i){}
B& operator=(const B &b){
valB=b.valB;
cout<<"B::op= ";
return *this;
}
private:
int valB;
};
class D:public B{
public:
D(int i, int j):B(i),valD(j){}
D& operator=(const D &d){
valD=d.valD;
cout<<"D::op= ";
return *this;
}
private:
int valD;
};
void main(){
B b(0), b1(1);
B *pB;
D d(0,0), d1(1,1);
b=b1;
d=d1;
b=d;
pB=&d;
*pB=d;
pB->operator=(d);
}
Care din afirmatiile următoare sunt adevărate:
a. Programul afisează :
B::op= D::op= D::op= B::op= D::op=
b. Programul afisează:
B::op= D::op= B::op= B::op= B::op=
c. Programul afisează:
B::op= D::op= B::op= D::op= D::op=
d. Programul afisează:
B::op= D::op= D::op= D::op= B::op=
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
25. Fie programul
class A{
int x;
public:
A(int x):x(x){};
};
class B{
protected:
int x;
public:
B(int x):x(x){};
};
class D:public B{
A a;
B b; // 1
public:
void m(){
x=1; // 2
b.x=1; //3
}
};
void main(){
B b(1); // 4
}

Care din următoarele instructiuni sunt eronate:


a. B b; // 1
b. x=1 // 2
c. b.x=1; // 3
d. B b(1); // 4 (din functia main)

You might also like