Q1.
A social media platform wants to manage user text posts efficiently.
Create a program that:
- Store a post’s author, timestamp, content, mood, and like count.
- Allow adding or removing a like.
- Prevent the like count from going below zero.
- Display the post details.
Instructions
Create a class for TextPost to store:
- Author (e.g., “Radha Mohan”)
- Timestamp (e.g., “2025-06-05 14:30”)
- Content (e.g., Hello, world! Excited to share my first text post.”)
- Mood (e.g., “Excitement”)
- Likes (initially 0)
Implement member functions to:
- Display all post details (author, timestamp, likes, content, mood).
- Set or update the content.
- Increment likes.
- Decrement likes (if likes > 0).
When a post object is destroyed, print this as a notification:
TextPost by <author> at <timestamp> is being deleted. Final likes: <likes>
Expected Output
// Post example 1 (valid):
Post:
Author: radha_mohan
Timestamp: 2025-06-05 14:30
Likes: 2
Content: Hello, world! Excited to share my first text post.
Mood: Happy
// Updating the content to "Hello, World! Excited to share my first text post here."
Post content updated successfully.
// Displaying the post again:
Post:
Author: radha_mohan
Timestamp: 2025-06-05 14:30
Likes: 2
Content: Hello, World! Excited to share my first text post here.
Mood: Happy
// Decrementing likes:
// Displaying the post again:
Post:
Author: radha_mohan
Timestamp: 2025-06-05 14:30
Likes: 1
Content: Hello, World! Excited to share my first text post here.
Mood: Happy
// Incrementing likes multiple times:
// Displaying the post again:
Post:
Author: radha_mohan
Timestamp: 2025-06-05 14:30
Likes: 10
Content: Hello, World! Excited to share my first text post here.
Mood: Happy
// Post example 2 (empty author & timestamp):
Post:
Author: UNKNOWN
Timestamp: UNKNOWN
Likes: 0
Content: Guess, who am I!
Mood:
// When the program ends:
Post by radha_mohan at 2025-06-05 14:30 is being deleted. Final likes: 2
Post by UNKNOWN at UNKNOWN is being deleted. Final likes: 0Q2.
What will be the output of the following programs?
You have to predict the output yourself without running/executing the code.
I.
#include <iostream> class Foo { private: int m_x; int m_y; public: Foo(int x, int y) { std::cout << "Foo(" << x << ", " << y << ") constructed\n"; } void print() { std::cout << "Foo(" << m_x << ", " << m_y << ")\n"; } }; int main() { Foo foo{6, 7}; foo.print(); return 0; }II.
#include <iostream> class Foo { private: int m_x; int m_y; public: Foo(int x, int y) { m_x = x; m_y = y; } void print() { std::cout << "Foo(" << m_x << ", " << m_y << ")\n"; } }; int main() { Foo foo{5, true}; foo.print(); return 0; }III.
#include <iostream> class Foo { private: int m_x; int m_y; public: Foo(int x, int y) : m_x{ x } , m_y{ y } { std::cout << "Foo(" << x << ", " << y << ") constructed\n"; } void print() { std::cout << "Foo(" << m_x << ", " << m_y << ")\n"; } }; int main() { Foo foo{6, 7}; foo.print(); return 0; }IV.
#include <iostream> class Foo { private: int m_x; int m_y; public: Foo(int x, int y) : m_y{ x } , m_x{ m_y } { std::cout << "Foo(" << x << ", " << y << ") constructed\n"; } void print() { std::cout << "Foo(" << m_x << ", " << m_y << ")\n"; } }; int main() { Foo foo{6, 7}; foo.print(); return 0; }V.
#include <iostream> class Foo { private: int m_x; int m_y = 2; int m_z; public: Foo(int x) : m_x{ x } { std::cout << "Foo constructed\n"; } void print() const { std::cout << "Foo(" << m_x << ", " << m_y << ", " << m_z << ")\n"; } }; int main() { Foo foo{6}; foo.print(); return 0; }
Q3.
What will be the output of the following programs?
You have to predict the output yourself without running/executing the code.
I.
#include <iostream> class Foo { Foo() { std::cout << "Foo default constructed\n"; } }; int main() { Foo foo; return 0; }II.
#include <iostream> class Foo { private: int m_x; int m_y; public: Foo(int x = 0, int y = 0) : m_x{x} , m_y{y} { std::cout << "Foo(" << m_x << ", " << m_y << ") constructed\n"; } }; int main() { Foo foo1; Foo foo2{6, 7}; return 0; }III.
#include <iostream> class Foo { private: int m_x; int m_y; public: Foo() { std::cout << "Foo constructed\n"; } Foo(int x = 0, int y = 0) : m_x{ x } , m_y{ y } { std::cout << "Foo(" << m_x << ", " << m_y << ") constructed\n"; } }; int main() { Foo foo1; Foo foo2{6, 7}; return 0; }IV.
#include <iostream> class Foo { private: int m_x; int m_y; public: Foo(int x, int y) : m_x{ x } , m_y{ y } { std::cout << "Foo(" << m_x << ", " << m_y << ") constructed\n"; } }; int main() { Foo foo; return 0; }
Q4.
A Smart Thermostat is installed in a home to maintain a comfortable and safe room temperature.
- The temperature should always stay within a reasonable limit to ensure comfort and efficiency (i.e. 10°C to 40°C).
- Users can check the current temperature and set a new one.
- If the user tries to set a temperature outside the allowed range, the system should automatically adjust it to the nearest valid temperature and warn the user.
- The thermostat should indicate whether heating or cooling is active based on the following logic:
- If the temperature is below 20°C, Heating mode should turn ON.
- If the temperature is above 30°C, Cooling mode should turn ON.
- If the temperature is between 20°C and 30°C, the room is comfortable, so no heating or cooling is required.
- The users can get useful information from thermostat, such as:
- The current temperature.
- Whether heating or cooling is required based on the set temperature.
- The energy-saving mode status, which activates if the temperature is within an efficient range.
Expected Output
// --- Initializing Thermostat with 18°C ---
Current Temperature: 18°C
Status: Heating Mode ON (Room is too cold)
Energy-Saving Mode: OFF
// --- Attempting to Set Temperature to 32°C ---
Warning: Temperature too high! Setting to maximum 30°C.
Current Temperature: 30°C
Status: Cooling Mode ON (Room is too warm)
Energy-Saving Mode: OFF
// --- Attempting to Set Temperature to 22°C ---
Temperature set to 22°C.
Current Temperature: 22°C
Status: Temperature is comfortable. No heating or cooling needed.
Energy-Saving Mode: ON
// --- Attempting to Set Temperature to 10°C ---
Warning: Temperature too low! Setting to minimum 16°C.
Current Temperature: 16°C
Status: Heating Mode ON (Room is too cold)
Energy-Saving Mode: OFFQ5.
What will be the output of the following programs?
You have to predict the output yourself without running/executing the code.
I. (Adv.)
#include <iostream> class User { private: int m_a; int m_b = 0; public: User() {} int a() { return m_a; } int b() { return m_b; } }; class Default { private: int m_a; int m_b = 0; public: Default() = default; int a() { return m_a; } int b() { return m_b; } }; class Implicit { private: int m_a; int m_b = 0; public: int a() { return m_a; } int b() { return m_b; } }; int main() { User user{}; std::cout << user.a() << ' ' << user.b() << '\n'; Default def{}; std::cout << def.a() << ' ' << def.b() << '\n'; Implicit imp{}; std::cout << imp.a() << ' ' << imp.b() << '\n'; return 0; }II.
#include <iostream> #include <string> class Employee { private: std::string m_name; int m_id; public: Employee() { } Employee(std::string name, int id) : m_name{name} , m_id{id} { } void print() { std::cout << "Employee(" << m_name << ", " << m_id << ")\n"; } }; int main() { Employee e1{"Jinie", 135}; e1.print(); Employee e2{}; e2.print(); return 0; }III.
#include <iostream> #include <string> class Player { private: std::string m_name; int m_health; public: Player(std::string name, int health) : m_name{name}, m_health{health} { } void print() { std::cout << "Player(" << m_name << ", " << m_health << ")\n"; } }; int main() { Player p1{"Jinie", 100}; p1.print(); Player p2{}; p2.print(); return 0; }
Q6.
A Vending Machine allows users to insert money, select a product, and receive change. However, if a user inserts money but does not select a product, the machine should automatically return the money when the session ends (e.g., due to inactivity or power failure). This ensures that no money remains stuck in the machine.
Instructions
Create a VendingMachine class that allows users to:
- Insert money into the machine.
- Select a product (if enough money is inserted).
- Dispense the product and return any change.
- Automatically return the money if the user does not select a product before exiting or if the machine shuts down unexpectedly.
Expected Output
// case 1: Successful Purchase
Inserted ₹50 into the vending machine.
Selected: Chips (₹30)
Product dispensed. Change returned: ₹20
// case 2: User exits without selecting a product
Inserted ₹50 into the vending machine.
User exited without selecting a product.
Money returned: ₹50
// case 3: Unexpected Shutdown
Inserted ₹100 into the vending machine.
Unexpected shutdown detected! Returning ₹100 to the user.
System exited.Q7.
What will be the output of the following programs?
You have to predict the output yourself without running/executing the code.
I.
#include <iostream> class Foo { ~Foo() { std::cout << "Destructor called.\n"; } }; int main() { Foo foo; return 0; }II.
#include <iostream> class User { private: int m_id {}; public: User(int id) : m_id { id } { std::cout << "Constructing User Object: " << m_id << '\n'; } ~User() { std::cout << "Destructing User Object: " << m_id << '\n'; } int getID() { return m_id; } }; int main() { User user1{1001}; User user2{1010}; return 0; }
Q8.
A text editor should ensure that users do not lose their progress, whether they manually save or forget to do so. The system should always create a backup in case of an unexpected shutdown.
Instructions
Design a TextEditor class that manages a document’s lifecycle. The system should:
- Display a message when a new document is created.
- Allow the user to write and edit the document.
- Provide an option to manually exit the editor.
- When exiting, prompt the user to save manually or not.
- If the user chooses to save, save the document normally.
- If the user chooses not to save, still create a backup save for safety.
- If the program closes unexpectedly, the destructor should automatically perform a backup save to prevent data loss.
Expected Output
// case 1: User chooses to manually save
Document 'ProjectNotes.txt' created.
Editing document...
User exits the editor.
Do you want to save manually? (y/n): y
Document saved successfully!
Backup save created.
Closing the text editor...
System exited.
// case 2: User chooses not to save
Document 'ProjectNotes.txt' created.
Editing document...
User exits the editor.
Do you want to save manually? (y/n): n
Backup save created.
Closing the text editor...
System exited.
// case 3: Unexpected shutdown
Document 'ProjectNotes.txt' created.
Editing document...
Unexpected shutdown detected!
Auto-saving unsaved changes...
Backup save created.
System exited.Q9.
A Smart Car Energy Management System is designed to optimize battery usage and adjust driving modes for efficient performance.
- The battery level should always remain between 0% and 100%.
- The system should display warnings when the battery is low (below 15%) and prevent driving when the battery is at 0%.
- The car can also boost the battery, but the level cannot exceed 100%.
- Users can manually switch the drive mode:
- Sport Mode requires at least 50% battery. It cannot be activated if the battery is below this threshold.
- If the battery level drops below 30%, the system automatically switches to Eco Mode unless manually overridden. The user can only override it to Normal Mode (not Sport Mode).
- Normal Mode has no specific battery requirements, meaning it can be set at any battery level.
- Users can get useful information from the system, such as:
- The current drive mode.
- The current battery level.
- The car’s status (e.g., low battery warning or driving condition).
Expected Output
// --- Initializing Smart Car with 50% Battery ---
Drive Mode: Normal Mode (Balanced)
Battery Level: 50%
Car Status: Car is running smoothly.
// --- Attempting to switch to Sport Mode ---
Error: Sport Mode requires at least 50% battery to activate.
// --- Driving 10 km in Normal Mode ---
Drove 10 km. Battery consumed: 30%.
Drive Mode: Eco Mode (Energy Saving)
Drive Mode: Eco Mode (Energy Saving)
Battery Level: 20%
Car Status: Low battery! Recharge soon.
// --- Boosting Battery by 40% ---
Boosted battery by 40%. Current level: 60%.
Drive Mode: Normal Mode (Balanced)
Drive Mode: Normal Mode (Balanced)
Battery Level: 60%
Car Status: Car is running smoothly.
// --- Driving 30 km in Normal Mode ---
Drove 30 km. Battery consumed: 90%.
Drive Mode: Eco Mode (Energy Saving)
Drive Mode: Eco Mode (Energy Saving)
Battery Level: 15%
Car Status: Low battery! Recharge soon.
// --- Attempting to switch to Sport Mode ---
Error: Sport Mode requires at least 50% battery to activate.
// --- Attempting to override to Sport Mode while in Eco Mode ---
Error: Cannot switch to Sport Mode. Battery is too low.
Drive Mode: Normal Mode (Balanced)
Battery Level: 60%
Car Status: Car is running smoothly.Q10.
A bank wants to manage customer accounts efficiently.
Create a program that:
- Store an account holder’s name, account number and balance. While setting the details, invalid account details should be handled (like negative balance, empty name, etc.).
- Allow depositing money into the account.
- Allow withdrawing money, charging a fee on each withdrawal as follows:
- If withdrawal amount > 10000, fee = 0.15% of the amount.
- If withdrawal amount is between 10000 and 20000 (inclusive), fee = 0.25% of the amount.
- Otherwise, no fee.
- Prevent any withdrawal if the balance is insufficient to cover both the amount and the fee.
- Display the account details.
Instructions
- Create a class for BankAccount to store:
- Account Holder’s Name (e.g., Ramesh Panipuriwala)
- Account Number (e.g., 78563249)
- Balance (e.g., rs.50000)
- Implement member functions to:
- Display the account details.
- Deposit money into the account (ignore if amount < 0).
- Withdraw money (if there is enough balance, apply the transaction fees according to the rules).
- Getter function to return the balance.
- In
main(), demonstrate:- Creating an object of BankAccount class, setting valid relevant details.
- Deposite some money, then withdraw some money multiple times (with and without transaction fees), and display the account details after each transaction.
- Use the access function to check the current balance.
- Try to withdraw more money than the current balance to verify that the withdrawal is blocked.
- Finally, create an object of BankAccount class with invalid inputs (e.g., empty name, negative balance, or account number).
Expected Output
// Creating a new account (Valid)
Account Details:
Name: Ramesh Panipuriwala
Account Number: 78563249
Balance: rs.50000.00
// After depositing rs.15000:
Account Details:
Name: Ramesh Panipuriwala
Account Number: 78563249
Balance: rs.65000.00
// After withdrawing rs.2500 (no fee):
Account Details:
Name: Ramesh Panipuriwala
Account Number: 78563249
Balance: rs.62500.00
// After withdrawing rs.12500:
// Checking the balance:
Balance: rs.49981.25
// Extra transaction fee of rs.18.75 (0.15% of rs.12500) applied for amount > 10000:
// After withdrawing rs.23000:
Account Details:
Name: Ramesh Panipuriwala
Account Number: 78563249
Balance: rs.26923.75
// Extra transaction fee of rs.57.5 (0.25% of rs.23000) applied for amount > 20000:
// Attempting to withdraw rs.30000 (insufficient balance):
Insufficient balance! Withdrawal failed.
// Creating a new account (Invalid)
Account Details:
Name: UNKNOWN
Account Number: 00000000
Balance: rs.0.00
// Setting default values for invalid account details like empty name, negative balance etc.: