Classes
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[];
Q2. What is the default access specifier for members of a C++ class?
• A) public
• B) private
• C) protected
• D) internal
Answer: B) private
• A) void Car() { }
• B) Car(void) { }
• C) int Car() { }
• D) Car(int a) { }
Answer: B) Car(void) { }
• 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() {
return 0;
}
2
• C) The constructor would not work for age initialization
• D) The program would not compile
#include <iostream>
class BankAccount {
private:
double balance;
public:
BankAccount(double initialBalance) {
balance = (initialBalance >= 0) ? initialBalance : 0;
}
double getBalance() {
return balance;
}
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?
3
• C) Runtime error when running the program.
• D) The program will execute but output nothing.
#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);
return 0;
}
Q10. What will be the output of the following code when it runs?
Q11. What happens when you create an object Rectangle rect3; without
passing any parameters?
• A) Area of rect3: 10
• B) Area of rect3: 21
• C) Area of rect3: 1
• D) Compilation error
#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?
Q17. If the public access specifier is removed from the Car class,
what will happen?
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?
#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;
return 0;
}
• A) int
9
• B) void
• C) float
• D) double
Answer: D) double
Q24. If the access specifier public was changed to private, what would
happen when accessing room1.length in main()?
10