Pps MRC Solution
Pps MRC Solution
Pps MRC Solution
(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:
Logical Errors:
The Operating System (OS) manages hardware and software resources in a computer
system. Its main functionalities include:
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.
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:
Output Explanation:
Assuming corrections:
void main()
printf("%d", result);
}Output: 1
#include<stdio.h>
main()
{
int i=1;
for(;;)
{ printf("%d",i);
if(i=5)
break;
}
}
Output Explanation:
Output: 1
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.
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.
Keywords: Reserved words that have special meaning in C and cannot be used
as identifiers. They are predefined and case-sensitive.
Garbage Value:
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.
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:
Output: 0 1 2 3 4
#include <stdio.h>
int main()
a++;
a--;
b--;
else
b--;
printf("%d\n", a + b);
return 0;
Output: 9
Explanation:
Working:
Example:
Explanation: Since a > b is false, max is assigned the value of b, which is 20.
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.
OR
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).
Memory: Stores data and instructions for the CPU. Divided into:
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.
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 |
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.
char:
int:
float:
double:
short:
long:
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;
return 0;
}This program demonstrates the usage of different primitive data types in C and their
respective format specifiers.
Arithmetic Operators in C:
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
result = (a + b) * c; // (10 + 5) * 2 = 15 * 2 = 30
return 0;
Example:
int x = 10, y = 5, z = 2;
int result;
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 | +------------------+
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
#include <stdio.h>
int main() {
int a = 10, b = 5, c = 2;
int result;
return 0;
6. Differentiate between:
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:
Linker:
Loader:
char:
int:
float:
double:
short:
long:
void function_name() {
// Code
Flowchart:
#include <stdio.h>
int main() {
int a, b, c;
if (a == b && b == c) {
printf("Equilateral triangle\n");
} else if (a == b || b == c || a == c) {
printf("Isosceles triangle\n");
printf("Right-angled triangle\n");
} else {
printf("Scalene triangle\n");
return 0;
Example:
MOV A, 1
ADD B, A MOV A, 1 ADD B, A
Example: Prolog
likes(mary, pizza). , pizza).
Operands:
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:
^
Associativity:
Example:
#include <stdio.h>
int main() {
int a = 10, b = 5, c = 2;
int result;
return 0;
}
11. Write the short notes on (i) Compiler (ii) Interpreter (iii) Linker
(iv) Loader 20-21
(i) Compiler:
(ii) 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.
Storage Classes in C:
1. Automatic (auto):
void function() {
2. }External (extern):
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() {
Example Program:
#include <stdio.h>
// External variable
local_count++;
global_count++;
int main() {
function();
return 0;
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;
scanf("%f", &num);
leftmost_digit = integral_part;
leftmost_digit /= 10;
return 0;
}14.
Define flowchart and draw a flowchart to find the largest
among three numbers. 21-22
Storage Classes in C:
1. Automatic (auto):
void function() {
}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:
3.Static:
4 Register:
void function() {
Example Program:
#include <stdio.h>
// Global static variable
// External variable
void function() {
local_count++;
global_count++;
int main() {
function();
return 0;
int a = 5, b = 10;
Unary Operators:
int a = 5;
if-else-if Ladder:
if (condition1) {
// Code
} else if (condition2) {
// Code
} else {
// Code
}
switch Case:
switch (variable) {
case value1:
// Code
break;
case value2:
// Code
break;
default:
// Code
}{
#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;
}
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;
return 0;
}
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:
int a = 5;
if (a > 0) {
printf("a is positive\n");
}
int a = -5;
if (a > 0) {
printf("a is positive\n");
} else {
printf("a is negative\n");
}
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. 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:
Iteration Statements:
}
int i = 0;
while (i < 5) {
}
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 {
i++;
Jump Statements:
if (i == 5) {
break;
if (i % 2 == 0) {
continue;
int a = 10;
if (a == 10) {
goto label;
label:
1. if statement:
int a = 5;
if (a > 0) {
printf("a is positive\n");
if-else statement:
int a = -5;
if (a > 0) {
printf("a is positive\n");
} else {
printf("a is negative\n");
if-else-if ladder:
2. switch statement:
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");