C Programming Question With Answers
- Posted by Engineering Helpline Pvt. Ltd
- Categories c programming, C Programming Question With Answers, First Semester
- Date April 1, 2018
- Comments 0 comment
61. What are nested loops? When are they used?
Nested loops are the combination of loops in which body or statements of one of the loops contain another loop. Nested loops can be formed combining different types of loop.
•Presence of for loop within a for/while/do while loop
•Inclusion of while loop within a while/for/do while loop
•Including do while loop within a do while/while/for loop
Nest loops are used in following cases, in general.
•A particular problem requires a loop, and the same problem to be repeated to number of other samples. For example, to determine if the number is prime number or not requires a single loop but if the range of numbers is to be checked then nested loop comes into action.
34
•Working with two or more dimensional structure such as in case of matrices. Accessing each element of two dimensional arrays requires nested loop.
62.Demonstrate how to change for loop to a while loop?
The following demonstration shows a change of for loop to a while loop.
statement 1;
|
statement 1;
|
initialization;
|
for(initialization ; condition ; update)while(condition)
|
{
|
{
|
|
statements;
|
statements;
|
|
}
|
update;
|
|
statement 2;
|
}
|
63. Explain the working flow of nested for loop with example.
The diagram shown below demonstrates the working flow of nested for loop. Let us take an example to display the following pattern
1
12
12 3
|
1 2 3 4
|
for (
|
x = 1 ; x < = 5 ; x + + )
|
||
|
1 2 3 4
|
5
|
|||
F{
|
T
|
||
|
for(
|
z = 1
|
; z < = x ; z + + )
|
|
{
|
||
|
F
|
T
|
printf(“%d ”, z);
|
}
printf(“n”);
}
35
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.
36
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:
oReturn_type function_name(argument list);
▪Example:
ofloat 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:
ovariable = function_name(argument list); if the function returns a value
ofunction_name(argument list); if the function doesn’t return a value
▪Example:
oS = sum(a,b); where a, b and S are of float type
37
•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:
oreturn_type function_name(argument list)
{
Local variable declaration; Statements;
Return statement;
}
▪Example:
ofloat 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:
|
SN
|
Actual arguments
|
Formal arguments
|
|
1.
|
Arguments passed in the function call
|
Arguments in the function definition
|
|
2.
|
Defined in the calling function
|
It belongs to the called function
|
|
3.
|
Change in actual arguments will change
|
Changing the value of formal arguments does
|
|
the value of formal arguments
|
not change the value of actual arguments,
|
|
|
unless reference is used.
|
||
38
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.
You may also like
Applied mechanis And Its Solutions
6 January, 2020
c programming notes
17 April, 2019
Structure: Array VS Structure: Pointer and Structure:
4 February, 2019
A structure is a collection of variable (of different data types) referenced under one name, providing of convenient means of keeping related information together. A structure definition forms a template that may be used to create structure variable. The general …
