0% found this document useful (0 votes)
452 views

C++ Test Questions

The document contains 20 multiple choice questions about C++ concepts and syntax. The questions cover topics like data types, operators, classes, inheritance, polymorphism, and more. Sample questions include what happens when main returns void in C vs C++, which escape sequence represents carriage return, how polymorphism is implemented in OOPs, and how wide characters are represented.

Uploaded by

BEN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
452 views

C++ Test Questions

The document contains 20 multiple choice questions about C++ concepts and syntax. The questions cover topics like data types, operators, classes, inheritance, polymorphism, and more. Sample questions include what happens when main returns void in C vs C++, which escape sequence represents carriage return, how polymorphism is implemented in OOPs, and how wide characters are represented.

Uploaded by

BEN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

C++ test questions

Question 1 of 20

1. What happens if the following program is executed in C and C++?


#include <stdio.h>
void main()
{
printf("Hello World");
}

 Successful run in both C and C++


 Error in C and successful execution in C++
 Error in both C and C++
 Error in C++ and successful execution in C

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.

 octal, hexadecimal, character, decimal


 octal, decimal, character, hexadecimal
 decimal, character, octal, hexadecimal
 hexadecimal, octal, decimal, character

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

3. Which of the following is called address operator?

 _
 *
 &
 %

Explanation: & operator is called address operator and is used to access the address of a variable.
Question 4 of 20

4. What will be the output of the following C++ code?


#include <stdio.h>
int main()
{
char a = '\012';

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

5. What will be the output of the following C++ code?


#include<iostream>
using namespace std;
class Base {
public:
Base()
{ cout<<"Constructing Base \n"; }
virtual~Base()
{ cout<<"Destructing Base \n"; }
};
class Derived: public Base {
public:
Derived()
{ cout<<"Constructing Derived \n"; }
~Derived()
{ cout<<"Destructing Derived \n"; }
};

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

6. Out of the following, which is not a member of the class?

 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

7. What does polymorphism in OOPs mean?

 Concept of keeping things in different modules/files


 Concept of wrapping things into a single unit
 Concept of hiding data
 Concept of allowing overriding of functions

Explanation: In OOPs, Polymorphism is the concept of allowing a user to override functions


either by changing the types or number of parameters passed.

Question 8 of 20

8. Which of the following escape sequence represents carriage return?


 \n\r
 \r
 \n
 \c

Explanation: \r is used to represent carriage return which means move the cursor to the beginning
of the next line.

Question 9 of 20

9. Which function is used to write a single character to console in C++?

 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

10. Which of the following approach is used by C++?

 Left-right
 Right-left
 Bottom-up
 Top-down

Explanation: C++ is an object-oriented language and OOL uses a bottom-up approach to


solve/view a problem.

Question 11 of 20

11. What happens if the following program is executed in C and C++?


#include <stdio.h>
void func()
{
printf("Hello");
}
void main()
{
func();
func(2);
}

 Error in both C and C++


 Outputs Hello twice in both C and C++
 Error in C++ and Outputs Hello twice in C
 Error in C and Outputs Hello twice in C++

Explanation: In C++ whenever a function without argument is declared it is equivalent to


function with void arguments i.e. func() == func(void) whereas in C a function without argument
is equivalent to func(...) i.e. it can take any number of arguments so func(2) call is also valid in C
but not valid in C++. Hence it gives error in C++ whereas no error in C.

Question 12 of 20

12. What is the other name used for functions inside a class?

 Class functions
 Member functions
 Class variables
 Member variables

Explanation: Functions of a class are also known as member functions of a class.

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

14. Which of the following explains the overloading of functions?

 Virtual polymorphism
 Pseudo polymorphism
 Ad-hoc polymorphism
 Transient polymorphism

Explanation: Ad-hoc polymorphism is a type of polymorphism in which a function denotes


heterogeneous implementation depending upon the types of argument.

Question 15 of 20

15. Which of the following statements are false?


 bool can be converted into integers implicitly
 bool can have two values and can be used to express logical expressions
 a bool value can be used in arithmetic expressions
 bool cannot be used as the type of the result of the function

Explanation: Boolean can be used as a return value of a function.

Question 16 of 20

16. What does '\a' escape code represent?

 form feed
 tab
 backslash
 alert

Explanation: Because \a is used to produce a beep sound.

Question 17 of 20

17. Which of the following is correct?

 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

18. How many characters are specified in the ASCII scheme?

 24
 128
 256
 64

Explanation: There are 128 characters defined in the C++ ASCII list.

Question 19 of 20

19. What will be the output of the following C++ code?


#include<stdio.h>
int main(int argc, char const *argv[])
{
char a = 'a';
printf("%d\n", (int)sizeof(a));
return 0;
}

 Output in C is 4 and in C++ is 1


 Output in C is 1 and in C++ is 1
 Output in C is 1 and in C++ is 4
 Output in C is 4 and in C++ is 4

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.

Next set of questions.


Question 1 of 20

1. How do we represent a wide character of the form wchar_t?

 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

2. Which of the following is the scope resolution operator?


 *
 ~
 .
 ::

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

4. The value 132.54 can be represented using which data type?

 bool
 int
 double
 void

Explanation: The given value is with decimal points, so float or double can be used.

Question 5 of 20

5. What will be the output of the following C++ code?


#include <iostream>
using namespace std;

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

7. What is the range of the floating point numbers?

 -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

8. What happens if the following program is executed in C and C++?


#include <stdio.h>
void func(void)
{
printf("Hello");
}
void main()
{
func();
func(2);
}

 Error in C++ and successful execution in C


 Error in both C and C++
 Outputs Hello twice in both C and C++
 Error in C and successful execution in C++

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

9. What happens if we run the following code in both C and C++?


#include<stdio.h>
struct STRUCT
{
int a = 5;
int func()
{
printf("%d\n", a);
}
};
int main()
{
struct STRUCT s;
s.func();
return 0;
}

 The program gives an error in case of both C and C++


 The program runs fine and both prints output "HELLO THIS IS STRUCTURE"
 The program gives an error in case of C but runs perfectly in case of C++
 The program gives an error in case of C++ but runs perfectly in case of C

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

10. What will be the output of the following C++ code?


#include <iostream>
int const s=9;
int main()
{
std::cout << s;
return 0;
}

 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?

 (~v & (v - 1)) == 0;


 (v | (v - 1)) == 0;
 (v | (v + 1)) == 0;
 (v & (v - 1)) == 0;

Explanation: Power of two integers have a single set bit followed by unset bits.

Question 13 of 20

13. Which of the following is a static polymorphism mechanism?

 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

14. What will be the output of the following C++ code?


#include <iostream>
using namespace std;
int main()
{
float f1 = 0.5;
double f2 = 0.5;
if (f1 == 0.5f)
cout << "equal";
else
cout << "not equal";
return 0;
}

 equal
 compile time error
 runtime error
 not equal

Explanation: 0.5f results in 0.5 to be stored in floating point representations.


Question 15 of 20

15. Which is used to indicate single precision value?

 F or f
 Either F or for L or l
 Neither F or for L or l
 L or l

Explanation: Either F or f can be used to indicate single precision values.

Question 16 of 20

16. What are the actual parameters in C++?

 Parameters with which functions are called


 Variables other than passed parameters in a function
 Variables that are never used in the function
 Parameters which are used in the definition of a function

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

17. Pick the odd one out.

 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

20. Which type is best suited to represent the logical values?

 integer
 boolean
 character
 float

Explanation: Logical values can be either true or false, so the boolean type is suited for it.

Next set of questions.


1. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int f(int p, int q)
{
if (p > q)
return p;
else
return q;
}
main()
{
int a = 5, b = 10;
int k;
bool x = true;
bool y = f(a, b);
k =((a * b) + (x + y));
cout << k;
}
 75
 52
 62
 55

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

2. How many types of polymorphism are there in C++?

 4
 2
 3
 1

Explanation: There are two types of polymorphism in C++ namely run-time and compile-time
polymorphisms.

3. What will be the output of the following C++ code?


#include <iostream>

int main(int argc, char const *argv[])


{
cout<<"Hello World";
return 0;
}

 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.

4. Which of the following is a valid floating-point literal?

 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.

6. In which part of the for loop termination condition is checked?


for(I;II;III)
{IV}

 IV
 II
 I
 III

Explanation: In II part the termination condition of the for loop is checked.

7. What is the value of the bool?


bool is_int(789.54)

 False
 True
 1
 2

Explanation: The given number is a double not an integer, so the function returns 0 which is
boolean false.

8. Why is “this” pointer used?

 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.

10. What is the difference between delete and delete[] in C++?

 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.

12. What will be the output of the following C++ function?


int main()
{
register int i = 1;
int *ptr = &i;
cout << *ptr;
return 0;
}

 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.

13. What does modularity mean?

 Hiding part of program


 Subdividing program into small independent parts
 Overriding parts of program
 Wrapping things into single unit

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

14. What will be the output of the following C++ code?


#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main(int argc, char const *argv[])


{
int a = 5;
int *p = &a;
int *(&q) = p;
cout<<q;
return 0;
}

 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.

15. What will be the output of the following C++ code?


#include <iostream>
using namespace std;
struct Time
{
int hours;
int minutes;
int seconds;
};
int toSeconds(Time now);
int main()
{
Time t;
t.hours = 5;
t.minutes = 30;
t.seconds = 45;
cout << "Total seconds: " << toSeconds(t) << endl;
return 0;
}
int toSeconds(Time now)
{
return 3600 * now.hours + 60 * now.minutes + now.seconds;
}

 20000
 19845
 19844
 15000

Explanation: In this program, we are just converting the given hours and minutes into seconds.

16. What will be the output of the following C++ code?


#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main(int argc, char const *argv[])


{
int a = 5;
int *p = &a;
int &q = p;
cout<<p;
return 0;
}

 Segmentation fault
 Compile-time error
 Run-time error
 5

Explanation: A pointer cannot be directly assigned to references, because types of pointer(int*)


and reference(int) are different here. You need to think before assigning two variable of different
types otherwise the program throws error.

17. Which function is used to check whether a character is a tab or space?


 isblank()
 isdigit()
 isalnum()
 isalpha()

Explanation: Character classification provides isblank() function to check whether a character in


C++ is space or tab.

18. Identify the incorrect statements.


int var = 10;
int *ptr = &(var + 1); //statement 1
int *ptr2 = &var; //statement 2
&&var = 40; //statement 3

 Statement 1, 2 and 3 are wrong


 Statement 2 and 3 are wrong
 Statement 1 and 3 are wrong
 Statement 1 and 2 are wrong

19. What will be the output of the following C++ code?


#include<iostream>
using namespace std;

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.

21. What will be the output of the following C++ code?


#include <iostream>
using namespace std;
void addprint()
{
static int s = 1;
s++;
cout << s;
}
int main()
{
addprint();
addprint();
addprint();
return 0;
}

 235
 111
 123
 234

Explanation: The variable that is declared as static has a file scope.

22. Identify the type of variables.


typedef char* CHAR;
CHAR p,q;

 char
 char*
 CHAR
 unknown

Explanation: The statement makes CHAR a synonym for char*.

23. What will be the output of the following C++ code?


#include <iostream>
using namespace std;
int add(int a, int b);
int main()
{
int i = 5, j = 6;
cout << add(i, j) << endl;
return 0;
}
int add(int a, int b )
{
int sum = a + b;
a = 7;
return a + b;
}

 13
 12
 compile time error
 11

Explanation: The value of a has been changed to 7, So it returns as 13.

24. Which is more effective while calling the functions?

 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

25. ______________ have the return type void.

 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.

26. Which function is used to check whether a character is a number?

 isdigit()
 isalpha()
 isalnum()
 isblank()

Explanation: Character classification provides isdigit() function to check whether a character in


C++ is number or not.

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.

28. Regarding the following statement which of the statements is true?


const int a = 100;

 Declares a constant a whose value will be 100


 Declares a construction a with 100 as its initial value
 Constructs an integer type variable with an as identifier and 100 as the value
 Declares a variable a with 100 as its initial value

Explanation: Because the const is used to declare non-changeable values only.

29. What is an inline function?

 A function that is not checked for syntax errors


 A function that is not checked for semantic analysis
 A function that is called during compile time
 A function that is expanded at each call during execution

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.

30. Which of the following is illegal?

 int *ip;
 int i; double* dp = &i;
 int *pi = 0;
 string s, *sp = 0;

Explanation: dp is initialized int value of i.


31. The difference between x and ‘x’ is?

 Both are same


 Both are string literal
 The first one is a character constant x and the second one is the string literal x
 The first one refers to a variable whose identifier is x and the second one refers to the
character constant x

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.

32. What will be the output of the following C++ code?


#include <iostream>
using namespace std;
int main ()
{
int x, y;
x = 5;
y = ++x * ++x;
cout << x << y;
x = 5;
y = x++ * ++x;
cout << x << y;
return 0;
}

 367597
 367497
 749735
 736749

Explanation: Because of the precedence the pre-increment and post increment operator, we got
the output as 749736.

33. Which of the following accesses a variable in structure *b?

 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.

You might also like