250+ TOP MCQs on seq_con Vector Class – 2 and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) on “seq_con Vector Class – 2”.

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

#include  
#include  
 
using namespace std; 
 
int main() 
{ 
    vector<int> v; 
 
    for (int i = 1; i <= 5; i++) 
        v.push_back(i); 
 
    cout<<v.size();
    cout<<endl<<v.capacity();  
    return 0; 
}

a)

5
5

b)

5
8

c)

5
10

d)

8
8

View Answer

Answer: b
Clarification: The size() returns the number of elements in the vector and capacity() returns the total number of elements that this vector can hold. Hence as the number of elements in vector is 5 and size is increased by 2 times. Therefore output is 5 and 8
Output:

$ ./a.out 
5
8

 
 

2. Which function is used to check whether the vector is empty or not?
a) empty()
b) isempty()
c) haveElements()
d) none()
Answer: a
Clarification: empty() function is provided by the vector container to check whether it is empty or not.

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

#include  
#include  
 
using namespace std; 
 
int main() 
{ 
    vector<int> v; 
 
    for (int i = 1; i <= 5; i++) 
        v.push_back(i); 
    v.resize(4);
	for (auto it = v.begin(); it != v.end(); it++) 
        cout << *it << " "; 
    return 0; 
}

a) 1 2 3 4 5
b) 1 2 3 4
c) 2 3 4 5
d) error
Answer: b
Clarification: resize() function is used to resize a vector container. It updates the size of vector and removes all the elements after n if new size(n) is less than previous size. Hence in the program initially the vector has 5 elements but after resizing the vector to 4 it has only 4 elements as 5 is removed.
Output:

$ ./a.out 
1 2 3 4

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

#include  
#include  
 
using namespace std; 
 
int main() 
{ 
    vector<int> v; 
    for (int i = 1; i <= 5; i++) 
        v.push_back(i); 
    cout<<v.size()<<endl;
    v.resize(4);
    cout<<v.size()<<endl;
    return 0; 
}

a)

5
4

b)

5
5

c)

4
4

d) Error
Answer: a
Clarification: Intitally the size of the vector is 5 as it contains only 5 elements. After resizing the elements 5 is terminated so only 4 remains therfore the size becomes 4. Hence out is as follow.
Output:

$ ./a.out 
5
4

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

#include  
#include  
 
using namespace std; 
 
int main() 
{ 
    vector<int> v; 
    for (int i = 1; i <= 5; i++) 
        v.push_back(i); 
    cout<<v.capacity()<<endl;
    v.resize(4);
    cout<<v.capacity()<<endl;
    return 0; 
}

a)

5
4

b)

8
8

c)

5
8

d)

4
8

View Answer

Answer: b
Clarification: The capacity denotes how many elements a avector can hold. On resizing a vector the capacity of a vector is not changed hence the capacity before and after is same. Therefore the output is as follows.
Output:

$ ./a.out 
8
8

 
 

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

#include  
#include  
 
using namespace std; 
 
int main() 
{ 
    vector<int> v; 
    for (int i = 1; i <= 5; i++) 
        v.push_back(i); 
    cout<<v.capacity()<<endl;
    v.shrink_to_fit();
    cout<<v.capacity()<<endl;
    return 0; 
}

a)

5
8

b)

8
5

c) Error
d) Segmentation fault
Answer: b
Clarification: Initially we have 5 elements in the vector therefore the capacity of the vector is 8(one can observe that as capacity doubles after vector is full). Now the function shrink_to_fit() makes the capacity of vector equal to its size hence removing the extra space occupied by the vector. Therefore as only 5 elements were there in the vectore therefore the capacity becomes 8.
Output:

$ ./a.out 
8
5

7. What will be the capacity of the vector after 10 is pushed into the vector in the following C++ code?

#include  
#include  
 
using namespace std; 
 
int main() 
{ 
    vector<int> v; 
    for (int i = 1; i <= 5; i++) 
        v.push_back(i); 
    cout<<v.capacity()<<endl;
    v.shrink_to_fit();
    cout<<v.capacity()<<endl;
    v.push_back(10);
    return 0; 
}

a) 8
b) 10
c) 5
d) 6
Answer: b
Clarification: After shrinking the capacity of vector the capacity of vector becomes 5. Now when a new element i.e. 10 is inserted into the vector then the capacity of the vector will double i.e. it will become 10. hence the final capacity will be 10.

8. What will be the capacity of vector at the end in the following C++ code?

#include  
#include  
 
using namespace std; 
 
int main() 
{ 
    vector<int> v; 
    for (int i = 1; i <= 5; i++) 
        v.push_back(i);
    v.reserve(50);
    cout<<v.capacity();
    return 0; 
}

a) 10
b) 8
c) 50
d) 60
Answer: c
Clarification: In this program reserve(n) function is used which is used to reserve the space for n elements in vector. Hence when the reserve(50) function is called for vector v then the we are trying to reserve memory for 50 elements, hence the capacity of vector v becomes 50.

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

#include  
#include  
 
using namespace std; 
 
int main() 
{ 
    vector<int> v; 
    for (int i = 1; i <= 5; i++) 
        v.push_back(i);
    int *pos = v.data();
    cout<<*(pos + 3);
    return 0; 
}

a) 5
b) 1
c) 4
d) 3
Answer: c
Clarification: data() function in vector returns the direct pointer to the memory array which the vector has used to store its elements. Hence a pointer to vector is returned. So when we are accessing *(pos + 3) we are trying to do v[3] which is 4. Hence the output is as follows.
Output:

$ ./a.out 
4

10. Which of the following function is used to insert an element at the end of a vector?
a) push_back()
b) pop_back()
c) front()
d) end()
Answer:a
Clarification: Vector provides push_back() function to insert an element at its end.

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

#include  
#include  
 
using namespace std; 
 
int main() 
{ 
    vector<int> v; 
    for (int i = 1; i <= 5; i++) 
        v.push_back(i);
    cout<<v.capacity()<<endl;
    v.pop_back();
    v.pop_back();
    cout<<v.capacity()<<endl;
    return 0; 
}

a)

8
4

b)

4
4

c)

8
8

d)

4
8

View Answer

Answer: c
Clarification: Vector never reduces its capacity on deleting any element which one may get confuse by thinking about the fact that vector doubles its memory on insertion. Hence both returns the same capacity.
Output:

$ ./a.out 
8
8

 
 

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

#include  
#include  
 
using namespace std; 
 
int main() 
{ 
    vector<int> v; 
    for (int i = 1; i <= 5; i++) 
        v.push_back(i);
    for(int i=0;i<v.size();i++)
    	cout<<v[i]<<" ";
    cout<<endl;
    v.assign(3, 8);
    for(int i=0;i<v.size();i++)
    	cout<<v[i]<<" ";
    cout<<endl;
    return 0; 
}

a)

1 2 3 4 5 
8 8 8

b)

1 2 3 4 5 
8 8 8 8 8

c)

1 2 3 4 5 
8 8 8 4 5

d) 1 2 3 4 5
Answer: a
Clarification: assign(m,n) function changes the vector values by assigning new values to vector. It copies m times the value n to the vector by first removing all the initial values. hence the vector has 3 8’s after updation i.e. using assign(3,8) function. Hence the output is as follows.
Output:

$ ./a.out 
1 2 3 4 5 
8 8 8

13. Which function is used to swap two vectors?
a) swap()
b) change()
c) merge()
d) exchange()
Answer: a
Clarification: Vectors allows the use of swap function to swap to vectors with each other of same type and size.

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

#include  
#include  
 
using namespace std; 
 
int main() 
{ 
    vector<int> v1;
    vector<char> v2; 
    for (int i = 1; i <= 5; i++) 
        v1.push_back(i);
    for (int i = 6; i <= 10; i++) 
        v2.push_back(i);
    v1.swap(v2);
    for(int i=0;i<v1.size();i++)
    	cout<<v1[i]<<" ";
    for(int i=0;i<v2.size();i++)
    	cout<<v2[i]<<" ";
    cout<<endl;
    return 0; 
}

a) 1 2 3 4 5 6 7 8 9 10
b) 6 7 8 9 10 1 2 3 4 5
c) Error
d) Segmentation fault
Answer: c
Clarification: swap() function in vector allows to swap two vectors of same size and type but here the vectors v1 and v2 have different types therefore the program gives the error.

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

#include  
#include  
 
using namespace std; 
 
int main() 
{ 
    vector<int> v1;
    vector<int> v2; 
    for (int i = 1; i <= 5; i++) 
        v1.push_back(i);
    for (int i = 6; i <= 10; i++) 
        v2.push_back(i);
    v1.swap(v2);
    for(int i=0;i<v1.size();i++)
    	cout<<v1[i]<<" ";
    for(int i=0;i<v2.size();i++)
    	cout<<v2[i]<<" ";
    cout<<endl;
    return 0; 
}

a) 1 2 3 4 5 6 7 8 9 10
b) 6 7 8 9 10 1 2 3 4 5
c) Error
d) Segmentation fault
Answer: b
Clarification: Here swap() function is used and the type and size of both vectors v1 and v2 are same therefore they can be swapped and hence the program allows such swap. Therefore no error and program runs perfectly.
Output:

$ ./a.out 
6 7 8 9 10 1 2 3 4 5

contest

250+ TOP MCQs on Bitset – 3 and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) on “Bitset – 3”.

1. What is the use of the flip function in bitset?
a) Used to flip bit(s) in a bitset
b) Used to flip a bit in a bitset
c) Used to flip all bits to 1
d) Used to flip alternate bits
Answer: a
Clarification: header provides the flip() function to flip bit(s) in a bitset variable i.e. change the bits in a bitset for example 1100 on flipping becomes 0011.

2. What happens when no argument is supplied to flip() function?
a) All alternate bits are flipped in a bitset
b) All bits are flipped to 1 in a bitset
c) All bits are flipped in a bitset
d) First bit is flipped
Answer: c
Clarification: When no argument is supplied to flip() function i.e. function is called with empty parameters then all the bits of the bitset variable is flipped.

3. What happens when only one argument is supplied to flip() function?
a) All bits are flipped in a bitset
b) Bit corresponding to argument bit is flipped
c) All alternate bits are flipped in a bitset
d) First bit is flipped
Answer: b
Clarification: When only one argument is supplied to flip() function then bit corresponding to that index only is flipped.

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

#include 
#include 
using namespace std;
int main()
{
	bitset<8> b1(95);
	bitset<8> b2(46);
	cout<<(b1^b2);
}

a) 00001101
b) 11111000
c) 01111111
d) 01110001
Answer: d
Clarification: ^ operator is used to take xor of two bitset variables i.e. if ith bit in both varaibles are either 1 or 0 then answer is 0 otherwise answer is 1.

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

#include 
#include 
using namespace std;
int main()
{
	bitset<8> b1(95);
	bitset<8> b2 = b1 << 3;
	cout<<b2;
}

a) 01110001
b) 11111000
c) 01111111
d) 00001101
Answer: b
Clarification: << operator is used to shift bits towards left side so if we have 1111 then on shifting this by 2 i.e. 1111 << 2 will result into 1100.

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

#include 
#include 
using namespace std;
int main()
{
	bitset<8> b1(95);
	bitset<8> b2 = b1 >> 3;
	cout<<b1<<endl<<b2;
}

a) 00001011
b) 11111000
c) 01111111
d) 00001101
Answer: a
Clarification: >> operator is used to shift bits towards right side so if we have 1111 then on shifting this by 2 i.e. 1111 >> 2 will result into 0011.

7. Which operator is used as not operator in bitset?
a) |
b) &
c) ~
d) ^
Answer: c
Clarification: ~ operator is used as not operator i.e. the negation of a bit for bitset variables in C++.

8. Which operator is used to take AND of two bitset variables?
a) ~
b) &
c) |
d) ^
Answer: b
Clarification: & operator is used as AND operator for bitset variables in C++. ANDing of two bits are 1 only if both are 1.

9. Which operator is used to take OR of two bitset variables?
a) ~
b) &
c) |
d) ^
Answer: c
Clarification: | operator is used as OR operator for bitset variables in C++. ORing of two bits are 0 only if both are 0.

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

#include 
#include 
using namespace std;
int main()
{
	bitset<8> b1(95);
	bitset<8> b2(45);
	cout<<~b1<<endl;
	cout<<(b1|b2)<<endl;
	cout<<(b1&b2)<<endl;
}

a)

10100000
01111111
00001101

b)

11111111
01111111
00001101

c)

10100000
00001111
00001101

d)

10100000
01111111
00000000

View Answer

Answer: a
Clarification: In this program we are doing not of b1. AND of b1 and b2 and OR of b1 and b2, answer to them are 10100000, 01111111 and 00001101.

 
 

250+ TOP MCQs on Iterators and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) on “Iterators”.

1. What are Iterators?
a) STL component used to point a memory address of a container
b) STL component used for vectors
c) STL component used to call functions efficiently
d) STL component used to define template classes
Answer: a
Clarification: Iterators are STL components used to point a memory address of a container. They are used to iterate over container classes.

2. Which function is used increment the iterator by a particular value?
a) next()
b) advance()
c) prev()
d) move()
Answer: b
Clarification: advance() function is used to increment an iterator by a given value.

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

#include 
#include 
#include 
using namespace std; 
int main() 
{ 
    vector<int> ar = { 1, 2, 3, 4, 5 }; 
    vector<int>::iterator ptr = ar.begin(); 
    advance(ptr, 2);
    cout << *ptr << endl; 
    return 0; 
}

a) 2
b) 3
c) 4
d) 5
Answer: b
Clarification: Initially the ptr is pointing to first element of vector and now as we are advancing the iterator by 2 which takes the iterator the value 3. Hence the output is 3.
Output:

$ ./a.out 
3

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

#include 
#include 
#include 
using namespace std; 
int main() 
{ 
    vector<int> ar = { 1, 2, 3, 4, 5 }; 
    vector<int>::iterator ptr = ar.begin(); 
    ptr = advance(ptr, 2);
    cout << *ptr << endl; 
    return 0; 
}

a) 3
b) 4
c) Error
d) Segmentation fault
Answer: c
Clarification: advance() function is used to increment/decrement the iterator by a given value so it does not returns any thing. So when we are doing ptr = advance(ptr, 2); we are expecting the advance() function to return some value but as it doesn’t returns anything therefore compiler throws an error.

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

#include 
#include 
#include 
using namespace std; 
int main() 
{ 
    vector<int> ar = { 1, 2, 3, 4, 5 }; 
    vector<int>::iterator ptr = ar.begin(); 
    ptr = next(ptr, 3);
    cout << *ptr << endl; 
    return 0; 
}

a) 1
b) 2
c) 3
d) 4
Answer: d
Clarification: next() function returns an iterator to the position which is ahead of current iterator by a distance of given value. Hence when we are calling the function next(ptr,3); then we are storing the result into ptr which is now an iterator pointing to 4.
Output:

$ ./a.out 
4

6. How many types of Iterators are there?
a) 5
b) 2
c) 3
d) 4
Answer: a
Clarification: There are 5 types of iterators discussed under STL namely:
i) Input Iterators
ii) Output Iterators
iii) Forward Iterators
iv) Bi-directional Iterators
v) Random-access Iterators.

7. Pick the correct statement.
a) Input iterator moves sequentially forward
b) Input iterator moves sequentially backward
c) Input iterator moves in both direction
d) Input iterator moves sequentially downwards
Answer: a
Clarification: By definition Input iterators moves sequentially forward.

8. Which of the following is correct about Input Iterators?
a) Input iterators can be used with all relational operators
b) Input iterators can work with arithmetic operators
c) No value can be assigned to the location pointed by Input Iterator
d) Input iterators can work with sequence operators
Answer: c
Clarification: Values cannot be assigned to the location pointed by input operators. Input operators cannot be used with all relational and arithmetic operators.

9. Which of the following is correct about Input Iterators?
a) They cannot be decremented
b) Cannot be used in multi-pass algorithms
c) Can only be incremented
d) All of the mentioned
Answer: d
Clarification: Input iterators can only be incremented and as it cannot be decremented it can be used in single-pass algorithms only.

10. Which of the following is correct?
a) Input Iterators are used for assigning
b) Output Iterators are used for assigning
c) Both Input and Output Iterators are used for accessing
d) Both Input and Output Iterators are used for assigning
Answer: b
Clarification: Input Iterators are used for accessing and Output Iterators are used for assigning.

11. What type of Iterator i1 is in the following C++ code snipet?

================ code ================
vector<int>::iterator i1; 
for (i1=v1.begin();i1!=v1.end();++i1) 
	*i1 = 1;
======================================

a) Input Iterator
b) Output Iterator
c) Both Input and Output Iterator
d) Neither Input nor Output Iterator
Answer: b
Clarification: As i1 iterator is used for assigning values to the vector elements therefore it it an output iterator.

12. If i1 is Input Iterator and i2 is Output Iterator, then which of the following things are correct?

i) cout<<*i1;
ii) i2 can be used with == operator
iii) *i1 = 1
iv) i2--

a) i and ii
b) i only
c) i, ii and iv
d) iii and iv
Answer: b
Clarification: Input iterators are used for accessing containers, therefore *i1 = 1 is wrong. Both Input and Output iterators cannot be decremented therefore i2– is also wrong. Output iterators cannot be used with == operator, therefore, that is also wrong. So only cout<<*i1 is correct.

13. Which of the following is an advantage of Forward iterator over input and output iterator?
a) Can be used as both accessing and assigning iterator
b) Forward iterator can be incremented or decremented
c) Can be used with relational operators also
d) Can be used with arithmetic operators also
Answer: a
Clarification: Forward iterator is a combination of both input and output iterator, therefore, can be used as both accessing and assigning iterator. Just like Input and output iterator this can also be not used with all relational and arithmetic operators and can be incremented only.

14. What are Bi-directional iterators?
a) Iterator same as Forward Iterator
b) Forward Iterator that can be used in both directions
c) Iterator that can only be used to access the sequence from both sides
d) Iterator that can only be used to assign the sequence from both sides
Answer: b
Clarification: Bi-directional iterator is a type of forward iterators that can be used for both directions access and assign.

15. What are Random-access Iterators?
a) Iterators that can be used to access elements at an arbitrary offset position
b) Same as Bi-directional iterator
c) Input iterator with the additional property of random access
d) Output iterator with the additional property of random access
Answer: a
Clarification: Random access iterators are those iterators that can be used to access elements at an arbitrary offset position relative to the memory that the iterator points.

250+ TOP MCQs on Generalized Numeric Algorithms and Answers

This section on C++ quiz on “Generalized Numeric Algorithms”. One shall practice these quizzes 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++ quiz comes with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ quiz on “Generalized Numeric Algorithms” along with answers, explanations and/or solutions:

1. Which header file is used to operate on numeric sequences?
a) number
b) numeric
c) algorithm
d) digit
Answer: b
Clarification: header file is used to operate on numeric sequences that support certain operations.

2. Which mathematics library is used for vector manipulation in c++?
a) cli++
b) vec++
c) blitz++
d) stac+++
Answer: c
Clarification: Blitz++ is a high-performance vector mathematics library written in C++.

3. What is the use of accumulate function in a numeric library?
a) Returns the number
b) Returns the result of accumulating all the values in the range
c) Returns the number & result
d) Return the characters
Answer: b
Clarification: Returns the result of accumulating all the values in the range from first to last.

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

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     int myop (int x, int y)
  6.     {
  7.         return x + y;
  8.     }
  9.     int main ()
  10.     {
  11.         int val[] = {1, 2, 3, 5};
  12.         int result[7];
  13.         adjacent_difference (val, val + 7, result);
  14.         for (int i = 0; i < 4; i++)
  15.             cout << result[i] <<' ';
  16.         return 0;
  17.     }

a) 1 1 1 2
b) 1 2 3 1
c) 1 2 3 5
d) 1 2 5 6
Answer: a
Clarification: In this program, We are calculating the adjacent difference of the given range by using function adjacent_difference.
Output:

$ g++ gnl.cpp
$ a.out
1 1 1 2

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

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     int myfunction (int x, int y) 
  6.     {
  7.         return x + 2 * y;
  8.     }
  9.     struct myclass 
  10.     {
  11.         int operator()(int x, int y) 
  12.         {
  13.             return x + 3 * y;
  14.         }
  15.     } myobject;
  16.     int main () 
  17.     {
  18.         int init = 100;
  19.         int numbers[] = {10, 20, 30};
  20.         cout << accumulate(numbers, numbers + 3, init);
  21.         cout << endl;
  22.     }

a) 100
b) 140
c) 160
d) 180
Answer: c
Clarification: In this program, We are calculating the product of every number in the given range by using accumulate function.
Output:

$ g++ gnl1.cpp
$ a.out
160

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

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     int myop (int x, int y) 
  6.     {
  7.         return x + y + 1;
  8.     }
  9.     int main () 
  10.     {
  11.         int val[] = {1, 2, 3, 4, 5};
  12.         int result[5];
  13.         partial_sum (val, val + 5, result);
  14.         for (int i = 0; i < 5; i++)
  15.             cout << result[i] << ' ';
  16.         return 0;
  17.     }

a) 1 3 6
b) 1 3 6 10 15
c) 1 3 6 10 16
d) 1 10 5 6 4
Answer: b
Clarification: In this program, We are calculating the sum of the given range by using partial_sum function.
Output:

$ g++ gnl2.cpp
$ a.out
1 3 6 10 15

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

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     int myfunction (int x, int y) 
  6.     {
  7.         return x + 2 * y;
  8.     }
  9.     struct myclass 
  10.     {
  11.         int operator()(int x, int y) 
  12.         {
  13.             return x + 3 * y;
  14.         }
  15.     } myobject;
  16.     int main () 
  17.     {
  18.         int init = 100;
  19.         int numbers[] = {10, 20, 30};
  20.         cout << accumulate (numbers, numbers + 3, init, minus<int>() );
  21.         cout << endl;
  22.         return 0;
  23.     }

a) 40
b) 100
c) 140
d) 524
Answer: a
Clarification: In this program, We are finding the difference between the init and the total of numbers range.
Output:

$ g++ gnl3.cpp
$ a.out
40

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

  1.     #include 
  2.     #include 
  3.     #include  
  4.     using namespace std;
  5.     int myaccumulator (int x, int y) 
  6.     {
  7.         return x - y;
  8.     }
  9.     int myproduct (int x, int y) 
  10.     {
  11.         return x + y;
  12.     }
  13.     int main () 
  14.     {
  15.         int a = 100;
  16.         int series1[] = {10, 20, 30};
  17.         int series2[] = {1, 2, 3};
  18.         cout << inner_product(series1, series1 + 3, series2, a ,myaccumulator, 
  19.         myproduct);
  20.         cout << endl;
  21.         return 0;
  22.     }

a) 40
b) 34
c) 32
d) 20
Answer: b
Clarification: In this program, We are forming the custom function from two ranges by using inner_product function.
Output:

$ g++ gnl4.cpp
$ a.out
34

9. How many parameters are available in partial_sum function in c++?
a) 2
b) 3
c) 2 or 3
d) 3 or 4
Answer: d
Clarification: There are three or four parameters available in partial_sum function in C++. They are first and last element, result and an optional binary operator.

10. What is the default operation of adjacent_difference function in numeric library?
a) Difference
b) Addition
c) Multiplication
d) Subtraction
Answer: a
Clarification: The default operation is to calculate the difference, but some other operation can be specified as binary operator instead.

250+ TOP MCQs on C++ vs C and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) on “C++ vs C”.

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

#include 
int main() 
{ 
   foo();
}  
int foo() 
{ 
   printf("Hello"); 
   return 0;  
}

a) Error in both C and C++
b) Warning in both C and C++
c) Error in C++ but Warning in C
d) Error in C but Warning in C++
Answer: c
Clarification: In C++ all the functions should be declared before it is called otherwise the C++ compiler will give an error but in case of C the compiler just gives a warning and the program can be executed.

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

#include  
int main(void) 
{ 
	const int j = 20; 
	int *ptr = &j;
	printf("*ptr: %dn", *ptr); 
	return 0; 
}

a) Error in both C and C++
b) Warning in both C and C++
c) Error in C but Warning in C++
d) Error in C++ but Warning in C
Answer: d
Clarification: C++ is strict on the use of types of variables hence when the programmer tries to assign const int to a normal pointer the program gives error whereas C is not strict on types therefore it gives warning only.

3. What happens if the following line is executed in C and C++?

a) Error in both C and C++
b) Warning in both C and C++
c) Error in C++ and successful execution in C
d) Error in C and successful execution in C++
Answer: c
Clarification: C++ is strict in type check but C is not and as malloc returns a void* which we are trying to assign to an int*, therefore, the C++ compiler gives error whereas C compiler executes the program successfully.

4. What happens if the following line is executed in C and C++?

a) Error in both C and C++
b) Warning in both C and C++
c) Error in C and successful execution in C++
d) Error in C++ and successful execution in C
Answer: d
Clarification: C++ compiler does not allow the programmer to declare a constant variable without initializing it hence the C++ compiler gives an error whereas C allows such declaration, therefore, the program compiles and runs successfully.

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

#include  
int main(void) 
{ 
	int new = 5;
	printf("%d", new); 
}

a) Error in both C and C++
b) A successful run in both C and C++
c) Error in C and successful execution in C++
d) Error in C++ and successful execution in C
Answer: d
Clarification: new is a keyword in C++, therefore, we cannot declare a variable with name new but as there is no such keyword new in C, therefore, the program is compiled and executed successfully in C.

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

#include  
void main() 
{ 
	printf("Hello World"); 
}

a) Error in both C and C++
b) Successful run in both C and C++
c) Error in C and successful execution in C++
d) Error in C++ and successful execution in C
Answer: d
Clarification: main() function in C++ must return int otherwise the C++ compiler gives the error whereas C does not forces such things on main() function. Thereas when we aremaking void main(){} function in this program the C++ compiler gives error whereas C compiler runs successfully.

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

#include  
void func(void)
{
	printf("Hello");
}
void main() 
{ 
	func();
	func(2);
}

a) Error in both C and C++
b) Outputs Hello twice in both C and C++
c) Error in C and successful execution in C++
d) Error in C++ and successful execution in C
Answer: a
Clarification: As the func(void) needs no argument during its call, hence when we are calling func(2) with 2 as passed as a parameter then this statement gives the error in both C++ and C compiler.

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

#include  
void func()
{
	printf("Hello");
}
void main() 
{ 
	func();
	func(2);
}

a) Error in both C and C++
b) Outputs Hello twice in both C and C++
c) Error in C and Outputs Hello twice in C++
d) Error in C++ and Outputs Hello twice in C
Answer: d
Clarification: In C++ whenever a function without argument is declared it is equivalent to function with void arguments i.e. func() == func(void) whereas in C a function without argument is equivalent to func(…) i.e. it can take any number of arguments so func(2) call is also valid in C but not valid in C++. Hence it gives error in C++ whereas no error in C.

9. Which of the following type is provided by C++ but not C?
a) int
b) bool
c) float
d) double
Answer: b
Clarification: C++ provides the boolean type to handle true and false values whereas no such type is provided in C.

10. Which of the following feature is not provided by C?
a) Pointers
b) Structures
c) References
d) Functions
Answer: c
Clarification: References are introduced in C++. They are not present in C.

250+ TOP MCQs on Pointers into Arrays and Answers

This section on C++ language interview questions and answers on “Pointers into Arrays”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in 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 the 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 the meaning of the 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 the array
d) p is pointer to array of function
Answer: b
Clarification: In the above declaration the variable p is the array, not the pointer.

2. What is size of generic pointer in C++ (in 32-bit platform)?
a) 2
b) 4
c) 8
d) 0
Answer: b
Clarification: Size of any type of pointer is 4 bytes in 32-bit platforms.

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

  1.    #include 
  2.    using namespace std;
  3.    int main()
  4.    {
  5.        int a[2][4] = {3, 6, 9, 12, 15, 18, 21, 24};
  6.        cout << *(a[1] + 2) << *(*(a + 1) + 2) << 2[1[a]];
  7.        return 0;
  8.    }

a) 15 18 21
b) 21 21 21
c) 24 24 24
d) Compile time error
Answer: b
Clarification: a[1][2] means 1 * (4)+2 = 6th element of an array starting from zero.
Output:

$ g++ point.cpp
$ a.out
21 21 21

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

  1.    #include 
  2.    using namespace std;
  3.    int main()
  4.    {
  5.        int i;
  6.        const char *arr[] = {"C", "C++", "Java", "VBA"};
  7.        const char *(*ptr)[4] = &arr;
  8.        cout << ++(*ptr)[2];
  9.        return 0;
  10.    }

a) ava
b) java
c) c++
d) compile time error
Answer: a
Clarification: 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 will be the output of the following C++ code?

  1.    #include 
  2.    using namespace std;
  3.    int main()
  4.    {
  5.        int arr[] = {4, 5, 6, 7};
  6.        int *p = (arr + 1);
  7.        cout << *p;
  8.        return 0;
  9.    }

a) 4
b) 5
c) 6
d) 7
Answer: b
Clarification: In this program, we are making the pointer point to next value and printing it.

$ g++ point3.cpp
$ a.out
5

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

  1.    #include 
  2.    using namespace std;
  3.    int main()
  4.    {
  5.        int arr[] = {4, 5, 6, 7};
  6.        int *p = (arr + 1);
  7.        cout << arr;
  8.        return 0;
  9.    }

a) 4
b) 5
c) address of arr
d) 7
Answer: c
Clarification: As we counted to print only arr, it will print the address of the array.
Output:

$ g++ point2.cpp
$ a.out
0xbfb1cff

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

  1.    #include 
  2.    using namespace std;
  3.    int main ()
  4.    {
  5.        int numbers[5];
  6.        int * p;
  7.        p = numbers;  *p = 10;
  8.        p++;  *p = 20;
  9.        p = &numbers[2];  *p = 30;
  10.        p = numbers + 3;  *p = 40;
  11.        p = numbers;  *(p + 4) = 50;
  12.        for (int n = 0; n < 5; n++)
  13.            cout << numbers[n] << ",";
  14.        return 0;
  15.    }

a) 10,20,30,40,50,
b) 1020304050
c) compile error
d) runtime error
Answer: a
Clarification: 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 will be the output of the following C++ code?

  1.    #include 
  2.    using namespace std;
  3.    int main()
  4.    {
  5.         int arr[] = {4, 5, 6, 7};
  6.         int *p = (arr + 1);
  7.         cout << *arr + 9;
  8.         return 0;
  9.    }

a) 12
b) 5
c) 13
d) error
Answer: c
Clarification: 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