This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1All members of a structure in C++ are ___________ by default.
Option A
In C++, a structure (struct) is similar to a class, but one key difference lies in the default access specifier. When you create a structure and define variables or functions inside it, those members are public by default.
While a class keeps its members private by default, meaning they cannot be accessed directly from outside unless specified.
Q: 2Find output of the following program where integer occupy 2 bytes memory?
int main()
{
enum suresh
{
sir, mr, guru
};
enum suresh sh;
sh=sir;
cout<<sizeof(sh);
return 0;
}
Option A
An enum in C++ is a user-defined data type whose variables are internally stored as integers. The variable sh is declared as type enum suresh and assigned the value sir.
When the sizeof(sh) expression is evaluated, it returns the size of the enum variable, not the number of elements in the enum.
Since the question clearly states that an integer occupies 2 bytes of memory, and an enum variable uses the same memory size as an int, the result of sizeof(sh) is 2 bytes.
Q: 3In C++, what does a class hold?
Option D
A class in C++ is a blueprint that can hold different types of members. Its main purpose is to group data and related functions together, but it can store many kinds of data, including arrays, simple variables, and functions.
Q: 4In C++, to release the dynamically allocated memory use ________.
Option B
In C++, memory that is allocated dynamically using the new keyword must be released using the delete keyword.
E.g.:
int *ip=new int; // Dynamic Memory Allocation.
delete ip; // Release Allocated Memory.
Q: 5The operator :: is known as?
Option D
In C++, a program can have multiple scopes, such as global scope, class scope, and local scope. Sometimes, the same name is used in different scopes. In such cases, the compiler needs a way to know which exact variable or function we want to access.
The :: operator, called the scope resolution operator, is used for this purpose. It tells the compiler to resolve the scope and access the member that belongs to a specific scope.
E.g.:
int num=10;
int main()
{
int num=20;
cout<<num; //Access local num.
cout<<::num; //Accesses global num.
}
Q: 6In C++, all members of a class are _________ by default.
Option B
In C++, the member of class whether variables or functions are private by default. This is an important basic concept in Object-Oriented Programming because it helps in data hiding.
struct Student
{
int marks;
};
int main()
{
Student s;
s.marks=101; // Works fine (public access).
return 0;
}
class Student
{
int marks;
};
int main()
{
Student s;
s.marks=101; // Compile time error (private access).
return 0;
}
Q: 7When a class is defined, memory allocation is done?
Option D
In C++, when a class is defined, no memory is allocated at that time. A class is only a blueprint or template that defines data members and member functions. Memory allocation happens only when an object of the class is created.
class Demo
{
int p;
};
Demo myObj; // Memory is allocated here.
Q: 8By default, all member functions defined inside the class are treated as?
Option A
In C++, when a member function is defined inside the class definition, the compiler treats it as an inline function by default. This means the compiler may replace the function call with the actual function code to reduce function call overhead.
class Demo
{
public:
void display()
{
cout << "Hello from display()";
}
};
Here, display() is considered an inline function.
Q: 9Which of the following statements is correct about the class?
Option A
A class is a blueprint, and an object is a real-world instance created using that blueprint. When you create an object of a class, you are creating an instance of that class, which holds the actual data.
Q: 10Which of the following is used to define the members of a class externally in C++?
Option B
In C++, class members functions can be declared inside the class but defined outside using the scope resolution operator ::. This operator tells the compiler that the function you are defining belongs to a specific class.
Syntax:
Return_Type Class_Name :: Function_Name (Arguments);
class MyClass
{
public:
void myFunction(); //Declaration inside class.
};
void MyClass::myFunction() //Definition outside class.
{
// Function body.
}
#include<iostream.h>
#include<conio.h>
class first
{
int a,b;
public:
void get();
void show();
};
class second
{
int x,y;
public:
void input()
{
x=5; y=10;
}
void show();
};
void first::get()
{
cout<<"Enter The Two Number: ";
cin>>a>>b;
}
void first::show()
{
cout<<"
Entered Number Is
";
cout<<a<<endl<<b;
}
void second::show()
{
cout<<"
In Class Second
";
cout<<x<<endl<<y;
}
void main()
{
first f;
f.get();
f.show();
second s;
s.input();
s.show();
}
Q: 11To allocate the memory dynamically in C++, which keyword is used?
Option C
In C++, dynamic memory allocation is done using the new keyword on the heap. Memory allocated using new should be released using the delete keyword.
#include <iostream>
using namespace std;
int main()
{
int *p=new int;
*p=22;
cout<<"Value : "<<*p;
delete p;
return 0;
}
OUTPUT
Value : 22
Q: 12How do structures and classes in C++ differ?
Option B
In C++, a structure is similar to a class, but one key difference lies in the default access specifier. When you create a structure and define variables or functions inside it, those members are public by default.
While a class keeps its members private by default, meaning they cannot be accessed directly from outside unless specified.
struct Student
{
int marks;
};
int main()
{
Student s;
s.marks=101; // Works fine (public access).
return 0;
}
class Student
{
int marks;
};
int main()
{
Student s;
s.marks=101; // Compile time error (private access).
return 0;
}
Q: 13By default, in C++, the members of a class are ______, while, by default the member of structure are ______.
Option C
In C++, the member of class whether variables or functions are private by default. This is an important basic concept in Object-Oriented Programming because it helps in data hiding.
struct Student
{
int marks;
};
int main()
{
Student s;
s.marks=101; // Works fine (public access).
return 0;
}
class Student
{
int marks;
};
int main()
{
Student s;
s.marks=101; // Compile time error (private access).
return 0;
}
Q: 14The object of class can directly access?
Option B
In C++, access specifiers such as private, protected, public control how class members can be accessed.
Q: 15How do structures and classes in C++ differ?
Option B
In C++, both structures (struct) and classes (class) are used to group data and functions together. They are almost identical in functionality, but the main difference is in default access specifiers.
Structures (struct): By default, all members are public. This means they can be accessed directly from outside the structure.
Classes (class): By default, all members are private. This means they cannot be accessed directly from outside the class unless specified as public.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.