C++ Test Questions
C++ Test Questions
Question 1 of 20
Explanation: main() function in C++ must return int otherwise the C++ compiler gives the error
whereas C does not forces such things on main() function. There as when we are making void
main(){} function in this program the C++ compiler gives error whereas C compiler runs
successfully.
Question 2 of 20
2. 0946, 786427373824, 'x' and 0X2f are _____ _____ ____ and _____ literals respectively.
Explanation: Literal integer constants that begin with 0x or 0X are interpreted as hexadecimal
and the ones that begin with 0 as octal. The character literal is written within ''.
Question 3 of 20
_
*
&
%
Explanation: & operator is called address operator and is used to access the address of a variable.
Question 4 of 20
printf("%d", a);
return 0;
}
Compiler error
Empty
10
12
Explanation: The value '\012' means the character with value 12 in octal, which is decimal 10.
Question 5 of 20
int main(void)
{
Derived *d = new Derived();
Base *b = d;
delete b;
return 0;
}
A. Constructing Base
Constructing Derived
Destructing Base
B. Constructing Base
Constructing Derived
Destructing Derived
Destructing Base
C. Constructing Base
Constructing Derived
Destructing Base
Destructing Derived
D. Constructing Derived
Constructing Base
Destructing Base
Destructing Derived
Explanation: In this case, we have made the destructor of base class virtual which will ensure
that any derived class object which is pointed by a base class pointer object on deletion should
call both base and derived class destructor.
Question 6 of 20
Constant function
Virtual function
Static function
Friend function
Explanation: Friend function is not a member of the class. They are given the same access rights
as the class member function have but they are not actual members of the class.
Question 7 of 20
Question 8 of 20
Explanation: \r is used to represent carriage return which means move the cursor to the beginning
of the next line.
Question 9 of 20
write(ch)
cout.putline(ch)
printf(ch)
cout.put(ch)
Explanation: C++ provides cout.put() function to write a single character to console whereas
others are used to write either a single or multiple characters.
Question 10 of 20
Left-right
Right-left
Bottom-up
Top-down
Question 11 of 20
Question 12 of 20
12. What is the other name used for functions inside a class?
Class functions
Member functions
Class variables
Member variables
Question 13 of 20
13. Which of the following is not a fundamental type is not present in C but present in C++?
bool
int
float
void
Explanation: Boolean type is not present as a fundamental type in C. int type is used as boolean
in C whereas in C++ bool is defined as a fundamental type for handling boolean outputs.
Question 14 of 20
Virtual polymorphism
Pseudo polymorphism
Ad-hoc polymorphism
Transient polymorphism
Question 15 of 20
Question 16 of 20
form feed
tab
backslash
alert
Question 17 of 20
struct is not required in C but required in C++ while declaring an object of the structure
struct tag is required in both C and C++ while declaring an object of the structure
struct tag is not required in both C and C++ while declaring an object of the structure
struct is not required in C++ but required in C while declaring an object of the structure
Explanation: C++ does not require struct keyword while declaring an object of the structure
whereas in C we require struct tag for declaring an object.
Question 18 of 20
24
128
256
64
Explanation: There are 128 characters defined in the C++ ASCII list.
Question 19 of 20
Explanation: Both in C and C++ the type char has same size which is 1. But a character enclosed
inside single quotes has difference sizes i.e. in case of char a; the size of a will be 1 in both C and
C++ but in case of 'a' size will be 4 in case of C but 1 in case of C++.
Question 20 of 20
20. A language which has the capability to generate new data types are called
________________
Extensible
Overloaded
Encapsulated
Reprehensible
Explanation: Languages that can produce/generate new data types are called extensible
languages as they have the ability to handle new data types.
la
l’a’
L[a]
L’a’
Explanation: A wide character is always indicated by immediately preceding the character literal
by an L.
Question 2 of 20
Explanation: :: operator is called scope resolution operator used for accessing a global variable
from a function which is having the same name as the variable declared in the function.
Question 4 of 20
bool
int
double
void
Explanation: The given value is with decimal points, so float or double can be used.
Question 5 of 20
class A
{
int a;
A() { a = 5;}
};
int main()
{
A *obj = new A;
cout << obj->a;
}
Garbage value
Compile-time error
Run-time error
5
Explanation: As Test() constructor is private member of the class therefore cannot be accessed
from the outside world therefore the program gives error.
Question 6 of 20
6. Suppose in a hypothetical machine, the size of char is 32 bits. What would sizeof(char) return?
Implementation dependent
1
Machine dependent
4
Explanation: The standard does NOT require a char to be 8-bits, but does require that
sizeof(char) return 1.
Question 7 of 20
-3.4E+38 to +3.4E+36
-3.4E+38 to +3.4E+32
-3.4E+38 to +3.4E+38
-3.4E+38 to +3.4E+34
Explanation: This is the defined range of floating type number sin C++. Also range for +ve and -
ve side should be same so the answer is -3.4E+38 to +3.4E+38.
Question 8 of 20
Explanation: As the func(void) needs no argument during its call, hence when we are calling
func(2) with 2 as passed as a parameter then this statement gives the error in both C++ and C
compiler.
Question 9 of 20
Explanation: As C does not allows to initialize any member inside the structure, therefore, the
program gives error whereas in case of C++ this is allowed therefore the program does not give
any error.
Question 10 of 20
Garbage value
Error
9
Segmentation fault
Explanation: The program is syntactically and semantically correct hence the program is
compiled and executed successfully.
Question 11 of 20
11. Which of these expressions will isolate the rightmost set bit?
x = x & (-x)
x = x & (~x)
x = x ^ (-x)
x = x ^ (~x)
Explanation: Negative of a number is stores as 2;s complement in C++, so when you will take
AND of x and (-x) the rightmost digit will be preserved.
Question 12 of 20
12. Which of these expressions will return true if the input integer v is a power of two?
Explanation: Power of two integers have a single set bit followed by unset bits.
Question 13 of 20
Operator overloading
Function overloading
All of the mentioned
Templates
Explanation: All the options mentioned above uses static polymorphism mechanism. As the
conflicts in all these types of functions are resolved during compile-time.
Question 14 of 20
equal
compile time error
runtime error
not equal
F or f
Either F or for L or l
Neither F or for L or l
L or l
Question 16 of 20
Explanation: Actual parameters are those using which a function call is made i.e. which are
actually passed in a function when that function is called.
Question 17 of 20
boolean type
array type
integer type
character type
Explanation: Array type is not the basic type and it is constructed using the basic type
Question 18 of 20
18. It is guaranteed that a ____ has at least 8 bits and a ____ has at least 16 bits.
int, float
char, int
char, short
bool, char
Explanation: char types in C++ require atleast 8 bits and short requires atleast 16 bits, whereas
for bool only 1 bit suffices and both int and float requires atleast 32 bits.
Question 19 of 20
19. Which of three sizes of floating point types should be used when extended precision is
required?
long double
extended float
float
double
Explanation: Float for single precision, double for double precision and long double for extended
precision.
Question 20 of 20
integer
boolean
character
float
Explanation: Logical values can be either true or false, so the boolean type is suited for it.
Explanation: In this question, value of x = true and value of y will be also true as f(a,b) will
return a non-zero value. Now when adding these values with integers, the implicit type
conversion takes place hence converting both x and y to 1(integer equivalent of bool true value).
So expression (a*b) + (x+y) is evaluated to 52
4
2
3
1
Explanation: There are two types of polymorphism in C++ namely run-time and compile-time
polymorphisms.
Compile-time error
Segmentation fault
Hellow World
Run-time error
Explanation: cout is defined under the namespace std and without including std namespace we
cannot cout, therefore, the program gives an error.
F287.333
287.e2
287.3.e2
f287.333
Explanation: To make a floating point literal, we should attach a suffix of 'f' or 'F' and there
should not be any blank space.
5. Const qualifier can be applied to which of the following?
i) Functions inside a class
ii) Arguments of a function
iii) Static data members
iv) Reference variables
i, ii and iii
i only
i, ii, iii, and iv
ii, iii and iv
Explanation: const keyword can be applied to all of the following mentioned above.
IV
II
I
III
False
True
1
2
Explanation: The given number is a double not an integer, so the function returns 0 which is
boolean false.
To access the members of a class which have the same name as local variables in that
scope
To access objects of other variables
To access objects of other class
To access all the data stored under that class
Explanation: “this” pointer is used to access the members of a class which have the same name
as local variables in that part of the code.
9. Which of the following provides a programmer with the facility of using object of a class
inside other classes?
Inheritance
Abstraction
Composition
Encapsulation
Explanation: The concept of using objects of one class into another class is known as
Composition.
delete is used to delete single object whereas delete[] is used to multiple(array/pointer of)
objects
delete is a keyword whereas delete[] is an identifier
delete is syntactically correct but delete[] is wrong and hence will give an error if used in
any case
delete is used to delete normal objects whereas delete[] is used to pointer objects
Explanation: delete is used to delete a single object initiated using new keyword whereas delete[]
is used to delete a group of objects initiated with the new operator.
11. Which of the following is not one of the sizes of the floating point types?
long double
float
short float
double
Explanation: Floating point types occur in only three sizes-float, long double and double.
0
Runtime error may be possible
1
Compiler error may be possible
Explanation: Using & on a register variable may be invalid, since the compiler may store the
variable in a register, and finding the address of it is illegal.
Explanation: Modularity means dividing a program into independent sub programs so that it can
be invoked from other parts of the same program or any other program
5
Address of a
Error
Address of pointer pwrong
Explanation: The program is correct so the the program runs perfectly. It is way to assign
pointers to references. The program prints the address of a because it is an alias for pointer p and
pointer p stores the address of a therefore answer is address of a.
20000
19845
19844
15000
Explanation: In this program, we are just converting the given hours and minutes into seconds.
Segmentation fault
Compile-time error
Run-time error
5
int main()
{
int x = 10;
int& ref = x;
ref = 20;
cout << x << endl ;
x = 30;
cout << ref << endl;
return 0;
}
10
20
20
30
10
30
10
10
Explanation: As we know references are alias for a variable so the value of a variable can be
changed using alias hence both ref and x are same therefore changing the value of one effects the
value of other.
20. Which value can we not assign to reference?
integer
unsigned
floating
null
Explanation: If it can be assigned with a null value means, it is a copy of the pointer.
235
111
123
234
char
char*
CHAR
unknown
13
12
compile time error
11
call by pointer
call by reference
call by value
call by object
Explanation: In the call by reference, it will just pass the reference of the memory addresses of
passed values rather than copying the value to new memories which reduces the overall time and
memory use
destructors
all functions
constructors
none of the mentioned
Explanation: Constructor creates an Object and Destructor destroys the object. They are not
supposed to return anything, not even void.
isdigit()
isalpha()
isalnum()
isblank()
27. Which of the following is the default return value of functions in C++?
int
char
float
void
Explanation: C++ uses int as the default return values for functions. It also restricts that the
return type of the main function must be int.
Explanation: Inline function is those which are expanded at each call during the execution of the
program to reduce the cost of jumping during execution.
int *ip;
int i; double* dp = &i;
int *pi = 0;
string s, *sp = 0;
Explanation: In a C++ code, names with quotes like 'x' represent a character or string (in case of
a collection of characters) whereas without quotes they represent an identifier.
367597
367497
749735
736749
Explanation: Because of the precedence the pre-increment and post increment operator, we got
the output as 749736.
b-var;
b>var;
b->var;
b.var;
Explanation: Because arrow operator(->) is used to access members of structure pointer whereas
dot operator(.) is used to access normal structure variables.