C++ Certified Associate Programmer v6.0 (CPA)

Page:    1 / 15   
Total 226 questions

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


Answer : C

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


Answer : C

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.


Answer : A,C

Which of the following is a logical operator?

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


Answer : B,C,D

How could you pass arguments to functions?

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


Answer : A,B,C

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


Answer : A

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


Answer : B

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


Answer : B

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.


Answer : D

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


Answer : C

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


Answer : C

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


Answer : B

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


Answer : B

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


Answer : A

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


Answer : A

Page:    1 / 15   
Total 226 questions