? Programming Fundamentals
? Programming Fundamentals
📗 Long Questions
cpp
CopyEdit
cout << "Enter name:";
cin >> name;
#include<iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
}
if (num % 2 == 0)
cout << "Even";
else
cout << "Odd";
cpp
CopyEdit
int day = 2;
switch(day) {
case 1: cout << "Mon"; break;
case 2: cout << "Tue"; break;
default: cout << "Other";
}
10. Write a program to check if a number is positive, negative, or zero using if-else-
if.
int n;
cin >> n;
if (n > 0)
cout << "Positive";
else if (n < 0)
cout << "Negative";
else
cout << "Zero";
int day;
cin >> day;
switch(day) {
case 1: cout << "Monday"; break;
case 2: cout << "Tuesday"; break;
// ...
default: cout << "Invalid day";
}
int i = 1;
while(i <= 3) {
cout << i;
i++;
}
2:
int i = 1;
do {
cout << i;
i++;
} while(i <= 3);
cpp
CopyEdit
for(int i=1; i<=2; i++) {
for(int j=1; j<=3; j++)
cout << "*";
}
int n;
{
if(n == 1) return 1;
return n * factorial(n-1);
}
int a[5];
for(int i=0; i<5; i++)
cin >> a[i];
for(int i=0; i<5; i++)
cout << a[i];
int a[2][2];
for(int i=0; i<2; i++)
for(int j=0; j<2; j++)
cin >> a[i][j];
for(int i=0; i<2; i++)
for(int j=0; j<2; j++)
cout << a[i][j];
#include<iostream>
using namespace std;
int main() {
string name;
getline(cin, name);
cout << name;
}
struct Student {
string name; int age;
};
int x = 5;
int *p = &x;
cout << *p; // prints 5
📗 Long Questions
📘 Short Questions
1. What is Object-Oriented Programming (OOP)?
OOP is a programming style that uses objects and classes to organize code. It focuses on
real-world concepts.
2. What is a class in C++?
A class is a blueprint for creating objects. It defines properties (variables) and behaviors
(functions).
3. What is an object?
An object is an instance of a class. It has its own values for the class’s variables and
functions.
4. What is data abstraction?
It means showing only necessary details and hiding the complex part from the user.
5. What is inheritance?
Inheritance lets one class (child) use properties and functions of another class (parent).
6. What is polymorphism?
Polymorphism means one function behaves differently based on the context. Example:
function overloading.
7. What is function overloading?
Function overloading means having multiple functions with the same name but different
parameters.
8. What are access specifiers?
Access specifiers control the access of class members. They are public, private, and
protected.
9. What is the difference between class and object?
Class is a plan or template; object is the real thing created from that plan.
10. What is a constructor?
A constructor is a special function that runs automatically when an object is created.
11. What is destructor?
A destructor is a special function that runs when an object is destroyed, usually to free
memory.
📗 Long Questions
#include<iostream>
using namespace std;
int main()
{
return 0;
}