What happens when you attempt to compile and run the following code?
#include
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:";
}
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Correct Answer:
D
Explanation
:
Q: 2
What happens when you attempt to compile and run the following code?
#include
using namespace std;
class A {
public :
void print() {
cout << "A ";
}
};
class B {
public :
void print() {
cout << "B ";
}
};
int main() {
B sc[2];
B *bc = (B*)sc;
for (int i=0; iprint();
return 0;
}
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Correct Answer:
B
Explanation
:
Q: 3
What happens when you attempt to compile and run the following code?
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Correct Answer:
B
Explanation
:
Q: 4
Which of the following is a user defined data type?
1:
struct person
{
char name[20];
int age;
};
2:
int l=2;
3:
enum color {red,blue, green};
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Correct Answer:
A, C
Explanation
:
Q: 5
What happens when you attempt to compile and run the following code?
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Correct Answer:
C
Explanation
:
Q: 6
What happens when you attempt to compile and run the following code?
#include
using namespace std;
int main (int argc, const char * argv[])
{
int tab[5]={1,2,3};
for (int i=0; i<5; i++)
cout <<tab[i];
return 0;
}
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Correct Answer:
B
Explanation
:
Q: 7
What happens when you attempt to compile and run the following code?
#include
using namespace std;
int main()
{
const char *s;
char str[] = "Hello";
s = str;
while(*s) {
cout << *s++;
}
return 0;
}
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Correct Answer:
B
Explanation
:
Q: 8
What happens when you attempt to compile and run the following code?
#include
#include
using namespace std;
int main()
{
string s1[]= {"H" , "t" };
string s;
for (int i=0; i<2; i++) {
s = s1[i];
s.insert(1,"o");
cout << s;
}
return( 0 );
}
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Correct Answer:
A
Explanation
:
Q: 9
What happens when you attempt to compile and run the following code?
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Correct Answer:
B
Explanation
:
Q: 10
Which code, inserted at line 18, generates the output "AB"
#include
using namespace std;
class A
{
public:
void Print(){ cout<< "A";}
void Print2(){ cout<< "a";}
};
class B:public A
{
public:
void Print(){ cout<< "B";}
void Print2(){ cout<< "b";}
};
int main()
{
B ob2;
//insert code here
ob2.Print();
}
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Correct Answer:
D
Explanation
:
Q: 11
What happens when you attempt to compile and run the following code?
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Correct Answer:
A
Explanation
:
Q: 12
What will be the output of the program?
#include
#include
using namespace std;
int fun(int);
int main()
{
float k=3;
k = fun(k);
cout<<k;
return 0;
}
int fun(int i)
{
i++;
return i;
}
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Correct Answer:
C
Explanation
:
Q: 13
What happens when you attempt to compile and run the following code?
#include
using namespace std;
class A
{
public:
virtual void Print(){ cout<<"A";}
};
class B:public A
{
public:
virtual void Print(){ cout<Print();
B ob2;
obj = &ob2;
obj?>Print();
}
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Correct Answer:
A
Explanation
:
Q: 14
What happens when you attempt to compile and run the following code?
#include
#include
using namespace std;
class complex{
double re;
double im;
public:
complex() : re(1),im(0.4) {}
bool operator==(complex &t);
};
bool complex::operator == (complex &t){
if((this?>re == t.re) && (this?>im == t.im))
return true;
else
return false;
}
int main(){
complex c1,c2;
if (c1==c2)
cout << "OK";
else {
cout << "ERROR";
}
}
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Correct Answer:
A
Explanation
:
Q: 15
Which code line inserted instead of the comment below will fix the program so that it produces the
expected output and does not cause any memory leak?
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Correct Answer:
A
Explanation
:
Q: 16
What will happen if the memory cannot be allocated?
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Correct Answer:
D
Explanation
:
Q: 17
Which line of code inserted instead of the comment will make the following code run properly
without causing memory leaks?
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Correct Answer:
D
Explanation
:
Q: 18
What happens if characters 'w', 'o', 'r', 'l' and 'd' are entered as input?
#include
#include
using namespace std;
int main()
{
string s1 = "Hello";
string s2;
getline( cin, s2 );
cout << s1 + s2;
return( 0 );
}
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Correct Answer:
A
Explanation
:
Q: 19
Which of the following structures are correct?
1:
struct s1{
int x;
char c;
};
2:
struct s2{
float f;
struct s2 *s;
};
3:
struct s3{
float f;
in i;
}
Options
Discussion
No comments yet. Be the first to comment.
Be respectful. No spam.
Correct Answer:
A, B
Explanation
:
Q: 20
What happens when you attempt to compile and run the following code?
#include
using namespace std;
void print(char *c);
int main (int argc, const char * argv[])
{
print("Test");
return 0;
}
void print(char *c)
{
cout<<c;
}