Question 1 ( Topic 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
Question 2 ( Topic 1 )
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
Question 3 ( Topic 1 )
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
Question 4 ( Topic 1 )
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
Question 5 ( Topic 1 )
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
Question 6 ( Topic 1 )
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
Answer : A
Question 7 ( Topic 1 )
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
Answer : D
Question 8 ( Topic 1 )
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
Answer : B
Question 9 ( Topic 1 )
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
Answer : A
Question 10 ( Topic 1 )
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
Answer : B
Question 11 ( Topic 1 )
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
Answer : B
Question 12 ( Topic 1 )
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
Answer : B
Question 13 ( Topic 1 )
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>
Answer : C
Question 14 ( Topic 1 )
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
Answer : A
Question 15 ( Topic 1 )
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
Answer : A
Question 16 ( Topic 1 )
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()
{
B ob2;
C ob3;
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
Answer : A
Question 17 ( Topic 1 )
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
Answer : B
Question 18 ( Topic 1 )
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
Answer : C
Question 19 ( Topic 1 )
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
Answer : C
Question 20 ( Topic 1 )
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
Answer : A
Question 21 ( Topic 1 )
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
Answer : C
Question 22 ( Topic 1 )
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
Answer : C
Question 23 ( Topic 1 )
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
Answer : A
Question 24 ( Topic 1 )
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 () {
B b1("Alan");
b1.Print();
return 0;
}
A. It prints: 111Alan
B. It prints: Bob
C. It prints: Alan
D. It prints: 0
Answer : C
Question 25 ( Topic 1 )
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
Answer : C
Question 26 ( Topic 1 )
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
Answer : B
Question 27 ( Topic 1 )
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
Answer : A
Question 28 ( Topic 1 )
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");
Answer : D
Question 29 ( Topic 1 )
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
Answer : C
Question 30 ( Topic 1 )
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
Answer : B
Question 31 ( Topic 1 )
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
Answer : B
Question 32 ( Topic 1 )
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
Answer : A
Question 33 ( Topic 1 )
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
Answer : B
Question 34 ( Topic 1 )
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
Answer : A
Question 35 ( Topic 1 )
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
Answer : A
Question 36 ( Topic 1 )
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
Answer : C
Question 37 ( Topic 1 )
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
Answer : A
Question 38 ( Topic 1 )
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
Answer : D
Question 39 ( Topic 1 )
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
Answer : D
Question 40 ( Topic 1 )
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
Answer : B
Question 41 ( Topic 1 )
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.Print();
}
A. It prints: from First
B. It prints: from Firstfrom First
C. Compilation error
D. Runtime error.
Answer : B
Question 42 ( Topic 1 )
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
Answer : C
Question 43 ( Topic 1 )
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
Answer : B
Question 44 ( Topic 1 )
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
Answer : D
Question 45 ( Topic 1 )
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
Answer : A
C++ Associate Programmer Questions + Answers
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am