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

A. 2 B. 3 C. 6 D. 8

The document contains a practice exam for a C++ programming course. It includes 23 multiple choice questions testing concepts like output of code snippets, function definitions, class definitions, inheritance, constructors, destructors, and friend functions.

Uploaded by

krishankant mani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

A. 2 B. 3 C. 6 D. 8

The document contains a practice exam for a C++ programming course. It includes 23 multiple choice questions testing concepts like output of code snippets, function definitions, class definitions, inheritance, constructors, destructors, and friend functions.

Uploaded by

krishankant mani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

QUESTION OF THIS EXAM IS RESERVED TO DEVBRAT ANAND.

MAX. MARK=100 TIME=1:30 HR.


PASSING MARKS=70
EACH QUESTION FROM 1 TO 22 CONTAINS 4.5 MARKS AND QUESTION NO 23 CONTAINS 1 MARKS
__________________________________________________________________________
1) OUTPUT WILL BE?
#include <iostream>
using namespace std;
int main(void) {
int i = 1, j = 2;
if(i > j && j > i)
i++;
if(i > j || j > i)
j++;
if(i | j)
i++;
if(i & j)
j++;
cout << i * j << endl;
return 0;
}

A. 2

B. 3

C. 6

D. 8

2).
#include <iostream>
using namespace std;
struct A {
int a;
float b;
};
struct B {
int b;
float a;
};
struct C {
A a; B b;
};
int main(void) {
C c1 = {1, 2, 3, 4}, c2 = {5, 6, 7, 8};
cout << c1.b.a + c2.a.b << endl;
return 0;
}
A. 6
B. 8
C. 10
D. 12

3).
What is the output of the following program?
#include <iostream>
using namespace std;
int main(void) {
int t[4] = { 8, 4, 2, 1 };
int *p1 = t + 2, *p2 = p1 - 1;
p1++;
cout << *p1 - t[p1 - p2] << endl;
return 0;
}
A. -2
B. -1
C. 1
D. 2

4).
What is the output of the following program?
#include <iostream>
using namespace std;
int fun1(int p) {
++p;
return p++;
}
int fun2(int &p) {
++p;
return p++;
}
int main(void) {
int a = 1, b, c;
b = fun1(a);
c = fun2(b);
cout << a + b + c << endl;
return 0;
}
A. 4
B. 6
C. 8
D. 10

5).
What is the output of the following program?
#include <iostream>
using namespace std;
int *fun(void) {
return new int[2];
}
int fun(int *p) {
delete [] p;
return 0;
}
void fun(int *p, int q) {
p[q] *= 2;
}
void fun(int *p, int q, int r) {
p[q] = r;
}
int main(void) {
int *v = fun();
fun(v,0,1);
fun(v,1,2);
fun(v,0);
cout << v[1] + v[0] << endl;
fun(v);
return 0;
}
A. 1
B. 2
C. 3
D. 4

6).
What is the output of the following program?
#include <iostream>
using namespace std;
char f1(char c) {
return c == 'z' ? 'a' : c + 1;
}
char f2(char &c) {
c = f1(c);
return c;
}
int main(void) {
char x = 'x';
cout << f2(x);
cout << f2(x);
cout << f2(x) << endl;
return 0;
}
A. XYZ
B. xyz
C. YZA
D. yza

7).
What is the output of the following program?
#include <iostream>
using namespace std;
int main(void) {
int *t[2] = { new int[2], new int[2] };
for(int i = 0; i < 4; i++)
t[i % 2][i / 2] = i;
cout << t[0][1] + t[1][0] << endl;
delete [] t[0];
delete [] t[1];
return 0;
}
A. 1
B. 2
C. 3
D. 4

8).
What is the output of the following program?
#include <iostream>
#include <string>
using namespace std;
int main(void) {
string s = "Abc", t = "A";
s = s + t;
t = t + s;
int i = s.compare(t) > 0;
int j = s.length() < t.length();
cout << i + j << endl;
return 0;
}
A. 0
B. 1
C. 2
D. 3

9).
What is the output of the following program?
#include <iostream>
using namespace std;
namespace alpha { int var = 1; }
namespace beta { int var = alpha::var + 1; }
int main(void) {
beta::var += alpha::var;
{
using namespace beta;
cout << var << endl;
}
return 0;
}
A. 1
B. 2
C. 3
D. 4

10).
What is the output of the following program?
#include <iostream>
using namespace std;
class A {
int a;
public:
A(void) { a = 1; }
int b(void) { return ++a; }
};
int main(void) {
A a;
a.b();
cout << a.b() << endl;
return 0;
}
A. The program will cause a compilation error
B. 1
C. 2
D. 3

11).
What is the output of the following program?
#include <iostream>
using namespace std;
class A {
public:
A() { a.a = a.b = 1; }
struct { int a,b; } a;
int b(void);
};
int A::b(void) { int x=a.a;a.a=a.b;a.b=x; return x; };
int main(void) {
A a;
a.a.a = 0;
a.b();
cout << a.b() << a.a.b << endl;
return 0;
}
A. The program will cause a compilation error
B. 10
C. 01
D. 11

12).
#include <iostream>
using namespace std;
class A {
public:
int a;
A() { a = 0; }
A(int b) { a = b + 1; }
};
class B {
public:
A a;
B() : a(0) { }
};
int main(void) {
B *b = new B();
cout << b->a.a << endl;
return 0;
}
A. The program will cause a compilation error
B. 1
C. 3
D. 5

13).
What is the output of the following program?
#include <iostream>
using namespace std;
class A {
public:
int x;
void d() { x /= 2; }
};
class B : public A {
public:
int y;
void d() { A::d(); }
};
int main(void) {
B b;
b.x = b.y = 4;
b.d();
cout << b.y / b.x << endl;
return 0;
}
A. The program will cause a compilation error
B. 1
C. 2
D. 4

14).
Which of the following functions can be used to allocate space for array in memory?
a).calloc()
b).malloc()
c).realloc()
d).both a and b

15).
What is the role of inline function in c++, despite of presence of other function, explain properly.
(keep in mind this is not your semester exam where you can write anything and teacher give you
the marks .)

16).
What is the output of the following program?
#include <iostream>
using namespace std;
class A {
public: static int a;
A() { a++; }
};
int A::a = 1;
void f(void) {
A a;
throw string("?");
}
int main(void) {
A a;
try { f(); }
catch (string &s) {
}
cout << A::a << endl;
return 0;
}
A. The program will cause a compilation error
B. 3
C. 4
D. 5

17).
What is the output of the following program?
#include <iostream>
#include <exception>
using namespace std;
int i = 1;
void f(void) {
throw 1;
i++;
}
void g(void) {
i++;
try { f(); }
catch(int &i) {
throw ++i;
}
}
int main(void) {
try { g(); i++; }
catch(...) { i++; }
cout << i << endl;
return 0;
}
A. The program will cause a compilation error
B. 3
C. 4
D. 5

18).
What is the output of the following program?
#include <iostream>
using namespace std;
class A {
public: int v;
A():v(1) {}
A(int i):v(i) {}
void operator&&(int a) { v = -v; }
};
int main(void) {
A i = 2;
i && 2;
cout << i << endl;
return 0;
}

19).
WHAT WILL BE THE OUTPUT:
#include <iostream>
using namespace std;
class A {
public: int v;
A():v(1) {}
A(int i):v(i) {}
void operator**(int a) { v *= a; }
};
int main(void) {
A i = 2;
i ** 2;
cout << i.v << endl;
return 0;
}

20).
Does c++ supports destructor, if yes then what will be the sequence of deleting of object, suppose
you had defined four object in the following order: obj1, obj2, obj3, obj4, then arrange the order
of deletion(starting from that which will be deleted first ) of the above object when destructor is
called.

21).
What is a constructor?
a). A class automatically called whenever a new object of this class is created.
b). A class automatically called whenever a new object of this class is destroyed.
c). A function automatically called whenever a new object of this class is created.
d). A function automatically called whenever a new object of this class is destroyed.

22).
How would you read the expression x.y as?

member y of object pointed by x

member y of object x

member x of object y

All of the above

23).
What is friend function in c++, explain briefly?.

COPYRIGHT OF QUESTION IS WITH DEVBRAT ANAND

You might also like