0% found this document useful (0 votes)
33 views18 pages

PPS Mid-1 Important Questions

Uploaded by

gracetheresa38
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)
33 views18 pages

PPS Mid-1 Important Questions

Uploaded by

gracetheresa38
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/ 18

Unit-1 (Important Questions)

Short Answer Questions


1. Write an algorithm to compute the sum of first n numbers.
for i from 1 to n
sum = sum+i
return sum
2. Write an algorithm to compute the sum of odd numbers from 1 to n.
initialize sum = 0
for i from 1 to n
//if I is odd then
sum = sum +i
return sum
3. What is an integer constant, floating constant and character constant? Give valid
examples
Integer constant :
A whole number without decimal is integer constant
Ex-10,20,1000,1000
Floating constant :
A number with decimal point or in exponential is floating constant
Ex.3.14,2.3,4.5
Character constant :
A single character enclosed in single or multiple quotes is called character constant
Ex- “apple”,”ayaan”,”Kaushal”
4. what are Primitive DataTypes? Give valid examples.
Primitive data types : they are data types which are basic type of built data in
programming language
Ex- integer constant ,float constant ,character constant
5. Write the Structure of C program.
#include <stido.h>
int main()
{
// statements
Return 0;
}

6. Draw a flowchart for finding the largest of three numbers.

7. Draw a flowchart to print even numbers from 1 to n.

8. What are format modifiers? Write the importance of the same.


Format modifiers are the modifiers in c language which are used to define input and output
data in functions

Like printf() for output

And Scanf for input

It is used to specify how data is displayed of read

Ex

%d for integer

%f float

%c for character

9. Write a program to check whether a given number is even or odd.

#include <stdio.h>

int main() {

int num;

printf(“enter the number”);

scanf(“%d” , &num);

if (num%2 == 0);

printf(“%d” is even ./n”,num)

else

printf(“%d” is odd ./n”,num)

return 0;

10. What are I/O statements? Give valid examples.

i/o statements are used to communicate between program and user

ex-
printf – used to display output

scanf – used to take input

Long Answer Questions


1. Distinguish between assembly, low-level and high-level languages.
1. Distinguish between Assembly, Low-level, and High-level Languages:
 Assembly Language:
o It is a low-level programming language that uses (symbols and abbreviations)
to represent machine-level code.
o It is architecture-specific, meaning that it is closely tied to the CPU instruction
set of a particular computer.
o Example: MOV A, 1 (move 1 into register A).
o Advantages: Gives fine control over hardware, fast execution.
o Disadvantages: Difficult to learn, maintain, and debug; not portable.
 Low-level Language:
o Low-level languages are closer to machine code (binary). Assembly language
is considered a low-level language.
o These languages provide minimal abstraction from the computer’s hardware.
o Pros: Efficient use of memory and execution speed.
o Cons: Highly machine-dependent and requires detailed knowledge of the
computer architecture.
 High-level Language:
o High-level languages are designed to be more user-friendly and easier to write,
using natural language elements (like English words).
o These are abstracted from the hardware and are portable across various
systems.
o Example: C, Java, Python.
o Pros: Easier to learn, portable, better for complex tasks.
o Cons: Lower control over hardware compared to low-level languages, slower
than assembly language.

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() {

int num, reversed num = 0, remainder, originalNum;

printf("Enter an integer: ");

scanf("%d", &num);

originalNum = num;

// Reverse the number

while (num != 0) {

remainder = num % 10;

reversedNum = reversedNum * 10 + remainder;


num /= 10;

printf("Reversed Number: %d\n", reversedNum);

// Check if the original number is a palindrome

if (originalNum == reversedNum)

printf("%d is a palindrome.\n", originalNum);

else

printf("%d is not a palindrome.\n", originalNum);

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.

4. Write a C program to find the roots of a quadratic equation ax + bx + c = 0 for all


possible combination values of a, b and c.
#include <stdio.h>
#include <math.h>

int main() {
double a, b, c, discriminant, root1, root2, realPart, imaginaryPart;

printf("Enter coefficients a, b, and c: ");


scanf("%lf %lf %lf", &a, &b, &c);

discriminant = b * b - 4 * a * c;

// Check the discriminant to find the nature of the roots


if (discriminant > 0) {
// Two real and distinct roots
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are real and distinct: %.2lf and %.2lf\n", root1, root2);
} else if (discriminant == 0) {
// Two real and equal roots
root1 = root2 = -b / (2 * a);
printf("Roots are real and equal: %.2lf\n", root1);
} else {
// Complex roots
realPart = -b / (2 * a);
imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("Roots are complex: %.2lf + %.2lfi and %.2lf - %.2lfi\n", realPart,
imaginaryPart, realPart, imaginaryPart);
}

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()`**:

- Evaluates conditions in sequence.

- Allows complex conditions.

- Can work with any data type.

- **`switch()`**:

- Works with a single expression (like `int`, `char`).

- Selects directly between cases.

- Efficient for constant values.

### 2. **Difference between assignment and equality operation**:

- **Assignment (`=`)**: Assigns a value to a variable (e.g., `x = 5`).

- **Equality (`==`)**: Compares two values to check if they are equal (e.g., `x == 5`).

3. Differences between `while` and `do-while` statements:

- **`while`**: Checks the condition before executing the loop body.

- **`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;

int result = (x > 5) ? 1 : 0; // result will be 1

```

5. Arithmetic and Logical operators:

- **Arithmetic operators**: Used for mathematical operations.

- Examples: `+`, `-`, `*`, `/`, `%`.

- **Logical operators**: Used for logical comparisons.

- Examples: `&&` (AND), `||` (OR), `!` (NOT).

6. Associative property of an operator:

- Associative property: Defines the order in which operators of the same precedence are
evaluated.

- Example: `+` is left-associative, so `a + b + c` is evaluated as `(a + b) + c`.

- Importance: It determines the order of evaluation, which can affect the result in
expressions involving multiple operators.

7. Explanation of the code :

```c

int x = 5;

int y = (x > 3) ? (x < 10 ? x + 1 : x + 2) : x + 3;

```

- `x > 3` is `true`, so it checks `(x < 10)`, which is also `true`.

- Therefore, `y = x + 1`, and the final value of `y` is `6`.


### 8. **Operator precedence**:

- Operator precedence determines the order in which operators are evaluated in an


expression.

- Example: In `a + b * c`, the multiplication (`*`) is evaluated before the addition (`+`), as
`*` has higher precedence than `+`.

### 9. **Convert infix expression `a + b * c - d` to prefix and postfix**:

- **Prefix**: `- + a * b c d`

- **Postfix**: `a b c * + d -`

### 10. **C Program demonstrating a `for` loop**:

```c

#include <stdio.h>

int main() {

int i;

for (i = 1; i <= 5; i++) {

printf("%d\n", i);

return 0;

```

- This program prints numbers from 1 to 5 using a `for` loop.

Long Answer Questions

1. Explain unary and binary operators with examples.


1. Unary and Binary Operators with Examples
- Unary Operators:

- These operators operate on a single operand.

- Examples:

- Increment (`++`): Adds 1 to the operand.

int x = 5;

x++; // x becomes 6

- Decrement (`--`): Subtracts 1 from the operand.

int x = 5;

x--; // x becomes 4

- Negation (`-`): Changes the sign of the operand.

int x = 5;

int y = -x; // y becomes -5

- Binary Operators:

- These operators operate on two operands.

- Examples:

- Addition (`+`): Adds two operands.

int a = 5, b = 10;

int sum = a + b; // sum becomes 15

- Multiplication (`*`)**: Multiplies two operands.

int a = 5, b = 3;

int product = a * b; // product becomes 15


- Equality (`==`)**: Checks if two operands are equal.

int a = 5, b = 5;

if (a == b) { // true

// Do something

---

2. C Program to Simulate a Basic Calculator Using `switch` Statement

#include <stdio.h>

int main() {

char operator;

double num1, num2, result;

// Input operation and numbers

printf("Enter an operator (+, -, *, /): ");

scanf("%c", &operator);

printf("Enter two numbers: ");

scanf("%lf %lf", &num1, &num2);

// Perform operation based on the operator

switch(operator) {
case '+':

result = num1 + num2;

printf("Result: %.2lf\n", result);

break;

case '-':

result = num1 - num2;

printf("Result: %.2lf\n", result);

break;

case '*':

result = num1 * num2;

printf("Result: %.2lf\n", result);

break;

case '/':

if (num2 != 0) {

result = num1 / num2;

printf("Result: %.2lf\n", result);

} else {

printf("Error! Division by zero.\n");

break;

default:

printf("Invalid operator.\n");

return 0;

```
---

3. Program to Calculate Percentage and Assign Grades Using Nested `if`


Statements

#include <stdio.h>

int main() {

int marks[8], total = 0;

float percentage;

// Read marks of 8 subjects

for (int i = 0; i < 8; i++) {

printf("Enter marks for subject %d: ", i + 1);

scanf("%d", &marks[i]);

total += marks[i];

// Calculate percentage

percentage = (total / 800.0) * 100;

// Determine grade using nested if

if (percentage >= 80) {

printf("Percentage: %.2f%% - Grade: Excellent\n", percentage);

} else if (percentage >= 70) {


printf("Percentage: %.2f%% - Grade: Very Good\n", percentage);

} else if (percentage >= 60) {

printf("Percentage: %.2f%% - Grade: Good\n", percentage);

} else if (percentage >= 50) {

printf("Percentage: %.2f%% - Grade: Satisfactory\n", percentage);

} else {

printf("Percentage: %.2f%% - Grade: Fail\n", percentage);

return 0;

```

---

4. Program to Print Prime Numbers from 1 to `n`.

#include <stdio.h>

int main() {

int n, i, j, isPrime;

printf("Enter a number: ");

scanf("%d", &n);

printf("Prime numbers from 1 to %d are:\n", n);


// Loop through all numbers from 2 to n

for (i = 2; i <= n; i++) {

isPrime = 1; // Assume the number is prime

// Check if the number has any divisors other than 1 and itself

for (j = 2; j <= i / 2; j++) {

if (i % j == 0) {

isPrime = 0; // Not a prime number

break;

if (isPrime) {

printf("%d ", i);

printf("\n");

return 0;

```

---

5. Relational, Logical, and Bitwise Operators with Example Programs


- Relational Operators: Used to compare values.

- 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() {

int a = 10, b = 20;

if (a < b) {

printf("a is less than b\n");

} else {

printf("a is not less than b\n");

return 0;

```

- Logical Operators: Used to combine or invert conditions.

- Examples: `&&` (AND), `||` (OR), `!` (NOT).

- Example program:

#include <stdio.h>

int main() {

int a = 1, b = 0;
if (a && b) {

printf("Both are true\n");

} else {

printf("At least one is false\n");

if (!(a && b)) {

printf("Negation: At least one is false\n");

return 0;

```

- Bitwise Operators: Used to perform operations on binary numbers.

- Examples: `&` (AND), `|` (OR), `^` (XOR), `~` (NOT), `<<` (left shift), `>>` (right shift).

- Example program:

#include <stdio.h>

int main() {

int a = 5, b = 9;

printf("a & b = %d\n", a & b); // Bitwise AND

printf("a | b = %d\n", a | b); // Bitwise OR

printf("a ^ b = %d\n", a ^ b); // Bitwise XOR

printf("~a = %d\n", ~a); // Bitwise NOT


printf("b << 1 = %d\n", b << 1); // Left shift

printf("b >> 1 = %d\n", b >> 1); // Right shift

return 0;

```

You might also like