MCQ

C++ MCQ Questions and Answers – Part 4

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 happens if the line below is executed in C and C++?
int *ptr = malloc(20);

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++

C
C++ is strict in type checking, but C is tolerant, and since malloc returns a void* which we try to assign to an int*, the C++ compiler returns an error.
 

 
While the C compiler runs the program successfully.

 

 

2. What is a dynamic binding?

A The process of binding the current code to a procedure call at runtime.

B The process of linking the actual code to a procedure call during compilation.

C Process of linking the current code to a procedure call at any time.

D All the answers are true
 

 
A
The binding of calls and variables to the actual code at runtime is called dynamic binding. For example, in the concept of polymorphism, types are defined at runtime of the code, which results in different function calls depending on the types used, which is called dynamic binding. Since the function call is decided at runtime, dynamic binding takes place at runtime.

 

 

3. What is a static binding?

A The process of binding the current code to a procedure call at runtime.

B The process of linking the actual code to a procedure call during compilation.

C Process of linking the current code to a procedure call at any time.

D All the answers are true

B
The binding of calls and variables to the actual code at compile time is called static binding. For example, normally, every time we declare a variable, we define its type. So the compiler knows what type should be bound to that variable.

 

 
 

4. What happens if the line below is executed in C and C++?
const int x;

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++

C
The C++ compiler does not allow the programmer to declare a constant variable without initializing it. Therefore, the C++ compiler generates an error.
 

 
While the C compiler allows such a declaration. Therefore, the program is compiled and executed successfully.

 

 

5. What is the output of the following C++ code?
#include <iostream> 

using namespace std; 

int main() 
{ 
    cout << sizeof('a') << endl; 
    cout << sizeof(char);     
    return 0; 
}

A 1 4

B 1 1

C 4 1

D Error
 

 
B

 
Note that character constants are stored with the type “char” in C++, but this is not the case in C. In ANSI C, the same program would produce the following result:

4 1 // result when the code is executed in C

Because in C character constants are converted to int.

 

 

6. Which of the following is the scope resolution operator?

A ~

B ::

C *

D .

B
The :: operator is called a scope resolution operator used to access a global variable from a function with the same name as the variable declared in the function.

 

 

7. What is the output of the following C++ code?
#include<iostream>

using namespace std;

int arr[50];
int main()
{
    cout << arr[49] << endl;
}

A Random value

B 49

C 0

D Error

C
In C++, all non-initialized variables are set to 0, so the value of all elements of the array is set to 0.

 

 

8. Which of the following is accessed by a member function of a class?

A The object of this class

B Public part of the class

C Private part of the class

D All members of the class

D
A function that is a member of a class can access all the members of its class, whether they are private, protected or public.

 

 
 

9. What is the size of a character in C and C++?

A 4 et 1

B 1 et 4

C 1 et 1

D 4 et 4

A
The size of a character is 4 in C, but 1 in C++. To check this, you can run the following code printf(“% d”, (int)sizeof ('a')); in C and C++.

 

 

10. What happens if the line below is executed in C and C++?
#include <stdio.h> 

int main(void) 
{ 
	int new = 10;
	printf("%d", new); 
}

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++

C
“new” is a keyword in C++; therefore, we cannot declare a variable with the name “new”.
 

 
But since there is no “new” keyword in C, the program is compiled and executed successfully in C.

 

 
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 *