Exam4Training

C++ Institute CPA-21-02 CPA – C++ Certified Associate Programmer Online Training

Question #1

What will the variable "age" be in class B?

class A {

int x;

protected:

int y;

public:

int age;

A () {age=5;};

};

class B: public A {

string name;

public:

B () {name="Bob";};

void Print () {

cout << name << age;

}

};

  • A . public
  • B . private
  • C . protected
  • D . None of these

Reveal Solution Hide Solution

Correct Answer: A
Question #2

What happens when you attempt to compile and run the following code?

#include <iostream>

#include <string>

using namespace std;

class complex{

double re, im;

public:

complex() : re(1),im(0.4) {}

complex operator?(complex &t);

void Print() { cout << re << " " << im; }

};

complex complex::operator? (complex &t){

complex temp;

temp.re = this?>re ? t.re;

temp.im = this?>im ? t.im;

return temp;

}

int main(){

complex c1, c2, c3;

c3 = c1 ? c2;

c3.Print();

}

  • A . It prints: 1 0.4
  • B . It prints: 2 0.8
  • C . It prints: 0 0
  • D . It prints: 1 0.8

Reveal Solution Hide Solution

Correct Answer: C
Question #3

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

class complex{

double re;

double im;

public:

complex() : re(0),im(0) {}

complex(double x) { re=x,im=x;};

complex(double x,double y) { re=x,im=y;}

void print() { cout << re << " " << im;}

};

int main(){

complex c1;

c1 = 3.0;

c1.print();

return 0;

}

  • A . It prints: 0 0
  • B . It prints: 1 1
  • C . It prints: 3 3
  • D . Compilation error

Reveal Solution Hide Solution

Correct Answer: C
Question #4

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

void fun(int);

int main()

{

int a=0;

fun(a);

return 0;

}

void fun(int n)

{

if(n < 2)

{

fun(++n);

cout << n;

}

}

  • A . It prints: 21
  • B . It prints: 012
  • C . It prints: 0
  • D . None of these

Reveal Solution Hide Solution

Correct Answer: A
Question #5

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int s(int n);

int main()

{

int a;

a = 3;

cout << s(a);

return 0;

}

int s(int n)

{

if(n == 0) return 1;

return s(n?1)*n;

}

  • A . It prints: 4
  • B . It prints: 6
  • C . It prints: 3
  • D . It prints: 0

Reveal Solution Hide Solution

Correct Answer: B
Question #6

What will be the output of the program?

#include <iostream>

using namespace std;

int fun(int);

int main()

{

cout << fun(5);

return 0;

}

int fun(int i)

{

return i*i;

}

  • A . 25
  • B . 5
  • C . 0
  • D . 1

Reveal Solution Hide Solution

Correct Answer: A
Question #7

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

#define FUN(arg) if(arg) cout<<"Test";

int main()

{

int i=1;

FUN(i<3);

return 0;

}

  • A . It prints: 0
  • B . It prints: T
  • C . It prints: T0
  • D . It prints: Test

Reveal Solution Hide Solution

Correct Answer: D
Question #8

What will the variable "y" be in class B?

class A {

int x;

protected:

int y;

public:

int age;

};

class B: private A {

string name;

public:

void Print() {

cout << name << age;

}

};

  • A . public
  • B . private
  • C . protected
  • D . None of these

Reveal Solution Hide Solution

Correct Answer: B
Question #9

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int main()

{

float x=3.5, y=1.6;

int i,j=2;

i = x + j + y;

cout << i;

return 0;

}

  • A . It prints: 7
  • B . It prints: 6
  • C . It prints: 7,1
  • D . Compilation error

Reveal Solution Hide Solution

Correct Answer: A
Question #10

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int main(){

int i = 1;

if (i==1) {

cout << i;

} else {

cout << i-1;

}

return 0;

}

  • A . It prints: 0
  • B . It prints: 1
  • C . It prints: -1
  • D . It prints: 2

Reveal Solution Hide Solution

Correct Answer: B

Question #11

What happens when you attempt to compile and run the following code?

#include <iostream>

#include <string>

using namespace std;

class complex{

double re, im;

public:

complex() : re(1),im(0.4) {}

complex operator+(complex &t);

void Print() { cout << re << " " << im; }

};

complex complex: operator+ (complex &t){

complex temp;

temp.re = this?>re + t.re;

temp.im = this?>im + t.im;

return temp;

}

int main(){

complex c1,c2,c3;

c3 = c1 + c2;

c3.Print();

}

  • A . It prints: 1 0.4
  • B . It prints: 2 0.8
  • C . It prints: 0 0
  • D . Garbage value

Reveal Solution Hide Solution

Correct Answer: B
Question #12

What happens when you attempt to compile and run the following code?

#include <cstdlib>

#include <iostream>

using namespace std;

float* sum(float a,float b);

float* sum(float a,float b)

{

float *f = new float;

*f = a+b;

return f;

}

int main()

{

float a,b,*f;

a = 1.5; b = 3.4;

f = sum(a,b);

cout<<*f;

return 0;

}

  • A . It prints: 0
  • B . It prints: 4.9
  • C . It prints: 5
  • D . It prints: 4

Reveal Solution Hide Solution

Correct Answer: B
Question #13

Which statement should be added in the following program to make work it correctly?

using namespace std;

int main (int argc, const char * argv[])

{

cout<<"Hello";

}

  • A . #include<stdio.h>
  • B . #include<stdlib.h>
  • C . #include <iostream>
  • D . #include<conio.h>

Reveal Solution Hide Solution

Correct Answer: C
Question #14

What is the output of the program?

#include <iostream>

using namespace std;

int main()

{

int tab[4]={10,20,30,40};

tab[1]=10;

int *p;

p=&tab[0];

cout<<*p;

return 0;

}

  • A . It prints: 10
  • B . It prints: 20
  • C . It prints: 11
  • D . It prints: 30

Reveal Solution Hide Solution

Correct Answer: A
Question #15

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int fun(int x) {

return 2*x;

}

int main(){

int i;

i = fun(1) & fun(0);

cout << i;

return 0;

}

  • A . It prints: 0
  • B . It prints: 1
  • C . It prints: -1
  • D . Compilation error

Reveal Solution Hide Solution

Correct Answer: A
Question #16

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

class A {

public:

virtual void Print()=0;

};

class B: public A {

public:

virtual void Print() { cout<< "B"; }

};

class C: public A {

public:

virtual void Print() { cout<< "C"; }

};

int main()

{

Bob2;

Cob3;

A*obj;

obj = &ob2;

obj?>Print();

obj = &ob3;

obj?>Print();

}

  • A . It prints: BC
  • B . It prints: CB
  • C . It prints: CC
  • D . It prints: BB

Reveal Solution Hide Solution

Correct Answer: A
Question #17

What will the variable "age" be in class B?

class A {

int x;

protected:

int y;

public:

int age;

};

class B: private A {

string name;

public:

void Print () {

cout << name << age;

}

};

  • A . public
  • B . private
  • C . protected
  • D . None of these

Reveal Solution Hide Solution

Correct Answer: B
Question #18

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int x=5;

static int y;

int i=0;

void static myFunction()

{

y=x++ + ++i;

}

int main (int argc, const char * argv[])

{

x++;

myFunction();

cout<<y<<" "<<x<< " " << i;

}

  • A . Compilation fails
  • B . It prints: 5 5 0
  • C . It prints: 7 7 1
  • D . It prints: 6 5 1

Reveal Solution Hide Solution

Correct Answer: C
Question #19

Which of the structures is incorrect?

1:

struct s1{

int x;

long int li;

};

2:

struct s2{

float f;

struct s2 *s;

};

3:

struct s3{

float f;

struct s3 s;

};

  • A . 1
  • B . 2
  • C . 3
  • D . 2, 3

Reveal Solution Hide Solution

Correct Answer: C
Question #20

What is the output of the program?

#include <iostream>

#include <string>

using namespace std;

int main()

{

string s1="Wo";

string s2;

s2 = s1;

string s3;

s3 = s2.append("rldHello");

cout << s3;

return(0);

}

  • A . It prints: WorldHello
  • B . It prints: HelloWo
  • C . It prints: World
  • D . It prints: Hello

Reveal Solution Hide Solution

Correct Answer: A

Question #21

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

class complex{

double re;

double im;

public:

complex() : re(0),im(0) {}

complex(double x) { re=x,im=x;};

complex(double x,double y) { re=x,im=y;}

void print() { cout << re << " " << im;}

};

int main(){

complex c1(1,2);

c1.print();

return 0;

}

  • A . It prints: 1 0
  • B . It prints: 1 1
  • C . It prints: 1 2
  • D . Compilation error

Reveal Solution Hide Solution

Correct Answer: C
Question #22

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int fun(int x) {

return x<<2;

}

int main(){

int i;

i = fun(1) / 2;

cout << i;

return 0;

}

  • A . It prints: 0
  • B . It prints: 1
  • C . It prints: 2
  • D . It prints: 4

Reveal Solution Hide Solution

Correct Answer: C
Question #23

What happens when you attempt to compile and run the following code?

#include <iostream>

#include <string>

using namespace std;

class A {

int x;

protected:

int y;

public:

int z;

A() { x=1; y=2; z=3; }

};

class B: public A {

string z;

public:

void set() {

y = 4;

z = "John";

}

void Print() {

cout << y << z;

}

};

int main () {

B b;

b.set();

b.Print(); return 0;

}

  • A . It prints: 4John
  • B . It prints: 2John
  • C . It prints: 23
  • D . It prints: 43

Reveal Solution Hide Solution

Correct Answer: A
Question #24

What happens when you attempt to compile and run the following code?

#include <iostream>

#include <string>

using namespace std;

const int size = 3;

class A {

public:

string name;

A() { name = "Bob";}

A(string s) { name = s;}

A(A &a) { name = a.name;}

};

class B: public A {

public:

B(){}

B(string s) : A(s) { }

void Print() {

cout << name;

};

int main () {

Bb1("Alan");

b1.Print();

return 0;

}

  • A . It prints: 111Alan
  • B . It prints: Bob
  • C . It prints: Alan
  • D . It prints: 0

Reveal Solution Hide Solution

Correct Answer: C
Question #25

What is the output of the program given below?

#include <iostream>

using namespace std;

int main (int argc, const char * argv[])

{

int i=10;

{

int i=0;

cout<<i;

}

{

i=5;

cout << i;

}

cout<<i;

return 0;

}

  • A . 1010
  • B . 101010
  • C . 055
  • D . None of these

Reveal Solution Hide Solution

Correct Answer: C
Question #26

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int main (int argc, const char * argv[])

{

int x,y;

union t

{

char tab[2];

int i;

};

union t u;

u.tab[0] = 1;

u.tab[1] = 2;

u.i = 0;

x = u.tab[0];

y = u.tab[1];

cout << x << "," << y << "," << u.i;

return 0;

}

  • A . compilation fails
  • B . It prints: 0,0,0
  • C . It prints: 1,2,0
  • D . None of these

Reveal Solution Hide Solution

Correct Answer: B
Question #27

What happens when you attempt to compile and run the following code?

#include <iostream>

#include <string>

using namespace std;

class A {

protected:

int y;

public:

int x,z;

A() : x(1), y(2), z(0) { z = x + y; }

A(int a, int b) : x(a), y(b) { z = x + y;}

void Print() { cout << z; }

};

class B: public A {

public:

int y;

B() : A() {}

B(int a, int b) : A(a,b) {}

void Print() { cout << z; }

};

int main () {

A b;

b.Print();

return 0;

}

  • A . It prints: 3
  • B . It prints: 0
  • C . It prints: 1
  • D . It prints: 2

Reveal Solution Hide Solution

Correct Answer: A
Question #28

Which code, inserted at line 10, generates the output "Hello World"?

#include <iostream>

#include <string>

using namespace std;

string fun(string, string);

int main()

{

string s="Hello";

string *ps;

ps = &s;

//insert code here

return 0;

}

string fun(string s1, string s2)

{

return s1+s2;

}

  • A . cout << fun(" World");
  • B . cout << fun(*ps);
  • C . cout << fun("Hello");
  • D . cout << fun("Hello", " World");

Reveal Solution Hide Solution

Correct Answer: D
Question #29

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int x=5;

static int y=0;

void myFunction(int a)

{

y=++a;

}

int main (int argc, const char * argv[])

{

int i=0;

myFunction(i);

cout<<y<<" "<<x;

}

  • A . It prints: 0 5
  • B . It prints: 5 1
  • C . It prints: 1 5
  • D . It prints: 5 0

Reveal Solution Hide Solution

Correct Answer: C
Question #30

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

class A {

public:

void Print(){ cout<<"A"; }

};

class B:public A {

public:

virtual void Print(){ cout<< "B"; }

};

class C:public B {

public:

void Print(){ cout<< "C"; }

};

int main()

{

A ob1;

B ob2;

C ob3;

A *obj;

obj = &ob1;

obj?>Print();

obj = &ob2;

obj?>Print();

obj = &ob3;

obj?>Print();

}

  • A . It prints: BBB
  • B . It prints: AAA
  • C . It prints: ABC
  • D . It prints: ABB

Reveal Solution Hide Solution

Correct Answer: B

Question #31

What happens when you attempt to compile and run the following code?

#include <iostream>

#include <string>

using namespace std;

class A {

public:

int x;

};

class B: public A {

public:

B() { x=1;}

B(int x) {this?>x = x;}

};

int main () {

B c1;

B c2(10);

cout << c1.x;

cout << c2.x;

return 0;

}

  • A . It prints: 010
  • B . It prints: 110
  • C . It prints: 00
  • D . It prints: 1

Reveal Solution Hide Solution

Correct Answer: B
Question #32

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

void fun(char*);

int main()

{

char t[4]={‘0’, ‘1’, ‘2’, ‘3’};

fun(&t[2]);

return 0;

}

void fun(char *a)

{

cout << *a;

}

  • A . It prints: 2
  • B . It prints: 21
  • C . It prints: 00
  • D . It prints: 02

Reveal Solution Hide Solution

Correct Answer: A
Question #33

What happens when you attempt to compile and run the following code?

#include <iostream>

#include <string>

using namespace std;

class A {

public:

A() { cout << "A no parameters";}

A(string s) { cout << "A string parameter";}

A(A &a) { cout << "A object A parameter";}

};

class B: public A {

public:

B() { cout << "B no parameters";}

B(string s) { cout << "B string parameter";}

};

int main () {

A a2("Test");

B b1("Alan");

B b2(b1);

return 0;

}

  • A . It prints: A no parametersA no parametersB string parameter
  • B . It prints: A string parameterA no parametersB string parameterA object A parameter
  • C . It prints: A no parametersB string parameter
  • D . It prints: A no parametersA no parameters

Reveal Solution Hide Solution

Correct Answer: B
Question #34

What happens when you attempt to compile and run the following code?

#include <iostream>

#include <string>

using namespace std;

class A {

public:

string s;

A(string s) { this?>s = s; }

};

class B {

public:

string s;

B (A a) { this?>s = a.s; }

void print() { cout<<s; }

};

int main()

{

A a("Hello world");

B b=a;

b.print();

}

  • A . It prints: Hello world
  • B . It prints: Hello
  • C . Compilation error
  • D . None of these

Reveal Solution Hide Solution

Correct Answer: A
Question #35

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int op(int x, int y);

int main()

{

float *pf;

float f=0.9;

pf=&f;

cout << op(1, *pf);

return 0;

}

int op(int x, int y)

{

return x*y;

}

  • A . It prints: 0
  • B . It prints: 0.5
  • C . It prints: 1
  • D . It prints: ?1

Reveal Solution Hide Solution

Correct Answer: A
Question #36

What happens when you attempt to compile and run the following code?

#include <iostream>

#include <string>

using namespace std;

class First

{

string *s;

public:

First() { s = new string("Text");}

~First() { delete s;}

void Print(){ cout<<*s;}

};

int main()

{

First FirstObject;

FirstObject.Print();

FirstObject.~First();

}

  • A . It prints: Text
  • B . Compilation error
  • C . Runtime error.
  • D . None of these

Reveal Solution Hide Solution

Correct Answer: C
Question #37

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

class A

{

public:

virtual void Print(){ cout<<"A";}

};

class B:public A

{

public:

void Print(){ cout<< "B";}

};

int main()

{

A *obj;

A ob1; obj = &ob1; obj?>Print();

B ob2;

obj = &ob2;

obj?>Print();

}

  • A . It prints: AB
  • B . It prints: AA
  • C . It prints: BA
  • D . It prints: BB

Reveal Solution Hide Solution

Correct Answer: A
Question #38

What happens when you attempt to compile and run the following code?

#include <iostream>

#include <sstream>

#include <string>

using namespace std;

int main(void)

{

string s;

s = "Test";

s.resize (s.size() ? 1);

cout<<s<<" "<<s.size();

return 0;

}

  • A . It prints: Test 4
  • B . It prints: Test 3
  • C . Compilation error
  • D . It prints: Tes 3

Reveal Solution Hide Solution

Correct Answer: D
Question #39

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

class A {

public:

int x;

A() { x=0;}

};

class B: public A {

public:

B() { x=1;}

};

class C: private B {

public:

C() { x=2;}

};

int main () {

C c1;

cout << c1.x;

return 0;

}

  • A . It prints: 210
  • B . It prints: 110
  • C . It prints: 010
  • D . Compilation error

Reveal Solution Hide Solution

Correct Answer: D
Question #40

What happens when you attempt to compile and run the following code?

#include <iostream>

#include <string>

using namespace std;

class A {

public:

A() { cout << "A no parameters";}

A(string s) { cout << "A string parameter";}

A(A &a) { cout << "A object A parameter";}

};

class B: public A {

public:

B() { cout << "B no parameters";}

B(string s) { cout << "B string parameter";}

B(int s) { cout << "B int parameter";}

};

int main () {

A a2("Test");

B b1(10);

B b2(b1);

return 0;

}

  • A . It prints: A no parametersA no parametersB string parameter
  • B . It prints: A string parameterA no parametersB int parameterA object A parameter
  • C . It prints: A no parametersB string parameter
  • D . It prints: A no parametersA no parameters

Reveal Solution Hide Solution

Correct Answer: B

Question #41

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

#include <iostream>

using namespace std;

class First

{

public:

void Print(){ cout<<"from First";}

};

int main()

{

First t[2];

for (int i=0; i<2; i++)

t[i].Print();

}

  • A . It prints: from First
  • B . It prints: from Firstfrom First
  • C . Compilation error
  • D . Runtime error.

Reveal Solution Hide Solution

Correct Answer: B
Question #42

What is the output of the program given below?

#include <iostream>

using namespace std;

int main (int argc, const char * argv[])

{

int i=10;

{

int i=0;

cout<<i;

}

cout<<i;

return 0;

}

  • A . 1010
  • B . 100
  • C . 010
  • D . None of these

Reveal Solution Hide Solution

Correct Answer: C
Question #43

What happens when you attempt to compile and run the following code?

#include <iostream>

#include <string>

using namespace std;

class A {

public:

int x;

A() { x=0;}

A(int x) { this?>x=x;}

};

class B: private A {

public:

using A:x;

B() { x=1;}

B(int x) {this?>x = x;}

};

int main () {

B c1;

B c2(?5);

cout << c1.x;

cout << c2.x;

return 0;

}

  • A . It prints: 5
  • B . It prints: 1?5
  • C . It prints: 05
  • D . It prints: 0

Reveal Solution Hide Solution

Correct Answer: B
Question #44

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int main (int argc, const char * argv[])

{

int a = 30, b = 1, c = 5, i=10;

i = b < a < c;

cout << i;

return 0;

}

  • A . compilation fails
  • B . It prints: 10
  • C . It prints: 0
  • D . It prints: 1

Reveal Solution Hide Solution

Correct Answer: D
Question #45

What happens when you attempt to compile and run the following code?

#include <iostream>

#include <string>

using namespace std;

class B;

class A {

int age;

public:

A () { age=5; };

friend class B;

};

class B {

string name;

public:

B () { name="Bob"; };

void Print(A ob) {

cout << name << ob.age;

}

};

int main () {

A a;

B b;

b.Print(a);

return 0;

}

  • A . It prints: Bob5
  • B . It prints: Bob
  • C . It prints: 5
  • D . None of these

Reveal Solution Hide Solution

Correct Answer: A
Question #46

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int main(){

int i = 1;

if (–i==1) {

cout << i;

} else {

cout << i-1;

}

return 0;

}

  • A . It prints: 0
  • B . It prints: 1
  • C . It prints: -1
  • D . It prints: 2

Reveal Solution Hide Solution

Correct Answer: C
Question #47

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

void fun(int &i);

int main()

{

int i=2;

fun(i);

cout<<i;

return 0;

}

void fun(int &i)

{

i+=2;

}

  • A . It prints: 2
  • B . It prints: 0
  • C . It prints: 4
  • D . It prints: 16

Reveal Solution Hide Solution

Correct Answer: C
Question #48

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int fun(int x);

int main() {

cout << fun(0);

return 0;

}

int fun(int x) {

if(x > 0)

return fun(x-1);

else

return 100;

}

  • A . It prints: 0
  • B . It prints: 10
  • C . It prints: 100
  • D . It prints: -1

Reveal Solution Hide Solution

Correct Answer: C
Question #49

What is the output of the program if character 2 is supplied as input?

#include <iostream>

using namespace std;

int main () {

int c;

cin >> c;

try

{

switch (c)

{

case 1:

throw 20;

case 2:

throw 5.2f;

}

}

catch (int e)

{ cout << "int exception. Exception Nr. " << e; }

catch (float e)

{ cout << "float exception. Exception Nr. " << e; } catch (…)

{ cout << "An exception occurred."; }

return 0;

}

  • A . It prints: float exception. Exception Nr.
  • B . It prints: int exception. Exception Nr. 20
  • C . It prints: An exception occurred
  • D . It prints: float exception. Exception Nr. 5.2

Reveal Solution Hide Solution

Correct Answer: D
Question #50

What is the output of the program if character 4 is supplied as input?

#include <iostream>

using namespace std;

int main () {

int c;

cin >> c;

try

{

switch (c)

{

case 1:

throw 20;

case 2:

throw 5.2f;

case 3:

throw ‘a’;

default:

cout<<"No exception";

}

}

catch (int e)

{ cout << "int exception. Exception Nr. " << e; }

catch (float e)

{ cout << "float exception. Exception Nr. " << e; }

catch (…)

{ cout << "An exception occurred."; }

return 0;

}

  • A . It prints: float exception. Exception Nr.
  • B . It prints: int exception. Exception Nr.
  • C . It prints: An exception occurred
  • D . It prints: No exception

Reveal Solution Hide Solution

Correct Answer: D

Question #51

Which code, inserted at line 14, generates the output "3.14 10"?

#include <iostream>

using namespace std;

namespace myNamespace1

{

int x = 5;

int y = 10;

}

namespace myNamespace2

{

float x = 3.14;

float y = 1.5;

}

int main () {

//insert code here

cout << x << " " << y;

return 0;

}

  • A . using myNamespace2::y; using myNamespace1::x;
  • B . using namespace myNamespace1;
  • C . using namespace myNamespace1; using namespace myNamespace2;
  • D . using myNamespace1::y; using myNamespace2::x;

Reveal Solution Hide Solution

Correct Answer: D
Question #52

What is the output of the program?

#include <iostream>

#include <string>

using namespace std;

int main()

{

string s1[]= {"Hello" , "World" };

for (int i=0; i<2; i++) {

cout << s1[i];

}

return (0);

}

  • A . It prints: HelloWorld
  • B . It prints: Hello
  • C . It prints: WorldHello
  • D . It prints: World

Reveal Solution Hide Solution

Correct Answer: A
Question #53

Which code, inserted at line 8, generates the output "0102020"?

#include <iostream>

using namespace std;

class Base {

static int age;

public:

Base () {};

~Base () {};

//insert code here

void Print() { cout << age;}

};

int Base::age=0;

int main () {

Base a,*b;

b = new Base();

a.Print();

a.setAge(10);

a.Print();

b?>setAge();

a.Print();

b?>Print();

return 0;

}

  • A . void setAge(int a) {age = a;}
  • B . void setAge() {age = 20;}
  • C . void setAge() {age = 10;}
  • D . void setAge(int a=20) {age = a;}

Reveal Solution Hide Solution

Correct Answer: D
Question #54

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

class First

{

public:

void Print(){ cout<<"from First";}

};

class Second

{

public:

void Print(){ cout<< "from Second";}

};

int main()

{

First FirstObject;

FirstObject.Print();

Second SecondObject;

SecondObject.Print();

}

  • A . It prints: from First
  • B . It prints: from Firstfrom First
  • C . It prints: from Firstfrom Second
  • D . It prints: from Secondfrom Second

Reveal Solution Hide Solution

Correct Answer: C
Question #55

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {

char *s = "ABCDEF";

cout << s+2;

return 0;

}

  • A . It prints: CDEF
  • B . It prints: ABCDEF
  • C . It prints: BCDEF
  • D . None of these

Reveal Solution Hide Solution

Correct Answer: A
Question #56

Which of the following can be checked in a switch? Case statement?

  • A . char
  • B . int
  • C . enum
  • D . double

Reveal Solution Hide Solution

Correct Answer: A, B, C
Question #57

What happens when you attempt to compile and run the following code?

#include <iostream>

#include <string>

using namespace std;

class A {

int x;

protected:

int y;

public:

int z;

};

class B: public A {

string name;

public:

void set() {

y = 2;

z = 3;

}

void Print() { cout << y << z; }

};

int main () {

B b;

b.set();

b.Print();

return 0;

}

  • A . It prints: 123
  • B . It prints: 000
  • C . It prints: 23
  • D . It prints: 12

Reveal Solution Hide Solution

Correct Answer: C
Question #58

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

class A

{

public:

void Print(){ cout<<"A";}

};

class B:public A

{

public:

void Print(){ cout<< "B";}

};

int main()

{

A *obj;

A ob1;

obj = &ob1;

obj?>Print();

B ob2;

obj = &ob2;

obj?>Print();

}

  • A . It prints: AB
  • B . It prints: AA
  • C . It prints: BA
  • D . It prints: BB

Reveal Solution Hide Solution

Correct Answer: B
Question #59

What happens when you attempt to compile and run the following code?

#include <iostream>

#include <string>

using namespace std;

class A {

protected:

int y;

public:

int x;

int z;

A() { x=2; y=2; z=3; }

A(int a, int b) : x(a), y(b) { z = x ? y;}

void Print() {

cout << z;

}

};

int main () {

A a(2,5);

a.Print();

return 0;

}

  • A . It prints: ?3
  • B . It prints: 2
  • C . It prints: 6
  • D . It prints: 5

Reveal Solution Hide Solution

Correct Answer: A
Question #60

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

void fun(int*);

int main()

{

int i=2;

fun(&i);

cout<<i;

return 0;

}

void fun(int *i)

{

*i = *i**i;

}

  • A . It prints: 1
  • B . It prints: 4
  • C . It prints: 10
  • D . It prints: 0

Reveal Solution Hide Solution

Correct Answer: B

Question #61

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int main()

{

int x=2, *y;

y = &x;

cout << *y + x;

return 0;

}

  • A . It prints: 1
  • B . It prints: 2
  • C . It prints: 4
  • D . It prints: 0

Reveal Solution Hide Solution

Correct Answer: C
Question #62

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int main(){

int *i;

i = new int;

*i = 1.0 / 2 * 2 / 1 * 2 / 4 * 4;

cout << *i;

return 0;

}

  • A . It prints: 0
  • B . It prints: 1
  • C . It prints: 2
  • D . It prints: 0.5

Reveal Solution Hide Solution

Correct Answer: C
Question #63

Which of the following statements are correct about an array?

int tab[10];

  • A . The array can store 10 elements.
  • B . The expression tab[1] designates the very first element in the array.
  • C . The expression tab[9] designates the last element in the array.
  • D . It is necessary to initialize the array at the time of declaration.

Reveal Solution Hide Solution

Correct Answer: A, C
Question #64

Which of the following is a logical operator?

  • A . &
  • B . &&
  • C . ||
  • D . !

Reveal Solution Hide Solution

Correct Answer: B, C, D
Question #65

How could you pass arguments to functions?

  • A . by value
  • B . by reference
  • C . by pointer
  • D . by void

Reveal Solution Hide Solution

Correct Answer: A, B, C
Question #66

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int main(){

int i, j;

for(i = 0, j = 1; j < 2, i < 4; i++, j++);

cout << i << " " << j;

return 0;

}

  • A . It prints: 4 5
  • B . It prints: 2 3
  • C . It prints: 3 2
  • D . It prints: 4 3

Reveal Solution Hide Solution

Correct Answer: A
Question #67

What happens when you attempt to compile and run the following code?

#include <iostream>

#include <cstdarg>

using namespace std;

int mult(int f, int s, int t);

int main()

{

cout << mult(1,2,3);

return 0;

}

int mult(int f, int s, int t)

{

int mult_res;

mult_res = f*s*t;

return mult_res;

}

  • A . It prints: 0
  • B . It prints: 6
  • C . It prints: 2
  • D . It prints: 3

Reveal Solution Hide Solution

Correct Answer: B
Question #68

Which code, inserted at line 5, generates the output "ABC"?

#include <iostream>

using namespace std;

class A {

public:

//insert code here

};

class B:public A {

public:

void Print(){ cout<< "B"; }

};

class C:public B {

public:

void Print(){ cout<< "C"; }

};

int main()

{

A ob1;

B ob2;

C ob3;

A *obj;

obj = &ob1;

obj?>Print();

obj = &ob2;

obj?>Print();

obj = &ob3;

obj?>Print();

}

  • A . void Print(){ cout<<"A";}
  • B . virtual void Print(){ cout<<"A";}
  • C . virtual void Print(string s){ cout<<s;}
  • D . None of these

Reveal Solution Hide Solution

Correct Answer: B
Question #69

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

class BaseClass

{

public:

int *ptr;

BaseClass(int i) { ptr = new int(i); }

~BaseClass() { delete ptr; delete ptr;}

void Print() { cout << *ptr; }

};

void fun(BaseClass x);

int main()

{

BaseClass o(10);

fun(o);

o.Print();

}

void fun(BaseClass x) {

cout << "Hello:";

}

  • A . It prints: Hello:1
  • B . It prints: Hello:
  • C . It prints: 10
  • D . Runtime error.

Reveal Solution Hide Solution

Correct Answer: D
Question #70

What will happen when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int getValue();

int main()

{

const int x = getValue();

cout<<x;

return 0;

}

int getValue()

{

return 5;

}

  • A . It will print 0
  • B . The code will not compile.
  • C . It will print 5
  • D . It will print garbage value

Reveal Solution Hide Solution

Correct Answer: C

Question #71

What happens when you attempt to compile and run the following code?

#include <iostream>

#include <string>

using namespace std;

class A {

public:

A() { cout << "A0 ";}

A(string s) { cout << "A1";}

};

class B: public A {

public:

B() { cout << "B0 ";}

B(string s) { cout << "B1 ";}

};

class C: private B {

public:

C() { cout << "C0 ";}

C(string s) { cout << "C1 ";}

};

int main () {

B b1;

C c1;

return 0;

}

  • A . It prints: A0 B0 A0 B1 A0 C0 A0 C1
  • B . It prints: B0 B1 C0 C1
  • C . It prints: A0 B0 A0 B0 C0
  • D . It prints: B0 B1

Reveal Solution Hide Solution

Correct Answer: C
Question #72

What is the output of the program?

#include <iostream>

#include <string>

using namespace std;

struct Person {

int age;

};

class First

{

Person *person;

public:

First() {person = new Person;

person?>age = 20;

}

void Print(){

cout << person?>age;

}

};

int main()

{

First t[2];

for (int i=0; i<2; i++)

t[i].Print();

}

  • A . It prints: 10
  • B . It prints: 2020
  • C . It prints: 22
  • D . It prints: 00

Reveal Solution Hide Solution

Correct Answer: B
Question #73

What happens when you attempt to compile and run the following code?

#include <cstdlib>

#include <iostream>

using namespace std;

inline float sum(float a,float b)

{

return a+b;

}

int main()

{

float a,b;

a = 1.5; b = 3.4;

cout<<sum(a,b);

return 0;

}

  • A . It prints: 0
  • B . It prints: 4.9
  • C . It prints: 5
  • D . It prints: 4

Reveal Solution Hide Solution

Correct Answer: B
Question #74

What is the output of the program?

#include <iostream>

#include <string>

using namespace std;

int main()

{

string s1="Hello";

string s2="World";

s1+=s2;

cout << s1;

return (0);

}

  • A . It prints: HelloWorld
  • B . It prints: Hello
  • C . It prints: World
  • D . It prints: HelWorld

Reveal Solution Hide Solution

Correct Answer: A
Question #75

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

#define DEF_A 0

#define DEF_B DEF_A+1

#define DEF_C DEF_B+1

int main(int argc, char *argv[]) {

cout << DEF_C;

return 0;

}

  • A . It prints: 2
  • B . It prints: 10
  • C . It prints: 0
  • D . It prints: 1

Reveal Solution Hide Solution

Correct Answer: A
Question #76

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

void set(struct person*);

struct person

{

int age;

};

int main()

{

struct person e = {18};

set(&e);

cout<< e.age;

return 0;

}

void set(struct person *p)

{

p?>age = p?>age + 1;

}

  • A . It prints: 18
  • B . It prints: 19
  • C . It prints: 20
  • D . It prints: 0

Reveal Solution Hide Solution

Correct Answer: B
Question #77

Point out an error in the program.

#include <iostream>

using namespace std;

int main()

{

const int x=1;

int const *y=&x;

cout<<*y;

return 0;

}

  • A . No error
  • B . Error: unknown pointer conversion
  • C . cannot convert from ‘const int *’ to ‘int *const’
  • D . Compilation error

Reveal Solution Hide Solution

Correct Answer: A
Question #78

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int main()

{

int i=2;

switch(i)

{

case 1:

cout<<"Hello";

case 2:

cout<<"world";

case 3:

cout<<"End";

}

return 0;

}

  • A . It prints: Hello
  • B . It prints: world
  • C . It prints: worldEnd
  • D . It prints: End

Reveal Solution Hide Solution

Correct Answer: C
Question #79

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

class complex{

double re;

double im;

public:

complex() : re(0),im(0) {}

complex(double x) { re=x,im=x;};

complex(double x,double y) { re=x,im=y;}

void print() { cout << re << " " << im;}

};

int main(){

complex c1;

double i=2;

c1 = i;

c1.print();

return 0;

}

  • A . It prints: 0 0
  • B . It prints: 1 1
  • C . It prints: 2 0
  • D . It prints: 2 2

Reveal Solution Hide Solution

Correct Answer: D
Question #80

What happens when you attempt to compile and run the following code?

#include <iostream>

#include <string>

using namespace std;

int f(int i, int b);

int main()

{

int i=0;

i++;

for (i=0; i<=2; i++)

{

cout<<f(0,i);

}

return 0;

}

int f(int a, int b)

{

return a+b;

}

  • A . It prints: 202020
  • B . It prints: 012
  • C . It prints: 0
  • D . It prints: 2

Reveal Solution Hide Solution

Correct Answer: B

Question #81

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

class Test {

float i,j;

};

class Add {

public:

int x,y;

Add (int a=3, int b=3) { x=a; y=b; }

int result() { return x+y;}

};

int main () {

Test test;

Add * padd;

padd = &test;

cout << padd?>result();

return 0;

}

  • A . It prints: 6
  • B . It prints: 9
  • C . Compilation error
  • D . It prints: 33

Reveal Solution Hide Solution

Correct Answer: C
Question #82

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

class BaseC

{

int *ptr;

public:

BaseC() { ptr = new int(10);}

BaseC(int i) { ptr = new int(i); }

~BaseC() { delete ptr; }

void Print() { cout << *ptr; }

};

int main()

{

BaseC *o = new BaseC(5);

o?>Print();

}

  • A . It prints: 5
  • B . It prints: 10
  • C . It prints: 1
  • D . It prints: 0

Reveal Solution Hide Solution

Correct Answer: A
Question #83

What will happen when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int main()

{

const char *s;

char str[] = "Hello ";

s = str;

while(*s) {

cout << *++s;

*s++;

}

return 0;

}

  • A . It will print:"el "
  • B . The code will not compile.
  • C . It will print:"Hello "
  • D . It will print garbage value

Reveal Solution Hide Solution

Correct Answer: A
Question #84

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

class A {

public:

void print()  {

cout << "A ";

}

};

class B {

public :

void print() {

cout << "B ";

}

};

int main() {

B sc[2];

A *bc = (A*)sc;

for (int i=0; i<2;i++)

(bc++)->print();

return 0;

}

  • A . It prints: A A
  • B . It prints: B B
  • C . It prints: A B
  • D . It prints: B A

Reveal Solution Hide Solution

Correct Answer: A
Question #85

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int main ()

{

int x,y=10;

float f;

f = 5.20;

x=(int) f;

cout << x <<", ";

f=float (y);

cout << f;

return 0;

}

  • A . It prints: 5, 10
  • B . It prints: 5.2, 10
  • C . It prints: 5.20, 10.0
  • D . It prints: 5.2, 10.00

Reveal Solution Hide Solution

Correct Answer: A
Question #86

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int op(int x, int y)

{

return x?y;

}

int op(int x, float y)

{

return x+y;

}

int main()

{

int i=1, j=2, k, l;

float f=0.23;

k = op(i, j);

l = op(j, f);

cout<< k << "," << l;

return 0;

}

  • A . It prints: ?1,?1
  • B . It prints: ?1,3
  • C . It prints: ?1,2
  • D . Compilation fails

Reveal Solution Hide Solution

Correct Answer: C
Question #87

Which of the following statements are correct?

  • A . A function can be defined inside another function
  • B . A function may have any number of return statements each returning different values.
  • C . A function can return floating point value
  • D . In a function two return statements should never occur.

Reveal Solution Hide Solution

Correct Answer: B, C
Question #88

Which code, inserted at line 15, generates the output "5 Bob"?

#include <iostream>

#include <string>

using namespace std;

class B;

class A {

int age;

public:

A () { age=5; };

friend void Print(A &ob, B &so);

};

class B {

string name;

public:

B () { name="Bob"; };

//insert code here

};

void Print(A &ob, B &so) {

cout<<ob.age << " " << so.name;

}

int main () {

A a;

B b;

Print(a,b);

return 0;

}

  • A . friend void Print(A ob, B so);
  • B . friend void Print(A &ob, B &so);
  • C . friend void Print(A *ob, B *so);
  • D . None of these

Reveal Solution Hide Solution

Correct Answer: B
Question #89

What is the output of the program if characters ‘t’, ‘e’, ‘s’ and ‘t’ enter are supplied as input?

#include <iostream>

#include <string>

using namespace std;

int main()

{

string s;

getline( cin, s );

cout << s << " " << s.length();

return (0);

}

  • A . It prints: test 4
  • B . It prints: test
  • C . It prints: test 5
  • D . It prints: 4

Reveal Solution Hide Solution

Correct Answer: A
Question #90

What happens if character 3 is entered as input?

#include <iostream>

using namespace std;

class A {

public:

int i;

};

int main () {

int c;

A obj;

obj.i = 5;

cin >> c;

try

{

switch (c)

{

case A. throw 20;

case B. throw 5.2f;

case C. throw obj;

default: cout<<"No exception";

}

}

catch (int e)

{ cout << "int exception. Exception Nr. " << e; }

catch (A e)

{ cout << "object exception. Exception Nr. " << e.i; }

catch (…)

{ cout << "An exception occurred."; }

return 0;

}

  • A . It prints: object exception. Exception Nr. 5
  • B . It prints: int exception. Exception Nr.
  • C . It prints: An exception occurred
  • D . It prints: No exception

Reveal Solution Hide Solution

Correct Answer: A

Question #91

Point out an error in the program.

#include <iostream>

using namespace std;

int main()

{

char s1[] = "Hello";

char s2[] = "world";

char *const ptr = s1;

*ptr = ‘a’;

ptr = s2;

return 0;

}

  • A . No error
  • B . Cannot modify a const object
  • C . Compilation error at line 9
  • D . None of these

Reveal Solution Hide Solution

Correct Answer: B, C
Question #92

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int main()

{

int x=20;

int *ptr;

ptr = &x;

cout<<*ptr;

return 0;

}

  • A . It prints: 20
  • B . It prints: 0
  • C . It prints address of ptr
  • D . It prints: 2

Reveal Solution Hide Solution

Correct Answer: A
Question #93

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int main()

{

int x=0;

int *ptr;

ptr = &x;

cout<<x<<" "<<*ptr;

return 0;

}

  • A . It prints: 0 0
  • B . It prints address of ptr
  • C . It prints: 1
  • D . It prints: 2

Reveal Solution Hide Solution

Correct Answer: A
Question #94

Given:

#include <iostream>

#include <exception>

using namespace std;

int main () {

try

{

int * myarray= new int[1000];

}

catch (bad_alloc&)

{

cout << "Error allocating memory";

}

catch (exception& e)

{

cout << "Standard exception";

}

catch (…)

{

cout << "Unknown exception";

}

return 0;

}

What will happen if we use the operator “new” and the memory cannot be allocated?

  • A . It prints: Error allocating memory
  • B . It prints: Standard exception
  • C . It prints: Unknown exception
  • D . Compilation error

Reveal Solution Hide Solution

Correct Answer: A
Question #95

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

struct {

int x;

char c;

union {

float f;

int i;

};

} s;

int main (int argc, const char * argv[])

{

s.x=10;

s.i=0;

cout << s.i << " " << s.x;

}

  • A . It prints: 0 10
  • B . It prints: 11
  • C . Compilation error
  • D . None of these

Reveal Solution Hide Solution

Correct Answer: A
Question #96

What is the output of the program?

#include <iostream>

#include <string>

using namespace std;

int main () {

string s1 = "Hello", s2 = "World";

s2 = s1 + s2;

cout << s2;

return 0;

}

  • A . It prints: Hello
  • B . It prints: HelloWorld
  • C . It prints: WorldHello
  • D . It prints: WorldHelloWorld

Reveal Solution Hide Solution

Correct Answer: B
Question #97

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int main()

{

int *t;

t = new int[2];

for (int i=0; i<2; i++) {

t[i]=0;

}

cout << t[1];

}

  • A . It prints: 0
  • B . It prints: 1
  • C . It prints: 2
  • D . It prints: 3

Reveal Solution Hide Solution

Correct Answer: A
Question #98

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

int main()

{

int x=2, *y, z=3;

y = &z;

cout<<x**y*x***y;

return 0;

}

  • A . It prints: 36
  • B . It prints: 14
  • C . It prints: 16
  • D . Compilation error

Reveal Solution Hide Solution

Correct Answer: D
Question #99

What happens when you attempt to compile and run the following code?

#include <iostream>

using namespace std;

#define DEF_A 0

int main(int argc, char *argv[]) {

cout << DEF_A;

return 0;

}

  • A . It prints: 1
  • B . It prints: 0
  • C . It prints: ?1
  • D . Compilation error

Reveal Solution Hide Solution

Correct Answer: B
Question #100

What happens when you attempt to compile and run the following code?

#include <iostream>

#include <exception>

using namespace std;

class myClass : public exception

{

virtual const char* what() const throw()

{

return "My exception.";

}

} obj;

int main () {

try

{

throw obj;

}

catch (exception& e)

{

cout << e.what() << endl;

}

return 0;

}

  • A . It prints: My exception.
  • B . It prints: 0
  • C . It prints: 1
  • D . Compilation error

Reveal Solution Hide Solution

Correct Answer: A
Exit mobile version