C Programming Practice Questions & Answers
Theory Questions
1. Inheritance
Inheritance is an OOP concept where a class (child) inherits properties and behaviors from another
class (parent). Example in C++:
class Animal { public: void speak() { cout << "Animal speaks"; } };
class Dog : public Animal {};
2. Polymorphism
Polymorphism means having many forms. A function or object can behave differently based on the
context.
Example in C++:
class Shape { public: virtual void draw() { cout << "Drawing Shape"; } };
class Circle : public Shape { public: void draw() override { cout << "Drawing Circle"; } };
3. Operators
Arithmetic: +, -, *, /, % (e.g., int sum = 3 + 2;)
Relational: ==, !=, <, > (e.g., if (a > b))
Logical: &&, ||, ! (e.g., if (a > 0 && b > 0))
Assignment: =, +=, -= (e.g., a += 5;)
4. Keywords in C
Examples: int (e.g., int a = 10;), if (e.g., if(a > 0)), return (e.g., return 0;)
5. printf and scanf
printf outputs to console (e.g., printf("Hello");)
scanf reads input (e.g., scanf("%d", &a);)
6. Types of Programming Languages
Low-level: Assembly
High-level: C, Python
OOP: Java, C++
Scripting: JavaScript, Python
7. One-Dimensional Array
Example: int numbers[5] = {1, 2, 3, 4, 5};
8. Components of Flowcharts
Terminal: Oval
Process: Rectangle
Decision: Diamond
Flow lines: Arrows
9. History of C
Developed by Dennis Ritchie in 1972 at Bell Labs. Used to develop UNIX OS.
10. Data Types in C
Basic: int, float, char, double
Derived: arrays, pointers
User-defined: struct, union, enum
Programming Questions
1. Check if a number is positive or negative
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if(num >= 0)
printf("Positive");
else
printf("Negative");
return 0;
}
2. Display first 10 odd numbers
#include <stdio.h>
int main() {
for(int i = 1, count = 0; count < 10; i += 2) {
printf("%d ", i);
count++;
}
return 0;
}
3. Calculate Simple and Compound Interest
#include <stdio.h>
#include <math.h>
int main() {
float p, r, t, si, ci;
printf("Enter P, R, T: ");
scanf("%f %f %f", &p, &r, &t);
si = (p * r * t) / 100;
ci = p * pow((1 + r/100), t) - p;
printf("Simple Interest: %.2f\n", si);
printf("Compound Interest: %.2f\n", ci);
return 0;
}
4. Calculate Area of a Figure (Circle)
#include <stdio.h>
#define PI 3.14
int main() {
float radius, area;
printf("Enter radius: ");
scanf("%f", &radius);
area = PI * radius * radius;
printf("Area = %.2f", area);
return 0;
}
5. Print 1 to 10 numbers in an array
#include <stdio.h>
int main() {
int arr[10];
for(int i = 0; i < 10; i++) {
arr[i] = i + 1;
}
for(int i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
return 0;
}