MCQ

C++ MCQ Questions and Answers – Part 6

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. What is the use of this pointer in C++?

A To access members of a class that have the same name as local variables in this scope.

B The pointer “this” points to the current object of the class.

C To access objects of another class.

D All the answers are true

B
The pointer “this” contains the address of the current object.

 

 
 

2. What happens if we execute the following code in C and C++?
#include<stdio.h>

struct STRUCT
{
  int x;
  int f()
  {
      printf("Welcom to StackHowTo\n");
  }
};
int main()
{
  struct STRUCT s;
  s.f();
  return 0;
}

A The program works correctly in C and C++ and displays “Welcom to StackHowTo” as output.

B The program returns an error in C but works perfectly in C++.

C The program returns an error in C++ but works perfectly in C.

D The program returns an error in C and C++

B
The C language does not allow a structure to have member functions, it generates an error in C, but C++ allows structures to have member functions, so C++ does not generate an error.
 

 

 

3. What does the following statement mean?
int (*fptr)(char*)

A Pointer to a pointer

B Pointer to an array of characters

C A pointer to a function taking a char * argument and returning an int

D Function taking a char * argument and returning a pointer to int

C
The (*fptr) represents a pointer to a function and char* as an argument, returning an integer. So, the above syntax represents a pointer to a function taking a “char*” as an argument and returning “int”.

 

 

4. Which of the following will access the fifth element stored in an array?

A arr[4];

B arr[5];

C arr(5);

D [5]arr;

A
The array position starts at zero, so the fifth element is accessible via arr[4].

 

 
 

5. The operator used for de-referencing or indirection is ____

A &

B *

C ->

D <>

B
The * operator is used as a de-reference operator, used to read the value stored at the specified address.

 

 

6. What is the output of the following C++ code?
#include <iostream>
 
int main(int argc, char const *argv[])
{
	cout << "Welcom to StackHowTo";
	return 0;
}

A Welcom to StackHowTo

B Compilation error

C Runtime error

D No segmentation

B
“cout” is defined under the std namespace, and without including the std namespace, we cannot use “cout”. Therefore, the program generates an error.
 

 

 

7. Choose the right option :
string* a, b;

A a is a pointer to a string, b is a string.

B b is a pointer to a string, a is a string

C a and b are pointers of type String.

D None of the above

A

 

 
 

8. What is the value returned by is_int function?
bool is_int(12.54)

A False

B True

C 1

D None of the above

A
The number given as parameter is a double and not an integer. The function therefore returns 0, which is “False”.

 

 

9. What is the value of “i”?
#include <iostream>

using namespace std;

int main()
{
  int i;
  bool x = true;
  bool y = false;
  int a = 10;
  int b = 5;
  i = ((a | b) + (x + y));
  cout << i;
  return 0;
}

A 15

B 16

C 0

D True

B
The “bitwise OR” operator | means bitwise operation so a | b. (0101 | 1010) will be evaluated to 1111, which corresponds to the integer 15, and x = true and y = false, so
x + y (1 + 0) = 1. The value of i will then be 15 + 1 = 16.

 

 

10. Which of the following instructions is illegal?

A int *p = 0;

B int i; double* p = &i;

C string s, *p = 0;

D int *p;

B
p is initialized with a value of type “int”.

 

 
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 *