PPS Mid-1 Important Questions
PPS Mid-1 Important Questions
Ex
%d for integer
%f float
%c for character
#include <stdio.h>
int main() {
int num;
scanf(“%d” , &num);
if (num%2 == 0);
else
return 0;
ex-
printf – used to display output
2. Explain the process of converting source code into executable code. Discuss the role
of a compiler and a linker in this process
Ans-The process of converting source code into executable code involves several
steps:
Source Code: The human-readable code written by the programmer in a high-level
language (e.g., C).
Compilation:
o The compiler takes the source code and translates it into an intermediate
machine language code (object code).
o During this process, the compiler also checks for syntax errors and converts
the high-level code to low-level instructions.
Linking:
o The linker takes the object code generated by the compiler and combines it
with necessary libraries and other object files (if any) to create the final
executable file.
o It resolves references to external functions or variables used in the program,
ensuring that everything the program needs to run is included in the final
executable.
Executable Code: The final output file that the operating system can execute directly.
Compiler Role: Translates source code to machine code, detects syntax errors.
Linker Role: Combines object files and resolves references to create the executable.
3. Write a C program to print the reverse of a given number and also check whether a given
number is palindrome or not.
121
121
123
321
#include <stdio.h>
int main() {
scanf("%d", &num);
originalNum = num;
while (num != 0) {
if (originalNum == reversedNum)
else
return 0;
Explanation:
The program reverses the number by repeatedly extracting the last digit and
constructing the reversed number.
It then compares the original number with the reversed number to check for
palindrome.
int main() {
double a, b, c, discriminant, root1, root2, realPart, imaginaryPart;
discriminant = b * b - 4 * a * c;
return 0;
}
Explanation:
The program calculates the discriminant to determine the nature of the roots and uses
the quadratic formula to compute them.
If the discriminant is positive, roots are real and distinct; if zero, they are real and
equal; and if negative, they are complex.
5. Write an algorithm and draw flow chart to print Fibonacci series from 0 to n.
Start
|
Input n
|
Set a = 0, b = 1
|
Print a, b
|
sum = a + b
|
Is sum <= n? ------> No -----> End
|
Print sum
|
a = b, b = sum
|
Repeat
Unit-2 (Important Questions)
Short Answer Questions
\ ### 1. **Differences between `else if()` and `switch()` statements**:
- **`else if()`**:
- **`switch()`**:
- **Equality (`==`)**: Compares two values to check if they are equal (e.g., `x == 5`).
- **`do-while`**: Executes the loop body at least once before checking the condition.
4. Conditional operator:
- The conditional operator is a shorthand for `if-else` and follows the syntax: `condition ?
expression_if_true : expression_if_false`.
- Example:
int x = 10;
```
- Associative property: Defines the order in which operators of the same precedence are
evaluated.
- Importance: It determines the order of evaluation, which can affect the result in
expressions involving multiple operators.
```c
int x = 5;
```
- Example: In `a + b * c`, the multiplication (`*`) is evaluated before the addition (`+`), as
`*` has higher precedence than `+`.
- **Prefix**: `- + a * b c d`
- **Postfix**: `a b c * + d -`
```c
#include <stdio.h>
int main() {
int i;
printf("%d\n", i);
return 0;
```
- Examples:
int x = 5;
x++; // x becomes 6
int x = 5;
x--; // x becomes 4
int x = 5;
- Binary Operators:
- Examples:
int a = 5, b = 10;
int a = 5, b = 3;
int a = 5, b = 5;
if (a == b) { // true
// Do something
---
#include <stdio.h>
int main() {
char operator;
scanf("%c", &operator);
switch(operator) {
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
if (num2 != 0) {
} else {
break;
default:
printf("Invalid operator.\n");
return 0;
```
---
#include <stdio.h>
int main() {
float percentage;
scanf("%d", &marks[i]);
total += marks[i];
// Calculate percentage
} else {
return 0;
```
---
#include <stdio.h>
int main() {
int n, i, j, isPrime;
scanf("%d", &n);
// Check if the number has any divisors other than 1 and itself
if (i % j == 0) {
break;
if (isPrime) {
printf("\n");
return 0;
```
---
- Examples: `==` (equal), `!=` (not equal), `>` (greater than), `<` (less than), `>=` (greater
than or equal to), `<=` (less than or equal to).
- Example program:
#include <stdio.h>
int main() {
if (a < b) {
} else {
return 0;
```
- Example program:
#include <stdio.h>
int main() {
int a = 1, b = 0;
if (a && b) {
} else {
return 0;
```
- Examples: `&` (AND), `|` (OR), `^` (XOR), `~` (NOT), `<<` (left shift), `>>` (right shift).
- Example program:
#include <stdio.h>
int main() {
int a = 5, b = 9;
return 0;
```