A class is a blueprint or template for creating objects. It defines properties (data members) and behaviors (member functions) that the objects created from the class will have.
Click to reveal answer
beginner
What is an object in C++?
An object is an instance of a class. It holds actual values for the properties defined by the class and can use the behaviors (functions) defined in the class.
Click to reveal answer
beginner
How do you define a simple class with one integer property and a function to set it?
Example:
class MyClass {
public:
int value;
void setValue(int v) {
value = v;
}
};
Click to reveal answer
intermediate
What is the difference between public and private members in a class?
Public members can be accessed from outside the class, while private members can only be accessed from within the class itself.
Click to reveal answer
beginner
How do you create an object of a class and call its member function?
Example:
MyClass obj; // create object
obj.setValue(10); // call member function
int x = obj.value; // access property
Click to reveal answer
What keyword is used to define a class in C++?
Adefine
Bobject
Cstruct
Dclass
✗ Incorrect
The keyword 'class' is used to define a class in C++.
Which of the following is true about objects?
AObjects are templates for classes
BObjects are instances of classes
CObjects cannot have functions
DObjects are the same as classes
✗ Incorrect
Objects are instances created from classes.
What access specifier allows members to be accessed outside the class?
Aprivate
Bprotected
Cpublic
Dinternal
✗ Incorrect
Public members can be accessed from outside the class.
How do you call a member function named 'setValue' on an object 'obj'?
Aobj.setValue()
BsetValue.obj()
Cobj->setValue()
Dobj:setValue()
✗ Incorrect
Use dot notation: obj.setValue() to call a member function.
Which of these is NOT a member of a class?
AObjects
BMember functions
CData members
DConstructors
✗ Incorrect
Objects are instances of classes, not members inside a class.
Explain what a class and an object are in your own words.
Think about how a cookie cutter (class) makes cookies (objects).
You got /3 concepts.
Describe how access specifiers like public and private affect class members.
Imagine a house with a front door (public) and a locked safe inside (private).
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a class in C++?
easy
A. To group data and functions that represent a real-world concept
B. To execute a program faster
C. To store only numbers
D. To replace variables with constants
Solution
Step 1: Understand what a class represents
A class groups related data (variables) and functions (methods) to model real-world things.
Step 2: Compare options with the definition
Only To group data and functions that represent a real-world concept correctly describes this purpose; others are unrelated to classes.
Final Answer:
To group data and functions that represent a real-world concept -> Option A
Quick Check:
Class = group data + functions [OK]
Hint: Classes bundle data and actions together [OK]
Common Mistakes:
Thinking classes only store numbers
Confusing classes with program speed
Believing classes replace variables
2. Which of the following is the correct way to declare a class named Car in C++?
easy
A. class Car { };
B. Car class { };
C. class = Car { };
D. class Car();
Solution
Step 1: Recall C++ class declaration syntax
The correct syntax is: class ClassName { }; with a semicolon at the end.
Step 2: Check each option
class Car { }; matches the correct syntax. Options A, C, and D have syntax errors or wrong order.
Final Answer:
class Car { }; -> Option A
Quick Check:
class ClassName { }; syntax [OK]
Hint: Remember: class name follows 'class' keyword, ends with semicolon [OK]
Common Mistakes:
Writing 'Car class' instead of 'class Car'
Missing semicolon after class body
Using parentheses after class name
3. What will be the output of this C++ code?
class Box {
public:
int length;
void setLength(int len) { length = len; }
int getLength() { return length; }
};
int main() {
Box box1;
box1.setLength(10);
std::cout << box1.getLength();
return 0;
}
medium
A. length
B. 0
C. Compilation error
D. 10
Solution
Step 1: Understand the class methods
The method setLength sets the length to 10 for box1. The method getLength returns this value.
Step 2: Trace the output statement
std::cout << box1.getLength(); prints the value 10 stored in length.
Final Answer:
10 -> Option D
Quick Check:
setLength(10) then getLength() = 10 [OK]
Hint: Setter sets value, getter returns it [OK]
Common Mistakes:
Forgetting to call setLength before getLength
Expecting default value 0 without initialization
Confusing method names
4. Identify the error in this class definition:
class Person {
string name;
int age;
public:
void setName(string n) { name = n; }
void setAge(int a) { age = a; }
};
medium
A. Missing semicolon after class definition
B. Missing public: before data members
C. Missing #include <string> header
D. Incorrect function return types
Solution
Step 1: Check class syntax
The class ends with a semicolon, and functions have correct return types (void). Data members are private by default, which is allowed.
Step 2: Check for missing includes
The code uses string but does not include <string> header, which is required for std::string.
Final Answer:
Missing #include <string> header -> Option C
Quick Check:
Use #include <string> for string type [OK]
Hint: Always include headers for standard types [OK]
Common Mistakes:
Assuming private members need public keyword
Forgetting to add semicolon after class
Confusing function return types
5. Given this class:
class Student {
int id;
string name;
public:
Student(int i, string n) : id(i), name(n) {}
void display() {
std::cout << "ID: " << id << ", Name: " << name << std::endl;
}
};
int main() {
Student s1(101, "Alice");
s1.display();
return 0;
}
What concept does this code demonstrate?
hard
A. Inheritance between classes
B. Constructor initializing object data
C. Function overloading
D. Static class members
Solution
Step 1: Identify the special function in the class
The function Student(int i, string n) : id(i), name(n) {} is a constructor initializing the object's data members.
Step 2: Check other options
There is no inheritance, no function overloading, and no static members shown.
Final Answer:
Constructor initializing object data -> Option B
Quick Check:
Constructor sets initial values [OK]
Hint: Look for function named as class for constructor [OK]
Common Mistakes:
Confusing constructor with regular method
Thinking this shows inheritance
Assuming static members without keyword
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.