MCQ

C++ MCQ Questions and Answers – Part 3

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 following program is run in C and C++?
#include<stdio.h>

int main() 
{ 
   display();
}  
int display() 
{ 
   printf("Hello World!"); 
   return 0;  
}

A Error in C and C++

B Warning in C and C++

C Error in C++ but warning in C

D Error in C but warning in C++

C
In C++, all functions must be declared before being called, otherwise the C++ compiler will generate an error, but in C, the compiler will simply give a warning and the program can be executed.
 

 

 

2. Which of the following types is not present in C but present in C++?

A void

B bool

C float

D int

B
The “bool” type is not present as a type in C.

 

 
 

3. What is the size of a boolean variable in C++?

A 1 bit

B 1 byte

C 4 bytes

D 2 bytes

A
A boolean uses only 1 bit because it only stores truth values that can be true(1) or false(0).

 

 

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

using namespace std;

int main()
{
  int a = 4;
  float b;
  cout << sizeof(++a + b) << endl;
  cout << a;
  return 0;
}

A 5 4

B 4 5

C 4 4

D 2 4

C
The variable “a” is of type “int”, it will be converted to “float” when calculating the size. The value of a variable is not modified in the sizeof operator. So the value of the variable “a” will remain 4, and the size of the type “float” is 4 bytes. So the output will be as follows:
 

 

 

5. Which of the following statements is equivalent to scanf() in C++?

A cin

B cout

C printf

D read

A
C++ uses “cin” to read user input. However, C++ also uses “scanf()”.

 

 
 

6. Which of the following statements is equivalent to printf() in C++?

A cin

B cout

C scanf

D write

B
C++ uses “cout” to display the result on the console. However, C++ also uses “printf()”.

 

 

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

int main(void) 
{ 
	const int a = 10; 
	int *ptr = &a;
	printf("*ptr = %d", *ptr); 
	return 0; 
}

A Error in C and C++

B Warning in C and C++

C Error in C++ but warning in C

D Error in C but warning in C++

C
C++ is strict about using variable types. Therefore, when the programmer tries to assign “const int” to a normal pointer, the program gives an error whereas C is not strict on types, so it only gives a warning.
 

 

 

8. What is the difference between cin and scanf()?

A Both are the same.

B cin is a flow object while scanf() is a function.

C scanf() is a flow object while cin is a function.

D cin is used for displaying while scanf() is used to read inputs.

B
“cin” is a stream object available only in C++ while “scanf()” is a function available in both C and C++. both are used to read user input.

 

 
 

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

int main()
{
	char x = 'x';
	printf("%d\n", (int)sizeof(x));
	return 0;
}

A The output in C is 4 and in C++ is 4

B The output in C is 1 and in C++ is 1

C The output in C is 4 and in C++ is 1

D The output in C is 1 and in C++ is 4

B
Both C and C++ have the same size of type “char” which equals 1.

 

 

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

using namespace std; 

int main() 
{ 
    char arr[4] = "abcd"; 
    cout << arr;     
    return 0; 
}

A abcd

B Warning

C Error: the string in the array is too long.

D None of the above

C

 
The code in C++ is similar to that in C. The only exception is the way in which the character arrays are initialized. When initializing the character array in ANSI C, the compiler will allow the following declaration:

char arr[4] = "abcd";  // allowed in ANSI C

 
This is because C assumes that the programmer intends to neglect the NULL character (‘0’) in the definition. But in C++, the size should be larger than the number of characters. Therefore, the following statement is correct in C++.

char arr[5] = "abcd";

 

 
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 *