MCQ

C++ MCQ Questions and Answers – Part 5

Multiple choice questions and answers (MCQs) on C++ to prepare for exams, tests, and certifications. These questions are taken from a real written exam and some parts are taken from an interview. So you will find questions on basic techniques such as Variables, Operators, Conditional Statement, Functions, and more. This quiz will easily prepare anyone to pass their online test.
 

1. Which of the following statements is correct?

A The keyword “struct” is required in C and C++ while declaring a structure object.

B The keyword “struct” is not required in C but required in C++ when declaring a structure object.

C The keyword “struct” is not required in C++ but required in C when declaring a structure object.

D The keyword “struct” is not required in C and C++ when declaring a structure object.

C
In C, we must use “struct” to declare a structure variable. In C++, the keyword “struct” is not necessary. For example, We have the Node structure. In C, we must use “struct Node” for Node variables. In C++, we do not need to use the keyword “struct” in front of the structure variable.

Structure in C

#include <stdio.h>
  
struct Node { 
    int x = 5; 
}; 
  
int main() 
{ 
    struct Node n;    // The keyword "struct" is required
    printf("%d", n.x);   // 5
    return 0; 
}

 
Structure in C++

#include <iostream> 
using namespace std; 
  
struct Node { 
    int x = 5; 
}; 
  
int main() 
{ 
    Node n;     // The keyword "struct" is not required
    cout << n.x << endl;    // 5
    return 0; 
}

 

 

2. Which of the following statements is correct?

A A structure cannot have a member function in C, but it is possible in C++.

B A structure cannot have a member function in C++ but it is possible in C.

C A structure cannot have a member function in C and C++.

D A structure can have a member function in C and in C++.
 

 
A
A structure can have a member function in C++ while member functions are not allowed in C.

Member functions of a structure in C++

// In the header file
struct MyStruct {
  int x, y, z;

  void myFunction();
};

// In the source file
void MyStruct::myFunction() {
  // Code for the function ...
}

 

 

3. What happens if the following program is run in C and C++?
#include <stdio.h> 
void main() 
{ 
	printf("Hello World"); 
}

A Error in C and C++

B Warning in C and C++

C Error in C and success in C++

D Error in C++ and success in C

D
The main() function in C++ must return an integer otherwise the C++ compiler returns an error.
 

 
Whereas C does not require anything like that from the main() function.

 

 
 

4. Which of the following statements correctly declares an array?

A int arr[10];

B int arr;

C arr{10};

D int [10]arr;

A
Array variables must be declared after the type.

 

 

5. What is the index of the last element of an array with 5 elements?

A 5

B 4

C 0

D Defined by the programmer

B
Because the first element always starts at 0. It is then at position 4.

 

 
 

6. What is the correct definition of an array?

A An array is a set of elements of the same type stored in contiguous memory locations

B An array is a set of elements

C An array is a set of elements of the same type stored in non-contiguous memory locations

D None of the above

A
The correct definition of an array is: An array is a set of elements of the same type stored in contiguous memory locations.

 

 

7. Which of the following statements is correct about this pointer in C++?

A this pointer is passed as a hidden argument in all functions of a class.

B this pointer is passed as a hidden argument in all non-static functions of a class.

C this pointer is passed as a hidden argument in all static functions of a class.

D None of the above

B
Since static functions are global for a class, all objects share the common instance of this static function, whereas all objects have their own instance for non-static functions and are therefore passed as hidden arguments. non-static members but not in static members.

 

 

8. What happens if the following program is run in C and C++?
#include <stdio.h> 

void f(void)
{
	printf("Hello World!");
}
void main() 
{ 
  f();
 f(5);
}

A Error in C and success in C++

B Error in C++ and success in C

C Error in C and C++

D Display “Hello World!” twice in C and C++
 

 
C

 
f(void) function does not need any argument during its call, therefore, when we call f(5) with 5 as a parameter, this statement returns an error in C++ and C.

 

 

9. Which of the following features is not provided by C?

A Pointers

B Structures

C References

D Functions

C
References are introduced in C++. They are not present in C.

 

 

10. Which of the following operators is used with a pointer to access members of a class?

A ~

B .

C ->

D !

C
The pointer object uses the arrow operator (->) to access the members of a class.

 

 
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *