0% found this document useful (0 votes)
34 views

Classes

explanation of classes in cpp

Uploaded by

aa5371596
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Classes

explanation of classes in cpp

Uploaded by

aa5371596
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Classes

Q1. What is the correct way to define a simple class in C++ named Student with no
members?

• A) class Student { };
• B) class Student() { };
• C) Student { };
• D) class Student[];

Answer: A) class Student { };

Q2. What is the default access specifier for members of a C++ class?

• A) public
• B) private
• C) protected
• D) internal

Answer: B) private

Q3. Which of the following is the correct way to declare a constructor


for a class Car?

• A) void Car() { }
• B) Car(void) { }
• C) int Car() { }
• D) Car(int a) { }

Answer: B) Car(void) { }

Q4. Which keyword is used to define a member function outside of the


class?

• A) ::
• B) ->
• C) ::=
• D) :

Answer: A) ::

1
#include <iostream>

class Person {
public:

std::string name;
int age;

Person(std::string n, int a) {
name = n;
age = a;
}

void displayInfo() {
std::cout << "Name: " << name << ", Age: " << age << std::endl;
}
};

int main() {

Person person1("Alice", 30);


person1.displayInfo();

return 0;
}

Q5.What will the output of the code ?

• A) Name: Alice, Age: 30


• B) Name: Alice, Age: Unknown
• C) Compilation error
• D) Runtime error

Answer: A) Name: Alice, Age: 30

Q6.What would be the output if the Person class's constructor is


modified as follows?

Person(std::string n, int a) : name(n), age(a) {}


• A) Compilation error
• B) It would behave the same as Person(std::string n, int a)
{ name = n; age = a; }

2
• C) The constructor would not work for age initialization
• D) The program would not compile

Answer: B) It would behave the same as Person(std::string n, int a)


{ name = n; age = a; }

#include <iostream>

class BankAccount {
private:
double balance;

public:
BankAccount(double initialBalance) {
balance = (initialBalance >= 0) ? initialBalance : 0;
}

double getBalance() {
return balance;
}

void deposit(double amount) {


if (amount > 0) {
balance += amount;
}
}

void withdraw(double amount) {


if (amount > 0 && amount <= balance) {
balance -= amount;
}
}
};

int main() {
BankAccount myAccount(1000);
return 0;
}

Q7. What will happen if you try to access the balance member variable
directly from main() in the following code?

• A) It will print the balance value.


• B) Compilation error because balance is private.

3
• C) Runtime error when running the program.
• D) The program will execute but output nothing.

Answer: B) Compilation error because balance is private.

Q8. In the BankAccount class, which method is capable of modifying the


balance variable?

• A) Only the constructor


• B) Only the deposit method
• C) Both deposit and withdraw methods
• D) No method can modify balance

Answer: C) Both deposit and withdraw methods

Q9. Why is balance declared as private in the BankAccount class?

• A) To prevent direct access to balance from outside the class.


• B) To allow only the getBalance() method to modify it.
• C) To make it immutable.
• D) To share balance among all instances.

Answer: A) To prevent direct access to balance from outside the class.

#include <iostream>

class Rectangle {
private:
int length;
int width;

public:
Rectangle() {
length = 1;
width = 1;
}

Rectangle(int l, int w) {
length = l;
width = w;
}

int area() {
return length * width;

4
}
};

int main() {
Rectangle rect1;
Rectangle rect2(5, 8);

std::cout << "Area of rect1: " << rect1.area() << std::endl;


std::cout << "Area of rect2: " << rect2.area() << std::endl;

return 0;
}

Q10. What will be the output of the following code when it runs?

• A) Area of rect1: 1, Area of rect2: 40


• B) Area of rect1: 2, Area of rect2: 40
• C) Area of rect1: 1, Area of rect2: 13
• D) Compilation error

Answer: A) Area of rect1: 1, Area of rect2: 40

Q11. What happens when you create an object Rectangle rect3; without
passing any parameters?

• A) It results in a compilation error.


• B) The default constructor Rectangle() is called.
• C) The parameterized constructor Rectangle(int, int) is called.
• D) The program throws an exception at runtime.

Answer: B) The default constructor Rectangle() is called.

Q12. Which is an advantage of using an initializer list in the class


constructor?

• A) It makes the code compile faster.


• B) It allows direct member initialization and can improve
performance.
• C) It prevents the use of default constructors.
• D) It enforces private data member access.

Answer: B) It allows direct member initialization and can improve


performance.
5
Q13. What will be printed if Rectangle rect3(7, 3); is created and
std::cout << "Area of rect3: " << rect3.area() << std::endl; is
called?

• A) Area of rect3: 10
• B) Area of rect3: 21
• C) Area of rect3: 1
• D) Compilation error

Answer: B) Area of rect3: 21

Q14. Which of the following statements is true about method


overloading in C++?

• A) Overloaded methods must have the same return type.


• B) Overloaded methods must have different parameter types or
numbers.
• C) Only public methods can be overloaded.
• D) Constructors cannot be overloaded.

Answer: B) Overloaded methods must have different parameter types or


numbers.

Q15. Which of the following is true about the default constructor?

• A) It must be explicitly defined in every class.


• B) It is provided by the compiler if no constructors are defined.
• C) It takes arguments to initialize the object.
• D) It can only be called manually.

Answer: B) It is provided by the compiler if no constructors are


defined.

#include <iostream>
#include <string>
using namespace std ;
class Car {
public:
string brand;

6
string model;
int year;
};

int main() {

Car carObj1;
carObj1.brand = "BMW";
carObj1.model = "X5";
carObj1.year = 1999;

Car carObj2;
carObj2.brand = "Ford";
carObj2.model = "Mustang";
carObj2.year = 1969;

cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
return 0;
}

Q16. What type of class members are brand, model, and year in the Car
class?

• A) Private data members.


• B) Public data members.
• C) Protected data members.
• D) Static data members.

Answer: B) Public data members.

Q17. If the public access specifier is removed from the Car class,
what will happen?

• A) The program will compile and run normally.


• B) The program will not compile because brand, model, and year
will be private by default.
• C) The program will run but give runtime errors.
• D) The data members will automatically become protected.

Answer: B) The program will not compile because brand, model, and year
will be private by default.

7
Q18. How are the data members brand, model, and year initialized for
carObj1 and carObj2?

• A) They are initialized by the default constructor.


• B) They are initialized using parameterized constructors.
• C) They are initialized using explicit assignment after object
creation.
• D) They are initialized using a copy constructor.

Answer: C) They are initialized using explicit assignment after object


creation.

Q19. What will be the output of the following program?

• A) BMW X5 1999 Ford Mustang 1969


• B) Ford Mustang 1969 BMW X5 1999
• C) Compilation error due to incorrect data assignment.
• D) No output; the code contains logical errors.

Answer: A) BMW X5 1999 Ford Mustang 1969

Q20. Why can carObj1.brand and carObj2.model be accessed directly in


main()?

• A) Because brand and model are declared as static.


• B) Because brand and model are public data members.
• C) Because main() is a friend function of the Car class.
• D) Because the Car class is declared without an access specifier,
making members public by default.

Answer: B) Because brand and model are public data members.

#include <iostream>
using namespace std;

class Room {

public:

8
double length;
double breadth;
double height;

double calculate_area() {
return length * breadth;
}

double calculate_volume() {
return length * breadth * height;
}
};

int main() {

Room room1;

room1.length = 42.5;
room1.breadth = 30.8;
room1.height = 19.2;

cout << "Area of Room = " << room1.calculate_area() << endl;


cout << "Volume of Room = " << room1.calculate_volume() << endl;

return 0;
}

Q21. What is the purpose of the Room class in the code?

• A) To store information about multiple rooms.


• B) To represent the dimensions of a room and provide methods to
calculate its area and volume.
• C) To read user input for room dimensions.
• D) To demonstrate the use of private member functions.

Answer: B) To represent the dimensions of a room and provide methods


to calculate its area and volume.

Q22. What is the return type of the calculate_area() and


calculate_volume() methods?

• A) int

9
• B) void
• C) float
• D) double

Answer: D) double

Q23. What will be the output of the following program when


room1.length = 42.5, room1.breadth = 30.8, and room1.height = 19.2?

• A) Area of Room = 1308 Volume of Room = 25049.6


• B) Area of Room = 1308 Volume of Room = 25188.48
• C) Area of Room = 1310.4 Volume of Room = 25010.8
• D) Compilation error.

Answer: B) Area of Room = 1308 Volume of Room = 25188.48

Q24. If the access specifier public was changed to private, what would
happen when accessing room1.length in main()?

• A) The program would compile and run normally.


• B) The program would give a compilation error due to access
violation.
• C) The program would produce runtime errors.
• D) The length variable would be accessible using get() methods.

Answer: B) The program would give a compilation error due to access


violation.

10

You might also like