Following PDF Contents Solution of Theory Based on IOE:
1. Define software. Explain different types of software.
Software is a collection of machine readable instructions that directs a computer to perform some
specific tasks. It is an ordered sequence of instructions given for changing the state of the computer
hardware in a certain predefined sequence. Generally software can be categorized as system
software and application software. The detail descriptions of system software and application
software along with their sub-categories are given below:
System software: It is a type of computer software designed to operate the computer
hardware, to provide basic functionality and to provide a platform for running application
software. System software include device drivers, operation systems etc. It is responsible for
controlling, integrating and managing the individual hardware components of a computer
system so that they can work together harmoniously. Its purpose is to hide the complex details
of the hardware.
Operating system is the main system software which controls all parts of the computer
system. The major function of OS are to handle all the hardware connected to the system,
coordinate and manage use of other resources and provide common services for computer
programs. Application programs usually require an operating system to function.
Device driver is a computer program that operates or controls a particular type of device
that is attached to a computer. A device driver simplifies programming by acting as
translator between a hardware device and the applications that use it.
Application software: It is a type of computer software that causes a computer to perform
useful tasks that a user wishes to perform. It utilizes capacities of a computer directly to a
dedicated task. Application software includes word processing software, database software,
multimedia software, graphics software etc. It can be categorized as:
Tailored (custom) software is software that is specially designed for some specific
organizations or other user. It is developed for a single user that accommodates the
particular preferences and expectations of the user.
Packaged (off the shelf) software is software which performs some specific function or
calculation which is useful for more than one computer user and is sufficiently well
documented to be used without modifications on a defined configuration of some computer
system.
2. Compare application software with system software.
The comparisons of application and system software are:
Application software utilizes the capacities of a computer to perform a dedicated task to benefit
the user whereas system software manages and integrates computer’s capabilities but doesn’t
utilize the capacities in the performance to benefit the user.
System software provides the platform for the application software to run on the system
whereas application software provides the platform for the end users to use the capacities of
the system to solve various problems.
System software deals with the hardware (architecture of the system) whereas the application
software interacts with end user.
System software consists of low level programs that interact with the computer at a very basic
level.
3. What is a programming language? Explain different types of programming language.
Programming Language is a standardized communication technique for describing instructions to a
computer. Each programming language has a set of syntactic and semantic rules used to define
computer programs. The different types of programming language are described below.
Low Level Languages are those languages which are closer to the hardware. They are specific to
the hardware so cannot run on different hardware. Knowledge of hardware is required to
develop such programs using low level languages. It can be divided into Machine language and
Assembly language.
Machine Language is the closest language to the hardware in which the instructions are
given in the form of 0’s and 1’s.
Advantages:
o It makes fast and efficient use of computers.
o Translator is not required.
Disadvantages:
o Programmer should have hardware knowledge.
o All memory addresses and operation codes are needed to be remembered.
o Testing and Debugging are difficult to carry out.
o They are machine dependent.
Assembly Languages are those languages which used mnemonics in place of 0’s and 1’s.
Each instruction is represented by symbols instead of just using bits. The program written in
assembly languages must be converted to machine language.
Advantages:
o Easier to understand and use as compared to machine language.
o Programs are normally very fast and very compact.
o Timings can be calculated very precisely and program flow is easily controlled.
Disadvantages:
o Good Understanding of the hardware is required.
o Machine dependent
High Level language is a language with strong abstraction from the details of the computer and
uses natural language elements, making the process of developing a program simpler and more
understandable relative to a lower level language. These languages are English like and are very
close to the human languages. They are machine independent. High level languages are
required to convert into machine level languages with the help of the translators. Rather than
dealing with registers, memory addresses and stacks, they deal with variables, arrays, objects,
functions, loops, threads etc. it can be categorized as:
Procedural oriented languages (Imperative languages): In these languages one or more
related blocks of statements that perform some complete function are grouped together
into a procedure. If the same sequence of operations is needed elsewhere in the program, a
simple statement can be used to refer back to that procedure. A procedure may be a list of
instructions directing a computer, step by step, what to do, usually having a linear order of
execution from the first statement to the second and so forth with occasional loops and
branches. Procedural programming languages include C, C++, FORTRAN, Pascal and BASIC.
Problem Oriented Languages (Declarative languages): These languages were developed to
solve specific problems or develop specific applications. These languages focus on what
work is to be performed rather than how to perform the work. These languages are usually
limited to a very specific application and it might use syntax that is never used in other
programming languages. These languages are specially focused on database management
systems. SQL, CSS, FUSION is some of the examples of problem oriented languages.
Natural Languages: There languages are supposed to have the form of human cognition and
human intelligence. With these languages human can easily communicate or interact with
c0mputers without the requirement of knowledge of any special programming languages.
These are generally used in Artificial Intelligence and Expert System. LISP and PROLOG are
few natural languages commonly in practice.
Advantages of High level Languages
Support high level of abstraction.
Easier to learn and use. Since the languages are very close to human languages.
Easier to test and debug.
They are machine independent. Software written in high level language can be used in
any computer system with different architecture.
Disadvantages of High Level Languages
Requires more memory space and time.
4. Compare assembly language with machine language.
Assembly language and machine language are both low level language. Its comparison is as shown.
Assembly languages used mnemonics while machine language uses 0’s and 1’s to represent
operational codes and operand.
Assembly languages need a translator to convert into machine language.
Comparatively assembly languages are easier to understand and use than machine language.
But machine languages are a bit fast and efficient.
Both are machine dependent and programmer must have the knowledge about hardware
before using each language.
5. Write the difference between high level and low level language.
The difference between high level and low level language are:
Low level languages are machine oriented whereas high level languages are problem oriented.
Programs developed using low level languages run faster and take less storage space.
Low level languages are used to develop system software whereas high level languages are used
to design application software.
6. Explain, briefly, about the generations of programming language.
The various generations of programming language are explained below.
First Generation: The first generation of programming languages is all about machine code.
Code of the first generation programming language was entered through physical switches on
the computer and involved commands to move data bits to and from registers, compute on
these and more. Machine language can be taken as first generation programming language.
Second Generation: To minimize the difficulty, the operational codes were represented by a
mnemonics which in some way describes the action of the machine code (such as save, mov,
add, load). The source codes were needed to be assembled into machine code before it can be
executed by a processor with the help of an assembler. Such languages are sometimes still used
for kernels and device drivers, i.e. the core of the operating system and for specific machine
parts. Assembly language was used as a second generation languages.
Third Generation: Languages of third generation were closer to human languages. The flaws of
first and second generation were dealt by making the language machine independent and
including programmer friendly features. These languages require a compiler to make machine
code for the processor. Third generations which are high level languages are sometimes referred
as imperative languages. Imperative means that the code is executed line by line in sequence.
Few Examples are FORTRAN, C, C++, Java, C# etc.
Fourth Generation Languages are designed to reduce programming effort and the time it takes
to develop software, resulting in a reduction in the cost of software development. Languages
have been designed with a specific purpose which includes query language, report generators
etc. These languages are referred as declarative languages since it describes what computation
should be performed but not how to perform it. Characteristics of fourth generation languages
can be summarized as portable, closer to human language, database supportive, simple and
requires less effort, non procedural. SQL, NOMAD, FOCUS are some of the examples of fourth
generation language.
Fifth Generation Language also known as natural language provides a visual or graphical
interface to develop software. It allows user to interact with computers without needing any
specialized knowledge. These languages are generally used for writing programs for artificial
intelligence, neural network, plasma computing etc. LISP, PROLOG, Mercury are some of the
examples of fifth generation languages.
7. What are the characteristics of a well written program?
A feature of software defines the distinguishing characteristics of software based on performance,
portability or functionality. Some of the important characteristics of well written computer
programs can be:
Integrity refers to the accuracy of the calculations.
Clarity refers to the overall readability of the program.
Simplicity: The structure and logic of the program must be as simple as possible to enhance
the clarity and accuracy of the program.
Efficiency is concerned with execution speed and efficient memory utilization.
Modularity means dividing the program into small modules which are independent of each
other. It enhances the accuracy and clarity and facilitates future program alterations.
Interoperability is the ability of the software to exchange information with other
applications and make use of information transparently.
Portability is the ability of the software to function in different platforms and environments
Some others desirable characteristics may be security, flexibility, scalability, reliability etc.
8. Explain about recent trends of software.
In the early days of software development, software was difficult to develop as they used machine
languages which made them efficient and faster but on the other hand became non portable and
lacked lot of user friendly features. But as the programming language developed, the software has
been very user friendly and provides a visual interface or a graphical user interface which has made
it much easier for user to interact.
Development of software has taken a huge stride in modern era. In recent years, DevOps and agile
development methodology has been heavily used for software development [1]. Nowadays the focus
is on cloud computing which in general means a virtual hard drive that is accessible to the network
of computers.
Cloud computing is a computing paradigm in which tasks are assigned to a combination of
connections, software and services accessed over a network. This network of servers and
connections is collectively known as cloud. Using different medium as an access point, users can
reach into the cloud for resources whenever required.
9. Explain different phases or steps of developing software.
The following phases are included in a software development.
Problem Analysis: This is the first step of software development in which we define our
objectives, analyze our input, processing and output requirements. Depending on the size and
complexity of the problem, a clear statement of the problem is made. And the types and
number of inputs and outputs are evaluated. Finally, a mechanism to convert available
resources into final productive results must be determined.
Algorithm Development and Flow Diagram: In this phase, a general outline of the problem is
sketched using algorithm and flow chart. Algorithms are used to define a set of steps or
procedures to be followed to solve a given problem while flowcharts are the diagrammatic
representation of the steps to solve a particular problem.
Coding: It is the process of writing a program using a particular programming language. Though
high level languages are more preferred, low level language can be used as well to write a
program. Depending upon the ease and accessibility, the programmer may use any platform to
write a program.
Compilation and Execution: When the programs are written into high level language, then it
needs to be converted into low level language. This conversion is done by compiler and the
process is called compilation. After the compilation process, an executable program will
generate which when opened is set to execution. Different inputs are provided to check if the
defined objectives are fulfilled or not.
Debugging and Testing: Testing is the process of finding errors in the program while debugging
is the mechanism to remove the different errors that are present in the program. General syntax
error will be pointed by the compiler, but the run time errors must be checked by providing
different sets of inputs to the program and tracing the output.
Program Documentation: It is done for future reference which can be analyzed for better
software development in later days. Documentation includes all the information about the
program.
10. List the advantages and disadvantages of using flowchart.
The various advantages and disadvantages of using flowchart are listed as:
Advantages:
Communication: As diagrams are better way to provide information, flowchart will
communicate the logic of the whole system concisely and efficiently.
Effective Analysis: Problem can be analyzed more effectively resulting in reduction in cost
and time
Proper Documentation: As flowchart contains information about the working of the system,
it helps to make documentation better.
Efficient Coding: the flowchart acts as a guide for the programmer to write a code in more
efficient manner, since the steps and requirements are clearly defined in diagram.
Proper Debugging: The flowchart, sometimes, can be helpful in debugging process.
Efficient Program Maintenance: The maintenance of operating program becomes easy with
the help of flowchart. It helps the programmer to put efforts more efficiently on that
section.
Disadvantages:
Complex and clumsy: when the program gets quite complicated, the flowchart can be more
complex than it is imagined to be.
Difficulty in modifications: When some alterations are to be done, the flowchart may
require re-drawing.
11. List different symbols used for drawing a flowchart.
The basically used symbols in flowchart are tabulated as:
12. Explain compilation process with relevant diagram.
The process of translation of high level language to low level language is called compilation process.
In this process, source code is converted into executable file. It basically consists of two phases.
Compilation: First, the compiler converts the source code, which is written using high level
language, to object code.
Linking: Secondly, the linker combines the object code with library modules that are used in
the program and also replaces the symbolic addresses with real addresses. And finally, it
generates a machine code in the form of executable file.
13. Write the difference between compiler and interpreter.
The difference between compiler and interpreter are:
14. Define the term source code, object code, compiler, and linker.
Source codes are the programs written using a particular programming language. The file with .c
extensions includes source code written in C programming language. Object codes are the
intermediate code built by the compiler during compilation process. The file with extension .obj
includes object code. Compiler is used to convert the high level language to low level language.
Linker is a program that combines different modules to form an executable file.
15. Explain different types of error in programming.
The different types of error in programming are:
Compile errors: it occurs at the compilation time. It includes syntax errors and semantic errors.
Syntax error occurs when rules of programming language are not followed. Missing semicolon,
no declaration of variables etc are few examples. Semantic errors occur when the statements
written in the program are not meaningful to the compiler.
Logical errors: it occurs in the logic of the program. The compiler cannot detect logical errors.
The program runs but leads to undesired output.
Runtime errors: it occurs due to some illegal operation performed during the execution of the
program. Dividing a number by zero, opening a file that does not exist, lacking memory space
etc are few examples of runtime error.
16. Describe different debugging techniques used in programming.
The various debugging techniques used in programming are mentioned below.
Error Isolation: In this method, a particular section is isolated by making them comments. If
error doesn’t occur after isolating certain part of code, then we may predict that the part of
code has some error.
Tracing: Using this method, we display the value of the variables at different places to see how
they change. And undesired value of such variables will help to locate the error.
Watch values: A watch value represents a value of a variable or expression, whose updated
values are continuously shown to the programmer during the execution of the program.
Inspecting the unexpected changes in the variable can help to determine what the fault is.
Breakpoints: These are the temporary stopping point of the program execution. The program
stops its execution at the breakpoint. It can be used with watch values to inspect the value of
different variables to the particular point.
Stepping: It is the process of executing one statement at a time. It will be easier to determine
which instruction is causing problem by stepping through the entire program.
17. Explain constants, variable, data types and qualifiers with example
CONSTANTS are fixed values that do not change during the execution of a program. Constants,
leaving few exceptions, are assigned to the variable of their corresponding type. It may may be in the
form of
Integer Constant: Every whole number: 4, -5, 66, 235
Floating Constant: Fractional numbers: 4.334, 757.3458
Character Constant: Alphabets, digits, operators, symbols written in single quote: ‘3’, ‘+’, ’a’
String Constant: Any combination of characters within double quote: “Nepal32”
VARIABLES are simply a named area of storage that can hold a single value. In C programming
variables are needed to be declared before making any use of it.
int num; - declaration of an integer variable, with name num and data type int.
char ch; - declaration of a character variable, with name ch and data type char.
DATA TYPE is an extensive system for declaring variables, and represents the type of data the
declared variable will hold.
int num; char ch; - int and char are data types; int data type represents variable num can
hold only integer value while char data type symbolizes variable ch can accept only
character value
o character and integer values can be used interchangeably.
QUALIFIERS are keywords used to modify the attribute of any declared variable. Generally used
qualifiers are sign and size along with const; sign qualifier includes signed and unsigned, size
qualifier include short and long.
unsigned int count; - unsigned is a qualifier which makes the variable count – which is of
integer data type- hold only positive values.
long double amount; - long is a size qualifier which increases the range of values that can be
hold by variable amount which is of type double.
18. Describe about all fundamental data types in C.
The basic types of data types used in C are as follows:
Character Data Type:
Keyword char is used to define characters in C which consists of ASCII character.
It requires 1 byte (8 bit) memory and has the range of -128 to 127.
Qualifier signed and unsigned can be applied. Char and signed char are similar.
Unsigned char has the range of 0 to 255.
Integer Data Type:
Keyword int is used to represent integer data type which in general requires 2 bytes of
memory (may vary from one compiler to another).
The range of an integer is limited from -32768 to +32767.
In general the range is given by -2
n-1
to 2n-1
-1, where n represents the no. of bits.
Different qualifiers can be applied to int varying the size and range of it.
If short int and int have same memory requirements (2 bytes) then long int will have double
the requirements (4 bytes).
If int and long int have the same memory requirements (4 bytes) then short int will have half
the requirements (2 bytes).
Signed int and int is similar with 2 bytes requirements and range is -32768 to 32767.
Unsigned int also require 2 bytes but range varies from 0 to 65535.
Unsigned qualifier can be applied to int as unsigned short int and unsigned long int.
Single Precision Real Data type (float):
Keyword float is used to represent float data type which require 4 bytes of memory and the
range is limited from -3.4e-38 to +3.4e38
Floating point numbers are stored in 32 bits with 6 digits of precisions.
The precision describes the number of significant decimal places that a real value carries.
Qualifiers are not required to apply since long float is equivalent to double.
Double Precision Real Data type (double):
A double precision number is required when very high precision is required i.e. accuracy of
float is not sufficient.
Keyword double is used to represent double precision real data type.
A double data type requires 8 bytes of memory giving a precision of 14 digits and the range
of -1.7e-308 to 1.7e+308
Qualifier long can be applied to extend the range in case required.
19. Differentiate between identifier and keyword.
The general difference that can be observed in C programming are listed as:
20. List the rules for writing a name of an identifier.
Rules for naming any identifier are as listed below:
Name can use only alphanumeric characters and underscore; A-Z, a-z, 0-9 and _ can be
used; _digit, num1, first_result etc
The first character must be alphabet of underscore; A-Z, a-z, _; _name, roll_no, salary
Its name should not be same to that of any keyword; while cannot be used as identifier.
White spaces are not included in any name; number of books, as an identifier, is invalid.
Only first 32 characters are significant; if the length of identifier’s name is more than 32 then
only 32 characters will be taken as its name, rest ignored.
21. How can we calculate the range of any data type? What is the size and range of signed int, float,long double?
General form for the range calculation is given by
Lower limit = -2
n-1
Upper limit = +2n-1
- 1;
Where n is the number of bits representing any data type.
For instance: int is represented by 2 bytes which is 16 bits, so range is given as
Lower limit = -2
15
= -32768 Upper Limit = 215
- 1 = 32767
64. What happens if nested loop contain same running variable?
It is always better to use different variable as a running variable when it comes to nested loop. If the
loops of nested structure use the same running variable then it will cause problem during runtime.
The second loop will change the value of running variable of first loop, causing the nested structure
behave erroneously.
65. Which condition will cause a loop to run infinite times? State few examples
The following condition will cause a loop to run infinite times
When condition of loop is always true, it occurs when
Initialization of running variable is not done, or done within the body of loop
Update of running variable is not done
Placing non zero value in condition part of loop; while(1)
Incompatibility in condition and update value.
66. Define function. Mention the advantages of using function.
A function is a module containing set of instructions that performs a specific task. It is referred to as
sub routines that can be processed independently. For instance, sqrt() is a function which contains
set of statements/code that calculates the square root of given number.
Various advantages of function can be listed as:
Reuse of code: The same set of codes can be used by any program which requires the
similar task to carry out.
Reduced program length: Since same set of codes can be used many times, whenever
required, the length of source program will be reduced.
Facilitates top-down modular programming: The main function consists of only the function
calls representing the high level approach to the problem while details of function
definitions are addressed later.
Easy to debug: Since the program will be in modular form, commenting the function call will
make the corresponding function inactive. Hence, it makes easy to locate and isolate the
faulty set of instructions within a particular function.
Division of tasks: Different tasks of the problem can be assigned to different personnel. Each
task is solved using a function and finally all functions are combined into a single program.
67. Explain main function. How it differs from other functions?
The main function is the entry point of any program. Every program must have a main function to
indicate the beginning of the execution of the program.
It differs from library functions as well as user defined functions. The library functions are simply
used in programs with no definition required but a header file needs to be addressed, while the user
defined function needs a function declaration, function call and function definition. But in case of
main function, the user writes the definition part only, and it is called by the operating system. Also
the main function is only the entry point to the program while other types of functions are not.
68. Describe about the function declaration (prototype), function call, and function definition.
Every user defined function, in general, consists of three elements, which are described below:
Function Declaration: it gives the information about what the function does, what it takes as
arguments, what it returns after execution. It consists of:
Function name: identifier or name given based on its task
Return type: represents what type of value it returns after processing
Argument list: indicates what type and how many values it requires to execute
Semicolon: used to terminate the declaration
General syntax is:
o Return_type function_name(argument list);
Example:
o float sum(float, float); this declaration indicates, the function calculates the
sum of two float values and returning the result from where the function is
called.
Function Call: It is used to make the function do its task. The function is called using the
name given in declaration, followed by the number and types of arguments as in
declaration. The function call need not to be assigned if the function does not returns any
value after processing. But when the function returns a value after execution, it better to
assign to a variable if the value returned is required later. After the function is called, the
control is transferred to the function definition. Then the function gets executed line by line
and the value is returned if there is presence of return statement.
General syntax:
o variable = function_name(argument list); if the function returns a value
o function_name(argument list); if the function doesn’t return a value
Example:
o S = sum(a,b); where a, b and S are of float type
Function Definition: It is the part where the working of the function is defined. It consists of
return type, function name, list of arguments, local variable declaration, and return
statement. After the execution of the statements the control is returned to the point from
where the function is called.
General Syntax:
o return_type function_name(argument list)
{
Local variable declaration;
Statements;
Return statement;
}
Example:
o float sum(float num1, float num2)
{
float s;
s = num1 + num2;
return s;
}
69. What do you mean by arguments? Differentiate formal arguments with actual arguments.
An argument is an expression passed to the function to carry out its specified task. It is an
expression which is written, and separated with comma if more than one, within the parenthesis of
function call, function declaration and function definition.
The difference between format and actual arguments can be listed as:
70. Explain the use of return statement. Is it mandatory to use return statement?
Whenever a function needs to return a value, then return statement can be used. The return
statement, however, can return only a single value.
If the function is built to return a certain value to the calling function then it is mandatory to use a
return statement. But if the function has void as a return type then return statement can be
avoided.
71. What happens to the statements written after return statement?
The statements after the return statement will not execute. Since the return statement sends the
control back to the calling function, the control never reaches the statements written after return
statement.
72. What does return type symbolizes in function definition and declaration? How is it related to
return statement?
Return type in function declaration and definition symbolizes whether the function returns a value
to the calling function or not. If the return type is void, it represents function doesn’t return a value
while other data types such as int, float, char in return type represent that the function returns a
value.
The return statement can be avoided if the return type is void. But if the return type in not void then
in return statement, the expression to be returned must be of same type to that of the return type
of the function.
73. What difference can be observed in arguments portion of function declaration, function call and
function definition?
The difference in arguments that can be seen is
In function declaration: only data type of variables is written
In function call: only the name of variables is written
In function definition: data type along with name of variables is written
74. What does function declaration suggest? How are function called?
Function declaration provides the following information.
Name of the function, generally, defines what its function is.
Return type symbolizes whether the function returns a value after processing.
List of argument or parameter represents what type of and how many values are required
by the function to operate.
Functions are called with their name followed by the list of parameters or arguments. Function call
must be used or assigned if the returned value should be taken into considerations. But if the
function does not return any value or it returns a value which is not of much use then the function
call need not be assigned to or used.
For example, simply calling power function which returns a value does not make any sense.
pow(x,y);
But if it is assigned to a variable or used in some calculations then only it gives some meaning.
z = pow(x,y);
z = a * pow(x,y)/b;
printf(“x to the power y is %f”, pow(x,y));
75. “The number and type of arguments must be same in function declaration, function call and
function definition”, clarify the statement.
The number and type of arguments must be same in function declaration, function call and function
definition; this statement can be clarified using following demonstration.
Let us take an example to calculate the largest number of three numbers, then
Function declaration: int large(int, int, int);
Function call: l = large(a, b, c);
Function Definition: int large( int x, int y, int z) { }
Since, the type and number of arguments match in function declaration, call and definition, there
would be no error. But if the number of arguments is changed, let us say in function call; l =
large(a,b), then the compiler would give error. Similarly if the type of either variable a, b, c is not
integer then again, it would result some kind of error. Hence, the number and type of arguments
must be same in function declaration, function call and function definition.
76. Is it mandatory to have function declaration? “Any entity must be declared first before it is used”,
explain the statement.
Function declaration can be omitted, if the function definition is written before its call. So function
declaration is not mandatory.
Every entity in C programming must be declared before it is used. Declaration gives information not
only to the user but also to the compiler about the type and name of the entity we are using. With
such information, the compiler will interpret statements correctly. Also the compiler can decide how
much storage space to allocate. Hence, every entity such as variables, functions must be declared
first prior to its use. For example, without declaration, statement sum = num1 + num2 can represent
sum of integers or sum of float numbers or even combining of characters or strings. This can cause
problem to compiler.
77. Describe different types of functions based on return type and arguments.
Based on return type and argument, the function can be categorized into four types. The description
is based on where the input, processing and output will take place.
Function with no arguments and no return type: This kind of function can have no
information exchange with other functions unless global variables are used. Generally, all
the operations such as input, processing and output are done within the function.
General form:
void function_name(void);
Example:
clrscr(); is used to clear the screen with no arguments and no return type.
Function with no arguments but with return type: In this kind of function, the inputs along
with the processing, if required, will be done within the function but the result will be
returned to the called function.
General form:
return_type function_name(void); where return type is not void
Example:
ch = getchar();
Function with arguments but with no return type: Such functions take input values in the
form of arguments, but processing and output part of the problem will be within the
function.
General form:
void function_name(argument list);
Example:
putchar(ch);
Function with arguments and return type: This function, usually, contains only the
processing part. The input and output of the problem will be in the calling function. The
inputs will be passed as arguments while the result will be returned through return
statement of function.
General form:
Return_type function_name(argument list); where return type is not void
Example:
sq = sqrt(num);
78. What are library functions? How it differs from user defined functions?
Library functions are inbuilt functions which consist of line of codes to carry out particular
operation. Library functions are declared in header files, so inclusion of corresponding header file is
a must whenever the library functions are used.
The difference between library function and user defined function are listed below.
79. What do you mean by passing reference? When are they used?
Passing reference in a function means to pass the address of the variable rather than its value.
When a function needs to return more than one value, which it cannot do through the return
statement, then in such situations passing reference are used.
80. Differentiate between pass by value and pass by reference.
The differences between pass by value and pass by reference can be summarized as:
81. What changes can be seen in the function declaration, function call and function definition while
using pass by reference?
When using pass by reference following changes can be seen:
Function declaration: use of pointer notation to hold the address
void function_name(datatype *, datatype *);
void swap(int *, int *);
Function call: use of address operator to pass the address.
Function_name(&var_name1, &var_name2);
swap(&num1, &num2);
Function definition: use of pointer notation to hold the address.
void function_name(datatype *var_name1, datatype *var_name2)
{ }
void swap(int *a, int *b)
{ }
82. What sort of variables require address operator in function call while using pass by reference?
Address operator is required by those variables whose value needs to be changed in the called
function but its changed value is required in the calling function. For example: To find the sum and
difference of two numbers, then there are two inputs, which don’t require change, and two outputs,
which require change. So the function call would be like:
calc(num1, num2, &sum, &diff);
83. Explain different ways of returning multiple values from a function.
The different ways of returning multiple values from a functions are:
Using pass by reference: It works directly on the location of actual arguments, so change can
be seen in calling function when changes are made in called function. So, it can return
multiple values.
Using array: Since array name, in itself, is an address of the first element. It is also a kind of
pass by reference.
Using structure: since structure can contain multiple members, returning a single structure
results in returning multiple values
84. Define recursion. Explain how it differs from iteration
Recursion is a process when a function calls itself repeatedly. The difference between recursion and
iteration is listed in the table below.
85. What are the requirements to solve a problem recursively?
The requirements to solve a problem recursively are:
Base condition: it is the known value to the programmer which is used to terminate the
recursion. For example: to calculate factorial, we know that 0! = 1 or 1! = 1, so either case
can be taken as a base condition.
Recursive solution: It is the solution that converges towards the base condition after each
execution. For example: to calculate factorial of number n, we know that
5! = 5 x 4 x 3 x 2 x 1
5! = 5 x 4!
n! = n x (n-1)!
In programming, fact(n) = n * fact(n-1), here n*fact(n-1) is the recursive solution to calculate
the factorial of given number.
86. List the merits and demerits of using recursive function
Advantages and disadvantages of the recursion can be listed as:
Advantages:
Problem can be solved in easier way
It makes program small; less coding to be done
Disadvantages:
Difficult to find the converging solution
It uses more memory
It is usually a slower process; push pop operation in stack causes it to be a bit slower
87. Define storage class. Compare automatic, extern, and static variables
Storage class in C programming represents the scope along with the life time of a variable. It also
represents the where the value is stored and what is its initial value when first declared. All variables
except register type are stored in main memory. Auto variables are equivalent to local variables; all
local variables are auto by default. Extern variables are equivalent to global variables.
The different storage class and their descriptions are listed in the table below.
88. Demonstrate the use of global variables and static variables.
The following program demonstrates the use of global variables and static variables.
# include<stdio.h>
# include<conio.h>
void globalCheck();
void staticCheck();
int x = 5;
void main()
{
clrscr();
printf("\nValue of x in main function is %d",x);
globalCheck();
staticCheck();
staticCheck();
staticCheck();
getch();
}
void globalCheck()
{
printf("\n\nThe value of x in another function is %d\n",x);
}
void staticCheck()
{
static int i = 0;
i++;
printf("\nThe value of i is %d",i);
}
In this program, the variable x is a global which is accessed in main function as well as in
globalCheck() function. Also, the variable i is declared as static which retains its value between
different function call.
89. What do you mean by derived data type?
Data types that are derived from fundamental data types are called derived data types. They simply
add some functionality to the basic data types. Array and pointer are the basic examples of derived
data types.
90. Define array. When and why are array required?
An array is simply a list or collection of variables of same type which share the common name but
are accessed through different index within a square bracket. The variables are assigned a
contagious memory location.
When the problem is related to not one or two variables but the collection of numbers then arrays
are used. Use of array ensures efficient programming. For example: if we need to add 100 numbers
then declaration of 100 variables individually would be tedious and impractical, but use of array
makes it easier and more efficient by making the declaration and access of numbers short and easy.
91. Explain about array declaration and initialization for different data types.
The array declaration and initialization for different data types are listed below:
One dimensional Array:
Declaration: data_type array_name[size];
Examples: int num[5]; char list[10]; float marks[20];
Initialization at declaration:
o int num[5]={1, 2, 3, 4, 5};
o char list[] = {‘a’, ’b’, ’c’, ’d’};
Initialization: num[0] = 1, num[1] = 2, list[0] = ‘a’, list[1] = ‘b’;
Two dimensional Array:
Declaration: data_type array_name[row_size][col_size];
Examples: int num[5][5]; char list[10][10];
Initialization at declaration:
o int num[2][2] = {{1, 2},{3, 4}};
o char list[][]={{‘a’, ’b’}, {‘c’, ’d’}};
Initialization: num[0][0] = 1, list[1][1] = ‘c’;
92. What does the name of an array represent? Explain with example how to access elements of an array
The name of an array holds the address of first element of an array. So, it represents the base
address of that array.
For one dimensional array, the elements are accessed by using array name followed by an index
within a square bracket. For example: if an array is declared as: int num[5]; then num[0], num[1] are
used to access first and second element respectively.
For two dimensional array, the elements are accessed using array name followed by two square
bracket within which row index and column index should be given. For example: if an array is
declared as: int list[3][3]; then to access the value we use list[0][1] to access the value of first row
and second column.
93. Demonstrate the difference that can be seen in the function declaration, function call and function definition, when a single array element is passed and when complete array is passed.
Let us consider a simple example to demonstrate the difference.
Assuming, Array declaration and initialization: int num[5] = {1 , 2, 3, 4, 5}; And a function check() to
do some operation.
Here, we can see that, in function declaration and definition the square brackets are not required
but square brackets with index are required in function call when single element is passed. On the
other hand, there is a presence of square bracket without index in function declaration and
definition while passing complete array. And in function call, only the name of an array is passed
without pair of square brackets. Since name of an array represents the address of first element, it
exhibits pass by reference whereas passing single element represent pass by value.
94. “Passing single element of an array indicates passing by value whereas passing complete array
(array name) represents pass by reference”, explain the statement with example.
When we pass a single element, we use array name with index. For instance, let num be an array
defined as: int num[10]; then num[2] represents the third element of an array. Since it stores value
only, passing in such way symbolizes passing by value. But when array name is passed, it passes the
starting address of an array, as we know that name of an array represents the address of first
element. For example, passing array name num to the function represents passing of the address of
num[0]; as num = &num[0]. Using that base address, all other elements can be accessed. Hence
passing array name or complete array represents pass by reference.
95. Define string and null character. Explain, with example, about string constants and string variables.
A string is an array of characters terminated with a null character. The null character represents the
end of a string. The null character is written as ‘\0’ in C programming.
Let us take an example to demonstrate string constants and variable.
char name[20] = “Johnson”;
Here, the variable name is declared as array of characters, which can be used as a string variable.
But to use it as a string variable, it must be assigned a string constant or a set of characters with a
null character at the end. The set of characters defined within double quotation marks is a string
constant. There will be a default null character in a string constant. While string formed from set of
characters must be terminated with null character manually.
96. How are string declared? Demonstrate the different ways of initializing strings.
Strings are declared using an array, with char as data type.
char str[10]; here, str is a string variable.
Stings are initialized as
char str[] = “Johnson”;
char str[] = {‘N’,’E’,’P’,’A’,’L’,’\0’};
Size of an array is optional, when strings are initialized in the declaration. The size of the string will
be equal to the number of elements. If size needs to be declared then it must be equal or greater
than the number of characters initialized.
97. What is the drawback of using %s in scanf to read a string? Illustrate different ways of reading a string.
When %s is used as format specifier in scanf then it does read a string with a space. All characters
before the space character will be read but others characters will be ignored.
Different ways of reading a string are as follows.
Using unformatted input function: gets(string_variable);
Using formatted input function: scanf(“%s”, string_variable);
Character search sets can be used
scanf(“%[^\n]”, string_variable); reads characters unless new line is detected.
Reading character wise
i = 0;
while(string_variable[i]!=’\n’)
{
string_variable[i] = getchar();
i++;
}
string_variable[i] = ‘\0’;
98. Explain why address operator (ampersand) is not used while reading a string?
String is the name of an array of character. Since name of an array represents the address of the first
elements, string also symbolizes the address of first character. Hence address operator is not used
while reading a string using scanf().
99. Describe the general form, purpose, and return value (if any) of the following string functions:
strlen(), strcat(), strcpy(), strcmp()
The description of different string functions are listed in the table below:
100. Explain briefly about array of strings.
As one dimensional array of characters represent a string, two dimensional arrays of characters
represent an array of strings.
General Form:
char string_name[number_of_strings][Number of characters in each string];
51
char namelist[10][20]; it represents an array of strings with total 10 strings with each 20
characters long.
Initialization:
char namelist[10][10] = {“Johnson”,
“Sarah”,
“Holmes”
};
In two dimensional declarations of arrays of characters, if the variable is followed with two pair
of square brackets then it represents a single character. For instance, namelist[1][1] represents
the character ‘a’. In simple, it selects the second row and then second column, pointing to
character ‘a’. But if the variable is followed with single pair of square brackets then it represents
a string. For example, namelist[2] symbolizes a string “Holmes”.
101. What is a pointer? Differentiate pointer with an array.
A pointer is a variable which holds the address of another variable. Like other variable, it must
be declared before using it. Its general form is:
data_type *var_name;
int *p; it represents a pointer declaration with variable name p and integer as its type.
The differences between pointer and array are as follows:
0 Comments