Questions On Types, Pointers, Arrays & Structures in C++
Questions On Types, Pointers, Arrays & Structures in C++
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:
Answer: d
Explanation: Compiler wants to make CPU as more efficient in accessing the next value.
Answer: a
Explanation: Array type is not the basic type and it is constructed using the basic type.
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.
Answer: b
Explanation: Logical values can be either true or false, so the boolean type is suited for it.
Answer: c
Explanation: They must be defined by the users before use unlike the other types which are
readily available.
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.
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++.
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.
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:
Answer: a
Explanation: C++ has bool as a fundamental data type.
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.
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.
Answer: c
Explanation: A pointer can be implicitly converted to a bool. A nonzero pointer converts to true
and zerovalued pointer converts to false.
Answer: b
Explanation: None.
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.
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.
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.
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:
Answer: b
Explanation: None.
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.
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.
Answer: a
Explanation: A wide character is always indicated by immediately preceding the character literal
by an L.
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.
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.
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.
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:
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.
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 << 7;
3. x = x >> 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 ”.
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. }
Answer: a
Explanation: None.
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. }
Answer: b
Explanation: Sign of result of mod operation on negative numbers is sign of the dividend.
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.
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.
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.
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.
#include <iostream>
int main()
if (num1 == num2)
else
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
#include <iomanip>
#include <iostream>
int main()
double d = 0.1;
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
#include <iostream>
int main()
float i = 123.0f;
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
Answer: a
Explanation: None.
int main()
float f1 = 0.5;
double f2 = 0.5;
if (f1 == 0.5f)
else
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
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.
Here is a listing of C++ interview questions on “Sizes” along with answers, explanations and/or
solutions:
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.
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.
Answer: c
Explanation: sizeof(char) <= sizeof(wchar_t) <= sizeof(long).
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
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
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
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.
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:
Answer: b
Explanation: None.
Answer: d
Explanation: Constructor creats an Object and Destructor destroys the object. They are not
supposed to return anything, not even void.
Answer: d
Explanation: There are no void objects.
Answer: b
Explanation: void fundamental type is used in the cases of a and c.
#include <iostream>
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.
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:
Answer: c
Explanation: Enumerators are used in order to create our own types whereas macros are textual
substitutions.
Answer: b
Explanation: None.
Answer: d
Explanation: Since enumerators evaluate to integers, and integers can be assigned to enumerators,
enumerators can be assigned to other enumerators.
Answer: a
Explanation: Enumerator will allocate the memory when its variables are defined.
Answer: a
Explanation: The enum variable are converted to integer and stored by compiler. So both are equal
in size.
#include <iostream>
enum cat
temp = 7
};
int main()
age /= temp;
cout << "If you were cat, you would be " << age << endl;
return 0;
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
#include <iostream>
enum test
A = 32, B, C
};
int main()
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
#include <iostream>
enum colour {
};
int main()
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
#include <iostream>
int i = 0;
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.
#include <iostream>
int main()
int i;
enum month {
JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
};
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.
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:
Answer: d
Explanation: The keyword extern is not a definition and is not allocated storage until it is
initialized.
Answer: c
Explanation: No extern are allowed for class declarations.
Answer: c
Explanation: Global values are implicitly initialised to 0, but local values have to be initialised
by the system.
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.
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
Output:
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.
Answer: c
Explanation: In statement 1 lvalue is required as unary ‘&’ operand and in statement 3 lvalue is
required as left operand.
Answer: a
Explanation: The statement makes CHAR a synonym for char*.
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:
Answer: c
Explanation: None.
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.
Answer: d
Explanation: A pointer can be in only 3 states a,b and c.
Answer: c
Explanation: dp is initialized int value of i.
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.
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.
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
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
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.
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>
int main()
result += array1[temp];
result += array2[temp];
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
#include <stdio.h>
using namespace std;
int main ()
int n, result = 0;
result += array[n];
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.
#include <stdio.h>
int main()
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’
#include <stdio.h>
int main()
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
#include <stdio.h>
int main()
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
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:
Answer: b
Explanation: In the above declaration the variable p is array not pointer.
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>
int main()
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
#include <iostream>
int main()
int i;
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
#include <iostream>
int main()
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
#include <iostream>
int main()
{
int arr[] = {4, 5, 6, 7};
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
#include <iostream>
int main ()
int numbers[5];
int * p;
p = numbers; *p = 10;
p++; *p = 20;
p = &numbers[2]; *p = 30;
p = numbers + 3; *p = 40;
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,
#include <iostream>
int main()
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
Here is a listing of online C++ test questions on “Constants” along with answers, explanations
and/or solutions:
Answer: c
EXplanation: None.
Answer: d
Explanation: Because these are the types used to declare variables and so these can be declared
as constants.
Answer: c
Explanation: The const will declare with a specific type values and #define is used to declare
user definied constants.
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.
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
Answer: c
Explanation: Because the const is used to declare non-changable values only.
Answer: a
Explanation: None.
Answer: a
Explanation: It can turn this as wide character instead of narrow characters.
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:
Answer: d
Explanation: If it can be assigned with a null value means, it is a copy of pointer.
Answer: b
Explanation: Reference is a thing which points to valid memory address, so it can’t be
redesigned.
Answer: a
Explanation: None.
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. }
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
Answer: b
Explanation: Because we are pointing memory address using temp variable.
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
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.
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:
Answer: d
Explanation: Because it doesn’t know the type of object it is pointing to, So it can point to all objects.
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.
Answer: d
Explanation: None.
5. What is the output of this program?
#include <iostream>
int main()
func(Str);
return 0;
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
#include <iostream>
int main()
int *p;
void *vp;
if (vp == p);
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
#include <iostream>
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
#include <iostream>
int main()
int n = 5;
void *p = &n;
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
#include <iostream>
using namespace std;
int main()
int a = 5, c;
void *p = &a;
double b = 3.14;
p = &b;
c = a + b;
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
Answer: a
Explanation: Because void pointer is used to cast the variables only, So pointer arithemetic can’t be
done in a void pointer.
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:
Answer: b
Explanation: None.
Answer: c
Explanation: While terminating a structure, a semi colon is used to end this up.
Answer: a
Explanation: While the structure is declared, it will not be initialized, So it will not allocate any
memory.
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>
int main()
struct student
int num;
char name[25];
};
student stu;
stu.num = 123;
strcpy(stu.name, "John");
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
struct Time
int hours;
int minutes;
int seconds;
};
int main()
Time t;
t.hours = 5;
t.minutes = 30;
t.seconds = 45;
return 0;
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>
int main()
struct ShoeType
string style;
double price;
};
shoe1.style = "Adidas";
shoe1.price = 9.99;
shoe2 = shoe1;
shoe2.price = shoe2.price / 9;
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
#include <iostream>
struct sec
int a;
char b;
};
int main()
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
Answer: a
Explanation: Because in a structure pointer, the data element is declared as above only.
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:
Answer: d
Explanation: None.
Answer: a
Explanation: The operator which is having highest precedence is postfix and lowest is equality.
Answer: a
Explanation: In this operator, if the condition is true means, it will return the first operator, otherwise
second operator.
#include <iostream>
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
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>
int main()
int a = 5, b = 6, c, d;
c = a, b;
d = (a, b);
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
#include <iostream>
int main()
int i, j;
j = 10;
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
#include <iostream>
int main ()
int x, y;
x = 5;
y = ++x * ++x;
x = 5;
y = x++ * ++x;
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
#include <iostream>
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
#include <iostream>
main()
double a = 21.09399;
float b = 10.20;
int c ,d;
c = (int) a;
d = (int) b;
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