Job Saarnee

# 2 marks Question of Programming for Problem Solving – Unit 1 | PPS

## 2 marks Question of Programming for Problem Solving – Unit 1

In this blog we cover 2 marks Question of Programming for Problem Solving from Unit 1. Here we try to cover all possible question from this unit

Q1 Draw a flow chart to find the greatest number among three numbers?

Q2 Write a function to interchange the two values of two variables without using third variable?

Solution:
Here’s an example of a function in C that can swap the values of two variables without using a third variable:
void swap_vars(int *a, int *b) {
*a = *a + *b;
*b = *a – *b;
*a = *a – *b;
}
In this function, we first add the values of the two variables pointed to by a and b together, and store the result in the variable pointed to by a. Then we subtract the original value of the variable pointed to by b from the variable pointed to by a, which gives us the original value of the variable pointed to by a, and store the result in the variable pointed to by b. Finally, we subtract the original value of the variable pointed to by a from the variable pointed to by b, which gives us the original value of the variable pointed to by b, and store the result back in the variable pointed to by a.
Here’s an example of how you could use this function:
#include <stdio.h>
void swap_vars(int *a, int *b); // here we declare a function named swap_vars which take two pointer as input.
int main() {
int x = 5;
int y = 10;
printf(“Before swapping: x = %d, y = %dn”, x, y);
swap_vars(&x, &y);
printf(“After swapping: x = %d, y = %dn”, x, y);
return 0;
}
void swap_vars(int *a, int *b) {
*a = *a + *b;
*b = *a – *b;
*a = *a – *b;
}
This would output:
Before swapping: x = 5, y = 10
After swapping: x = 10, y = 5

Q3 Differentiate complier and interpreter?

Solution:
A compiler and an interpreter are two different types of software that are used to convert human-readable code into machine-readable code. Here are the main differences between compilers and interpreters:
1. Compilation vs. Interpretation: A compiler translates the entire source code of a program into machine code in one go, whereas an interpreter executes the source code line-by-line and translates each line into machine code at runtime.
2. Execution: A compiled program is saved as a machine code file and can be executed directly on a computer without any additional software. On the other hand, an interpreted program requires an interpreter to execute the program.
3. Efficiency: Compiled programs are generally faster and more efficient because the entire program is translated into machine code ahead of time. Interpreted programs are generally slower because each line of code must be translated at runtime.
4. Debugging: Debugging a compiled program can be more difficult because errors in the code may not be discovered until after the program has been compiled. In contrast, debugging an interpreted program is generally easier because errors can be detected and corrected as they occur.
5 Portability: Compiled programs are generally less portable than interpreted programs because the machine code is specific to the platform it was compiled on. Interpreted programs can be more portable because the interpreter can be run on multiple platforms.
In summary, a compiler is a software that translates the entire source code of a program into machine code in one go, while an interpreter translates the source code line-by-line and executes it at runtime. Compiled programs are generally faster and more efficient, but less portable, while interpreted programs are generally slower but more portable and easier to debug.

Q4 #define PRODUCT(n) n*n

void main()
int j;
j=64/PRODUCT(4);
printf(“%d”,j);
What will be the output of the above program?
Solution:
The output of the above program will be:
4
Here’s how the program is executed:
The #define statement defines a macro named PRODUCT that takes a single argument n and returns n*n.
In the main() function, we declare an integer variable j. We then assign the result of 64/PRODUCT(4) to j.
Since PRODUCT(4) is expanded to 4*4 by the preprocessor, the expression 64/PRODUCT(4) becomes 64/(4*4), which simplifies to 64/16, which is equal to 4.
Finally, the value of j (which is 4) is printed to the console using the printf() function.
Q5 what are the good characteristics of an algorithm?
Solution:
Here are some good characteristics of an algorithm:
• Clarity: The algorithm should be clear and unambiguous so that it can be easily understood and followed by anyone who reads it.
• Input and output: The algorithm should have well-defined input and output parameters, which should be clearly stated at the beginning of the algorithm.
• Correctness: The algorithm should produce the correct output for all valid inputs. It should solve the problem it is intended to solve, without errors or bugs.
• Finiteness: The algorithm should terminate after a finite number of steps. It should not run indefinitely or enter an infinite loop.
• Efficiency: The algorithm should be efficient and run in a reasonable amount of time, given the size and complexity of the problem it is solving.
• Generality: The algorithm should be general enough to handle a wide range of inputs, and not be specific to a single problem or input.
• Modularity: The algorithm should be modular, meaning that it can be broken down into smaller, more manageable parts that can be tested and optimized separately.
• Reusability: The algorithm should be designed in such a way that it can be easily adapted and reused for similar problems or tasks.
• Robustness: The algorithm should be able to handle unexpected or invalid inputs in a graceful manner, without crashing or producing incorrect output.
• Maintainability: The algorithm should be designed in such a way that it can be easily updated, modified, or extended in the future, as new requirements arise or as the problem evolves.

Q6 What do you mean by scope and life time of a variable?

Solution:
Scope and lifetime are two important concepts related to variables in programming. Here’s what they mean:
• Scope of a variable: The scope of a variable is the portion of the program where the variable is visible and can be accessed. In other words, it determines where the variable can be used in the program. The scope of a variable is defined by the block of code in which the variable is declared. In C, for example, variables declared inside a function have a local scope and can only be accessed within that function, while variables declared outside any function have a global scope and can be accessed from anywhere in the program.
• Lifetime of a variable: The lifetime of a variable is the period of time during which the variable exists and retains its value. In other words, it determines how long the variable remains in memory. The lifetime of a variable is also determined by its scope. Local variables have a shorter lifetime than global variables. When a function is called, its local variables are created and allocated memory, and when the function returns, the local variables are destroyed and their memory is freed. Global variables, on the other hand, exist for the entire duration of the program.
To summarize, the scope of a variable determines where it can be accessed in the program, while the lifetime of a variable determines how long it exists and retains its value. Understanding these concepts is important for writing efficient and error-free code.

Q7 What do you mean by precedence and associativity while solving some arithmetic expressions?

Solution:
Precedence and associativity are two concepts used to determine the order of evaluation in arithmetic expressions.
• Precedence:Precedence determines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated before operators with lower precedence. For example, in the expression 4 + 5 * 3, the multiplication operator * has a higher precedence than the addition operator +, so it is evaluated first. Therefore, the expression is evaluated as 4 + (5 * 3), which results in the value 19.
• Associativity: Associativity determines the order in which operators of the same precedence are evaluated. Operators can be left-associative or right-associative. Left-associative operators are evaluated from left to right, while right-associative operators are evaluated from right to left. For example, in the expression 9 – 3 – 2, both subtraction operators have the same precedence and are left-associative. Therefore, the expression is evaluated as (9 – 3) – 2, which results in the value 4.
In some cases, it may be necessary to use parentheses to override the default order of evaluation. For example, in the expression (4 + 5) * 3, the parentheses indicate that the addition should be evaluated first, resulting in the value 27.
Understanding the precedence and associativity of operators is important for writing correct arithmetic expressions and avoiding errors in programming.

Q8 draw the memory hierarchical structure of computer system?

Solution:

The memory hierarchy of a computer system can be visualized as a pyramid or a series of levels, with each level representing a different type of memory, arranged from the fastest and most expensive to the slowest and least expensive. Here’s a basic diagram of the memory hierarchy of a typical computer system:
• Registers: This is the fastest and smallest type of memory in a computer system, consisting of a few dozen or hundred bytes of storage that are integrated into the CPU. Registers are used to hold data that is being actively processed by the CPU, such as instructions, data operands, and memory addresses.
• Cache: This is a type of high-speed memory that sits between the registers and the main memory. Caches are used to store copies of frequently accessed data from the main memory, which can be accessed faster than reading the data from the main memory. Caches are typically divided into several levels, with each level having a larger capacity but slower access time than the previous level.
• Main Memory: This is the primary memory in a computer system, also known as RAM (Random Access Memory). It provides a large, fast storage area for the operating system and applications to store data and program instructions. The main memory is volatile, meaning that its contents are lost when the power is turned off.
• Secondary Storage: This includes non-volatile storage devices such as hard disk drives (HDDs), solid-state drives (SSDs), and optical drives. These devices provide a large and relatively slow storage area for storing data and programs that are not currently in use. Secondary storage devices have much larger capacity than main memory but are slower to access.
• Tertiary Storage: This includes long-term, off-line storage devices such as magnetic tape and optical disks. These devices are used for archival storage and backup purposes and have very slow access times compared to other types of memory.

Overall, the memory hierarchy is designed to balance the need for fast access to data with the cost and capacity of different types of memory. By using a combination of fast, expensive memory and slower, cheaper memory, computer systems can provide the best possible performance at a reasonable cost.

Q9 List various functions of an operating system?
Solution:
An operating system (OS) performs a wide range of functions to manage the resources and operations of a computer system. Some of the major functions of an operating system include:
• Memory Management: An OS manages the allocation and deallocation of memory to running processes and ensures that each process has access to the required amount of memory.
• Process Management: An OS manages the execution of processes or programs, including their creation, scheduling, and termination.
• File Management: An OS manages the creation, deletion, and organization of files and directories, and provides mechanisms for accessing and manipulating file data.
• Device Management: An OS manages input and output (I/O) devices, such as keyboards, mice, printers, and network adapters, and provides a consistent interface for accessing these devices.
• Security:An OS provides mechanisms to protect the system and user data from unauthorized access and ensure the privacy and integrity of information.
• Networking: An OS provides networking support to allow communication between computers, including protocols for transferring data, managing connections, and handling errors.
• User Interface: An OS provides a user-friendly interface to interact with the computer system, including graphical user interfaces (GUIs), command-line interfaces (CLIs), and touch-based interfaces.
• Error Handling: An OS detects and handles errors that occur in the system, including software errors, hardware errors, and user errors.
• Resource Allocation:An OS manages system resources, including CPU time, memory, and I/O bandwidth, to ensure that all processes have a fair share of available resources.
• System Monitoring: An OS monitors system performance and generates statistics and reports to help administrators manage the system and troubleshoot problems
Overall, an OS plays a critical role in managing the resources and operations of a computer system, and provides a layer of abstraction between the hardware and the user or applications that use the system.
Q10 Why ‘C’ programming is called structured programming?
Solution:
‘C’ programming is called structured programming because it emphasizes on structured programming practices. Structured programming is a programming paradigm that emphasizes on the use of structured control flow constructs and subroutines to write software. The structured programming approach was developed in the late 1960s to improve the quality of software and to make software development more manageable and efficient.
C programming language was developed in the early 1970s and was designed to support structured programming. C language provides a set of structured control flow constructs, such as if-else, for-loop, while-loop, switch-case, and functions, that can be used to write structured programs. These constructs allow the programmer to write programs that are easy to read, understand, and modify.
Structured programming emphasizes on modular design, top-down design, and stepwise refinement of algorithms. It encourages the use of subroutines or functions to break a complex problem into smaller, more manageable pieces. This approach makes it easier to develop, debug, and maintain large software systems. C language provides a rich set of built-in functions and libraries that can be used to implement complex algorithms and data structures.
In summary, ‘C’ programming is called structured programming because it supports the structured programming approach, which emphasizes on modular design, top-down design, and stepwise refinement of algorithms. This approach makes it easier to write, debug, and maintain large software systems.

Q11 Write a function in C to exchange the content of two integer variables without using a third variable?

Solution:
Refer Question 2
Q12 What is an algorithm?
Solution:
An algorithm is a set of step-by-step instructions or a sequence of logical operations that are followed to solve a problem or perform a task. It is a precise and unambiguous description of how to solve a specific problem, which can be executed by a computer or other devices. Algorithms are used in a wide variety of fields, including mathematics, computer science, engineering, and many others. They can be expressed in various forms, including flowcharts, pseudocode, or programming languages, and are used in various applications such as data processing, machine learning, image processing, and many others.
Q13 Explain flow chart and the symbols used?
Solution:
A flowchart is a graphical representation of an algorithm or a process, which uses different symbols to represent various steps or operations involved in the process. The main purpose of a flowchart is to provide a visual representation of a process or a system, which makes it easy to understand, analyze and improve. Flowcharts are widely used in various fields such as software development, business process modeling, and engineering.
The symbols used in a flowchart represent different actions or operations in a process. Here are some commonly used symbols in flowcharts:
• Start/End: This symbol represents the beginning or end of a process and is typically represented by an oval shape.
• Process: This symbol represents a specific operation or action performed in the process and is typically represented by a rectangular shape.
• Decision: This symbol represents a decision point in the process, where the flow can take different paths based on a condition. It is typically represented by a diamond shape.
• Input/Output: This symbol represents the input or output of data in the process and is typically represented by a parallelogram shape.
• Connector:This symbol represents a point where the flowchart connects to another part of the process or another page in the flowchart. It is typically represented by a small circle.
• Flowlines: These lines connect the symbols and show the flow of the process.
By using these symbols, a flowchart can provide a clear and easy-to-understand representation of a complex process or algorithm.

Q14 Write an algorithm to find the greatest number among three numbers?

Solution:
Here is an algorithm to find the greatest number among three numbers:
1. Start
2. Input three numbers a, b, c
3. Set a variable max to a
4. If b > max, set max to b
5. If c > max, set max to c
6. Output max as the greatest number
7. End
Explanation:
1. The algorithm starts.
2. The user inputs three numbers a, b, and c.
3. The variable max is initialized to a, assuming that a is the greatest number.
4. If b is greater than max, then max is set to b.
5. If c is greater than max, then max is set to c.
6. The value of max is the greatest number, so it is outputted.
7. The algorithm ends.
By following this algorithm, we can determine the greatest number among three given numbers.

Q15 What is meant by storage classes of a variable?

Solution:
In computer programming, a storage class defines the scope and lifetime of a variable, as well as its initial value and location in memory. There are four storage classes in C programming language, which are as follows:
• Automatic Storage Class: This is the default storage class for variables declared inside a function. Variables declared with this storage class are created when the function is called and destroyed when the function exits. The initial value of automatic variables is garbage.
• Static Storage Class: Variables declared with this storage class have a lifetime throughout the execution of the program. They are initialized only once, at the beginning of the program, and retain their values until the program terminates. Static variables can only be accessed within the scope they are declared.
• Register Storage Class: This storage class is used to declare variables that will be stored in the CPU registers instead of memory. The number of CPU registers available is limited, so the use of register variables should be limited to frequently accessed variables to improve performance.
• Extern Storage Class: This storage class is used to declare variables that are defined in another file or module. It is used when a variable needs to be shared between different files or modules in a program.
By specifying the storage class of a variable, we can control its scope, lifetime, and behavior within a program.

Q16 What is token in ‘C’ language?

Solution:

In the C programming language, a token is the smallest unit in the source code. C programs are made up of various tokens, which serve as building blocks for writing code. Tokens can be classified into several categories:

1. Keywords: Keywords are reserved words in C that have special meanings. Examples include int, if, else, for, while, etc.
2. Identifiers: Identifiers are used to name variables, functions, arrays, etc., in a C program. An identifier can be composed of letters, digits, and underscores, but it cannot start with a digit. For example, sum, counter, and total Amount are identifiers.
3. Constants: Constants are literal values that do not change during program execution. They can be of various types, such as integer constants (5, 10), floating-point constants (3.14), character constants (‘A’, ‘b’), or string constants (“Hello, World!”).
4. String literals: String literals are sequences of characters enclosed within double quotes, like “Hello, World!”.
5. Operators: Operators perform operations on variables and values. Examples include +, -, *, /, and %.
6. Punctuation symbols: Punctuation symbols like , (comma), ; (semicolon), { (left curly brace), } (right curly brace), ( (left parenthesis), and ) (right parenthesis) are used to structure the code.
7. Special symbols: Special symbols include # (hash), = (assignment), == (equality), and != (not equal), among others.

During the compilation process, the C compiler breaks down the source code into these tokens, which are then used for syntax analysis and further processing to generate executable code. Understanding tokens is essential for writing correct and meaningful C programs.

Q17 What do you mean by formatted output in C language? Explain with Example?
Solution:

In C programming, formatted output refers to the process of printing data on the screen or other output devices in a specific, well-formatted manner. The printf() function is commonly used for formatted output in C. It allows you to print variables and literal strings while specifying the format in which the data should be displayed.

Here’s the basic syntax of the printf() function:

printf(“format string”, variable1, variable2, …);
In the format string, placeholders are used to specify the type and format of the variables you want to print. For example:

%d is used for integers
%f is used for floating-point numbers
%c is used for characters
%s is used for strings

Let’s look at an example to illustrate formatted output in C:

#include <stdio.h>

int main() {
int num = 42;
float pi = 3.14159;
char ch = ‘A’;
char name[] = “John”;

// Printing formatted output
printf(“Integer: %d\n”, num);
printf(“Float: %.2f\n”, pi); // %.2f limits the floating-point output to 2 decimal places
printf(“Character: %c\n”, ch);
printf(“String: %s\n”, name);

return 0;
}

In this example, the printf() function is used to print the integer num, the floating-point number pi with two decimal places, the character ch, and the string name. The format specifiers (%d, %.2f, %c, %s) are replaced by the corresponding values during execution, producing a well-formatted

Output:

Integer: 42
Float: 3.14
Character: A
String: John
Formatted output is essential for displaying data in a readable and organized manner, especially when dealing with complex programs where various data types need to be presented clearly to the user.

Q18 Write down the output of the following
main()
{
int i=1;
for( ;  ; )
{
printf(“%d”,i);
if(i==7)
break;
}
}
Q19 What is meant by modular programming approach?
Q20 Explain various data types?
Q21 Write any three differences between Application software and System software?
Q22 Differentiate between static and register storage class in C language.
Q23 What is an enumerated data type in C language? Explain?
Q24 List five internal and external commands used in DOS operating system?
Q25 Explain the basic structure of C program?
Q26 Explain the role of C preprocessor?
Q27  Write an algorithm for addition of two number?
Q28 Write the fundamental data type in c programming and its range?
Q29 Why operating system is required?
Q30 What do curly braces denote in C?
Q31 What do you mean by an algorithm?
Q32 Name different storage class with one example of each?
Q33 List the components of C language?
Q34 Differentiate assignment and equality operators in C?
Shopping Cart