100% found this document useful (1 vote)
938 views

C++ Ques For Practice PDF

Uploaded by

Mohammad Annus
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
100% found this document useful (1 vote)
938 views

C++ Ques For Practice PDF

Uploaded by

Mohammad Annus
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/ 41

UNIT I

Which of the following term is used for a function defined inside a class?

A. Member Variable

B. Member function

C. Class function

D. Classic function

Which of the following concepts of OOPS means exposing only necessary information to client?

A. Encapsulation

B. Abstraction

C. Data hiding

D. Data binding

cout is a/an :

A. operator

B. function

C. object

D. macro

cin and cout are related to which header file:

(a) iostream

(b) conio

(c) stdio

(d) getch
Which of the following concept of oops allows compiler to insert arguments in a function call if it is not
specified?

A. Call by value

B. Call by reference

C. Default arguments

D. Call by pointer

Which of the following is correct about function overloading?

A. The types of arguments are different.

B. The order of argument is different.

C. The number of argument is same.

D. Both A and B.

Which of the following is correct about class and structure?

A. class can have member functions while structure cannot.

B. class data members are public by default while that of structure are private.

C. Pointer to structure or classes cannot be declared.

D. class data members are private by default while that of structure are public by default.

Which of the following concepts means wrapping up of data and functions together?

A. Abstraction

B. Encapsulation

C. Inheritance

D. Polymorphism

Which of the following is the correct class of the object cout?


A. iostream

B. istream

C. ostream

D. ifstream

Which of the following statements regarding inline functions is correct?

A. It speeds up execution.

B. It slows down execution.

C. It increases the code size.

D. Both A and C.

Which of the following statements is correct in C++?

A. Classes cannot have data as protected members.

B. Structures can have functions as members.

C. Class members are public by default.

D. Structure members are private by default.

Which of the following access specifier is used as a default in a class definition?

A. protected

B. public

C. private

D. friend

Which of the following statement is correct?

A. Class is an instance of object.


B. Object is an instance of a class.

C. Class is an instance of data type.

D. Object is an instance of data type.

What is correct about the static data member of a class?

A. A static member function can access only static data members of a class.

B. A static data member is shared among all the object of the class.

C. A static data member can be accessed directly from main().

D. Both A and B.

Which of the following statement will be correct if the function has three arguments passed to it?

A. The trailing argument will be the default argument.

B. The first argument will be the default argument.

C. The middle argument will be the default argument.

D. All the argument will be the default argument.

Which of the following statement is correct?

A. Overloaded functions can have at most one default argument.

B. An overloaded function cannot have default argument.

C. All arguments of an overloaded function can be default.

D. A function if overloaded more than once cannot have default argument.

Where the default value of parameter have to be specified?

A. Function call
B. Function definition

C. Function prototype

D. Both B or C

Which of the following statement is correct?

A. The order of the default argument will be right to left.

B. The order of the default argument will be left to right.

C. The order of the default argument will be alternate.

D. The order of the default argument will be random.

One of the following is true for an inline function.

A - It executes faster as it is treated as a macro internally

B - It executes faster because it priority is more than normal function

C - It doesn’t executes faster compared to a normal function

D - None of the above holds true for an inline function

Which operator is used to resolve the scope of the global variable?

A-<

B-.

C-*

D - ::

What is the output of the following program?

#include<isotream>
using namespace std;

int main()

const int a = 5;

a++;

cout<<a;

A. 5

B. 6

C . Run time error

D . Compile error

What is the size of the following union definition?(Assume int size is 2 Byte and char size is 1 Byte)

#include<isotream>

using namespace std;

int main()

union abc

char a, b, c, d, e, f, g, h;

int i;

};

cout<<sizeof(abc);

return 0;

}
(a) 1 Byte

(b) 2 Byte

(c) 4 Byte

(d) 10 Byte

Which of the following is the correct operator to compare two variables?

A. :=

B. =

C. equal

D. ==

Which of the following is not a correct variable type?

A. float

B. real

C. int

D. double

Which of the following is a correct comment?

A. */ Comments */

B. ** Comment **

C. /* Comment */

D. { Comment }
What is the correct value to return to the operating system upon the successful completion of a
program?

A. -1

B. 1

C. 0

D. Programs do not return a value.

(a) Write a Program in C++ to find largest among two number using class.

(b) Write a Program in C++ to calculate simple interest using class.

Write a Program in C++ to calculate area of circle,square,triangle and rectangle using function
overloading.

Write short note on:

(a) inline function

(b) friend function

(a) Write a program in C++ to calculate simple interest by taking rate of interest as default argument.

(b) Write a program in C++ to swap two numbers using friend function.

UNIT II

What punctuation is used to signal the beginning and end of code blocks?

A. { }
B. -> and <-

C. BEGIN and END

D. ( and )

A reference is declared using the _____ symbol.

A. &&

B. &

C. ||

D. !

Which of the following statements is correct?

A reference is not a constant pointer.

A referenced is automatically de-referenced.

A. Only 1 is correct.

B. Only 2 is correct.

C. Both 1 and 2 are correct.

D. Both 1 and 2 are incorrect.

Reference is like a _____.

A. Pointer B. Structure

C. Macro D. Enum

Which of the following statement is correct?


A. A reference is a constant pointer.

B. A reference is not a constant pointer.

C. An array of references is acceptable.

D. It is possible to create a reference to a reference.

The operator used for dereferencing or indirection is ____

a) *

b) &

c) ->

d) –>>

Which of the following is illegal?

a) int *ip;

b) string s, *sp = 0;

c) int i; double *dp = &i;

d) int *pi = 0;

What will happen in this code?

int a = 100, b = 200;

int *p = &a, *q = &b;

p = q;

a) b is assigned to a

b) p now points to b

c) a is assigned to b
d) q now points to a

What is the output of this program?

#include <iostream>

using namespace std;

int main()

char arr[20];

int i;

for(i = 0; i < 10; i++)

*(arr + i) = 65 + i;

*(arr + i) = '\0';

cout << arr;

return(0);

a) ABCDEFGHIJ

b) AAAAAAAAAA

c) JJJJJJJJ

d) BBBBBBBBBB

What is the output of this program?

#include <iostream>

using namespace std;

int main()

{
char *ptr;

char Str[] = "abcdefg";

ptr = Str;

ptr += 5;

cout << ptr;

return 0;

a) fg

b) cdef

c) defg

d) abcd

Which of the following correctly declares an array?

a) int array[10];

b) int array;

c) array{10};

d) array array[10];

What is the index number of the last element of an array with 9 elements?

a) 9

b) 8

c) 0

d) 10
What is a array?

a) An array is a series of elements of the same type in contiguous memory locations

b) An array is a series of element

c) An array is a series of elements of the same type placed in non-contiguous memory locations

d) None of the mentioned

Which of the following gives the memory address of the first element in array?

Array is declared as: int array[10];

a) array[0];

b) array[1];

c) array(2);

d) array;

What will be the output of this program?

#include <stdio.h>

using namespace std;

int array1[] = {1200, 200, 2300, 1230, 1543};

int array2[] = {12, 14, 16, 18, 20};

int temp, result = 0;

int main()

for (temp = 0; temp < 5; temp++) {

result += array1[temp];
}

for (temp = 0; temp < 4; temp++) {

result += array2[temp];

cout << result;

return 0;

a) 6553

b) 6533

c) 6522

d) 12200

What will be the output of the this program?

#include <stdio.h>

using namespace std;

int main ()

int array[] = {0, 2, 4, 6, 7, 5, 3};

int n, result = 0;

for (n = 0; n < 8; n++) {

result += array[n];

cout << result;

return 0;

}
a) 25

b) 26

c) 27

d) Garbage Value

What is the output of this program?

#include <stdio.h>

using namespace std;

int main()

char str[5] = "ABC";

cout << str;

return 0;

a) ABC

b) ABCD

c) AB

d) CBA

Void pointer can point to which type of objects?

a) int

b) float

c) double

d) All of the mentioned


What is the output of this program?

#include <iostream>

using namespace std;

int main()

int a = 5, c;

void *p = &a;

double b = 3.14;

p = &b;

c = a + b;

cout << c << '\n' << p;

return 0;

a) 8, memory address

b) 8.14

c) memory address

d) memory address, memory address

Which keyword is used to define the user defined data types?

a) def

b) union

c) typedef

d) type
What is the output of this program?

#include <iostream>

using namespace std;

int main()

typedef int num;

num a = 10, b = 15;

num c = a + b + a - b;

cout << c;

return 0;

a) 20

b) 15

c) 30

d) 25

What is the syntax of user-defined data types?

a) typedef existing data type new name;

b) typedef new name existing data type;

c) def existing data type new name;

d) def new name existing data type;


Which variable stores the memory address of another variable?

A) Reference

B) Pointer

C) Array

D) union

The implicit argument passed to a member function of C++ class is called

(a) this pointer

(b) sender object

(c) constructor

(d) void pointer

Which of the following cannot be passed to a function ?

(a) Array

(b) Reference variable

(c) Object

(d) File

Among following which will give the size of object or type ?

(a) delete

(b) new

(c) sizeof

(d) realloc
Write a program in C++ to implement linear search using class.

Write short note on:

(a) Dangling pointer

(b) constant pointer

(a) Write a program in C++ to concate two strings.

(b) Write a program in C++ to find maximum and minimum element from an array of 10 elements.

(a) Write a program in C++ to perform matrix multiplication.

(b) Write a program in C++ to reverse a given string.

UNIT III

Which of the following is not a type of constructor?

A. Copy constructor

B. Friend constructor

C. Default constructor

D. Parameterized constructor

Which of the following statement is correct?

A. A constructor is called at the time of declaration of an object.

B. A constructor is called at the time of use of an object.

C. A constructor is called at the time of declaration of a class.

D. A constructor is called at the time of use of a class.


Which of the following functions are performed by a constructor?

A. Construct a new class

B. Construct a new object

C. Construct a new function

D. Initialize objects

Which of the following statement is correct?

A. Constructors can have default parameters.

B. Constructors cannot have default parameters.

C. Constructors cannot have more than one default parameter.

D. Constructors can have at most five default parameters.

A copy constructor takes

(A) no argument

(B) one argument

(C) two arguments

(D) arbitrary no. of arguments

Destructor is using the approach:

(a) LIFO (Last-in-first-out)

(b) FIFO (First-in-first-out)

(c) Random order

(d) LILO (Last-in-Last-out)


How many constructors can a class have?

(A) 0

(B) 1

(C) 2

(D) any number

Given a class named Book, which of the following is not a valid constructor?

(A) Book ( ) { }

(B) Book ( Book b) { }

(C) Book ( Book &b) { }

(D) Book (char* author, char* title) { }

A constructor that does not have any parameters is called____________ constructor.

a. Custom

b. dynamic

c. static

d. default

If default constructor is not defined, then how the objects of the class will be created?

a. The compiler will generate error

b. Error will occur at run-time.

c. Compiler provides its default constructor to build the object

d. Using default arguments


Which of the followings are true about constructors?

1. A class can have more than one constructor

2. Their address can be referred

3. Constructors is always declared in public section of the class

4. Constructors cannot be declared in private section of the class

5. Constructors cannot return values

(a) 1,2

(b) 2,3

(c) 1,3,4,5

(d)1,2,3,5

Assume class TEST. Which of the following statements is/are responsible to invoke copy constructor?

a. TEST T2(T1)

b. TEST T4 = T1

c. T2 = T1

d. both a and b

Which of the following statements are not true about destructor?

1. It is invoked when object goes out of the scope

2. Like constructor, it can also have parameters

3. It can be virtual

4. It can be declared in private section


5. It bears same name as that of the class and precedes Lambda sign

a. Only 2, 3, 5

b. Only 2, 3, 4

c. Only 2, 4, 5

d. Only 3, 4, 5

If new operator is used, then the constructor function is ?

(a) Copy constructor

(b) Default constructor

(c) Static constructor

(d) Dynamic constructor

To perform File I/O operations, we must use _____________ header file.

a. <ifstream>

b. <ofstream>

c. <fstream>

d. <iostream>

Which of the following is not a file opening mode ____ .

a. ios::ate

b. ios::nocreate

c. ios::noreplace

d. ios::truncate
To create an output stream, we must declare the stream to be of class ___________ .

a. ofstream

b. ifstream

c. iostream

d. iostream

A file stream refers to the flow of data between a ...............

A) Program and object

B) Program and stream

C) Program and file

D) Program to Program

eof( ) is the function used for

a. asserting no errors in a file

b. appending data to a file

c. counting the amount of data in a file

d. checking for end of file

ifstream fin; would be used when

a. creating a file

b. reading a file

c. appending a file

d. removing a file
In the code fout.open("scores.dat", ios::out);

a. ios::out is the stream operation mode.

b. fout is the header file reference.

c. ios::out is the stream variable name..

d. fout is the name of the file.

Which stream class is to only write on files ?

A. ofstream

B. ifstream

C. fstream

D. iostream

Which among following is used to open a file in binary mode ?

A. ios:app

B. ios::out

C. ios::in

D. ios::binary

Which is correct syntax ?

A. myfile:open ("example.bin", ios::out);

B. myfile.open ("example.bin", ios::out);

C. myfile::open ("example.bin", ios::out);

D. myfile.open ("example.bin", ios:out);

Which among following is correct syntax of closing a file in c++ ?


A. myfile$close();

B. myfile@close();

C. myfile:close();

D. myfile.close();

Which is among following is used to Open a file for output and move the read/write control to the end
of the file ?

A. ios::ate

B. ios::at

C. ios::ann

D. ios::end

A constructor that accepts __________ parameters is called the default constructor.

A. One

B. two

C. No

D. three

Which of the following gets called when an object goes out of scope?

A. constructor

B. destructor

C. main

D. virtual function

Write Short note on:

(a) copy constructor


(b) default constructor

Write a program in C++ to add two complex number using constructor overloading.\

(a) Explain the concept of Dynamic Constructor with help of suitable example.

(b) Write a program in C++ to illustrate the concept of destructor.

Write short note on:

(a) parametrized constructor

(b) File operating modes

UNIT IV

Predict the order in which the constructor will be execute

class B: public A

};

A.class A, class B

B.class B, class A

C.class B, class B

D.Cannot be determined

Predict the order in which the constructor will be called

class A: public B, public C

{
};

A.class B, class C, class A

B.class A, class B, class C

C.class C, class A, class B

D.class B, class A , class C

Predict the order in which the constructor will be called

class A:public B, virtual C

};

A.class B, class C, class A

B.class A, class B, class C

C.class C, class B, class A

D.class B, class A, class C

Predict the order in which the constructor will be called

class A: virtual public B, Public C

};

A.class B, class C, class A

B.class A, class B, class C

C.class C, class B, class A

D.class B, class A, class C

Which class is used to design the base class?


a) abstract class

b) derived class

c) base class

d) None of the mentioned

Consider the math department of a school, which is made up of one or more teachers and the
department does not own the teachers (they merely work there), When the department is destroyed,
the teachers should still exist independently; this can be taken as an example of?

A. Aggregation

B. Composition

C. Inheritance

D. None of these

An operator function is created using _____________ keyword.

a. iterator

b. allocator

c. constructor

d. operator

Which of the following is the perfect set of operators that can’t be overloaded in CPP ?

a. +=, ?, :: , >>

b. >>, <<, ?, *, sizeof()

c. :: , . , .* , ?:

d. :: , ->, * , new, delete


Scope resolution operator is used______ .

a. to resolve the scope of global variables only

b. to resolve the scope of functions of the classes only

c. to resolve scope of global variables as well as functions of the classes

d. None of these

Operators such as ...................... cannot be overloaded.

A) +

B) ++

C) : :

D) = =

The derivation of Child class from Base class is indicated by ____ symbol.

a. ::

b. :

c. ;

d. |

Reusability of the code can be achieved in CPP through ______ .

a. Polymorphism

b. Encapsulation

c. Inheritance

d. Both a and c

Write a Program in C++ to implement the concept of Multiple Inheritance.


Write a Program in C++ to implement concept of Multilevel Inheritance.

Write a Program in C++ to perform addition of two complex numbers using binary operator overloading.

Write Short note on:

(a) Ambiguity in Inheritance

(b) Type Conversion

UNIT V

Which class is used to design the base class?

a) abstract class

b) derived class

c) base class

d) None of the mentioned

Which is used to create a pure virtual function ?

a) $

b) =0

c) &

d) !

What is the output of the following program?

#include<isotream>

using namespace std;

main()

{
const int a = 5;

a++;

cout<<a;

A-5

B-6

C - Runtime error

D - Compile error

Early Binding is implemented with help of:

(a) virtual function

(b) function overloading

(c) friend function

(d) All of the above

Late Binding is implemented by:

(a) virtual function

(b) function overloading

(c) constructor

(d) destructor

Which operator is used to allocate dynamic memory:

(a) new

(b) delete

(c) free
(d) malloc

Which operator is used to deallocate dynamic memory:

(a) new

(b) delete

(c) free

(d) malloc

What is polymorphism

(a) Ability to take more than one form

(b) Ability to destroy destructor

(c) Ability to create constructor

(d) None of above

In order for an instance of a derived class to completely take over a class member from a base class, the
base class has to declare that member as

(a) new

(b) base

(c) virtual

(d) overrides

expression x.y represents as

(a) member x of object y

(b) member y of object x

(c) member y of object pointed by x


(d) all of above

During dynamic memory allocation in CPP, new operator returns _________ value if memory allocation
is unsuccessful.

a. False

b. NULL

c. Zero

d. None of these

In CPP, dynamic memory allocation is done using ______________ operator.

a. calloc()

b. malloc()

c. allocate

d. New

_______________ is a member function that is declared within a base class and redefined by derived
class.

a. virtual function

b. static function

c. friend function

d. const member function

When a virtual function is redefined by the derived class, it is called___________.

a. Overloading

b. Overriding
c. Rewriting

d. All of these

Write a Program in C++ to implement concept of virtual function and give proper explanation.

Write a program in C++ to show use of new and delete operator and give proper explanation.

Write Short note on:

(a) self referential class

(b) new and delete operator

Write Short note on:

(a) virtual destructor

(b) Run time polymorphism

UNIT VI

Exception handlers are declared with ____________ keyword.

a. Try

b. catch

c. throw

d. Finally

Which of the following statements are true about Catch handler?

1. It must be placed immediately after try block T.


2. There must be only one catch handler for every try block.

3. There can be multiple catch handler for a try block T.

(a) 1,2,3

(b) 1,2

(c) 1,3

(d) only 3

An exception is caused by

a. A hardware problem
b. A problem in the operating system
c. A run-time error
d. A syntax error

Which keyword is not related to exception Handling.

(a) try

(b) rethrow

(c) throw

(d) catch

What is the output of this program?

#include <iostream>

using namespace std;

int main ()

try

{
throw 20;

catch (int e)

cout << "An exception occurred " << e << endl;

return 0;

a) 20

b) An exception occurred

c) error

d) An exception occurred 20

Which is used to throw a exception?

a) throw

b) try

c) catch

d) none of the mentioned

Which statement is used to catch all types of exceptions?

a) catch()

b) catch(Test t)

c) catch(...)

d) none of the mentioned


What is the output of this program?

#include <iostream>

using namespace std;

int main()

int x = -1;

try

if (x < 0)

throw x;

else

cout<<x;

catch (int x )

cout << "Exception occurred: Thrown value is " << x << endl;

return 0;

a) -1
b) 0

c) Exception occurred: Thrown value is -1

d) error

What does the size of the vector refers to in c++?

a) Size of vector

b) Type of vector

c) Number of elements

d) None of the mentioned

What kind of iteration does forward_list provide in C++?

a) Uni-directional

b) Bi-directional

c) Multi-directional

d) None of the mentioned

Which is used to iterate over container?

a) Associated iterator type

b) Data type of objects

c) Return type of variables

d) None of the mentioned

Meaning of STL

a. Standard Tree Library


b. Standard Term Library
c. Standard Template Library
d. None of above
What is meant by template parameter?

a) It can be used to pass a type as argument

b) It can be used to evaluate a type.

c) It can of no return type

d) None of the mentioned

What is the output of this program?

#include <iostream>

using namespace std;

template <class T>

T max (T& a, T& b)

return (a>b?a:b);

int main ()

int i = 5, j = 6, k;

long l = 10, m = 5, n;

k = max(i, j);

n = max(l, m);

cout << k << endl;

cout << n << endl;

return 0;

a) 6
b) 6

10

c) 5

10

d) 6

Write a Program in C++ to multiply two vectors using class template.

Write a Program in C++ to add two numbers using the concept of function template.

Write a program in C++ to caught an exception(Division by zero), using exception Handling(try->throw-


>catch)

Write Short note on:

(a) Standard template library

(b) Rethrowing an exception

You might also like