IDIT1010 – Important Question Answers
for Backlog Exam
Q2(a) Write the differences between machine language, assembly language,
and high-level languages.
**Machine Language:** Low-level binary code (0s and 1s) understood directly by
computers. Fastest execution but very hard to write.
**Assembly Language:** Uses mnemonics (e.g., ADD, SUB). Easier than machine
language but still hardware-specific. Requires assembler.
**High-Level Language:** English-like syntax (e.g., C, Python). Easy to understand,
portable, and requires a compiler or interpreter.
Q2(b) Write the interactive program to input operators like “+ - * /” and two
integers, and display result using switch statement.
```c
#include <stdio.h>
int main() {
int a, b;
char op;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Enter operator (+, -, *, /): ");
scanf(" %c", &op);
switch(op) {
case '+': printf("Result: %d", a + b); break;
case '-': printf("Result: %d", a - b); break;
case '*': printf("Result: %d", a * b); break;
case '/': if(b != 0) printf("Result: %d", a / b);
else printf("Cannot divide by zero."); break;
default: printf("Invalid operator");
}
return 0;
}
```
Q3(a) Explain while loop with syntax and use.
**Syntax:**
```c
while(condition) {
// code
}
```
**Use:** Executes code repeatedly while the condition is true. Used when the number of
iterations is not known beforehand.
Q3(b) Write a program to print even numbers between 1 to 100 using for loop.
```c
#include <stdio.h>
int main() {
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0)
printf("%d ", i);
}
return 0;
}
```
Q4: Define the following terms: Compiler, Variable, Constant, Operator, Loop.
- **Compiler:** Translates entire C code into machine code before execution.
- **Variable:** A named memory location used to store data.
- **Constant:** A value that does not change during program execution.
- **Operator:** A symbol that performs operations (e.g., +, -, *, /).
- **Loop:** A control structure to repeat a block of code (e.g., for, while).
Q2(a) Define: Array. Declare an array of 10 students’ marks. What are the
different ways to initialize this array?
**Array:** A collection of similar data types stored in contiguous memory locations.
**Declaration:** `int marks[10];`
**Initialization:**
1. Static: `int marks[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};`
2. Partial: `int marks[10] = {10, 20, 30};` (rest become 0)
3. Runtime: Using loop and user input.
Q2(b) Write a program to input 10 elements from user using Array and add all
the elements and print result.
```c
#include <stdio.h>
int main() {
int a[10], sum = 0;
for (int i = 0; i < 10; i++) {
printf("Enter number %d: ", i+1);
scanf("%d", &a[i]);
sum += a[i];
}
printf("Sum = %d", sum);
return 0;
}
```
Q3(a) Write a C program to read one string from user and print the length
(without using built-in function).
```c
#include <stdio.h>
int main() {
char str[100];
int i = 0;
printf("Enter a string: ");
scanf("%s", str);
while (str[i] != '\0') {
i++;
}
printf("Length = %d", i);
return 0;
}
```
Q3(b) Explain the function definition, function call and declaration with
example.
**Function Declaration:** `int add(int, int);`
**Function Definition:**
```c
int add(int a, int b) {
return a + b;
}
```
**Function Call:** `int sum = add(3, 5);`
Q3(b) What do you mean by recursion? Write a C program to find factorial of a
number using recursion.
**Recursion:** A function calling itself.
```c
#include <stdio.h>
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
int n;
printf("Enter number: ");
scanf("%d", &n);
printf("Factorial = %d", factorial(n));
return 0;
}
```
Q4: Discuss the role of switch statement in C. Compare it with if-else.
**Switch:** Used when multiple conditions are based on a single variable.
```c
switch(choice) {
case 1: break;
case 2: break;
default: break;
}
```
**if-else:** More flexible, used for range or complex conditions.
Q4 (alternate): Explain different symbols used in flowcharts.
- **Terminator:** Start/End (Oval)
- **Process:** Task or action (Rectangle)
- **Decision:** Yes/No (Diamond)
- **Input/Output:** Data input/output (Parallelogram)
- **Arrow:** Flow direction