0% found this document useful (0 votes)
40 views18 pages

Q C C++ Student

None

Uploaded by

Dipesh Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views18 pages

Q C C++ Student

None

Uploaded by

Dipesh Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Predict the output of following program

#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

In C++, const qualifier can be applied to 1) Member functions of a class 2)


Function arguments 3) To a class data member which is declared as static 4)
Reference variables
A only 1, 2 and 3
B only 1, 2 and 4
C All
D Only 1, 3 and 4

Predict the output of following program.


#include <iostream>
using namespace std;
class Point
{
int x, y;
public:
Point(int i = 0, int j =0)
{ x = i; y = j; }
int getX() const { return x; }
int getY() {return y;}
};

int main()
{
const Point t;
cout << t.getX() << " ";
return 0;
}
A Garbage Values
B0
C Compiler Error in line cout << t.getX() << " ";
D none

Predict the output of following C++ program.


#include <iostream>
using namespace std;

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

Predict the output of following C++ program.


#include <iostream>
using namespace std;
class Player
{
private:
int id;
static int next_id;
public:
int getID() { return id; }
Player() { id = ++next_id; }
};
int Player::next_id = 1;

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

Which of the following is true?


A Static methods cannot be overloaded.
B Static data members can only be accessed by static methods.
C Non-static data members can be accessed by static methods.
D Static methods can only access static members

Predict the output of following C++ program.

#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

Which of the following is true about this pointer?


A It is passed as a hidden argument to all function calls
B It is passed as a hidden argument to all non-static function calls
C It is passed as a hidden argument to all static functions
D None of the above

What is the use of this pointer?


A When local variable’s name is same as member’s name, we can access
member using this pointer.
B To return reference to the calling object
C Can be used for chained function calls on an object
D All of the above
Predict the output of following C++ program.
#include<iostream>
using namespace std;

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]

A int *arr = new int *[10];


B int **arr = new int *[10];
C int *arr = new int [10];
D not possible

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

Predict the output?


#include <iostream>
using namespace std;

class Test
{
int x;
Test() { x = 5;}
};

int main()
{
Test *t = new Test;
cout << t->x;
}

A Compiler Error
B5
C Garbage Value
D0

Which of the following is true about pure virtual functions? 1) Their


implementation is not provided in a class where they are declared. 2) If a class
has a pure virtual function, then the class becomes abstract class and an instance
of this class cannot be created.
A Both 1 and 2
B Only 1
C Only 2
D Neither 1 nor 2

Predict the output of following program.


#include<iostream>
using namespace std;
class Base
{
public:
virtual void show() = 0;
};

class Derived : public Base { };

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

Can a constructor be virtual? Will the following program compile?


#include <iostream>
using namespace std;
class Base {
public:
virtual Base() {}
};
int main() {
return 0;
}
A Yes
B No

Which of the followings is/are automatically added to every class, if we do not


write our own.
A Copy Constructor
B Assignment Operator
C A constructor without any parameter
D All of the above

When a copy constructor may be called?


A When an object of the class is returned by value.
B When an object of the class is passed (to a function) by value as an argument.
C When an object is constructed based on another object of the same class
D When compiler generates a temporary object.
E All of the above

Output of following program?


#include<iostream>
using namespace std;
class Point {
Point() { cout << "Constructor called"; }
};

int main()
{
Point t1;
return 0;
}
A Compiler Error
B Runtime Error
C Constructor called

Predict the output of following program.


#include<iostream>
#include<stdlib.h>
using namespace std;

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

If a function is friend of a class, which one of the following is wrong?


A A function can only be declared a friend by a class itself.
B Friend functions are not members of a class, they are associated with it.
C Friend functions are members of a class.
D It can have access to all members of the class, even private ones.

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

Which of the following is FALSE about references in C++


A References cannot be NULL
B A reference must be initialized when declared
C Once a reference is created, it cannot be later made to reference another
object; it cannot be reset.
D References cannot refer to constant value
For 16-bit compiler allowable range for integer constants is ______ ?
A. -3.4e38 to 3.4e38
B. -32767 to 32768
C. -32768 to 32767
D. -32668 to 32667

Which of the following shows the correct hierarchy of arithmetic operations in


C
A./ + * -
B. * -/ +
C. + -/ *
D. * / + -

In switch statement, each case instance value must be _______?


A. Constant
B. Variable
C. Special Symbol
D. None of the above

The statement print f ("%d", 10 ? 0 ? 5 : 1 : 12); will print?


A. 10
B. 0
C. 12
D. 1

The statement printf("%c", 100); will print?


A. prints 100
B. print garbage
C. prints ASCII equivalent of 100
D. None of the above

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 happen if in a C program you assign a value to an array element


whose subscript exceeds the size of array?
A. The element will be set to 0.
B. The compiler would report an error.
C. The program may crash if some important data gets overwritten.
D. The array size would appropriately grow.

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

Consider the following C function:


int f(int n)
{
static int i = 1;
if (n >= 5)
return n;
n = n+i;
i++;
return f(n);
}
The value returned by f(1) is
5
6
7
8

What is the value printed by the following C program?


#include
int f(int *a, int n)
{
if(n <= 0) return 0;
else if(*a % 2 == 0) return *a + f(a+1, n-1);
else return *a - f(a+1, n-1);
}
int main()
{
int a[] = {12, 7, 13, 4, 11, 6};
printf("%d", f(a, 6));
getchar();
return 0;
}
-9
5
15
19

What is printed by the following C program?


int f(int x, int *py, int **ppz)
{
int y, z;
**ppz += 1;
z = **ppz;
*py += 2;
y = *py;
x += 3;
return x + y + z;
}
void main()
{
int c, *b, **a;
c = 4;
b = &c;
a = &b;
printf( "%d", f(c,b,a));
getchar();
}

18
19
21
22

What is the output


void f(int a,b)
{
a=a+b;
}

Void main()
{
int a=5, b=3;
f(a,b);
printf(“%d\n”,a);
}
A3
B8
C5
D2

void fun(int *ptr)


{
*ptr = 50;
}

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

Which of the following are themselves a collection of different data types?


a) String
b) Array
c) Character
d) Structure

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

A function prototype is used for

a) Declaring the function logic

b) Calling the function from the main body

c) Telling the compiler, the kind of arguments used in the function

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;

printf("%d ", *ptr++);


printf("%d ", (*ptr)++);
printf("%d ", *ptr);
printf("%d ", *++ptr);
printf("%d ", ++*ptr);
}

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

What does the following fragment of C-program print?


void main()
{
char c[] = "BARE2011";
char *p =c;
printf("%s", p + p[3] - p[1]) ;
}
A BARE2011 B 2011 C 11 D E2011

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;}

int i=0, j=1;


int main() {
f(&i, &j);
printf("%d %d\n", i,j);
return 0; }
A22 B21 C02D22

Which of the following is a valid C/C++ function pointer definition


A int (*f)() B int* f() C (int*)f() D none
Answer A
# include <stdio.h>
int main()
{
char s1[7] = "1234", *p;
p = s1 + 2;
*p = '0';
printf("%s", s1);
}
What will be printed by the program?
A 12 B 120400 C 1204 D 1034

You might also like