100% found this document useful (2 votes)
2K views

Questions On Types, Pointers, Arrays & Structures in C++

This document contains questions and answers about various topics in C++ including types, pointers, arrays, structures, characters, and booleans. It begins with an introduction that explains the purpose is to provide practice interview questions to improve C++ programming skills. It then lists multiple choice questions on these topics along with explanations for the answers.

Uploaded by

kibrom atsbha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
2K views

Questions On Types, Pointers, Arrays & Structures in C++

This document contains questions and answers about various topics in C++ including types, pointers, arrays, structures, characters, and booleans. It begins with an introduction that explains the purpose is to provide practice interview questions to improve C++ programming skills. It then lists multiple choice questions on these topics along with explanations for the answers.

Uploaded by

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

1.

Questions on Types, Pointers, Arrays & Structures in C++

C++ Interview Questions and Answers - Types - Sanfoundry


by Manish

This section on C++ interview questions and answers focuses on “Types”. One shall practice
these interview questions to improve their C++ programming skills needed for various
interviews (campus interviews, walkin interviews, company interviews), placements, entrance
exams and other competitive exams. These questions can be attempted by anyone focusing on
learning C++ programming language. They can be a beginner, fresher, engineering graduate or
an experienced IT professional. Our C++ interview questions come with detailed explanation of
the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ interview questions on “Types” along with answers, explanations and/or
solutions:

1. What is the size of wchar_t in C++?


a) 2
b) 4
c) 2 or 4
d) based on the number of bits in the system
View Answer

Answer: d
Explanation: Compiler wants to make CPU as more efficient in accessing the next value.

2. Pick the odd one out


a) array type
b) character type
c) boolean type
d) integer type
View Answer

Answer: a
Explanation: Array type is not the basic type and it is constructed using the basic type.

3. Which datatype is used to represent the absence of parameters?


a) int
b) short
c) void
d) float
View Answer

Answer: c
Explanation: void will not return anything.
4. What does a escape code represent?
a) alert
b) backslash
c) tab
d) form feed
View Answer

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

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


a) integer
b) boolean
c) character
d) all of the mentioned
View Answer

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

6. Identify the user-defined types from the following?


a) enumeration
b) classes
c) both enumeration and classes
d) int
View Answer

Answer: c
Explanation: They must be defined by the users before use unlike the other types which are
readily available.

7. Which of the following statements are true?


int f(float)
a) f is a function taking an argument of type int and retruning a floating point number
b) f is a function taking an argument of type float and returning a integer
c) f is a function of type float
d) none of the mentioned
View Answer

Answer: b
Explanation: The argument that is passed to a function f is of float type and the function finally
retruns a value that id is of integer type.

8. The value 132.54 can represented using which data type?


a) double
b) void
c) int
d) bool
View Answer

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

9. When a language has the capability to produce new data type mean, it can be called as
a) overloaded
b) extensible
c) encapsulated
d) reprehensible
View Answer

Answer: b
Explanation: Extensible is used to add new features to C++.

10. Pick the odd one out.


a) integer, character, boolean, floating
b) enumeration, classes
c) integer, enum, void
d) arrays, pointer, classes
View Answer

Answer: c
Explanation: Option a consists of all fundamental types, option b consists of user-definied types
and option d consists of derived types but option c is a mixture.

C++ Language Interview Questions - Booleans - Sanfoundry


by Manish

This section on C++ language interview questions and answers focuses on “Booleans”. One shall
practice these interview questions to improve their C++ programming skills needed for various
interviews (campus interviews, walkin interviews, company interviews), placements, entrance
exams and other competitive exams. These questions can be attempted by anyone focusing on
learning C++ programming language. They can be a beginner, fresher, engineering graduate or
an experienced IT professional. Our C++ language interview questions come with detailed
explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ language interview questions on “Booleans” along with answers,
explanations and/or solutions:

1. Is bool a fundamental datatype in C++?


a) Yes
b) No, it is a typedef of unsigned char
c) No, it is an enum of {false, true}
d) No, it is expanded from macros
View Answer

Answer: a
Explanation: C++ has bool as a fundamental data type.

2. Find the odd one out:


a) std::vector<int>
b) std::vector<short>
c) std::vector<long>
d) std::vector<bool>
View Answer

Answer: d
Explanation: std::vector<bool> is a specialized version of vector, which is used for elements of
type bool and optimizes for space. It behaves like the unspecialized version of vector and the
storage is not necessarily an array of bool values, but the library implementation may optimize
storage so that each value is stored in a single bit.

3. What is the value of the bool?

a) True
b) False
c) 1
d) None of the mentioned
View Answer

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

4. What happens when a null pointer is converted into bool?


a) an error is flagged
b) bool value evaluates to true
c) bool value evaluates to false
d) the statement is ignored
View Answer

Answer: c
Explanation: A pointer can be implicitly converted to a bool. A nonzero pointer converts to true
and zerovalued pointer converts to false.

5. Which of the following statements are false?


a) bool can have two values and can be used to express logical expressions
b) bool cannot be used as the type of the result of the function
c) bool can be converted into integers implicitly
d) a bool value can be used in arithmetic expressions
View Answer

Answer: b
Explanation: None.

6. For what values of the expression is an if-statement block not executed?


a) 0 and all negative values
b) 0 and -1
c) 0
d) 0, all negative values, all positive values except 1
View Answer

Answer: c
Explanation: The if-statement block is only not executed when the expression evaluates to 0. It’s
just syntactic sugar for a branch-if-zero instruction.

7. Which of the two operators ++ and — work for the bool datatype in C++?
a) None
b) ++
c) —
d) ++ & —
View Answer

Answer: b
Explanation: Due to history of using integer values as booleans, if an integer is used as a
boolean, then incrementing will mean that whatever its truth value before the operation, it will
have a truth-value of true after it. However, it’s not possible to predict the result of — given
knowledge only of the truth value of x, as it could result in false.

8. What is the output of the following program?

1. #include <iostream>
2. using namespace std;
3. int f(int p, int q)
4. {
5. if (p > q)
6. return p;
7. else
8. return q;
9. }
10. main()
11. {
12. int a = 5, b = 10;
13. int k;
14. bool x = true;
15. bool y = f(a, b);
16. k =((a * b) + (x + y));
17. cout << k;
18. }

a) 55
b) 62
c) 52
d) none of the mentioned
View Answer

Answer: c
Explanation: None.

9. What is the value of p?

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int p;
6. bool a = true;
7. bool b = false;
8. int x = 10;
9. int y = 5;
10. p = ((x | y) + (a + b));
11. cout << p;
12. return 0;
13. }

a) 0
b) 16
c) 12
d) 2
View Answer

Answer: b
Explanation: None.

10. Evaluate the following


(false && true) || false || true
a) 0
b) 1
c) false
d) none of the mentioned
View Answer

Answer: b
Explanation: None.
Online C++ Test - Character Types - Sanfoundry
by Manish

This section on online C++ test focuses on “Character Types”. One shall practice these test
questions to improve their C++ programming skills needed for various interviews (campus
interviews, walkin interviews, company interviews), placements, entrance exams and other
competitive exams. These questions can be attempted by anyone focusing on learning C++
programming language. They can be a beginner, fresher, engineering graduate or an experienced
IT professional. Our online C++ test questions come with detailed explanation of the answers
which helps in better understanding of C++ concepts.

Here is a listing of online C++ test questions on “Character Types” along with answers,
explanations and/or solutions:

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


a) 64
b) 128
c) 256
d) none of the mentioned
View Answer

Answer: b
Explanation: None.

2. Select the right option.


Given the variables p, q are of char type and r, s, t are of int type
1. t = (r * s) / (r + s);
2. t = (p * q) / (r + s);
a) 1 is true but 2 is false
b) 1 is false and 2 is true
c) both 1 and 2 are true
d) both 1 and 2 are false
View Answer

Answer: c
Explanation: Every character constant has an integer value. Also char belongs to the integral type
hence arithmetic and logical operations can be performed on them.

3. Which of the following belongs to the set of character types?


a) char
b) wchar_t
c) only a
d) both wchar_t and char
View Answer
Answer: d
Explanation: wchar_t and char is used to represent wide character and character.

4. What will be the output of this program?

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. char c = 74;
6. cout << c;
7. return 0;
8. }

a) A
b) N
c) J
d) I
View Answer

Answer: c
Explanation: The literal value for 74 is J. So it will be printing J.

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


a) L’a’
b) l’a’
c) L[a].
d) la
View Answer

Answer: a
Explanation: A wide character is always indicated by immediately preceding the character literal
by an L.

6. What is the output of this program?

1. #include <stdio.h>
2. int main()
3. {
4. char a = '\012';
5.
6. printf("%d", a);
7. return 0;
8. }

a) Compiler error
b) 12
c) 10
d) Empty
View Answer

Answer: c
Explanation: The value ‘\012’ means the character with value 12 in octal, which is decimal 10.

7. In C++, what is the sign of character data type by default?


a) Signed
b) Unsigned
c) Implementation dependent
d) None of the mentioned
View Answer

Answer: c
Explanation: The standard does not specify if plain char is signed or unsigned. There are three
distinct character types according to the standard: char, signed char and unsigned char.

8. Is the size of character literals different in C and C++?


a) Implementation defined
b) Can’t say
c) Yes, they are different
d) No, they are not different
View Answer

Answer: c
Explanation: In C++, sizeof(‘a’) == sizeof(char) == 1. In C however, sizeof(‘a’) == sizeof(int).

9. Suppose in a hypothetical machine, the size of char is 32 bits. What would sizeof(char) return?
a) 4
b) 1
c) Implementation dependent
d) Machine dependent
View Answer

Answer: b
Explanation: The standard does NOT require a char to be 8-bits, but does require that
sizeof(char) return 1.

10. What constant defined in <climits> header returns the number of bits in a char?
a) CHAR_SIZE
b) SIZE_CHAR
c) BIT_CHAR
d) CHAR_BIT
View Answer
Answer: d
Explanation: None.

Interview Questions Answers C++ - Integer Types - Sanfoundry


by Manish

This section on C++ interview questions and answers focuses on “Integer Types”. One shall
practice these interview questions to improve their C++ programming skills needed for various
interviews (campus interviews, walkin interviews, company interviews), placements, entrance
exams and other competitive exams. These questions can be attempted by anyone focusing on
learning C++ programming language. They can be a beginner, fresher, engineering graduate or
an experienced IT professional. Our C++ interview questions come with detailed explanation of
the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ interview questions on “Integer Types” along with answers, explanations
and/or solutions:

1. The size_t integer type in C++ is?


a) Unsigned integer of at least 64 bits
b) Signed integer of at least 16 bits
c) Unsigned integer of at least 16 bits
d) Signed integer of at least 64 bits
View Answer

Answer: c
Explanation: The size_t type is used to represent the size of an object. Hence, it’s always
unsigned. According to the language specification, it is at least 16 bits.

2. What is the output of the following program?

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int x = -1;
6. unsigned int y = 2;
7.
8. if(x > y)
9. {
10. cout << "x is greater";
11. }
12. else
13. {
14. cout << "y is greater";
15. }
16. }
a) x is greater
b) y is greater
c) implementation defined
d) arbitrary
View Answer

Answer: a
Explanation: x is promoted to unsigned int on comparison. On conversion x has all bits set,
making it the bigger one.

3. Which of these expressions will return true if the input integer v is a power of two?
a) (v | (v + 1)) == 0;
b) (~v & (v – 1)) == 0;
c) (v | (v – 1)) == 0;
d) (v & (v – 1)) == 0;
View Answer

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

4. What is the value of the following 8-bit integer after all statements are executed?

1. int x = 1;
2. x = x &lt;&lt; 7;
3. x = x &gt;&gt; 7;

a) 1
b) -1
c) 127
d) Implementation defined
View Answer

Answer: d
Explanation: Right shift of signed integers is undefined, and has implementation-defined
behaviour.

5. Which of these expressions will make the rightmost set bit zero in an input integer x?
a) x = x | (x-1)
b) x = x & (x-1)
c) x = x | (x+1)
d) x = x & (x+1)
View Answer

Answer: b
Explanation: None.
6. Which of these expressions will isolate the rightmost set bit?
a) x = x & (~x)
b) x = x ^ (~x)
c) x = x & (-x)
d) x = x ^ (-x)
View Answer

Answer: c
Explanation: None.

7. 0946, 786427373824, ‘x’ and 0X2f are _____ _____ ____ and _____ literals respectively.
a) decimal, character,octal, hexadecimal
b) octal, hexadecimal, character, decimal
c) hexadecimal, octal, decimal, character
d) octal, decimal, character, hexadecimal
View Answer

Answer: d
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 are written within ”.

8. What will be the output of this program?

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a = 8;
6. cout << "ANDing integer 'a' with 'true' :" << a && true;
7. return 0;
8. }

a) ANDing integer ‘a’ with ‘true’ :8


b) ANDing integer ‘a’ with ‘true’ :0
c) ANDing integer ‘a’ with ‘true’ :1
d) None of the mentioned
View Answer

Answer: a
Explanation: None.

9. What will be output of this program?

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int i = 3;
6. int l = i / -2;
7. int k = i % -2;
8. cout << l << k;
9. return 0;
10. }

a) compile time error


b) -1 1
c) 1 -1
d) implementation defined
View Answer

Answer: b
Explanation: Sign of result of mod operation on negative numbers is sign of the dividend.

10. What will be output of this function?

1. int main()
2. {
3. register int i = 1;
4. int *ptr = &i;
5. cout << *ptr;
6. return 0;
7. }

a) 0
b) 1
c) Compiler error may be possible
d) Runtime error may be possible
View Answer

Answer: c
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.

C++ Programming Questions and Answers – Floating Point Types

This section on advanced C++ programming questions focuses on “Floating Point Types”. One shall
practice these advanced C++ questions to improve their C++ programming skills needed for various
interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams
and other competitive exams. These questions can be attempted by anyone focusing on learning
C++ programming language. They can be a beginner, fresher, engineering graduate or an
experienced IT professional. Our advanced C++ questions come with detailed explanation of the
answers which helps in better understanding of C++ concepts.

Here is a listing of advanced C++ programming questions on “Floating Point Types” along with
answers, explanations and/or solutions:

1. Which of the following is not one of the sizes of the floating point types?
a) short float
b) float
c) long double
d) double
View Answer

Answer: a
Explanation: Floating point types occur in only three sizes-float, long double and double.

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


a) f287.333
b) F287.333
c) 287.e2
d) 287.3.e2
View Answer

Answer: c
Explanation: To make a floating point literal, we should attach a suffix of ‘f’ or ‘F’ and there should
not be any blank space.

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


a) -3.4E+38 to +3.4E+38
b) -3.4E+38 to +3.4E+34
c) -3.4E+38 to +3.4E+36
d) -3.4E+38 to +3.4E+32
View Answer

Answer: a
Explanation: None.

4. Which of three sizes of floating point types should be used when extended precision is required?
a) float
b) double
c) long double
d) extended float
View Answer

Answer: c
Explanation: Float for single precision, double for double precision and long double for extended
precision.

5. What is the output of this program?

#include <iostream>

using namespace std;

int main()

float num1 = 1.1;


double num2 = 1.1;

if (num1 == num2)

cout << "stanford";

else

cout << "harvard";

return 0;

a) harvard
b) stanford
c) compile time error
d) runtime error
View Answer

Answer: a
Explanation: Float store floating point numbers with 8 place accuracy and requires 4 bytes of
Memory. Double has 16 place accuracy having size of 8 bytes.
Output:
$ g++ float3.cpp
$ a.out
harvard

6. What is the output of this program?

#include <iomanip>

#include <iostream>

using namespace std;

int main()

cout << setprecision(17);

double d = 0.1;

cout << d << endl;

return 0;

a) 0.11
b) 0.10000000000000001
c) 0.100001
d) compile time error
View Answer

Answer: b
Explantion: The double had to truncate the approximation due to it’s limited memory, which resulted
in a number that is not exactly 0.1.
Output:
$ g++ float2.out
$ a.out
0.10000000000000001

7. What is the output of the following program?

#include <iostream>

using namespace std;

int main()

float i = 123.0f;

cout << i << endl;

return 0;

a) 123.00
b) 1.23
c) 123
d) compile time error
View Answer

Answer: c
Explanation: The value 123 is printed because of its precision.
$ g++ float.cpp
$ a.out
123

8. Which is used to indicate single precision value?


a) F or f
b) L or l
c) Either F or f or L or l
d) Neither F or f or L or l
View Answer

Answer: a
Explanation: None.

9. What is the output of this program?


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

a) equal
b) not equal
c) compile time error
d) runtime error
View Answer

Answer: a
Explanation: 0.5f results in 0.5 to be stored in floating point representations.
Output:
$ g++ float.cpp
$ a.out
equal

10. Which is correct with respect to size of the datatypes?


a) char > int < float
b) int < char > float
c) char < int < float
d) char < int < double
View Answer

Answer: d
Explanation: The char has lesser bytes than int and int has lesser bytes than double whereas int and
float can potentially have same sizes.

C++ Interview Questions Answers - Sizes - Sanfoundry


by Manish
This section on C++ interview questions and answers focuses on “Sizes”. One shall practice
these interview questions to improve their C++ programming skills needed for various
interviews (campus interviews, walkin interviews, company interviews), placements, entrance
exams and other competitive exams. These questions can be attempted by anyone focusing on
learning C++ programming language. They can be a beginner, fresher, engineering graduate or
an experienced IT professional. Our C++ interview questions come with detailed explanation of
the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ interview questions on “Sizes” along with answers, explanations and/or
solutions:

1. The size of an object or a type can be determined using which operator?


a) malloc
b) sizeof
c) malloc
d) calloc
View Answer

Answer: b
Explanation: The sizeof operator gives the size of the object or type.

2. It is guaranteed that a ____ has atleast 8bits and a ____ has atleast 16 bits.
a) int, float
b) char, int
c) bool, char
d) char, short
View Answer

Answer: d
Explanation: None.

3. Implementation dependent aspects about an implementation can be found in ____


a) <implementation>
b) <limits>
c) <limit>
d) <numeric>
View Answer

Answer: b
Explanation: The limit header holds the details of the machine dependent details.

4. Size of C++ objects are expressed in terms of multiples of the size of a ____ and the size of a
char is _______
a) char, 1
b) int, 1
c) float, 8
d) char, 4
View Answer

Answer: a
Explanation: None.

5. Identify the incorrect option.


a) 1 <= sizeof(bool) <= sizeof(long)
b) sizeof(float) <= sizeof(double) <= sizeof(long double)
c) sizeof(char) <= sizeof(long) <=sizeof(wchar_t)
d) sizeof(N) = sizeof(signed N) = sizeof(unsigned N)
View Answer

Answer: c
Explanation: sizeof(char) <= sizeof(wchar_t) <= sizeof(long).

6. What is the output of the following program?

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int num = 0x20 + 020 + 20;
6. cout << sizeof(num)<<'\n';
7. return 0;
8. }

a) 2
b) 4
c) Depends on compiler
d) Garbage
View Answer

Answer: c
Explanation: The sum of three numbers are belongs to different number systems, so the result is
typecasted into integer.
Output:
$ g++ size.cpp
$ a.out
4

7. What is the output of the following program?

1. #include <iostream>
2. using namespace std;
3. int main ( )
4. {
5. static double i;
6. i = 20;
7. cout << sizeof(i);
8. return 0;
9. }

a) 4
b) 2
c) 8
d) garbage
View Answer

Answer: c
Explanation: The size of the double data type is 8.
$ g++ size1.cpp
$ a.out
8

8. What is the output of the following program?

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int num1 = 10;
6. float num2 = 20;
7. cout << sizeof(num1 + num2);
8. return 0;
9. }

a) 2
b) 4
c) 8
d) garbage
View Answer

Answer: b
Explanation: In this program, integer is converted into float. Therefore the result of num1 and
num2 is float. And it is returning the size of the float.
Output:
$ g++ size2.cpp
$ a.out
4

9. What is the output of the following program?

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a = 5;
6. float b;
7. cout << sizeof(++a + b);
8. cout << a;
9. return 0;
10. }

a) 2 6
b) 4 6
c) 2 5
d) 4 5
View Answer

Answer: d
Explanation: The a as a integer will be converted to float while calculating the size. The value of
any variable doesn’t modify inside sizeof operator. Hence value of variable a will remain 5.
Output:
$ g++ size3.cpp
$ a.out
45

10. What would be the output of the following program (in 32-bit systems)?

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. cout << sizeof(char);
6. cout << sizeof(int);
7. cout << sizeof(float);
8. return 0;
9. }

a) 1 4 4
b) 1 4 8
c) 1 8 8
d) none of the mentioned
View Answer

Answer: a
Explanation: Character is 1 byte, integer 4 bytes and float 4 bytes.

C++ Programming Questions and Answers – Void

This section on C++ language interview questions and answers focuses on “Void”. One shall
practice these interview questions to improve their C++ programming skills needed for various
interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams
and other competitive exams. These questions can be attempted by anyone focusing on learning
C++ programming language. They can be a beginner, fresher, engineering graduate or an
experienced IT professional. Our C++ language interview questions come with detailed explanation
of the answers which helps in better understanding of C++ concepts.
Here is a listing of C++ language interview questions on “Void” along with answers, explanations
and/or solutions:

1. Which of the following will not return a value?


a) null
b) void
c) empty
d) free
View Answer

Answer: b
Explanation: None.

2. ____ have the return type void?


a) all functions
b) constructors
c) destructors
d) none of the mentioned
View Answer

Answer: d
Explanation: Constructor creats an Object and Destructor destroys the object. They are not
supposed to return anything, not even void.

3. What does the following statement mean?


void a;
a) variable a is of type void
b) a is an object of type void
c) declares a variable with value a
d) flags an error
View Answer

Answer: d
Explanation: There are no void objects.

4. Choose the incorrect option


a) void is used when the function does not return a value
b) void is also used when the value of a pointer is null
c) void is used as the base type for pointers to objects of unknown type
d) void is a special fundamental type
View Answer

Answer: b
Explanation: void fundamental type is used in the cases of a and c.

5. What is the output of this program?

#include <iostream>

using namespace std;


int main()

void a = 10, b = 10;

int c;

c = a + b;

cout << c;

return 0;

a) 20
b) compile time error
c) runtime error
d) none of the mentioned
View Answer

Answer: b
Explanation: void will not accept any values to its type.

C++ Programming Questions and Answers – Enumerations

This section on online C++ test focuses on “Enumerations”. One shall practice these test questions
to improve their C++ programming skills needed for various interviews (campus interviews, walkin
interviews, company interviews), placements, entrance exams and other competitive exams. These
questions can be attempted by anyone focusing on learning C++ programming language. They can
be a beginner, fresher, engineering graduate or an experienced IT professional. Our C online test
questions come with detailed explanation of the answers which helps in better understanding of C++
concepts.

Here is a listing of online C++ test questions on “Enumerations” along with answers, explanations
and/or solutions:

1. Identify the incorrect option.


a) enumerators are constants
b) enumerators are user defined types
c) enumerators are same as macros
d) enumerator values start from 0 by default
View Answer

Answer: c
Explanation: Enumerators are used in order to create our own types whereas macros are textual
substitutions.

2. In which type does the enumerators are stored by the compiler?


a) string
b) integer
c) float
d) none of the mentioned
View Answer

Answer: b
Explanation: None.

3. To which of these enumerators can be assigned?


a) integer
b) negative
c) enumerator
d) all of the mentioned
View Answer

Answer: d
Explanation: Since enumerators evaluate to integers, and integers can be assigned to enumerators,
enumerators can be assigned to other enumerators.

4. What will happen when defining the enumerated type?


a) it will not allocate memory
b) it will allocate memory
c) it will not allocate memory to its variables
d) none of the mentioned
View Answer

Answer: a
Explanation: Enumerator will allocate the memory when its variables are defined.

5. Which variable does equals in size with enum variable?


a) int variable
b) float variable
c) string variable
d) none of the mentioned
View Answer

Answer: a
Explanation: The enum variable are converted to integer and stored by compiler. So both are equal
in size.

6. What is the output of this program?

#include <iostream>

using namespace std;

enum cat

temp = 7
};

int main()

int age = 14;

age /= temp;

cout << "If you were cat, you would be " << age << endl;

return 0;

a) If you were cat, you would be 5


b) If you were cat, you would be 2
c) If you were cat, you would be 7
d) None of the mentioned
View Answer

Answer: b
Explanation: The age will be divided by using compound assignment operator and so it will return
the age of the cat according to your age.
$ g++ enum1.cpp
$ a.out
If you were cat, you would be 2

7. What is the output of this program?

#include <iostream>

using namespace std;

enum test

A = 32, B, C

};

int main()

cout << A << B<< C;

return 0;

a) 323334
b) 323232
c) 323130
d) none of the mentioned
View Answer

Answer: a
Explanation: If we not assigned any value to enum variable means, then the next number to
initialized number will be allocated to the variable.
Output:
$ g++ enum2.cpp
$ a.out
323334

8. What is the output of this program?

#include <iostream>

using namespace std;

enum colour {

green, red, blue, white, yellow, pink

};

int main()

cout << green<< red<< blue<< white<< yellow<< pink;

return 0;

a) 012345
b) 123456
c) compile time error
d) runtime error
View Answer

Answer: a
Explanation: The enumerator values start from zero if it is unassigned.
Output:
$ g++ enum3.cpp
$ a.out
012345

9. What is the output of this program?

#include <iostream>

using namespace std;


int main()

enum channel {star, sony, zee};

enum symbol {hash, star};

int i = 0;

for (i = star; i <= zee; i++) {

printf("%d ", i);

return 0;

a) 012
b) 123
c) compile time error
d) runtime error
View Answer

Answer: c
Explanation: Enumartion variable ‘star’ appears two times in main() which causes the error. An
enumaration constant must be unique within the scope.

10. What is output of the this program?

#include <iostream>

using namespace std;

int main()

int i;

enum month {

JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC

};

for (i = MAR; i <= NOV; i++)

cout << i;

return 0;

}
a) 01234567891011
b) 123456789101112
c) 34567891011
d) 123456789
View Answer

Answer: c
Explanation: We are getting the values from march to november and printing its concern number.

Interview Questions Answers C++ - Declaration - Sanfoundry


by Manish

This section on C++ interview questions and answers focuses on “Declaration”. One shall
practice these interview questions to improve their C++ programming skills needed for various
interviews (campus interviews, walkin interviews, company interviews), placements, entrance
exams and other competitive exams. These questions can be attempted by anyone focusing on
learning C++ programming language. They can be a beginner, fresher, engineering graduate or
an experienced IT professional. Our C++ interview questions come with detailed explanation of
the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ interview questions on “Declaration” along with answers, explanations
and/or solutions:

1. Choose the correct option.


extern int i;
int i;
a) both 1 and 2 declare i
b) 1 declares the variable i and 2 defines i
c) 1 declares and defines i, 2 declares i
d) 1 declares i,2 declares and defines i
View Answer

Answer: d
Explanation: The keyword extern is not a definition and is not allocated storage until it is
initialized.

2. Pick the right option


Statement 1:A definition is also a declaration.
Statement 2:An identifier can be declared just once.
a) Statement 1 is true, Statement 2 is false
b) Statement 2 is true, Statement 1 is false
c) Both are false
d) Both are true
View Answer
Answer: b
Explanation: An identifier can be declared many times must be defined just once.

3. Which of the given statements are false.


1. extern int func;
2. extern int func2(int,int);
3. int func2(int,int);
4. extern class foo;
a) 3 and 4 only
b) 2 and 3 only
c) only 4
d) 2, 3 and 4
View Answer

Answer: c
Explanation: No extern are allowed for class declarations.

4. Pick the right option


Statement 1:Global values are not initialized by the stream.
Statement 2:Local values are implicitly initialised to 0.
a) Statement 1 is true, Statement 2 is false
b) Statement 2 is true, Statement 1 is false
c) Both are false
d) Both are true
View Answer

Answer: c
Explanation: Global values are implicitly initialised to 0, but local values have to be initialised
by the system.

5. What is the output of this program?

1. #include <iostream>
2. using namespace std;
3. int g = 100;
4. int main()
5. {
6. int a;
7. {
8. int b;
9. b = 20;
10. a = 35;
11. g = 65;
12. cout << b << a << g;
13. }
14. a = 50;
15. cout << a << g;
16. return 0;
17. }
a) 2035655065
b) 2035655035
c) 2035635065
d) none of the mentioned
View Answer

Answer: a
Explanation: The local values of a and g within the block are more dominant than the global
values.
Output:
$ g++ dec1.cpp
$ a.out
2035655065

6. Can two functions declare variables(non static) with the same name.
a) No
b) Yes
c) Yes, but not a very efficient way to write programs
d) No, it gives a runtime error
View Answer

Answer: c
Explanation: We can declare variables with the same name in two functions because their scope
lies within the function.

7. What is the output of this program?

1. #include <iostream>
2. using namespace std;
3. void addprint()
4. {
5. static int s = 1;
6. s++;
7. cout << s;
8. }
9. int main()
10. {
11. addprint();
12. addprint();
13. addprint();
14. return 0;
15. }

a) 234
b) 111
c) 123
d) 235
View Answer
Answer: a

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

Output:

8. What is the output of this program?

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a = 10;
6. if (a < 10) {
7. for (i = 0; i < 10; i++)
8. cout << i;
9. }
10. else {
11. cout << i;
12. }
13. return 0;
14. }

a) 0123456789
b) 123456789
c) 0
d) error
View Answer

Answer: d
Explanation: We will get compilation error because ‘i’ is an undeclared identifier.

9. Identify the incorrect statements.


int var = 10;
int *ptr = &(var + 1); //statement 1
int *ptr2 = &var; //statement 2
&var = 40; //statement 3
a) Statement 1 and 2 are wrong
b) Statement 2 and 3 are wrong
c) Statement 1 and 3 are wrong
d) All the three are wrong
View Answer

Answer: c
Explanation: In statement 1 lvalue is required as unary ‘&’ operand and in statement 3 lvalue is
required as left operand.

10. Identify the type of the variables.


typedef char* CHAR;
CHAR p,q;
a) char*
b) char
c) CHAR
d) unknown
View Answer

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

C++ Aptitude Question Answer - Pointers - Sanfoundry


by Manish

This section on C++ aptitude questions and answers focuses on “Pointers”. One shall practice
these aptitude questions to improve their C++ programming skills needed for various interviews
(campus interviews, walkin interviews, company interviews), placements, entrance exams,
aptitude tests and other competitive exams. These questions can be attempted by anyone
focusing on learning C++ programming language. They can be a beginner, fresher, engineering
graduate or an experienced IT professional. Our C++ questions come with detailed explanation
of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ aptitude questions on “Pointers” along with answers, explanations and/or
solutions:

1. What does the following statement mean?


int (*fp)(char*)
a) pointer to a pointer
b) pointer to an array of chars
c) pointer to function taking a char* argument and returns an int
d) function taking a char* argument and returning a pointer to int
View Answer

Answer: c
Explanation: None.

2. The operator used for dereferencing or indirection is ____


a) *
b) &
c) ->
d) –>>
View Answer

Answer: a
Explanation: None.
3. Choose the right option
string* x, y;
a) x is a pointer to a string, y is a string
b) y is a pointer to a string, x is a string
c) both x and y are pointer to string types
d) none of the mentioned
View Answer

Answer: a
Explanation: * is to be grouped with the variables not the data types.

4. Which one of the following is not a possible state for a pointer.


a) hold the address of the specific object
b) point one past the end of an object
c) zero
d) point to a tye
View Answer

Answer: d
Explanation: A pointer can be in only 3 states a,b and c.

5. Which of the following is illegal?


a) int *ip;
b) string s, *sp = 0;
c) int i; double* dp = &i;
d) int *pi = 0;
View Answer

Answer: c
Explanation: dp is initialized int value of i.

6. What will happen in this code?

1. &nbsp;&nbsp;&nbsp;&nbsp;int a = 100, b = 200;


2. &nbsp;&nbsp;&nbsp;&nbsp;int *p = &a, *q = &b;
3. &nbsp;&nbsp;&nbsp;&nbsp;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
View Answer

Answer: b
Explanation: Assigning to refrence changes the object to which the refrence is bound.

7. What is the output of this program?


1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a = 5, b = 10, c = 15;
6. int *arr[ ] = {&a, &b, &c};
7. cout << arr[1];
8. return 0;
9. }

a) 5
b) 10
c) 15
d) it will return some random number
View Answer

Answer: d
Explanation: Array element cannot be address of auto variable. It can be address of static or
extern variables.

8. The correct statement for a function that takes pointer to a float, a pointer to a pointer to a char
and returns a pointer to a pointer to a integer is
a) int **fun(float**, char**)
b) int *fun(float*, char*)
c) int ***fun(float*, char**)
d) int ***fun(*float, **char)
View Answer

Answer: c
Explanation: None.

9. What is the output of this program?

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. char arr[20];
6. int i;
7. for(i = 0; i < 10; i++)
8. *(arr + i) = 65 + i;
9. *(arr + i) = '\0';
10. cout << arr;
11. return(0);
12. }

a) ABCDEFGHIJ
b) AAAAAAAAAA
c) JJJJJJJJ
d) None of the mentioned
View Answer

Answer: a
Explanation: Each time we are assigning 65 + i. In first iteration i = 0 and 65 is assigned. So it
will print from A to J.
$ g++ point1.cpp
$ a.out
ABCDEFGHIJ

10. What is the output of this program?

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. char *ptr;
6. char Str[] = "abcdefg";
7. ptr = Str;
8. ptr += 5;
9. cout << ptr;
10. return 0;
11. }

a) fg
b) cdef
c) defg
d) abcd
View Answer

Answer: a
Explanation: Pointer ptr points to string ‘fg’. So it prints fg.
Output:
$ g++ point.cpp
$ a.out
fg

C++ Programming Questions and Answers – Arrays

This section on C++ interview questions and answers focuses on “Arrays”. One shall practice these
interview questions to improve their C++ programming skills needed for various interviews (campus
interviews, walkin interviews, company interviews), placements, entrance exams and other
competitive exams. These questions can be attempted by anyone focusing on learning C++
programming language. They can be a beginner, fresher, engineering graduate or an experienced IT
professional. Our C++ interview questions come with detailed explanation of the answers which
helps in better understanding of C++ concepts.

Here is a listing of C++ interview questions on “Arrays” along with answers, explanations and/or
solutions:
1. Which of the following correctly declares an array?
a) int array[10];
b) int array;
c) array{10};
d) array array[10];
View Answer

Answer: a
Explanation: Because array variable and values need to be declared after the datatype only.

2. What is the index number of the last element of an array with 9 elements?
a) 9
b) 8
c) 0
d) Programmer-defined
View Answer

Answer: b
Explanation: Because the first element always starts at 0. So it is on 8 position.

3. 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
View Answer

Answer: a
Explanation: None.

4. Which of the following accesses the seventh element stored in array?


a) array[6];
b) array[7];
c) array(7);
d) array;
View Answer

Answer: a
Explanation: The array location starts from zero, So it can accessed by array[6].

5. Which of the following gives the memory address of the first element in array?
a) array[0];
b) array[1];
c) array(2);
d) array;
View Answer

Answer: d
Explanation: None.
6. 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
View Answer

Answer: b
Explanation: In this program we are adding the every element of two arrays. Finally we got output as
6533.
Output:
$ g++ array.cpp
$ a.out
6533

7. 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) None of the mentioned
View Answer

Answer: d
Explanation: We are adding all the elements in the array and printing it. Total elements in the array is
7, but our for loop will go beyond 7 and add a garbage value.

8. What is the output of this program?

#include <stdio.h>

using namespace std;

int main()

int a = 5, b = 10, c = 15;

int arr[3] = {&a, &b, &c};

cout << *arr[*arr[1] - 8];

return 0;

a) 15
b) 18
c) garbage value
d) compile time error
View Answer

Answer: d
Explanation: The conversion is invalid in this array. So it will arise error. The following compilation
error will be raised:
cannot convert from ‘int *’ to ‘int’

9. What is the output of this program?

#include <stdio.h>

using namespace std;

int main()

char str[5] = "ABC";

cout << str[3];

cout << str;

return 0;

a) ABC
b) ABCD
c) AB
d) None of the mentioned
View Answer

Answer: a
Explanation: We are just printing the values of first 3 values.
$ g++ array.cpp
$ a.out
ABC

10. What is the output of this program?

#include <stdio.h>

using namespace std;

int main()

int array[] = {10, 20, 30};

cout << -2[array];


return 0;

a) -15
b) -30
c) compile time error
d) garbage value
View Answer

Answer: b
Explanation: It’s just printing the negative value of the concern element.
$ g++ array.cpp
$ a.out
-30

C++ Programming Questions and Answers – Pointers into Arrays

This section on C++ language interview questions and answers focuses on “Pointers into Arrays”.
One shall practice these interview questions to improve their C++ programming skills needed for
various interviews (campus interviews, walkin interviews, company interviews), placements,
entrance exams and other competitive exams. These questions can be attempted by anyone
focusing on learning C++ programming language. They can be a beginner, fresher, engineering
graduate or an experienced IT professional. Our C++ language interview questions come with
detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ language interview questions “Pointers into Arrays” along with answers,
explanations and/or solutions:

1. What is meaning of following declaration?


int(*p[5])();
a) p is pointer to function
b) p is array of pointer to function
c) p is pointer to such function which return type is array
d) p is pointer to array of function
View Answer

Answer: b
Explanation: In the above declaration the variable p is array not pointer.

2. What is size of generic pointer in C++ (in 32-bit platform) ?


a) 2
b) 4
c) 8
d) 0
View Answer

Answer: b
Explanation: Size of any type of pointer is 4 bytes in 32-bit platforms.
3. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int a[2][4] = {3, 6, 9, 12, 15, 18, 21, 24};

cout << *(a[1] + 2) << *(*(a + 1) + 2) << 2[1[a]];

return 0;

a) 15 18 21
b) 21 21 21
c) 24 24 24
d) Compile time error
View Answer

Answer: b
Explanation: a[1][2] means 1 * (4)+2 = 6th element of an array staring from zero.
Output:
$ g++ point.cpp
$ a.out
21 21 21

4. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int i;

char *arr[] = {"C", "C++", "Java", "VBA"};

char *(*ptr)[4] = &arr;

cout << ++(*ptr)[2];

return 0;

a) ava
b) java
c) c++
d) compile time error
View Answer

Answer: a
Explanation: In this program we are moving the pointer from first position to second position and
printing the remaining value.
Output:
$ g++ point1.cpp
$ a.out
ava

5. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int arr[] = {4, 5, 6, 7};

int *p = (arr + 1);

cout << *p;

return 0;

a) 4
b) 5
c) 6
d) 7
View Answer

Answer: b
Explanation: In this program, we are making the pointer point to next value and printing it.
$ g++ point3.cpp
$ a.out
5

6. What is the output of this program?

#include <iostream>

using namespace std;

int main()

{
int arr[] = {4, 5, 6, 7};

int *p = (arr + 1);

cout << arr;

return 0;

a) 4
b) 5
c) address of arr
d) 7
View Answer

Answer: c
Explanation: As we couted to print only arr, it will print the address of the array.
Output:
$ g++ point2.cpp
$ a.out
0xbfb1cff

7. What is the output of this program?

#include <iostream>

using namespace std;

int main ()

int numbers[5];

int * p;

p = numbers; *p = 10;

p++; *p = 20;

p = &numbers[2]; *p = 30;

p = numbers + 3; *p = 40;

p = numbers; *(p + 4) = 50;

for (int n = 0; n < 5; n++)

cout << numbers[n] << ",";

return 0;

}
a) 10,20,30,40,50,
b) 1020304050
c) compile error
d) runtime error
View Answer

Answer: a
Explanation: In this program, we are just assigning a value to the array and printing it and
immediately dereferencing it.
Output:
$ g++ point4.cpp
$ a.out
10,20,30,40,50,

8. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int arr[] = {4, 5, 6, 7};

int *p = (arr + 1);

cout << *arr + 9;

return 0;

a) 12
b) 5
c) 13
d) error
View Answer

Answer: c
Explanation: In this program, we are adding the value 9 to the initial value of the array, So it’s
printing as 13.
Output:
$ g++ point5.cpp
$ a.out
13

Online C++ Test - Constants - Sanfoundry


by Manish
This section on online C++ test focuses on “Constants”. One shall practice these test questions to
improve their C++ programming skills needed for various interviews (campus interviews, walkin
interviews, company interviews), placements, entrance exams and other competitive exams.
These questions can be attempted by anyone focusing on learning C++ programming language.
They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our
online C++ test questions come with detailed explanation of the answers which helps in better
understanding of C++ concepts.

Here is a listing of online C++ test questions on “Constants” along with answers, explanations
and/or solutions:

1. The constants are also called as


a) const
b) preprocessor
c) literals
d) none of the mentioned
View Answer

Answer: c
EXplanation: None.

2. What are the parts of the literal constants?


a) integer numerals
b) floating-point numerals
c) strings and boolean values
d) all of the mentioned
View Answer

Answer: d
Explanation: Because these are the types used to declare variables and so these can be declared
as constants.

3. How the constants are declared?


a) const keyword
b) #define preprocessor
c) both const keyword and #define preprocessor
d) none of the mentioned
View Answer

Answer: c
Explanation: The const will declare with a specific type values and #define is used to declare
user definied constants.

4. What is the output of this program?

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int const p = 5;
6. cout << ++p;
7. return 0;
8. }

a) 5
b) 6
c) Error
d) None of the mentioned
View Answer

Answer: c
Explanation: We cannot modify a constant integer value.

5. What is the output of this program?

1. #include <iostream>
2. using namespace std;
3. #define PI 3.14159
4. int main ()
5. {
6. float r = 2;
7. float circle;
8. circle = 2 * PI * r;
9. cout << circle;
10. return 0;
11. }

a) 12.5664
b) 13.5664
c) 10
d) compile time error
View Answer

Answer: a
Explanation: In this program, we are finding the area of the circle by using concern formula.
Output:
$ g++ cons.cpp
$ a.out
12.5664

6. Which of the following statement is not true about preprocessor directives?


a) These are lines read and processed by the preprocessor
b) They do not produce any code by themselves
c) These must be written on their own line
d) They end with a semicolon
View Answer
Answer: d
Explanation: None.

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


const int a = 100;
a) Declares a variable a with 100 as its initial value
b) Declares a construction a with 100 as its initial value
c) Declares a constant a whose value will be 100
d) Constructs an integer type variable with a as identifier and 100 as value
View Answer

Answer: c
Explanation: Because the const is used to declare non-changable values only.

8. The difference between x and ‘x’ is


a) The first one refers to a variable whose identifier is x and the second one refers to the
character constant x
b) The first one is a character constant x and second one is the string literal x
c) Both are same
d) None of the mentioned
View Answer

Answer: a
Explanation: None.

9. How to declare a wide character in string literal?


a) L prefix
b) l prefix
c) W prefix
d) none of the mentioned
View Answer

Answer: a
Explanation: It can turn this as wide character instead of narrow characters.

C++ Interview Questions Experienced - References - Sanfoundry


by Manish

This section on C++ interview questions and answers focuses on “References”. One shall
practice these interview questions to improve their C++ programming skills needed for various
interviews (campus interviews, walkin interviews, company interviews), placements, entrance
exams and other competitive exams. These questions can be attempted by anyone focusing on
learning C++ programming language. They can be a beginner, fresher, engineering graduate or
an experienced IT professional. Our C++ interview questions come with detailed explanation of
the answers which helps in better understanding of C++ concepts.
Here is a listing of C++ interview questions on “References” along with answers, explanations
and/or solutions:

1. Which value we cannot assign to reference?


a) integer
b) floating
c) unsigned
d) null
View Answer

Answer: d
Explanation: If it can be assigned with a null value means, it is a copy of pointer.

2. Identify the incorrect statement


a) Reference is the alternate name of the object
b) A reference value once defined can be reassigned
c) A reference value once defined cannot be reassigned
d) None of the mentioned
View Answer

Answer: b
Explanation: Reference is a thing which points to valid memory address, so it can’t be
redesigned.

3. Which reference modifier is used to define reference variable?


a) &
b) $
c) #
d) none of the mentioned
View Answer

Answer: a
Explanation: None.

4. What is the output of this program?

1. #include <iostream>
2. using namespace std;
3. void swap(int &a, int &b);
4. int main()
5. {
6. int a = 5, b = 10;
7. swap(a, b);
8. cout << "In main " << a << b;
9. return 0;
10. }
11. void swap(int &a, int &b)
12. {
13. int temp;
14. temp = a;
15. a = b;
16. b = temp;
17. cout << "In swap " << a << b;
18. }

a) In swap 105 In main 105


b) In swap 105 In main 510
c) In swap 510 In main 105
d) None of the mentioned
View Answer

Answer: a
Explanation: As we are calling by reference the values in the address also changed. So the main
and swap values also changed.
Output:
$ g++ ref.cpp
$ a.out
In swap 105 In main 105

5. What does a reference provide?


a) Alternate name for the class
b) Alternate name for the variable
c) Alternate name for the pointer
d) None of the mentioned
View Answer

Answer: b
Explanation: Because we are pointing memory address using temp variable.

6. What is the output of this program?

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a = 9;
6. int & aref = a;
7. a++;
8. cout << "The value of a is " << aref;
9. return 0;
10. }

a) 9
b) 10
c) error
d) 11
View Answer
Answer: b
Explanation: The value is declared and it is post incremented, so it’s value is 10.
$ g++ ref1.cpp
$ a.out
10

7. What is the output of this program?

1. #include <iostream>
2. using namespace std;
3. void print (char * a)
4. {
5. cout << a << endl;
6. }
7. int main ()
8. {
9. const char * a = "Hello world";
10. print(const_cast<char *> (a) );
11. return 0;
12. }

a) Hello world
b) Hello
c) world
d) compile time error
View Answer

Answer: a
Explanation: In this program we used the concept of constant casting to cast the variable and
printing it.
Output:
$ g++ ref2.cpp
$ a.out
Hello world

8. Identify the correct sentence regarding inequality between reference and pointer.
a) we can not create the array of reference
b) we can create the Array of reference
c) we can use reference to reference
d) none of the mentioned
View Answer

Answer: a
Explanation: None.

C++ Programming Questions and Answers – Pointer to Void

This section on C++ programming questions and answers focuses on “Pointer to Void”. One shall
practice these questions to improve their C++ programming skills needed for various interviews
(campus interviews, walkin interviews, company interviews), placements, entrance exams and other
competitive exams. These questions can be attempted by anyone focusing on learning C++
programming language. They can be a beginner, fresher, engineering graduate or an experienced IT
professional. Our C++ programming questions come with detailed explanation of the answers which
helps in better understanding of C++ concepts.

Here is a listing of C++ programming questions on “Pointer to Void” along with answers,
explanations and/or solutions:

1. Void pointer can point to which type of objects?


a) int
b) float
c) double
d) all of the mentioned
View Answer

Answer: d
Explanation: Because it doesn’t know the type of object it is pointing to, So it can point to all objects.

2. When does the void pointer can be dereferenced?


a) when it doesn’t point to any value
b) when it cast to another type of object
c) using delete keyword
d) none of the mentioned
View Answer

Answer: b
Explanation: By casting the pointer to another data type, it can dereferenced from void pointer.

3. The pointer can point to any variable that is not declared with which of these?
a) const
b) volatile
c) both const & volatile
d) static
View Answer

Answer: c
Explanation: None.

4. A void pointer cannot point to which of these?


a) methods in c++
b) class member in c++
c) all of the mentioned
d) none of the mentioned
View Answer

Answer: d
Explanation: None.
5. What is the output of this program?

#include <iostream>

using namespace std;

int func(void *Ptr);

int main()

char *Str = "abcdefghij";

func(Str);

return 0;

int func(void *Ptr)

cout << Ptr;

return 0;

a) abcdefghij
b) address of string “abcdefghij”
c) compile time error
d) runtime error
View Answer

Answer: b
Explanation: Even though it is a void pointer, we gets the address.
Output:
$ g++ b.cpp
$ a.out
0x8048714

6. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int *p;
void *vp;

if (vp == p);

cout << "equal";

return 0;

a) equal
b) no output
c) compile error
d) runtime error
View Answer

Answer: a
Explanation: The void pointer is easily converted to any other type of pointer, so these are equal.
Output:
$ g++ poi4.cpp
$ a.out
equal

7. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int i;

char c;

void *data;

i = 2;

c = 'd';

data = &i;

cout << "the data points to the integer value" << data;

data = &c;

cout << "the data now points to the character" << data;

return 0;

}
a) 2d
b) two memory addresses
c) both of the mentioned
d) none of the mentioned
View Answer

Answer: b
Explanation: Because the data points to the address value of the variables only, So it is printing the
memory address of these two variable.
Output:
$ g++ poi2.cpp
$ a.out
the data points to the integer value0xbfc81824 the data now points to the character0xbfc8182f

8. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int n = 5;

void *p = &n;

int *pi = static_cast<int*>(p);

cout << *pi << endl;

return 0;

a) 5
b) 6
c) compile time error
d) runtime error
View Answer

Answer: a
Explanation: We just casted this from void to int, so it prints 5
Output:
$ g++ poi1.cpp
$ a.out
5

9. 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) none of the mentioned
View Answer

Answer: a
Explanation: In this program, we are just adding the two values and printing it.
Output:
$ g++ poi.cpp
$ a.out
8
0xbfef0378

10. What we can’t do on a void pointer?


a) pointer arithemetic
b) pointer functions
c) both of the mentioned
d) none of the mentioned
View Answer

Answer: a
Explanation: Because void pointer is used to cast the variables only, So pointer arithemetic can’t be
done in a void pointer.

C++ Programming Questions and Answers – Structures

This section on C++ interview questions and answers focuses on “Structures”. One shall practice
these interview questions to improve their C++ programming skills needed for various interviews
(campus interviews, walkin interviews, company interviews), placements, entrance exams and other
competitive exams. These questions can be attempted by anyone focusing on learning C++
programming language. They can be a beginner, fresher, engineering graduate or an experienced IT
professional. Our C++ interview questions come with detailed explanation of the answers which
helps in better understanding of C++ concepts.

Here is a listing of C++ interview questions on “Structures” along with answers, explanations and/or
solutions:

1. The data elements in structure are also known as what?


a) objects
b) members
c) datas
d) none of the mentioned
View Answer

Answer: b
Explanation: None.

2. What will be used when terminating a structure?


a) :
b) }
c) ;
d) ;;
View Answer

Answer: c
Explanation: While terminating a structure, a semi colon is used to end this up.

3. What will happen when the structure is declared?


a) it will not allocate any memory
b) it will allocate the memory
c) it will be declared and initialized
d) none of the mentioned
View Answer

Answer: a
Explanation: While the structure is declared, it will not be initialized, So it will not allocate any
memory.

4. The declaration of structure is also called as?


a) sructure creator
b) structure signifier
c) structure specifier
d) none of the mentioned
View Answer

Answer: c
Explanation: The structure declaration with open and close braces and with a semicolon is also
called structure specifier.
5. What is the output of this program?

#include <iostream>

#include <string.h>

using namespace std;

int main()

struct student

int num;

char name[25];

};

student stu;

stu.num = 123;

strcpy(stu.name, "John");

cout << stu.num << endl;

cout << stu.name << endl;

return 0;

a) 123
john
b) john
john
c) compile time error
d) none of the mentioned
View Answer

Answer: a
Explanation: We are coping the value john to the name and then we are printing the values that are
in the program.
Output:
$ g++ stu.cpp
$ a.out
123
john

6. What is the output of this program?


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

a) 19845
b) 20000
c) 15000
d) 19844
View Answer

Answer: a
Explanation: In this program, we are just converting the given hours and minutes into seconds.
Output:
$ g++ stu1.cpp
$ a.out
Total seconds:19845
7. What will be the output of this program?

#include <iostream>

using namespace std;

int main()

struct ShoeType

string style;

double price;

};

ShoeType shoe1, shoe2;

shoe1.style = "Adidas";

shoe1.price = 9.99;

cout << shoe1.style << " $ "<< shoe1.price;

shoe2 = shoe1;

shoe2.price = shoe2.price / 9;

cout << shoe2.style << " $ "<< shoe2.price;

return 0;

a) Adidas $ 9.99
Adidas $ 1.11
b) Adidas $ 9.99
Adidas $ 9.11
c) Adidas $ 9.99
Adidas $ 11.11
d) none of the mentioned
View Answer

Answer: a
Explanation: We copied the value of shoe1 into shoe2 and divide the shoe2 value by 9, So this is the
output.
Output:
$ g++ stu2.cpp
$ a.out
Adidas $ 9.99
Adidas $ 1.11

8. What is the output of this program?

#include <iostream>

using namespace std;

struct sec

int a;

char b;

};

int main()

struct sec s ={25,50};

struct sec *ps =(struct sec *)&s;

cout << ps->a << ps->b;

return 0;

a) 252
b) 253
c) 254
d) 262
View Answer

Answer: a
Explanation: In this program, We are dividing the values of a and b, printing it.
Output:
$ g++ stu5.cpp
$ a.out
252

9. Which of the following is a properly defined structure?


a) struct {int a;}
b) struct a_struct {int a;}
c) struct a_struct int a;
d) struct a_struct {int a;};
View Answer
Answer: d
Explanation: The a_struct is declared as structure name and its data element is a.

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


a) b->var;
b) b.var;
c) b-var;
d) b>var;
View Answer

Answer: a
Explanation: Because in a structure pointer, the data element is declared as above only.

C++ Programming Questions and Answers – Operators

This section on C++ language interview questions and answers focuses on “Operators”. One shall
practice these interview questions to improve their C++ programming skills needed for various
interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams
and other competitive exams. These questions can be attempted by anyone focusing on learning
C++ programming language. They can be a beginner, fresher, engineering graduate or an
experienced IT professional. Our C++ language interview questions come with detailed explanation
of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ language interview questions on “Operators” along with answers,
explanations and/or solutions:

1. Which operator is having right to left associativity in the following?


a) Array subscripting
b) Function call
c) Addition and subtraction
d) Type cast
View Answer

Answer: d
Explanation: None.

2. Which operator is having the highest precedence?


a) postfix
b) unary
c) shift
d) equality
View Answer

Answer: a
Explanation: The operator which is having highest precedence is postfix and lowest is equality.

3. What is this operator called ?: ?


a) conditional
b) relational
c) casting operator
d) none of the mentioned
View Answer

Answer: a
Explanation: In this operator, if the condition is true means, it will return the first operator, otherwise
second operator.

4. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int a;

a = 5 + 3 * 5;

cout << a;

return 0;

a) 35
b) 20
c) 25
d) 30
View Answer

Answer: b
Explanation: Because the * operator is having highest precedence, So it is executed first and then
the + operator will be executed.
Output:
$ g++ op1.cpp
$ a.out
20

5. What is the use of dynamic_cast operator?


a) it converts virtual base class to derived class
b) it converts virtual base object to derived objeccts
c) it will convert the operator based on precedence
d) none of the mentioned
View Answer

Answer: a
Explanation: Because the dynamic_cast operator is used to convert from base class to derived
class.
6. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int a = 5, b = 6, c, d;

c = a, b;

d = (a, b);

cout << c << ' ' << d;

return 0;

a) 5 6
b) 6 5
c) 6 7
d) none of the mentioned
View Answer

Answer: a
Explanation: It is a separtor here.In c,the value a is stored in c and in d the value b is stored in d
because of the bracket.
Output:
$ g++ op3.cpp
$ a.out
56

7. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int i, j;

j = 10;

i = (j++, j + 100, 999 + j);

cout << i;

return 0;
}

a) 1000
b) 11
c) 1010
d) 1001
View Answer

Answer: c
Explanation: j starts with the value 10. j is then incremented to 11. Next, j is added to 100. Finally, j
(still containing 11) is added to 999 which yields the result 1010.
Output:
$ g++ op2.cpp
$ a.out
1010

8. What is the output of this program?

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

a) 749736
b) 736749
c) 367497
d) none of the mentioned
View Answer

Answer: a
Explanation: Because of the precedence the pre-increment and post increment operator, we got the
output as 749736.
Output:
$ g++ op.cpp
$ a.out
749736

9. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int a = 5, b = 6, c;

c = (a > b) ? a : b;

cout << c;

return 0;

a) 6
b) 5
c) 4
d) 7
View Answer

Answer: a
Explanation: Here the condition is false on conditional operator, so the b value is assigned to c.
Output:
$ g++ op1.cpp
$ a.out
6

10. What is the output of this program?

#include <iostream>

using namespace std;

main()

double a = 21.09399;

float b = 10.20;

int c ,d;
c = (int) a;

d = (int) b;

cout << c <<' '<< d;

return 0;

a) 20 10
b) 10 21
c) 21 10
d) none of the mentioned
View Answer

Answer: c
Explanation: In this program, we are casting the operator to integer, So it is printing as 21 and 10.
Output:
$ g++ op5.cpp
$ a.out
21 10

You might also like