Q C C++ Student
Q C C++ Student
#include <iostream>
using namespace std;
int main()
{
const char* p = "12345";
const char **q = &p;
*q = "abcde";
const char *s = ++p;
p = "XYZWVU";
++s;
cout << *++s;
return 0;
}
A Compile error
Bc
C d;
D Garbage value
int main()
{
const Point t;
cout << t.getX() << " ";
return 0;
}
A Garbage Values
B0
C Compiler Error in line cout << t.getX() << " ";
D none
class Test
{
static int x;
public:
Test() { x++; }
static int getX() {return x;}
};
int Test::x = 0;
int main()
{
cout << Test::getX() << " ";
Test t[3];
cout << Test::getX();
}
A03
B55
C05
D Compiler Error
int main()
{
Player p1;
Player p2;
Player p3;
cout << p1.getID() << " ";
cout << p2.getID() << " ";
cout << p3.getID();
return 0;
}
A Compiler Error
B123
C111
D234
E000
#include<iostream>
using namespace std;
class Test
{
private:
static int count;
public:
Test& fun();
};
int Test::count = 0;
Test& Test::fun()
{
Test::count++;
cout << Test::count << " ";
return *this;
}
int main()
{
Test t;
t.fun().fun().fun().fun();
return 0;
}
A Compiler Error
B4444
C1111
D1234
class Test
{
private:
int x;
public:
Test(int x = 0) { this->x = x; }
void change(Test *t) { *this = *t; }
void print() { cout << "x = " << x << endl; }
};
int main()
{
Test obj(5);
Test *ptr = new Test (10);
obj.change(ptr);
obj.print();
return 0;
}
Ax=5
B x = 10
C Compiler Error
D Runtime Error
How to create a dynamic array of pointers (to integers) of size 10 using new in
C++? Hint: We can create a non-dynamic array using int *arr[10]
Which of the following is true about new when compared with malloc. 1) new
is an operator, malloc is a function 2) new calls constructor, malloc doesn't 3)
new returns appropriate pointer, malloc returns void * and pointer needs to
typecast to appropriate type.
A 1 and 3
B 2 and 3
C 1 and 2
D All 1, 2 and 3
class Test
{
int x;
Test() { x = 5;}
};
int main()
{
Test *t = new Test;
cout << t->x;
}
A Compiler Error
B5
C Garbage Value
D0
int main(void)
{
Derived q;
return 0;
}
A Compiler Error: there cannot be an empty derived class
B Compiler Error: Derived is abstract
C No compiler Error
int main()
{
Point t1;
return 0;
}
A Compiler Error
B Runtime Error
C Constructor called
class Test
{
public:
Test()
{ cout << "Constructor called"; }
};
int main()
{
Test *t = (Test *) malloc(sizeof(Test));
return 0;
}
A Constructor called
B Empty
C Compiler Error
D Runtime error
Like constructors, can there be more than one destructors in a class?
A Yes
B No
Which one of the following is correct, when a class grants friend status to
another class?
A The member functions of the class generating friendship can access the
members of the friend class.
B All member functions of the class granted friendship have unrestricted access
to the members of the class granting the friendship.
C Class friendship is reciprocal to each other.
D There is no such concept
void main()
{
int a=10,b=20;
char x=1,y=0;
if(a,b,x,y)
{
printf("EXAM");
}
}
What is the output?
a) XAM is printed
b) exam is printed
c) Compiler Error
d) Nothing is printed
What will be output if you will compile and execute the following c code?
int extern x;
void main()
{
printf("%d",x);
x=2;
getch();
}
int x=23;
(a) 0
(b) 2
(c) 23
(d) Compiler error
(e) None of these
What will be output if you will compile and execute the following c code?
void main(){
if(printf("cquestionbank"))
printf("I know c");
else
printf("I know c++");
}
(a) I know c
(b) I know c++
(c) cquestionbankI know c
(d) cquestionbankI know c++
(e) Compiler error
18
19
21
22
Void main()
{
int a=5, b=3;
f(a,b);
printf(“%d\n”,a);
}
A3
B8
C5
D2
int main()
{
int y = 10;
fun(&y);
printf("%d", y);
return 0;
}
A 10
B 20
C 30
D 50
union test
{
int x;
char arr[2];
int y;
};
int main()
{
printf("%d", sizeof(union test));
return 0;
}
Predict the output of above program. Assume that the size of an integer is 4
bytes and size of character is 1 byte. Also assume that there is no alignment
needed.
A2
B4
C 10
D6
A 19 19 11
b) 7 7 11
c) 9 7 11
d) 19 7 11
) 10
b) a
c) address of a
d) compilation error
d) Telling the user for proper use of syntax while calling the function
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;
int *ptr = (int *) malloc(5 * sizeof(int));
for (i=0; i<5; i++)
*(ptr + i) = i;
B01233
C01223
D01234
Answer C
Consider the following recursive C function that takes two arguments
unsigned int foo(unsigned int n, unsigned int r)
{ if (n > 0) return (n%r + foo (n/r, r ));
else return 0;
}
Called function foo(351,10) what will be the return value?
A 1 B 9 C 5 D 8
If a binary operator is overloaded as a non-member function, how many parameters will the non-
member function required?
A None, Both operands are already known
B One, to pass the first operand. The second operand will be a member of the first.
C Binary operators can't be overloaded as global functions, only as member functions of a class.
D Two, to pass the first and second operands.
What is the output of the following program
#include<stdio.h>
void f(int *p, int *q) {
p=q;
*p=2;}