Top 50+ CPlusPlus Asked Question in Interview
The correct answer is:
d. basic input/output operation ✅
Explanation:
-
The
#include
directive is used to include header files in a C++ program. -
For example,
#include <iostream>
is essential to use basic input/output operations likecin
andcout
. -
Without including
<iostream>
, you cannot use these standard I/O functionalities.
Other options:
-
a. basic mathematical operation — Usually handled by built-in operators, no special include needed.
-
b. advanced mathematical operation — Might require
<cmath>
, but#include
itself is not specifically essential here. -
c. data hiding — A concept in OOP, unrelated to
#include
.
✅ So, the best choice is:
d. basic input/output operation
The correct answer is:
a. All of these ✅
Explanation:
Object-Oriented Programming (OOP) provides multiple key features, including:
-
Modularity — Code is organized into separate, manageable modules (classes/objects).
-
Encapsulation — Hiding internal details and exposing only necessary parts.
-
Polymorphism — Ability to treat objects of different classes through a common interface.
✅ So, the answer is a. All of these.
The correct answer is:
d. Top down approach ✅
Explanation:
-
Procedural Programming typically follows the Top-Down Approach, where a system is broken down into smaller subroutines or functions step by step, starting from the highest-level overview.
Other options:
-
b. bottom up approach — More common in Object-Oriented Programming.
-
c. mid way approach — Not a standard term.
-
a. none of the above — Incorrect.
✅ Final answer: d. Top down approach
The correct answer is:
d. Skips the current iteration and proceeds to the next iteration ✅
Explanation:
-
The
continue
statement in C++ skips the remaining code inside the current loop iteration and immediately jumps to the next iteration of the loop.
Example:
Other options:
-
a. continues from already existing break statement ❌ — Not correct.
-
b. continues the loop without doing anything ❌ — It does skip code but moves to the next iteration, not just "doing nothing".
-
c. none of these ❌ — Incorrect.
✅ So, the right answer is:
d. Skips the current iteration and proceeds to the next iteration
The correct answer is:
d. public ✅
Explanation:
Visibility specifiers in C++ control access to class members:
-
private: Most restrictive — accessible only within the class.
-
protected: Accessible within the class and its derived classes.
-
public: Least restrictive — accessible from anywhere.
-
const: Not a visibility specifier, it means the value cannot be changed.
✅ So, the least restrictive visibility specifier is public.
The correct answer is:
c. >> operator should be used ✅
Explanation:
-
cin
uses the extraction operator>>
to take input. -
The code
cin << x;
is incorrect because<<
is the insertion operator (used withcout
). -
The correct syntax to read input into
x
is:
Other Options:
-
a. >>> operator should be used ❌ → No such operator in C++.
-
b. cin("%d",x) should be used ❌ → This is C-style
scanf
, not valid withcin
. -
d. < operator should be used ❌ → Single
<
is not used for input extraction.
✅ Final Answer: c. >> operator should be used
The correct answer is:
b. int arr[10]; ✅
Explanation:
In C++, to declare an array of 10 integers, you use the following syntax:
This creates an array named arr
that can hold 10 integers, indexed from 0
to 9
.
Other Options:
-
a. array int[10]; ❌ → Incorrect syntax.
-
c. array arr[10] ❌ →
array
is not a valid keyword in this context. -
d. int arr(10); ❌ → This declares a single integer variable, not an array.
✅ Final Answer: b. int arr[10];
The correct answer is:
b. 0 ✅
Explanation:
In C++, when you declare an enum
like this:
The enumerators (like Monday
and Tuesday
) are automatically assigned integer values starting from 0 by default.
So:
-
Monday = 0
-
Tuesday = 1
✅ Final Answer: b. 0
The correct answer is:
c. long ✅
Explanation:
Among the options provided, we are looking for the largest non-floating point (integer) data type:
-
a. double ❌ → This is a floating-point type, so it's excluded.
-
b. short ❌ → Smaller than
int
, usually 2 bytes. -
d. int ❌ → Typically 4 bytes.
-
c. long ✅ → Larger than or equal to
int
, typically used for storing bigger integers.
On most systems:
-
short
= 2 bytes -
int
= 4 bytes -
long
= 4 or 8 bytes depending on the compiler and system architecture (usually 8 bytes on 64-bit systems)
✅ So, the correct answer is: c. long
The correct answer is:
a. >> ✅
Explanation:
In C++, the **cin**
object is used for input (getting data from the user), and it works with the extraction operator >>
.
Example:
This reads a value from the user and stores it in the age
variable.
Other Options:
-
b.
<<
— Used with**cout**
for output. -
c.
>=
— Greater than or equal to (comparison operator). -
d.
<=
— Less than or equal to (comparison operator).
✅ Final Answer: a. >>
The correct answer is:
d. class ✅
Explanation:
In Object-Oriented Programming (OOP):
-
A class is a blueprint or template for creating objects.
-
It defines the properties (fields/attributes) and behaviors (methods/functions) that the objects created from it will have.
Example in C++:
Here, Car
is a class, and you can create objects like:
Other Options:
-
a. fields — These are variables inside a class.
-
b. functions — These are methods inside a class.
-
c. constant — A fixed value; not related to creating objects.
✅ So, the correct answer is: d. class.
The correct answer is:
a. "\n" ✅
Explanation:
-
endl
in C++ is used to insert a newline character (just like\n
) and flush the output buffer.Example:
This prints
"Hello"
and moves the cursor to the next line. -
\n
is the newline character, also moves the cursor to the next line.
Other Options:
-
b. "a"
→ Just a character, not related to newline. -
c. "\s"
→ Not a valid escape sequence in C++. -
d. "\t"
→ Inserts a tab, not a newline.
✅ So, the closest match to endl
is: a. "\n"
.
Breakdown:
-
for (int i = 1; i <= 10; i++)
— loops from1
to10
. -
if (i % 2 == 1)
— checks ifi
is odd (%
is the modulo operator, andi % 2 == 1
means the remainder is 1, which happens with odd numbers). -
cout << i << " "
— prints the odd number followed by a space.
Numbers from 1 to 10 that are odd:
1, 3, 5, 7, 9