0% found this document useful (0 votes)
4 views19 pages

? Programming Fundamentals

The document covers programming fundamentals over five weeks, focusing on problem-solving, programming constructs, control flow, looping structures, and functions in C++. Key concepts include the Von-Neumann architecture, algorithms, flowcharts, and various control statements like if, switch, and loops. It emphasizes the importance of structured programming and debugging for efficient code development.

Uploaded by

zaheerudeen808
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)
4 views19 pages

? Programming Fundamentals

The document covers programming fundamentals over five weeks, focusing on problem-solving, programming constructs, control flow, looping structures, and functions in C++. Key concepts include the Von-Neumann architecture, algorithms, flowcharts, and various control statements like if, switch, and loops. It emphasizes the importance of structured programming and debugging for efficient code development.

Uploaded by

zaheerudeen808
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/ 19

💻 Programming Fundamentals

✅ Week 1 – Introduction to Problem Solving & Von-


Neumann Architecture

📌 Short Questions (Simple English, 3–5 Lines)

1. What is problem solving in programming?


Problem solving means understanding a task and planning how to solve it using
programming. It helps create correct and logical code.
2. What are the basic steps of problem solving?
First understand the problem, then plan a solution, write the algorithm, code it, test the
program, and fix any errors (debugging).
3. What is an algorithm?
An algorithm is a set of clear, step-by-step instructions to solve a problem. It must be
correct, simple, and easy to follow.
4. What is a flowchart?
A flowchart is a diagram that shows the steps of an algorithm using symbols like
rectangles, diamonds, and arrows.
5. What is Von-Neumann Architecture?
Von-Neumann Architecture is a computer model where both data and instructions are
stored in the same memory and processed one by one.
6. What are the main parts of Von-Neumann Architecture?
The key components are Memory, Control Unit, Arithmetic Logic Unit (ALU), Input,
and Output units.
7. What is the function of the Control Unit?
The Control Unit controls the working of the CPU. It reads instructions and tells other
parts what to do.
8. What is input and output in computing?
Input means giving data to the computer (like from a keyboard), and output means
getting the result (like on a screen).
9. Why is problem solving important in programming?
It helps in writing code that is clear, logical, and error-free. It saves time and makes
programming easier.
10. What is debugging?
Debugging is the process of finding and removing errors in a program to make it work
correctly.
11. What does the memory do in a computer?
Memory stores data and instructions temporarily while the computer is working on them.
📗 Long Questions

Explain the steps involved in solving a programming problem.


Solving a programming problem involves six main steps:

o Understanding the problem: Know what the program must do.


o Planning the solution: Decide how to solve it logically.
o Writing the algorithm: List the steps of the solution.
o Coding: Translate the algorithm into a programming language like C++.
o Testing: Run the program to check if it works.
o Debugging: Fix any errors found in the code.
2. What is Von-Neumann Architecture? Describe its parts.
Von-Neumann Architecture is a model of how a computer works. It uses a single
memory to store both data and instructions.
o Memory: Stores data and program instructions.
o Control Unit (CU): Fetches and manages execution of instructions.
o ALU: Performs calculations and logical decisions.
o Input unit: Takes data from outside (e.g., keyboard).
o Output unit: Shows results (e.g., monitor).
This model processes one instruction at a time in a cycle: fetch → decode →
execute.
3. What is an algorithm? Write its features with an example.
An algorithm is a clear and logical set of steps to solve a specific problem.
Features:
o Clear and simple
o Has a starting and ending point
o Uses limited steps
o Solves the problem completely
Example:
To find the largest of two numbers:
o Step 1: Read two numbers
o Step 2: Compare them
o Step 3: Print the larger number
4. Describe a flowchart and its importance in programming.
A flowchart is a visual representation of an algorithm. It uses symbols like:
o Oval: Start/End
o Rectangle: Process
o Diamond: Decision
Flowcharts help programmers understand and explain program logic before
writing code. They are useful for planning and debugging.
5. What are the advantages of using problem solving techniques in programming?
Problem solving helps in writing efficient and correct programs. It improves thinking,
reduces errors, and makes coding faster. It also makes it easier to maintain and update
code in the future.
6. What is the fetch-decode-execute cycle in Von-Neumann Architecture?
This is how a CPU works step-by-step:
o Fetch: Get instruction from memory
o Decode: Understand the instruction
o Execute: Carry out the instruction
This cycle repeats for each instruction in a program.

✅ Week 2 – Basic Programming Constructs & C++


Program Structure

📘 Short Questions (Simple English – 3–5 lines)

1. What are programming constructs?


These are the basic parts used to build a program, like input/output, variables, data types,
and control statements.
2. What is a variable in C++?
A variable is a storage location in memory that holds a value which can change during
program execution.
3. What is a constant in C++?
A constant is a fixed value that doesn’t change while the program is running. It’s declared
using const.
4. What is a data type?
A data type tells the compiler what type of data a variable will store, like int, float,
char, or bool.
5. What are the types of data types in C++?
They include:
o Basic (int, float, char)
o Derived (arrays, functions)
o User-defined (struct, class)
o Pointer types
6. What is the use of ‘cin’ and ‘cout’?
cin takes input from the user, and cout displays output to the screen.
7. What is the structure of a simple C++ program?
It includes:
o Header files
o main() function
o Statements inside curly braces {}
8. What is the ‘main()’ function?
The main() function is the starting point of every C++ program. The code execution
begins from here.
9. What are comments in C++?
Comments are notes written for programmers. They are ignored by the compiler. Use //
for single-line and /* */ for multi-line comments.
10. What is syntax in programming?
Syntax is the set of rules that define how programs must be written in a particular
language.
11. What is a header file?
A header file contains predefined code or functions. #include<iostream> lets us use
cin and cout.
12. Why is using namespace std; used?
It allows you to use standard functions like cout and cin without writing std:: every
time.

📗 Long Questions

1. What are variables and how are they declared in C++?


Variables are used to store data in memory. They are declared with a data type followed
by a name, like int age;. Their value can be changed later. Example: age = 20;.
2. Explain the use of input and output in C++.
In C++, input is taken using cin and output is shown using cout. They belong to the
iostream library. Example:

cpp
CopyEdit
cout << "Enter name:";
cin >> name;

3. What are different types of data in C++?


C++ supports several data types:
o int for integers
o float for decimal numbers
o char for characters
o bool for true/false values
Each type has a specific use.
4. Explain constants in C++ with an example.
A constant holds a value that does not change. It is declared using const.
Example:

const float PI = 3.14;

If you try to change PI, it will cause an error.

5. What are comments in C++ and why are they important?


Comments are used to explain code. They are ignored by the compiler.
o // for single-line
o /* */ for multi-line
Comments make the code easier to understand.
6. Write a simple C++ program to display a message.

#include<iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
}

This program prints “Hello, World!” on the screen.

7. Why is the main() function important in C++?


The main() function is where program execution begins. Without main(), a C++
program won’t run. It usually returns 0 to show successful execution.
8. What is the purpose of header files in C++?
Header files contain code written by others that we can use. For example,
#include<iostream> lets us use cout and cin.
9. Differentiate between variable and constant.
A variable’s value can change, while a constant’s value remains the same.
Example:

 Variable: int age = 20;


 Constant: const int max = 100;

✅ Week 3 – Flow of Control (if, if-else, if-else-if, switch)

📘 Short Questions (3–5 lines)

1. What is flow of control in programming?


Flow of control means the order in which the computer executes statements. It can be
changed using decision-making statements like if, if-else, and switch.
2. What is an if statement?
The if statement checks a condition. If the condition is true, the block runs; otherwise, it
is skipped.
3. What is an if-else statement?
It checks a condition. If it is true, one block runs; if false, another block runs.
4. What is an if-else-if ladder?
It is used when there are multiple conditions. It checks each condition in order until one
is true.
5. What is a switch statement?
A switch selects one out of many blocks based on the value of a variable. It works like a
multiple-choice decision.
6. What is a condition in C++?
A condition is an expression that is either true or false. It is used in decision-making.
7. Which operators are used in conditions?
Relational (<, >, ==, !=) and logical (&&, ||, !) operators are used in conditions.
8. Can we nest if statements?
Yes, you can place one if inside another if to make complex decisions. This is called
nested if.
9. What is the default statement in switch?
The default block runs when no case matches the value. It is optional but useful.
10. What is the difference between if and switch?
if works with all conditions. switch works only with discrete values like numbers or
characters.
11. Why is the break statement used in switch?
break stops the execution after a case is matched. Without it, the next cases will also run.
12. Can we use logical operators in switch?
No, switch only works with one value, not conditions using logical operators.

📗 Long Questions (5–6 lines each, easy wording)

1. Explain the if statement with an example.


The if statement runs a block of code if a condition is true.
Example:
2. int age = 20;

if (age > 18)


cout << "You are adult";

If age is greater than 18, it prints the message.

3. Describe the if-else statement with example.


The if-else statement checks a condition. If true, one block runs; else, another runs.
Example:
4. int num = 5;

if (num % 2 == 0)
cout << "Even";
else
cout << "Odd";

5. What is the purpose of if-else-if ladder?


It is used when we need to check multiple conditions one after another.
Example:
if (marks >= 90)
cout << "A Grade";
else if (marks >= 80)
cout << "B Grade";
else
cout << "Fail";

6. Explain switch statement with example.


switch chooses a block based on a value.
Example:

cpp
CopyEdit
int day = 2;
switch(day) {
case 1: cout << "Mon"; break;
case 2: cout << "Tue"; break;
default: cout << "Other";
}

7. How is switch different from if-else?


o if-else can check ranges and conditions
o switch works with fixed values only
o switch is easier to read for fixed choices
Example: use switch for menu options
8. What are logical operators and how are they used?
Logical operators combine conditions:
o && (AND), || (OR), ! (NOT)
o Example:

if(age > 18 && city == "Lahore")


cout << "Welcome";

9. What is nesting of if statements?


Nesting means placing one if inside another. It helps in complex decisions.
Example:

if (age > 18) {


if (gender == 'M')
cout << "Adult Male";
}

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";

11. Write a switch program to display days of the week.

int day;
cin >> day;
switch(day) {
case 1: cout << "Monday"; break;
case 2: cout << "Tuesday"; break;
// ...
default: cout << "Invalid day";
}

✅ Week 4 – Looping Structures (for, while, do-while)

📘 Short Questions (3–5 lines each)

1. What is a loop in programming?


A loop is a way to repeat a block of code multiple times until a condition is false.
2. What is a for loop?
A for loop repeats code a fixed number of times. It has three parts: initialization,
condition, and increment/decrement.
3. What is a while loop?
A while loop runs a block of code as long as the condition is true. The condition is
checked before each run.
4. What is a do-while loop?
A do-while loop runs the block at least once because the condition is checked after the
loop.
5. What are the parts of a for loop?
Initialization, condition, and update expression (e.g., for(int i=0; i<5; i++)).
6. What is the difference between while and do-while loop?
In while, the condition is checked first. In do-while, the block runs first, then the
condition is checked.
7. What is an infinite loop?
An infinite loop never ends because the condition never becomes false. It may crash the
program.
8. Can we use loops inside loops?
Yes, using one loop inside another is called a nested loop. It is used for complex tasks
like matrix printing.
9. What is a loop counter?
A loop counter is a variable that keeps track of how many times a loop has run.
📗 Long Questions

1. Explain the working of a for loop with example.


A for loop runs when the condition is true and stops when it's false.
Example:
2. for(int i = 1; i <= 5; i++)

cout << i << " ";

It prints numbers from 1 to 5.

3. Describe a while loop with an example.


The while loop checks the condition first. If it’s true, the block runs.
Example:

int i = 1;
while(i <= 3) {
cout << i;
i++;
}
2:
int i = 1;
do {
cout << i;
i++;
} while(i <= 3);

4. What are nested loops? Provide an example.


A loop inside another loop is a nested loop.
Example:

cpp
CopyEdit
for(int i=1; i<=2; i++) {
for(int j=1; j<=3; j++)
cout << "*";
}

5. Write a program to print even numbers from 1 to 10 using for loop.

for(int i=2; i<=10; i+=2)


cout << i << " ";

✅ Week 5 – Functions in C++


📘 Short Questions (3–5 lines each)

1. What is a function in C++?


A function is a block of code that performs a specific task. It helps make the code
reusable and organized.
2. What is a built-in function?
A built-in function is already available in C++, like sqrt(), pow(), or cin, cout.
3. What is a user-defined function?
A function created by the programmer to perform a task is called user-defined.
4. What is function declaration?
Function declaration tells the compiler the function's name, return type, and parameters
before it's used.
5. What are function parameters?
Parameters are values passed to a function when it is called. They are also called
arguments.
6. What is a void function?
A void function does not return any value. It just performs an action.
7. What is a recursive function?
A function that calls itself is called recursive. It continues until a stopping condition is
met.

📗 Long Questions (5–6 lines each)

1. Write a program using a function to add two numbers.

int add(int x, int y) {


return x + y;
}
int main() {
cout << add(4, 5);
return 0;
}

2. Differentiate between built-in and user-defined functions.


o Built-in: Already available, like sqrt()
o User-defined: Created by the programmer
Built-in functions save time, while user-defined functions are customized.
3. Explain function declaration and definition.
o Declaration: Tells compiler about the function (before main)
o Definition: Actual block with the function code
Example:
4. What are parameters and how are they passed?
Parameters are values passed to a function.
Example:
5. Write a program using a function to check even or odd.
int n;
{
if(n % 2 == 0) cout << "Even";
else cout << "Odd";
}

6. What is recursion? Explain with example.


A function that calls itself is recursive.
Example:

int n;
{
if(n == 1) return 1;
return n * factorial(n-1);
}

✅ Week 6 – Arrays (1D and 2D Arrays)

📘 Short Questions (3–5 lines each)

1. What is an array in C++?


An array is a group of similar data items stored under one name. It stores values in
continuous memory locations.
2. How do you declare an array in C++?
Syntax: dataType arrayName[size];
Example: int marks[10];
3. What is the size of an array?
It is the total number of elements the array can store. Declared in square brackets.
Example: int arr[10]; stores 10 items.
4. Can array size be changed during program execution?
No, array size is fixed at the time of declaration in C++.
5. How to input values in a 1D array?
Use a loop like for(int i=0; i<5; i++) cin >> arr[i]; to enter elements.
6. What is the difference between 1D and 2D array?
1D array stores values in a single line; 2D array stores data in a table (rows and columns).
7. Can we store different data types in one array?
No, an array can only store values of the same type.
8. How are 2D array elements accessed?
Use two indices: one for row and one for column. Example: matrix[0][1]
9. What is array initialization?
It means giving values to the array at the time of declaration. Example: int a[3] = {1,
2, 3};
📗 Long Questions

1. Write a program to input and display elements of a 1D array.

int a[5];
for(int i=0; i<5; i++)
cin >> a[i];
for(int i=0; i<5; i++)
cout << a[i];

2. How does indexing work in arrays?


Indexing starts from 0. So the first element is arr[0], second is arr[1], and so on. If
array has 5 elements, the last is arr[4].
3. Write a program to find the sum of all elements in a 1D array.

int a[5], sum = 0;


for(int i=0; i<5; i++)
cin >> a[i];
for(int i=0; i<5; i++)
sum += a[i];
cout << sum;

4. What is a 2D array? Give a simple example.


A 2D array holds data in a matrix form (rows and columns).
Example:

int mat[2][2] = {{1, 2}, {3, 4}};


cout << mat[1][0]; // prints 3

5. Write a program to input and display a 2D array.

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];

6. Write a program to find the largest number in a 1D array.

int a[5], max = a[0];


for(int i=0; i<5; i++)
cin >> a[i];
for(int i=0; i<5; i++)
if(a[i] > max) max = a[i];
cout << max;

7. Compare 1D and 2D arrays with examples.

 1D array: int a[3] = {1, 2, 3}; (single row)


 2D array: int b[2][2] = {{1,2}, {3,4}}; (table format)
1D is for list-type data, 2D is for matrix-type data.

✅ Week 7 – Strings in C++

📘 Short Questions (3–5 lines each)

1. What is a string in C++?


A string is a collection of characters. It is used to store text, like names or messages.
2. How can we declare a string in C++?
There are two ways:
o Using character arrays: char name[20];
o Using string class: string name;
3. What is the difference between character array and string class?
o Character arrays are basic and need manual handling.
o string class is easier and has built-in functions.
4. What is string concatenation?
Joining two strings together is called concatenation. Use + operator or append().
5. How do we compare two strings?
Use == to check if two strings are the same. For character arrays, use strcmp().
6. What are some functions of the string class?
.length(), .append(), .substr(), .find(), .erase() are commonly used.
7. What is the difference between cin and getline()?
cin reads only one word; getline() reads the full sentence.
8. Can we change characters inside a string?
Yes, we can access and change characters using index, like name[0] = 'Z';

📗 Long Questions (5–6 lines each)

1. Explain different ways to declare strings in C++.


Strings can be declared as:
o Character array: char name[20];
o String class: string name;
Character arrays need care with null characters. String class is easier and safer.
2. Write a program to input and display a string using string class.

#include<iostream>
using namespace std;
int main() {
string name;
getline(cin, name);
cout << name;
}

3. Write a program to reverse a string.

string str = "abc";


reverse(str.begin(), str.end());
cout << str; // prints "cba"

4. Write a program to count vowels in a string.

string s; int count = 0;


getline(cin, s);
for(char c : s)
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
count++;
cout << count;

✅ Week 8 – Structures in C++

📘 Short Questions (3–5 lines each)

1. What is a structure in C++?


A structure is a user-defined data type that lets you group different data types under one
name.
2. Why are structures used in C++?
Structures help store related data together, like a student’s name, age, and marks.
3. How do you declare a structure?
Use the struct keyword. Example:

struct Student {
string name; int age;
};

4. What is a structure variable?


A variable declared from a structure type. Example: Student s1;

1. Write a program to input and display student data using structure.

struct Student { string name; int age; };


int main() {
Student s;
cin >> s.name >> s.age;
cout << s.name << s.age;
}

2. What are the benefits of using structures?


o Store different types of data together
o Organize complex data
o Pass full records to functions easily
o Better code readability

✅ Week 9 – Pointers in C++

📘 Short Questions (3–5 lines each)

1. What is a pointer in C++?


A pointer is a variable that stores the memory address of another variable.
2. How do we declare a pointer?
Syntax: dataType *pointerName;
Example: int *ptr;
3. What is the address-of (&) operator?
It gives the memory address of a variable. Example: &a gives the address of a.
4. What is the dereference (*) operator?
It accesses the value stored at a pointer's address. Example: *ptr gives the value at that
memory.
5. What is the use of pointers?
Pointers allow direct memory access, dynamic memory allocation, and passing by
reference.
6. What is a NULL pointer?
A pointer that does not point to any memory location. It is initialized with NULL or
nullptr.
7. What are the advantages of using pointers?

 Faster access to data


 Efficient memory usage
 Used in dynamic memory and data structures

📗 Long Questions (5–6 lines each)

1. Explain pointers in C++ with example.


A pointer holds the address of another variable.
Example:
int a = 10;
int *p = &a;
cout << *p; // Output: 10

Here, *p gives the value stored at address of a.

2. What is the difference between & and * operators?


o & gets the address of a variable
o * gets the value at the address
Example:

int x = 5;
int *p = &x;
cout << *p; // prints 5

✅ Week 10 – File Handling in C++

📘 Short Questions (3–5 lines each)

1. What is file handling in C++?


File handling means reading from and writing data to files stored on the computer.
2. Which header file is used for file handling?
We use <fstream> for file handling in C++. It includes ifstream, ofstream, and

📗 Long Questions

1. What is the difference between ifstream and ofstream?


o ifstream: reads data from files
o ofstream: writes data to files
Both are part of <fstream> and used for file handling.

✅ Week 11 – Object-Oriented Programming (OOP)


Concepts

📘 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

1. Explain the main features of OOP.


o Encapsulation: Protects data
o Abstraction: Hides complexity
o Inheritance: Reuses code
o Polymorphism: One interface, many forms
These features make code easier to manage.
2. What is the role of access specifiers?
They control visibility of class members:
o private: accessible only inside class
o public: accessible everywhere
o protected: accessible in child classes
3. What is the difference between constructor and destructor?
o Constructor: called when object is created
o Destructor: called when object is destroyed
Example:
4. Write a program using inheritance.

class A { public: void show() { cout << "A"; } };


class B : public A {};
int main() { B obj; obj.show(); }

5. Why is OOP better than procedural programming?


OOP manages complexity using classes, objects, and reuse. It supports modular code and
is easier to update and debug.

✅ Week 12 – Final Concepts (Revision, Mini Project,


MCQs)

📘 Short Questions (3–5 lines each)

1. What is the difference between compiler and interpreter?


A compiler translates the whole program at once. An interpreter translates line by line.
2. What are the four pillars of OOP?
Encapsulation, Abstraction, Inheritance, and Polymorphism.
3. What is the purpose of using functions?
Functions reduce repetition and make code easier to manage and reuse.
4. What are data types in C++?
Types of data we use, like int, float, char, bool.
5. What is the use of arrays?
Arrays store multiple values of the same type in a single variable.
6. What are operators in C++?
Symbols that perform operations: +, -, *, /, ==, etc.
7. What is the use of cin and cout?
cin takes input from the user, cout displays output.
8. What is object in OOP?
Object is an instance of a class; it stores data and performs actions.

📗 Long Questions (5–6 lines each)


1. Write a short summary of C++ key concepts.
C++ is an object-oriented language. It includes concepts like variables, data types,
conditions, loops, functions, arrays, pointers, classes, and file handling. It is powerful and
widely used.
2. Describe the structure of a C++ program.
o Preprocessor directives
o Main function
o Variable declarations
o Logic / statements
o Functions
Example:

#include<iostream>
using namespace std;
int main()
{

return 0;
}

3. What are the advantages of OOP?


o Code reuse with inheritance
o Easier debugging with encapsulation
o Cleaner code with abstraction
o Flexibility with polymorphism
4. List and explain five MCQ-type concepts for exams.
o Function syntax: returnType name(params)
o Array index starts from: 0
o cin keyword is used for: Input
o if condition checks: True/False
o Class creates: Object

You might also like