Q: 1 All members of a structure in C++ are ___________ by default.
public
private
protected
None of the above
[ 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: 2 Find 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;
}
2
12
6
Compile time error
[ 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: 3 In C++, what does a class hold?
Array
Data
Data and functions
More than one of the above
[ 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: 4 In C++, to release the dynamically allocated memory use ________.
remove
delete
release
free
[ 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: 5 The operator :: is known as?
Membership label operator
Double colon operator
Indirect member access operator
Scope resolution operator
[ 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: 6 In C++, all members of a class are _________ by default.
public
private
protected
None of the above
[ 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: 7 When a class is defined, memory allocation is done?
At the same time
When function is called
When values are passed
None of these
[ 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: 8 By default, all member functions defined inside the class are treated as?
Inline function
Static function
External function
Friend function
[ 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: 9 Which of the following statements is correct about the class?
An object is an instance of its class.
A class is an instance of its object.
An object is the instance of the data type of that class.
More than one of the above
[ 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: 10 Which of the following is used to define the members of a class externally in C++?
@
::
#
More than one of the above
[ 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: 11 To allocate the memory dynamically in C++, which keyword is used?
allocate
dynamic
new
malloc
[ 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: 12 How do structures and classes in C++ differ?
In structures, members are private by default whereas in classes, they are public by default
In structures, members are public by default whereas in classes, they are private by default
Structures cannot have private members whereas classes can have
More than one of the above
[ 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: 13 The object of class can directly access?
Private member
Public member
Neither (a) nor (b)
Both (a) and (b)
[ Option B ]
In C++, access specifiers such as private, protected, public control how class members can be accessed.
Q: 14 How do structures and classes in C++ differ?
Structure by default hide every member whereas classes do not
In structure, members are public by default, whereas, in classes, they are private by default
Structures cannot have private members, whereas classes can have
More than one of the above
[ 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.
Q: 15 Which of the following cannot be declared static in C++?
Class
Member variables
Constructor
Function
[ Option C ]
In C++, static can be used in various contexts, but there are restrictions:
(1) You can declare static members inside a class, but not the class itself as static in global scope. However, nested classes inside a class can be declared static. So, this is a bit tricky, but not the correct answer for “cannot be declared static.”
(2) The member variables can be declared static. They belong to the class, not to individual objects.
(3) The functions can be declared as static, either inside a class (meaning it is a class-level function) or in global scope.
(4) The constructor cannot be declared as static in C++, because a constructor is meant to create an instance of a class. But a static function does not have access to this pointer, i.e., it is not tied to any instance. Hence, declaring a constructor as static makes no sense, and is not allowed.
Q: 16 A system where integer occupy 2 bytes of memory. Find output of the following program?
class platform
{
static int a;
int b,c;
public:
void insertValue() { }
};
int main()
{
platform p1;
cout<<sizeof(p1);
}
8
6
4
12
[ Option C ]
In C++, the sizeof operator applied to an object returns the memory occupied by its non-static data members only.
class platform
{
static int a; // Static Data Member.
int b, c; // Non-Static Data Members.
};
The question clearly states that an integer occupies 2 bytes of memory, so b requires 2 bytes and c also requires 2 bytes. Therefore, the total memory occupied by the object p1 is 4 bytes.
Q: 17 Member function can be defined
Only inside the class
Only outside the class
Inside as well as outside class
None of these
[ Option C ]
In C++, a member function of a class can be defined either inside the class or outside the class.
When a member function is defined inside the class definition, it is usually treated as an inline function, which may improve performance.
class Demo
{
public:
void show()
{
cout<<"Hello from show()";
}
};
A member function can also be defined outside the class by using the scope resolution operator (::), which helps in keeping the class definition clean and readable.
class Demo
{
public:
void show();
};
void Demo::show()
{
cout<<"Hello from show()";
}
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.