Important Question PPS
Important Question PPS
1. Draw block diagram of computer system and explain its various components
2. Enlist types of operators and explain any two in detail with example.
9. Explain how string is defined in C. List the various inbuilt string functions
10. Enlist branching statements with suitable example. For loop, do while loop, while
loop with syntax.
12. List out basic data types in C with its Memory size.
13. Explain Entry controlled loop and Exit controlled loop with appropriate flow
chart.
15. What is conditional statement? Describe If, else If, nested Else If, Else if ladder
and switch case statement with example.
25. List out various mode of File open. Also discuss File Function.
Note:
This is important questions which were asked in previous year GTU external examination
(both Regular as well as remedial)
Answer of this questions are given below.
In exam, if it is asked any question from above list then you need to write their answer
according to their marks weightage (like 3 marks, 4 marks, 7 marks as per GTU paper style)
For the programs, you need to refer practical list. Any question from that can be ask in final
exam.
1. Draw block diagram of computer system and explain its various components
Ans:
There are basic five components which are
1. Central Processing Unit (CPU) (ALU and CU)
2. Memory
3. Input Device
4. Output Device
5. Secondary Storage Device
Central Processing Unit
The Central Processing Unit (CPU) is also known as “brain” of the computer system.
Based on Instruction it received, the CPU performs operation on data.
It is also responsible for controlling the data that is flowing from the computer system.
CPU consist of (ALU + CU = CPU)
1. Arithmetic Logic Unit (ALU) and 2. Control Unit (CU)
Some input devices, such as the keyboard, enable the user to communicate directly with
the machine.
Output Devices:
Output device supply result of processing from primary storage or secondary storage.
When a program executed and the results must be available in a human readable form.
The output devices translate processed data from a machine coded form to a form that can
be read and used by people.
Most common types of output devices are monitor which resembles a screen, printer prints
copy from computer on paper, plotter plot diagram or figure on paper.
2. Enlist types of operators and explain any two in detail with example.
Operators help in doing various operations. The operator can be divided into different
categories as shown below.
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment and Decrement Operators
6. Conditional Operators
7. Bitwise Operators
8. Special Operators
Arithmetic Operators
They are used to perform arithmetic operations.
‘C’ language supports following arithmetic operators.
Except % operator all arithmetic operators can be used with any type of numeric operands,
while % operator can only be used with integer data type only.
Example: Program illustrating use of arithmetic operators.
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf(“Enter the value of x and y:”);
scanf(“%d %d”,&x, &y);
printf(“\nAddition of x and y is: %d\n”, x+y);
printf(“Addition of x and y is: %d\n”, x-y);
printf(“Addition of x and y is: %d\n”, x*y);
printf(“Addition of x and y is: %d\n”, x/y);
printf(“Addition of x and y is: %d\n”, x%y);
}
Note: After every program write output of your code.
Output : ??
Relational Operators
Relational operators are used to compare variables or constants.
Relational operators are used to compare whether some variable value is higher (greater),
equal or lower with other variables.
Following operators are the relational operators in ‘C’ language.
System Software
System software is designed to control operation and extend the processing capabilities of
computer system.
They control internal computer functions, such as reading data from input device, transmit
processing information to output devices, provide various facilities like file management,
Input/output management and CPU time to user program during execution of program.
They help hardware component to work together and provide support for development and
execution of application software.
There are various system software available.
1. Operating System (OS)
2. Compiler
3. Assembler
4. Loader
5. Linker
6. Editor
7. Translator
8. Macro Processor
9. Interpreters
6. Describe following string functions in C – Language.
(1) strcpy( ) , (2) strcat( ) , (3) strlen( ) , (4) strcmp ( )
strlen() function
strlen() is used to return the length of the string , that means counts the number of characters
present in a string.
Syntax
integer variable = strlen (string variable);
strcat() function
The strcat() is used to concatenate two strings. The second string will be appended to the end
of the first string. This process is called concatenation.
Syntax
strcat (StringVariable1, StringVariable 2);
strcmp() function
strcmp() function is used to compare two strings. strcmp() function does a case sensitive
comparison between two strings. The two strings are compared character by character until
there is a mismatch or end of one of the strings is reached (whichever occurs first). If the two
strings are identical, strcmp( ) returns a value zero. If they‟re not, it returns the numeric
difference between the ASCII values of the first non-matching pairs of characters.
Syntax
strcmp(StringVariable1, StringVariable2);
strcpy() function:
strcpy() function is used to copy one string to another. strcpy() function copy the contents of
second string to first string.
Syntax
strcpy(StringVariable1, StringVariable2);
7. What is array? Give example and advantages of array. Explain initialization of an
array value by example.
An array is collection of variable having same data type.
An array permits homogeneous data. It means that similar types of elements are stored
contiguously in the memory under one variable name.
Suppose we have to store the roll numbers of the 100 students the we have to declare 100
variables named as roll1, roll2, roll3, …, roll100 which is very difficult job. C programming
arrays is introduced in C which gives the capability to store the 100 roll numbers in the
contiguous memory which has 100 blocks and which can be accessed by single variable name.
Syntax: <data type> array name [size1][size2].....[sizen]
Examples for initialization:
int iarr[3] = {2, 3, 4};
char carr[20] = “program”;
float farr[3] = {12.5, 13.5, 14.5};
10. Enlist branching statements with suitable example. For loop, do while loop, while loop
with syntax.
The statements that help us to execute set of statements repeatedly are called as looping
statements.
There are 3 loops
1. For Loop
2. do..While loop
3. While Loop
While Loop
The while statement is typically used in situations where it is not known in advance
how many iterations are required.
Syntax:
while ( control expression )
{
An action or Series of Actions ; //body of loop
}
Here, first the control expression is evaluated to TRUE or FALSE. If it is evaluated to
TRUE, then an action or series of action i.e. body of loop is executed. The process of executing
the body of loop is repeated until the control expression is FALSE. Once control expression is
evaluated to FALSE, then the control is transferred out of the loop. The execution resumes
from the statement following the loop (if any).
Generally, when initialization and updating is included in the while loop, it looks as follows.
Initialization // initialization is used before the start of loop and sets
// the variable(s) to control the loop
while ( control expression )
{
An action or Series of Actions ; //body of loop
}
Flowchart:
for statement
The for statement is most often used in situations where the programmer knows in advance
how many times a particular set of statements are to be repeated. The for statement is
sometimes termed as counter controlled loop.
Syntax :
for ( expression1 ; expression2 ; expression3 )
{
An action or series of actions ; // body of loop
}
expression1:- This is usually an assignment/initialization statement to set a loop control
variable(s) for example. But it is not restricted to only initialization, it can be any valid C
statement.
expression2:- This is usually a control expression which determines when loop will
terminate. But, this can be any statement of C that evaluates to TRUE or FALSE.
expression3:- This usually defines how the loop control variable(s) will change each time the
loop is executed i.e. updating.
Body of loop:- Can be a single statement, no statement or a block of statements.
do…while statement
The terminating condition in the for and while loops is always tested before the body of the
loop is executed -- so of course the body of the loop may not be executed at all.
In the do…while statement on the other hand the body of the loop is always executed at least
once as the condition is tested at the end of the body of the loop.
Syntax :
Initialization
do
{
//body of the loop
} while (control expression ) ;
The flowchart of do…while loop is shown below
11. Explain the use of following statements.
(I) break (II) continue (III) goto
goto
The goto statement is known as jump statement in C. As the name suggests, goto is used to
transfer the program control to a predefined label. The goto statment can be used to repeat
some part of the code for a particular condition.
12. List out basic data types in C with its Memory size
13. Explain Entry controlled loop and Exit controlled loop with appropriate flow chart.
Flow chart
14. Show 1D array, 2D array declaration, initialization and iteration
Single / One Dimensional Array:
1. Single or One Dimensional array is used to represent and store data in a linear form.
2. Array having only one subscript variable is called One-Dimensional array
3. It is also called as Single Dimensional Array or Linear Array
Single Dimensional Array Declaration and initialization:
Syntax for declaration: <data type> <array name> [size];
Examples for declaration: int iarr[3]; char carr[20]; float farr[3];
Examples for initialization:
int iarr[3] ;
char carr[20] = “program”;
float farr[3] = {12.5, 13.5, 14.5}
You can enter elements using for loop (From Keyboard)
for (i=0;i<5;i++)
{
scanf(“%d”, &num[i]);
}
15. What is conditional statement? Describe If, else If, nested Else If, Else if ladder and
switch case statement with example.
There come situations in real life when we need to make some decisions and based on these
decisions, we decide what we should do next. Similar situations arise in programming also
where we need to make some decisions and based on these decisions we will execute the next
block of code.
Decision making statements in programming languages decides the direction of flow of
program execution. Decision making statements available in C are:
1. if statement
2. if..else statements
3. nested if statements
4. if-else-if ladder
5. switch statements
if statement in C
if statement is the most simple decision making statement. It is used to decide whether a certain
statement or block of statements will be executed or not i.e if a certain condition is true then a
block of statement is executed otherwise not.
Syntax:
if(condition)
{
// Statements to execute if condition is true
}
Here, condition after evaluation will be either true or false. C if statement accepts boolean
values – if the value is true then it will execute the block of statements below it otherwise not.
If we do not provide the curly braces ‘{‘ and ‘}’ after if(condition) then by default if statement
will consider the first immediately below statement to be inside its block.
Example
#include <stdio.h>
void main()
{
int i = 10;
if (i > 15)
{
printf("10 is less than 15");
}
printf("I am Not in if");
}
Output:
I am Not in if
if-else in C
The if statement alone tells us that if a condition is true it will execute a block of statements
and if the condition is false it won’t. But what if we want to do something else if the condition
is false. Here comes theC else statement. We can use the else statement with if statement to
execute a block of code when the condition is false.
Syntax:
if (condition)
{
// Executes this block if condition is true
}
else
{
// Executes this block if // condition is false
}
Example
#include <stdio.h>
void main()
{
int i = 20;
if (i < 15)
{
printf("i is smaller than 15");
}
else
{
printf("i is greater than 15");
}
}
Output:
i is greater than 15
nested-if in C
A nested if in C is an if statement that is the target of another if statement. Nested if statements
means an if statement inside another if statement. C allows us to nested if statements within if
statements, i.e, we can place an if statement inside another if statement.
Syntax:
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
Example:
#include <stdio.h>
void main()
{
int i = 10;
if (i == 10)
{
// First if statement
if (i < 15)
{
printf("i is smaller than 15\n");
// Will only be executed if statement above is true
}
if (i < 12)
{
printf("i is smaller than 12 too\n");
}
else
{
printf("i is greater than 15");
}
}
}
Output:
i is smaller than 15
i is smaller than 12 too
if-else-if ladder in C
Here, a user can decide among multiple options. The C if statements are executed from the
top down. As soon as one of the conditions controlling the if is true, the statement associated
with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the
conditions are true, then the final else statement will be executed.
Syntax:
if (condition)
statement;
else if (condition)
statement;
..
else
statement;
Example:
#include <stdio.h>
void main()
{
int i = 20;
if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
}
Output:
i is 20
switch case in c
The switch statement allows us to execute one code block among many alternatives.
You can do the same thing with the if...else..if ladder. However, the syntax of the switch
statement is much easier to read and write.
Syntax
switch (expression)
{
case constant1:
// statements
break;
case constant2:
// statements
break;
…
default:
// default statements
}
The expression is evaluated once and compared with the values of each case label.
If there is a match, the corresponding statements after the matching label are executed. For
example, if the value of the expression is equal to constant2, statements after case constant2:
are executed until break is encountered.
If there is no match, the default statements are executed.
Example
#include <stdio.h>
void main()
{
char operation;
double n1, n2;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operation);
printf("Enter two operands: ");
scanf("%lf %lf",&n1, &n2);
switch(operation)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",n1, n2, n1+n2);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf",n1, n2, n1-n2);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf",n1, n2, n1*n2);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",n1, n2, n1/n2);
break;
default:
printf("Error! operator is not correct");
}
}
16. Explain the concept of Pointers. What is Array of Pointers?
A pointer is a variable whose value is the address of another variable, i.e., direct address of
the memory location. Like any variable or constant, you must declare a pointer before using it
to store any variable address.
Syntax
data_type *var-name;
The asterisk * used to declare a pointer
int *ip; /* pointer to an integer */
Example
#include <stdio.h>
int main ()
{
int var = 20; /* actual variable declaration */
int *ip; /* pointer variable declaration */
ip = &var; /* store address of var in pointer variable*/
printf("Address of var variable: %x\n", &var ); /* address stored in pointer variable */
printf("Address stored in ip variable: %x\n", ip ); /* access the value using the pointer */
printf("Value of *ip variable: %d\n", *ip );
return 0;
}
Output:
Address of var variable: bffd8b3c
Address stored in ip variable: bffd8b3c
Value of *ip variable: 20
Array of pointers
There may be a situation when we want to maintain an array, which can store pointers to an int
or char or any other data type available. Following is the declaration of an array of pointers.
int *ptr[MAX];
It declares ptr as an array of MAX integer pointers. Thus, each element in ptr, holds a pointer
to an int value.
Example
#include <stdio.h>
const int MAX = 3;
void main ()
{
int var[] = {10, 100, 200};
int i, *ptr[MAX];
for ( i = 0; i < MAX; i++)
{
ptr[i] = &var[i]; /* assign the address of integer. */
}
for ( i = 0; i < MAX; i++)
{
printf("Value of var[%d] = %d\n", i, *ptr[i] );
}
}
When the above code is compiled and executed, it produces the following result −
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
Storage classes in C are used to determine the lifetime, visibility, memory location, and
initial value of a variable. There are four types of storage classes in C
o Automatic
o External
o Static
o Register
Note: Please refer book or Internet for the description and example if it is ask in exam having
7 marks.
23. Give difference between Structure and Union.
Note: Please refer book or Internet for the description and example if it is ask in exam having
7 marks.
24. What is Dynamic Allocation? List out memory allocation functions.
An array is a collection of a fixed number of values. Once the size of an array is declared, you
cannot change it.
int a[10];
If there is a situation where only 5 elements are needed to be entered in this array. In this case,
the remaining 5 indices are just wasting memory in this array. So there is a requirement to
lessen the length (size) of the array from 10 to 5.
Take another situation. In this, there is an array of 10 elements with all 10 indices filled. But
there is a need to enter 3 more elements in this array. In this case, 3 indices more are required.
So the length (size) of the array needs to be changed from 10 to 13.
To solve this issue, you can allocate memory manually during run-time. This is known as
dynamic memory allocation in C programming. To allocate memory dynamically, library
functions are malloc(), calloc(), realloc() and free() are used. These functions are defined in
the <stdlib.h> header file.
malloc() method: memory allocation
The “malloc” or “memory allocation” method in C is used to dynamically allocate a single
large block of memory with the specified size. It returns a pointer of type void which can be
cast into a pointer of any form. It doesn’t initialize memory at execution time so that it has
initialized each block with the default garbage value initially.
Syntax:
ptr = (type *) malloc(byte-size)
Example:
ptr = (int*) malloc(100 * sizeof(int));
Since the size of int is 2 bytes, this statement will allocate 200 bytes of memory. And, the
pointer ptr holds the address of the first byte in the allocated memory.
calloc() method: contiguous allocation
“calloc” or “contiguous allocation” method in C is used to dynamically allocate the specified
number of blocks of memory of the specified type. It is very much similar to malloc() but has
two different points and these are:
It initializes each block with a default value ‘0’.
It has two parameters or arguments as compare to malloc().
Syntax:
ptr = (type*)calloc(n, element-size);
Here, n is the no. of elements and element-size is the size of each element.
For Example:
ptr = (float*) calloc(25, sizeof(float));
This statement allocates contiguous space in memory for 25 elements each with the size of the
float.
free() method
“free” method in C is used to dynamically de-allocate the memory. The memory allocated
using functions malloc() and calloc() is not de-allocated on their own. Hence the free() method
is used, whenever the dynamic memory allocation takes place. It helps to reduce wastage of
memory by freeing it.
Syntax:
free(ptr);
realloc() method
“realloc” or “re-allocation” method in C is used to dynamically change the memory allocation
of a previously allocated memory. In other words, if the memory previously allocated with the
help of malloc or calloc is insufficient, realloc can be used to dynamically re-allocate memory.
re-allocation of memory maintains the already present value and new blocks will be initialized
with the default garbage value.
Syntax:
ptr = realloc(ptr, newSize);
Where, ptr is reallocated with new size 'newSize'
25. List out various mode of File open. Also discuss File Function.
File handling in C enables us to create, update, read, and delete the files stored on the local file
system through our C program. The following operations can be performed on a file.