Pps MRC Solution

Download as pdf or txt
Download as pdf or txt
You are on page 1of 45

1. What is the difference between compiler and interpreter?

(18-
19)

Compiler:

 Translates the entire source code of a program into machine code before
execution.
 Produces an executable file.
 Examples: GCC for C/C++, javac for Java.

Interpreter:

 Translates and executes the source code line by line.


 Does not produce an intermediate executable file.
 Examples: Python interpreter, Ruby interpreter.

2. What are the good characteristics of algorithms? (18-19)

 Correctness: Produces the correct output for all valid inputs.


 Efficiency: Uses optimal time and space resources.
 Finiteness: Terminates after a finite number of steps.
 Clarity: Steps are clearly and unambiguously defined.
 Feasibility: Practically executable with available resources.

3. What do you mean by precedence and associativity while


solving some arithmetic expression? (18-19)

 Precedence: Determines the order in which different operators are evaluated in


an expression. Higher precedence operators are evaluated before lower
precedence ones. For example, * has higher precedence than +.
 Associativity: Determines the order in which operators of the same precedence
are evaluated. Most arithmetic operators are left-associative, meaning they are
evaluated from left to right. For example, 4 - 3 - 2 is evaluated as (4 - 3) - 2.

4. While compiling a code, write name of two syntax and two


logical errors. (18-19)
Syntax Errors:

 Missing semicolon (;).


 Mismatched parentheses or braces.

Logical Errors:

 Incorrect algorithm implementation.


 Using the wrong variable in a calculation.

5. Draw memory hierarchy structure of computer system. (18-19)


plaintext
Copy code
Fastest to Slowest Most Expensive to Least Expensive ----------------------- | Registers | ----------------------- |
Cache Memory | ----------------------- | Main Memory (RAM) | ----------------------- | Secondary Storage | ------------
----------- | Tertiary Storage | -----------------------

6. Describe the functionality of OS? (19-20)

The Operating System (OS) manages hardware and software resources in a computer
system. Its main functionalities include:

 Process Management: Handling creation, scheduling, and termination of


processes.
 Memory Management: Allocating and deallocating memory spaces as needed
by programs.
 File System Management: Organizing and controlling access to files on storage
devices.
 Device Management: Managing input and output devices, ensuring proper
communication between devices and the system.
 Security and Access Control: Protecting data and resources from unauthorized
access.
 User Interface: Providing an interface for users to interact with the system (e.g.,
command line, graphical user interface).

7. Differentiate between implicit and explicit type conversion.


(19-20)
 Implicit Type Conversion: Also known as coercion, it is automatically performed
by the compiler when operands are of different types. For example, in int x =
10; float y = x; , x is implicitly converted to a float.
 Explicit Type Conversion: Also known as casting, it is performed manually by
the programmer using a cast operator. For example, float x = 10.5; int y =
(int)x; explicitly converts x to an integer.

8. What do you understand by mixed operands? Explain with


example. (19-20)

Mixed operands refer to operands of different data types used in the same expression.
For example, in the expression int a = 5; float b = 6.5; float c = a + b; , a (an
integer) and b (a float) are mixed operands. The integer a is implicitly converted to a
float for the addition operation.

9. Write the algorithm for the addition of two numbers. (20-21)


plaintext
Copy code
Algorithm AddTwoNumbers 1. Start 2. Declare variables num1, num2, sum 3. Input num1 4. Input num2 5.
sum = num1 + num2 6. Output sum 7. End

10. Define all Arithmetic operators. (20-21)

 Addition (+): Adds two operands. a + b


 Subtraction (-): Subtracts the second operand from the first. a - b
 Multiplication (*): Multiplies two operands. a * b
 Division (/): Divides the first operand by the second. a / b
 Modulus (%): Returns the remainder of the division of the first operand by the
second. a % b

11. Differentiate between algorithm and pseudocode. (21-22)

 Algorithm: A step-by-step procedure for solving a problem, expressed in natural


language or as a flowchart. It is focused on the logic and steps needed to achieve
a particular outcome.
 Pseudocode: A high-level description of an algorithm, using a mix of natural
language and programming language syntax. It is used to plan and visualize the
algorithm before actual coding.
12. What are header files? Why are they important? (21-22)

Header Files:

 Files that contain declarations of functions and macros used in a C/C++ program.
 Commonly have a .h extension, such as stdio.h or math.h.

Importance:

 Allow code modularity and reusability by separating interface declarations from


implementation.
 Enable inclusion of library functions and definitions, facilitating code maintenance
and readability.

13. Find the output of the following code:


void main()
{
int x=3, y = 4, a=6, z=7, result;
result = (x>y) +++a || !c;
printf("%d", result);
} ); }

Output Explanation:

 The code has a few issues:


 The +++a is a syntax error.
 !c uses an undeclared variable c, leading to a compile error.

Assuming corrections:

 If ++a and !z are used, the logic evaluates as follows:


 ++a increments a to 7.

 (x > y) is false (0).

 0 + 7 evaluates to 7, which is true.

 !z with z=7 is false (0).

 true || false is true (1).


Corrected Code:

void main()

int x=3, y = 4, a=6, z=7, result;

result = (x>y) + ++a || !z;

printf("%d", result);

}Output: 1

14. Find the output of following code written in C-Language:

#include<stdio.h>
main()
{
int i=1;
for(;;)
{ printf("%d",i);
if(i=5)
break;
}
}

Output Explanation:

 if(i-5) is always true (non-zero) since i is initialized to 1 and never changes.

 The loop breaks immediately after the first iteration.

Output: 1

15. Write limitations of switch case. (21-22)


 No Range-Based Cases: Cannot handle ranges of values directly.
 No Floating-Point Cases: Only works with integer and enumerated types.
 No Duplicate Case Values: Each case value must be unique.
 No Complex Conditions: Cannot handle conditions or complex expressions
directly.
 Fall-through Behavior: May require explicit break statements to prevent
unintended fall-through, which can lead to bugs if overlooked.

16. Write the fundamental data type in C Programming and its


range. (20-21)

Fundamental Data Types:

 char: Typically 1 byte. Range: -128 to 127 (signed), 0 to 255 (unsigned).


 int: Typically 4 bytes. Range: -2,147,483,648 to 2,147,483,647 (signed), 0 to
4,294,967,295 (unsigned).
 float: Typically 4 bytes. Range: Approximately 1.2E-38 to 3.4E+38 with 6 decimal
places precision.
 double: Typically 8 bytes. Range: Approximately 2.3E-308 to 1.7E+308 with 15
decimal places precision.
 short: Typically 2 bytes. Range: -32,768 to 32,767 (signed), 0 to 65,535
(unsigned).
 long: Typically 8 bytes on modern systems. Range: -9,223,372,036,854,775,808
to 9,223,372,036,854,775,807 (signed), 0 to 18,446,744,073,709,551,615
(unsigned).

17. Write the difference between Logical error and Syntax error.

Logical Error:

 Description: Occurs when the program compiles and runs, but produces
incorrect results.
 Cause: Flaws in the algorithm or incorrect logic implementation.
 Detection: Often discovered during testing and debugging.
 Example: Using = instead of == in a condition.

Syntax Error:
 Description: Occurs when the code violates the syntax rules of the programming
language.
 Cause: Misspellings, missing semicolons, mismatched parentheses, etc.
 Detection: Detected by the compiler during compilation.
 Example: Missing semicolon at the end of a statement.

18. Explain identifiers and keywords in C Language.

 Identifiers: Names used to identify variables, functions, arrays, etc. Must begin
with a letter (a-z, A-Z) or an underscore (_), followed by letters, digits (0-9), or
underscores. They are case-sensitive.

 Example: int totalSum;, float _average;.

 Keywords: Reserved words that have special meaning in C and cannot be used
as identifiers. They are predefined and case-sensitive.

 Example: int, return, if, else, while.

19. Explain the concept of garbage value in C with a suitable


example.

Garbage Value:

 Description: An uninitialized variable in C may hold a garbage value, which is an


undefined value left in the memory location by previous operations.
 Example:
#include <stdio.h>

int main() {
int x;
printf("Value of uninitialized x: %d\n", x);
return 0;
}

Output: The output will display a random value (garbage value) for x since it is
not initialized.

20. Differentiate between #include<filename.h> and #include


"filename.h".
 #include <filename.h>: Used to include standard library header files. The
compiler searches for the file in the standard system directories.

 Example: #include <stdio.h>

 #include "filename.h": Used to include user-defined header files. The compiler


searches for the file in the current directory first and then in the standard
directories.

 Example: #include "myheader.h"

21. Write the use of continue and break statement.

 continue:

 Usage: Skips the remaining code inside the loop for the current iteration
and proceeds with the next iteration of the loop.
 Example:
 for (int i = 0; i < 10; i++) {
 if (i % 2 == 0) {
 continue;
 }
 printf("%d ", i);
 }

 Output: 1 3 5 7 9

 break:

 Usage: Exits from the current loop or switch statement immediately.


 Example:
 for (int i = 0; i < 10; i++) {
 if (i == 5) {
 break;
 }
 printf("%d ", i);
 }

 Output: 0 1 2 3 4

22. Write the output of following code:

#include <stdio.h>

int main()

int a = -10, b = 20;

if(a > 0 && b < 0)

a++;

else if(a < 0 && b < 0)

a--;

else if(a < 0 && b > 0)

b--;

else

b--;

printf("%d\n", a + b);

return 0;

Output: 9

Explanation:

 a < 0 is true and b > 0 is true, so b-- is executed. b becomes 19.


 The final sum is a + b = -10 + 19 = 9.

23. Describe the syntax and working of Ternary operator.

Syntax: condition ? expression1 : expression2;

Working:

 Evaluates the condition.


 If the condition is true, expression1 is evaluated and returned.
 If the condition is false, expression2 is evaluated and returned.

Example:

int a = 10, b = 20;


int max = (a > b) ? a : b; ;

Explanation: Since a > b is false, max is assigned the value of b, which is 20.

24. Differentiate assignment and equality operators in C.

 Assignment Operator (=):

 Usage: Assigns the value on the right to the variable on the left.
 Example: int x = 5; assigns 5 to x.
 Function: x = 5 sets x to 5.

 Equality Operator (==):

 Usage: Compares two values for equality.


 Example: if (x == 5) checks if x is equal to 5.
 Function: x == 5 evaluates to true if x is 5, otherwise false.
LONG TYPE QUESTION ANSWER
1. Describe basic components of a computer system with a neat
and clean block diagram. What do you mean by operating
system? (18-19)

OR

Draw block diagram of computer and explain each of its


components in brief. (21-22)

Basic Components of a Computer System:

 Central Processing Unit (CPU): The brain of the computer where most
calculations take place. It consists of the Control Unit (CU) and Arithmetic Logic
Unit (ALU).

 Control Unit (CU): Directs the operation of the processor.


 Arithmetic Logic Unit (ALU): Performs arithmetic and logical operations.

 Memory: Stores data and instructions for the CPU. Divided into:

 Primary Memory (RAM): Volatile memory used for temporary storage


while a program is running.
 Secondary Memory: Non-volatile storage used for permanent data
storage (e.g., hard drives, SSDs).

 Input Devices: Hardware used to input data into the computer (e.g., keyboard,
mouse).

 Output Devices: Hardware used to output data from the computer (e.g.,
monitor, printer).

 Storage Devices: Hardware used to store data permanently (e.g., hard disk, SSD).
 Motherboard: The main circuit board that connects all components of the
computer.

 Power Supply: Provides electrical power to the computer.

 I/O Devices: Interfaces for input and output operations (e.g., USB ports, network
cards).

Block Diagram:

+------------------+

| Input Devices |

+------------------+

+------------------+

| CPU |

| +--------------+ |

| | Control Unit | |

| +--------------+ |

|| ALU ||

| +--------------+ |

+------------------+

+------------------+

| Memory |

| +--------------+ |

| | Primary RAM | |

| +--------------+ |

| | Secondary | |

| | Storage ||
| +--------------+ |

+------------------+

+------------------+

| Output Devices |

Operating System (OS):

 A system software that manages computer hardware, software resources, and


provides common services for computer programs. It acts as an intermediary
between users and the computer hardware. Examples include Windows, Linux,
macOS.

2. Define data type in C. Discuss primitive data types in terms of


memory occupied, format specifier, and range. (18-19)

Data Type in C: A classification that specifies which type of value a variable can hold.
Data types define the operations that can be done on the data and the way the values
are stored.

Primitive Data Types in C:

 char:

 Memory Occupied: 1 byte


 Format Specifier: %c
 Range: -128 to 127 (signed), 0 to 255 (unsigned)

 int:

 Memory Occupied: 4 bytes (can be 2 bytes on older systems)


 Format Specifier: %d (signed), %u (unsigned)
 Range: -2,147,483,648 to 2,147,483,647 (signed), 0 to 4,294,967,295
(unsigned)

 float:

 Memory Occupied: 4 bytes


 Format Specifier: %f
 Range: Approximately 1.2E-38 to 3.4E+38 with 6 decimal places precision

 double:

 Memory Occupied: 8 bytes


 Format Specifier: %lf
 Range: Approximately 2.3E-308 to 1.7E+308 with 15 decimal places
precision

 short:

 Memory Occupied: 2 bytes


 Format Specifier: %hd (signed), %hu (unsigned)
 Range: -32,768 to 32,767 (signed), 0 to 65,535 (unsigned)

 long:

 Memory Occupied: 8 bytes (typically, but can be 4 bytes on older


systems)
 Format Specifier: %ld (signed), %lu (unsigned)
 Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
(signed), 0 to 18,446,744,073,709,551,615 (unsigned)

Example of Usage in C:

#include <stdio.h>

int main() {

char ch = 'A';
int num = 12345;

float f = 3.14;

double d = 6.28;

short s = 32767;

long l = 1234567890;

printf("char: %c\n", ch);

printf("int: %d\n", num);

printf("float: %f\n", f);

printf("double: %lf\n", d);

printf("short: %hd\n", s);

printf("long: %ld\n", l);

return 0;

}This program demonstrates the usage of different primitive data types in C and their
respective format specifiers.

3. Explain various types of arithmetic operators in C language


with the help of an example. When the precedence of two
operators in an arithmetic expression is the same, how does
associativity help in identifying which operators will be evaluated
first? Illustrate it with an example. (18-19)

Arithmetic Operators in C:

 Addition (+): Adds two operands. Example: a + b


 Subtraction (-): Subtracts the second operand from the first. Example: a - b
 Multiplication (*): Multiplies two operands. Example: a * b
 Division (/): Divides the first operand by the second. Example: a / b
 Modulus (%): Finds the remainder of the division of one operand by another.
Example: a % b

Example:

#include <stdio.h>

int main() {

int a = 10, b = 5, c = 2;

int result;

result = a + b * c; // 10 + (5 * 2) = 10 + 10 = 20

printf("Result: %d\n", result);

result = (a + b) * c; // (10 + 5) * 2 = 15 * 2 = 30

printf("Result: %d\n", result);

return 0;

}Operator Precedence and Associativity:

 Precedence determines the order in which different operators are evaluated in


an expression.
 Associativity defines the direction (left-to-right or right-to-left) in which
operators of the same precedence level are evaluated.

Example:
int x = 10, y = 5, z = 2;
int result;

result = x - y - z; // Left-to-right associativity: (10 - 5) - 2 = 5 - 2 = 3


printf("Result: %d\n", result);

result = x / y * z; // Left-to-right associativity: (10 / 5) * 2 = 2 * 2 = 4


printf("Result: %d\n", result); );

4. Discuss the major components of a digital computer with a


suitable block diagram. Also discuss the function of each
component. (19-20)

Major Components of a Digital Computer:

1. Central Processing Unit (CPU): Executes instructions from programs.

 Control Unit (CU): Directs operations of the CPU.


 Arithmetic Logic Unit (ALU): Performs arithmetic and logical operations.

2. Memory: Stores data and instructions.

 Primary Memory (RAM): Temporary storage for data and instructions in


use.
 Secondary Memory (Storage): Permanent storage for data and programs
(e.g., HDD, SSD).

3. Input Devices: Allow users to input data (e.g., keyboard, mouse).

4. Output Devices: Display or output data (e.g., monitor, printer).

5. Motherboard: The main circuit board that connects all components.

6. Power Supply: Provides electrical power to the computer.

7. I/O Devices: Interfaces for input and output operations (e.g., USB ports, network
cards).

Block Diagram:
plaintext
Copy code
+------------------+ | Input Devices | +------------------+ | v +------------------+ | CPU | | +--------------+ | | | Control
Unit | | | +--------------+ | | | ALU | | | +--------------+ | +------------------+ | v +------------------+ | Memory | | +----------
----+ | | | Primary RAM | | | +--------------+ | | | Secondary | | | | Storage | | | +--------------+ | +------------------+ | v
+------------------+ | Output Devices | +------------------+

Functions of Each Component:

 CPU: Executes instructions and processes data.


 Memory: Stores data and instructions.
 Input Devices: Allow users to input data.
 Output Devices: Display or output data.
 Motherboard: Connects all components.
 Power Supply: Supplies power to the system.
 I/O Devices: Manage input and output operations.

5. What are operators? Mention different types of operators in C.


Explain the difference between operator precedence and
associativity with a suitable example. (19-20)

Operators in C:

 Arithmetic Operators: +, -, *, /, %
 Relational Operators: ==, !=, >, <, >=, <=
 Logical Operators: &&, ||, !
 Bitwise Operators: &, |, ^, ~, <<, >>
 Assignment Operators: =, +=, -=, *=, /=, %=
 Increment and Decrement Operators: ++, --
 Conditional (Ternary) Operator: ? :
 Comma Operator: ,
 Sizeof Operator: sizeof

Operator Precedence and Associativity:

 Precedence: Determines the order in which different types of operators in an


expression are evaluated.
 Associativity: Determines the order in which operators of the same precedence
level are evaluated.
Example:

#include <stdio.h>

int main() {

int a = 10, b = 5, c = 2;

int result;

result = a + b * c; // Multiplication has higher precedence than addition

printf("Result: %d\n", result); // 10 + (5 * 2) = 20

result = a - b - c; // Left-to-right associativity: (10 - 5) - 2 = 3

printf("Result: %d\n", result);

return 0;

6. Differentiate between:

a) Compiler and Interpreter (19-20) b) Linker and Loader (19-20)

a) Compiler vs. Interpreter:

 Compiler:

 Translates the entire source code into machine code before execution.
 Generates an executable file.
 Error detection occurs at compile-time.
 Example: C, C++
 Interpreter:

 Translates and executes source code line-by-line.


 No separate executable file is created.
 Error detection occurs at runtime.
 Example: Python, JavaScript

b) Linker vs. Loader:

 Linker:

 Combines object files generated by the compiler into a single executable


file.
 Resolves symbol references between different modules.
 Produces the final executable program.

 Loader:

 Loads the executable file into memory for execution.


 Sets up the execution environment.
 Begins the execution of the program.

7. i. Define data types in C. Discuss primitive data types in terms


of memory size, format specifier, and range. ii. Explain the
structure of a C program. (19-20)

i. Data Types in C: Primitive Data Types:

 char:

 Memory Size: 1 byte


 Format Specifier: %c
 Range: -128 to 127 (signed), 0 to 255 (unsigned)

 int:

 Memory Size: 4 bytes


 Format Specifier: %d (signed), %u (unsigned)
 Range: -2,147,483,648 to 2,147,483,647 (signed), 0 to 4,294,967,295
(unsigned)

 float:

 Memory Size: 4 bytes


 Format Specifier: %f
 Range: Approximately 1.2E-38 to 3.4E+38

 double:

 Memory Size: 8 bytes


 Format Specifier: %lf
 Range: Approximately 2.3E-308 to 1.7E+308

 short:

 Memory Size: 2 bytes


 Format Specifier: %hd (signed), %hu (unsigned)
 Range: -32,768 to 32,767 (signed), 0 to 65,535 (unsigned)

 long:

 Memory Size: 8 bytes


 Format Specifier: %ld (signed), %lu (unsigned)
 Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
(signed), 0 to 18,446,744,073,709,551,615 (unsigned)

ii. Structure of a C Program:

1. Preprocessor Directives: Includes library files.


#include <stdio.h>

2. Global Declarations: Global variables and function declarations.


int global_var;

3. main() Function: Entry point of the program.


int main() {
// Code
return 0;

4. }Function Definitions: User-defined functions.

void function_name() {

// Code

8. If three sides of a triangle are input through the keyboard,


draw a flowchart to check whether a triangle is isosceles,
equilateral, scalene, or right-angled. Also, write a program in C for
the same. (19-20)

Flowchart:
#include <stdio.h>

int main() {

int a, b, c;

printf("Enter three sides of a triangle: ");

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

if (a == b && b == c) {

printf("Equilateral triangle\n");
} else if (a == b || b == c || a == c) {

printf("Isosceles triangle\n");

} else if ((a * a + b * b == c * c) || (b * b + c * c == a * a) || (a * a + c * c == b * b)) {

printf("Right-angled triangle\n");

} else {

printf("Scalene triangle\n");

return 0;

9. Brief the Generations of Programming Languages with


examples. (20-21)

Generations of Programming Languages:

1. First Generation (1GL): Machine Language

 Example: Binary code (101010)

2. Second Generation (2GL): Assembly Language

 Example:
MOV A, 1
ADD B, A MOV A, 1 ADD B, A

3. Third Generation (3GL): High-Level Languages


int main() {
printf("Hello, World!\n");
return 0;

4. Fourth Generation (4GL): Problem-Oriented Languages

 Example: SQL (Structured Query Language)

SELECT * FROM employees WHERE salary > 50000;

5. Fifth Generation (5GL): Logic and Constraint-Based Languages

 Example: Prolog
likes(mary, pizza). , pizza).

10. What do you mean by operands? Discuss the operator


precedence and associativity of all the operators. (20-21)

Operands:

 Operands are the values or variables on which operators perform operations.

Operator Precedence and Associativity:

Operator Precedence:

1. Highest Precedence:
 ()
 []

 ., ->

2. Unary Operators:
 !, ~, ++, --, +, -, *, &, sizeof

3. Multiplicative Operators:
 *, /, %

4. Additive Operators:
 +, -

5. Shift Operators:
 <<, >>

6. Relational Operators:
 <, <=, >, >=

7. Equality Operators:
 ==, !=

8. Bitwise AND:
 &

9. Bitwise XOR:
 ^

10. Bitwise OR:


 |

11. Logical AND:


 &&

12. Logical OR:


 ||

13. Conditional (Ternary) Operator:


 ? :

14. Assignment Operators:


 =, +=, -=, *=, /=, %=

15. Comma Operator:


 ,

Associativity:

 Left-to-Right: Most operators.


 Right-to-Left: Unary operators, assignment operators, and conditional operator.

Example:

#include <stdio.h>
int main() {
int a = 10, b = 5, c = 2;
int result;

result = a + b * c; // Multiplication has higher precedence than addition


printf("Result: %d\n", result); // 10 + (5 * 2) = 20

result = a - b - c; // Left-to-right associativity: (10 - 5) - 2 = 3


printf("Result: %d\n", result);

return 0;
}

11. Write the short notes on (i) Compiler (ii) Interpreter (iii) Linker
(iv) Loader 20-21

(i) Compiler:

 A compiler is a program that translates high-level source code written in a


programming language into machine code that the computer's processor can
execute.
 It performs various stages of translation: lexical analysis, syntax analysis, semantic
analysis, optimization, and code generation.
 Example: gcc for C/C++, javac for Java.

(ii) Interpreter:

 An interpreter translates high-level source code into machine code line-by-line


and executes it immediately.
 It does not produce an intermediate object code or executable file.
 Example: Python interpreter, Ruby interpreter.

(iii) Linker:

 The linker combines multiple object files produced by the compiler into a single
executable file.
 It resolves references to undefined symbols by finding the correct memory
addresses.
 Example: ld on Unix systems, linker in Visual Studio.

(iv) Loader:

 The loader is responsible for loading the executable file into the computer's
memory and preparing it for execution.
 It sets up the program's runtime environment, including memory allocation and
linking libraries.
 Example: ld.so on Unix systems, Windows loader.

12. Brief the storage classes in C with a proper example. 20-21

Storage Classes in C:

1. Automatic (auto):

 The default storage class for local variables.


 Local variables are stored in the stack and are destroyed when the function
exits.
 Example:

void function() {

auto int a = 10;

2. }External (extern):

 Used to declare a global variable or function that is defined in another file


or later in the same file.
 Allows multiple files to access the same variable or function.
 Example:
extern int globalVar; ;

3. Static:
 Local static variables retain their value between function calls.
 Global static variables and functions have internal linkage, meaning they
are not visible outside the file.
 Example:
void function() {
static int count = 0;
count++;
printf("Count: %d\n", count);
} ); }

4. Register:

 Suggests that the variable be stored in a CPU register for faster access.
 The compiler may ignore this suggestion.
 Example:

void function() {

register int a = 10;

Example Program:

#include <stdio.h>

// Global static variable

static int global_count = 0;

// External variable

extern int externalVar;


void function() {

// Local static variable

static int local_count = 0;

local_count++;

global_count++;

printf("Local count: %d, Global count: %d\n", local_count, global_count);

int main() {

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

function();

return 0;

}13.Differentiate between type conversion and typecasting. Write


a program to input a floating-point number and find the leftmost
digit of the integral part of a number 21-22

Type Conversion vs. Typecasting:

 Type Conversion: Automatic conversion of data from one type to another by the
compiler. Also known as implicit conversion.
 Typecasting: Explicit conversion of data from one type to another by the
programmer. Also known as explicit conversion.

Example Program:

#include <stdio.h>

#include <math.h>
int main() {

float num;

int integral_part, leftmost_digit;

printf("Enter a floating-point number: ");

scanf("%f", &num);

integral_part = (int)num; // Typecasting

// Find leftmost digit

leftmost_digit = integral_part;

while (leftmost_digit >= 10) {

leftmost_digit /= 10;

printf("Leftmost digit of the integral part: %d\n", leftmost_digit);

return 0;

}14.
Define flowchart and draw a flowchart to find the largest
among three numbers. 21-22

Flowchart: A flowchart is a graphical representation of an algorithm or a process,


showing the steps as boxes of various kinds, and their order by connecting these with
arrows.

Flowchart to Find the Largest Among Three Numbers:


plaintext

15. Explain in detail about all storage classes with a proper


example. 21-22

Storage Classes in C:

1. Automatic (auto):

 Scope: Local to the block in which it is defined.


 Lifetime: Until the block is exited.
 Default storage class for local variables.
 Example:

void function() {

auto int a = 10;

}2.External (extern):
 Scope: Global.
 Lifetime: As long as the program is running.
 Used to declare a global variable or function in another file.
 Example:

extern int globalVar;

3.Static:

 Local static: Retains value between function calls.


 Global static: Visible only within the file.
 Example:
void function() {
static int count = 0;
count++;
printf("Count: %d\n", count);
} ); }

4 Register:

 Scope: Local to the block in which it is defined.


 Lifetime: Until the block is exited.
 Hints the compiler to store the variable in a register for faster access.
 Example:

void function() {

register int a = 10;

Example Program:

#include <stdio.h>
// Global static variable

static int global_count = 0;

// External variable

extern int externalVar;

void function() {

// Local static variable

static int local_count = 0;

local_count++;

global_count++;

printf("Local count: %d, Global count: %d\n", local_count, global_count);

int main() {

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

function();

return 0;

16. Explain Logical, Unary, and Bitwise operators in detail. 21-22


Logical Operators:

 Used to perform logical operations.


 AND (&&): True if both operands are true.
 OR (||): True if at least one operand is true.
 NOT (!): True if the operand is false.
 Example:

int a = 5, b = 10;

if (a > 0 && b > 0) {

printf("Both are positive\n");

Unary Operators:

 Operate on a single operand.


 Unary Plus (+): Indicates positive value.
 Unary Minus (-): Negates the value.
 Increment (++): Increases the value by 1.
 Decrement (--): Decreases the value by 1.
 Logical NOT (!): Inverts the logical state.
 Bitwise NOT (~): Inverts the bits.
 Address of (&): Returns the address of a variable.
 Dereference (*): Accesses the value at a given address.
 Example:

int a = 5;

int b = -a; // Unary minus

printf("b: %d\n", b);


Bitwise Operators:

 Operate on binary representations of integers.


 AND (&): Sets each bit to 1 if both bits are 1.
 OR (|): Sets each bit to 1 if at least one of the bits is 1.
 XOR (^): Sets each bit to 1 if only one of the bits is 1.
 NOT (~): Inverts all the bits.
 Left Shift (<<): Shifts bits to the left.
 Right Shift (>>): Shifts bits to the right.
 Example:

int a = 5; // 0101 in binary

int b = a << 1; // 1010 in binary, which is 10 in decimal

printf("b: %d\n", b);

17. Compare if-else-if ladder and switch case. Write a menu-


driven program to perform basic functions of a calculator. 21-22

if-else-if Ladder:

 Used when there are multiple conditions to evaluate.


 Syntax:

if (condition1) {

// Code

} else if (condition2) {

// Code

} else {

// Code
}

switch Case:

 Used when a single variable is compared against multiple values.


 Syntax:

switch (variable) {

case value1:

// Code

break;

case value2:

// Code

break;

default:

// Code

}{

Menu-Driven Program for Calculator:

#include <stdio.h>

int main() {
int choice;
float num1, num2, result;

while (1) {
prin ("Menu:\n");
prin ("1. Addi on\n");
prin ("2. Subtrac on\n");
prin ("3. Mul plica on\n");
prin ("4. Division\n");
prin ("5. Exit\n");
prin ("Enter your choice: ");
scanf("%d", &choice);

if (choice == 5) {
break;
}

prin ("Enter two numbers: ");


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

switch (choice) {
case 1:
result = num1 + num2;
prin ("Result: %.2f\n", result);
break;
case 2:
result = num1 - num2;
prin ("Result: %.2f\n", result);
break;
case 3:
result = num1 * num2;
prin ("Result: %.2f\n", result);
break;
case 4:
if (num2 != 0) {
result = num1 / num2;
prin ("Result: %.2f\n", result);
} else {
prin ("Error: Division by zero\n");
}
break;
default:
prin ("Invalid choice\n");
}
}

return 0;
}

18. Write a program to find out the greatest number out of three
numbers. 20-21
Program to Find the Greatest Number:

#include <stdio.h>
int main() {
int num1, num2, num3;

printf("Enter three numbers: ");


scanf("%d %d %d", &num1, &num2, &num3);

if (num1 >= num2 && num1 >= num3) {


printf("Greatest number: %d\n", num1);
} else if (num2 >= num1 && num2 >= num3) {
printf("Greatest number: %d\n", num2);
} else {
printf("Greatest number: %d\n", num3);
}

return 0;
}

19. Explain different types of control statements used in C


programming with examples. 20-21

Control statements in C are used to control the flow of execution of the program. They
are categorized into three types: selection, iteration, and jump statements.

1. Selection Statements:

 if statement: Used to execute a block of code if a condition is true.

 int a = 5;

 if (a > 0) {

 printf("a is positive\n");

 }

if-else statement: Used to execute one block of code if a condition is true


and another block if it is false.
c

 int a = -5;

 if (a > 0) {

 printf("a is positive\n");

 } else {

 printf("a is negative\n");

 }

if-else-if ladder: Used to test multiple conditions.

 int a = 0;

 if (a > 0) {

 printf("a is positive\n");

 } else if (a < 0) {

 printf("a is negative\n");

 } else {

 printf("a is zero\n");

 }

switch statement: Used to select one of many code blocks to execute.

2. int day = 3;

3. switch (day) {

4. case 1:
5. printf("Monday\n");

6. break;

7. case 2:

8. printf("Tuesday\n");

9. break;

10. case 3:

11. printf("Wednesday\n");

12. break;

13. default:

14. printf("Invalid day\n");

Iteration Statements:

 for loop: Used to execute a block of code a fixed number of times.

 for (int i = 0; i < 5; i++) {

 printf("%d ", i);

 }

while loop: Used to execute a block of code as long as a condition is true.

 int i = 0;

 while (i < 5) {

 printf("%d ", i);


 i++;

 }

do-while loop: Similar to the while loop, but it guarantees that the block
of code will be executed at least once.

int i = 0;

do {

printf("%d ", i);

i++;

} while (i < 5);

Jump Statements:

break statement: Used to exit a loop or switch statement.

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

if (i == 5) {

break;

printf("%d ", i);

continue statement: Used to skip the current iteration of a loop and


proceed to the next iteration.
for (int i = 0; i < 10; i++) {

if (i % 2 == 0) {

continue;

printf("%d ", i);

goto statement: Used to jump to a labeled statement.

int a = 10;

if (a == 10) {

goto label;

printf("This will not be printed\n");

label:

printf("Goto label executed\n");

20. What are different conditional statements in C programming?


Explain with proper example of each. 19-20

Conditional statements in C are used to perform different actions based on different


conditions. The primary conditional statements are if, if-else, if-else-if, and switch.

1. if statement:

 The if statement is used to execute a block of code if a specified


condition is true.
 Example:

int a = 5;

if (a > 0) {

printf("a is positive\n");

if-else statement:

The if-else statement is used to execute one block of code if a condition is


true and another block if it is false.
Example:

int a = -5;

if (a > 0) {

printf("a is positive\n");

} else {

printf("a is negative\n");

if-else-if ladder:

 The if-else-if ladder is used to test multiple conditions sequentially.


 Example:
int a = 0;
if (a > 0) {
printf("a is positive\n");
} else if (a < 0) {
printf("a is negative\n");
} else {
printf("a is zero\n");
}}

2. switch statement:

 The switch statement is used to execute one of many blocks of code


based on the value of an expression.
 Example:

int day = 3;

switch (day) {

case 1:

printf("Monday\n");

break;

case 2:

printf("Tuesday\n");

break;

case 3:

printf("Wednesday\n");

break;

default:

printf("Invalid day\n");

These conditional statements allow for more complex decision-making processes in C


programs, making them essential for creating dynamic and responsive applications.

You might also like