You are on page 1of 8

1. What are Printf() and scanf() Functions ?

Ans: printf() and scanf() functions are inbuilt library functions in C/predefined functions which
are available in which are available in “ stdio.h” header file.
printf() function is used to print the “character, string, float, integer, octal and hexadecimal
values” onto the output screen.
scanf() function is used to read character, string, numeric data from keyboard

The syntax of printf is:


printf(format_string, argument_list);

Example 1: printf("You entered: %d", num); - formatted printf() function where %d is format
specifier

Example 2: printf("Enter a number: "); - unformatted printf() function

The syntax of scanf is:


scanf(format_string, argument_list);

Example : scanf("%d", &num);

2. What is a Key word in C?


Ans: Keywords are reserved words in C and Keywords are may not be used as constant
or variable or any other identifier names.
There are 32 reserved keywords are there in C. They are:
auto break case char const continue do double default else enum extern
float for goto if int long return register short switch struct static
signed sizeof typedef union unsigned volatile void while

NOTE : write few keywords need not to memorize all 32 keywords as the question is for 1mark
only

3. Define a Variable?
Ans: Variable is the name of a memory location that we use for storing data. We can change the
value of a variable in C language and we can also reuse it multiple times.
Syntax for variable Declaration:
DataType VariableList;

Example 1: int i,j;


Example 2: char c, ch;
4. Define Conditional Statements and give the list of them?
Ans: Conditional Statements which allows to perform actions depending upon some conditions
provided by the programmer. The Different types of conditional statements are:
1) If Statement
2) If else statement
3) Nested- if else statement
4) Switch Statement

5. What are Iterative or Looping Statements?


Ans: Iterative or Looping statement which executes the statements with in the compound
statement by checking the condition, and performs same set of statements as a iterative process
or as a loop until the condition false.
The Iterative or Looping Statements are: While, do-while, for

6. Define algorithm and flowchart.


Ans: An algorithm is a step-by-step procedure for solving a problem or accomplishing a goal.
The pictorial representation of algorithm is known as flowchart. In flowchart we use different
geometric shapes for different operations.

S.NO Name of the Shape Use


shape
1 Oval Start/stop
2 Rectangle Processing/calculating
3 Parallelogram Input/output
4 Rhombus Decision making
4 Arrows Connecting lines
5 Circle On page connector
6 Pentagon Off page connector

7. What is Enum data type?

Ans: Enumeration or an enum in C is a user-defined data type that consists of a set of named
integer constants. Enums are used to represent a group of related values, such as the days of the
week, the months of the year, or the colors of the rainbow.

example:
enum Color {
RED,
GREEN,
BLUE
};

Here RED = 0, GREEN=1 and BLUE= 2


8. What are the uses of Pointers?
Ans: Pointer is used in the following cases
 It is used to access array elements.
 It is used for dynamic memory allocation.
 It is used in Call by reference.
 It is used in data structures like trees, graph, linked list etc.

9. What is a String?
Ans: C Strings are nothing but array of characters ended with null character (‘\0’).Strings are
always enclosed by double quotes. Whereas, character is enclosed by single quotes in C.
Example:
char string[20] = { ‘H’ , ‘e’ , ‘l’ , ‘l’ , ‘o’ , ‘\0’};
(or)
char string[20] = “Hello”;
(or)
char string [] = “Hello”;

10. What are self referential structures?


Ans: The self-referential structure is a structure that points to the same type of structure. It
contains one or more pointers that ultimately point to the same structure. Structures are a user-
defined data structure type in C

11. Define Structure. Write its syntax.


Ans: A structure is a user-defined data type in C that allows you to group together related data
items of different types. This can be useful for storing information about a particular entity, such
as a student, employee, or product.

Example:

struct Student {
char name[20];
int age;
float grade;
};

12. What is a NULL pointer in C?


Ans: A NULL pointer is a pointer that does not point to any memory location and hence does not
hold the address of any variables. the null pointer in C holds the value Null

13. How is fopen()used ?


Ans: The function fopen() returns a file pointer. Hence a file pointer is declared and it is
assigned as
FILE *fp;
fp= fopen(filename,mode);
filename is a string representing the name of the file and the mode represents:
“r” for read operation
“w” for write operation
“a” for append operation
“r+”,”w+”,”a+” for update operation
14. Explain various text file opening modes?
Ans: There are various text file opening modes in programming. Some of the most common ones
are:

 r: Opens the file in read mode. This is the default mode.


 w: Opens the file in write mode. If the file does not exist, it will be created. If the file exists, its
contents will be overwritten.
 a: Opens the file in append mode. If the file does not exist, it will be created. If the file exists,
new data will be appended to the end of the file.
 r+: Opens the file in both read and write mode. The file must exist.
 w+: Opens the file in both read and write mode. If the file exists, its contents will be
overwritten. If the file does not exist, it will be created.
 a+: Opens the file in both read and append mode. New data will be appended to the end of the
file.

15. What are preprocessor commands?


Ans: Preprocessor commands, also known as preprocessor directives, are instructions given to a
compiler to perform compilation. It starts with #, The # is followed by an identifier that is the
directive name. For example, #define is the directive that defines a macro.
Example #define
#include
#else
#elif
#if

16. Differentiate between text and binary file?


 Text files
are made up of characters, which can be read by humans. They are typically used to store
documents, such as letters, reports, and source code. These files are less secure.
 Binary files
are made up of bytes, which are numbers 0’s and 1’s that represent machine instructions or
data these files are difficult to read. They are typically used to store executable programs,
images, and music. These files are more secure.

17. Explain syntax of fprintf( ) and fscanf ( ) functions?


Ans: The fprintf() and fscanf() functions are used to write and read formatted data to and from a
file, respectively. The syntax for both functions is similar:
int fprintf(FILE *stream, const char *format, ...);
int fscanf(FILE *stream, const char *format, ...);

18. What is file appending?


Ans: File appending is the process of adding new data to the end of an existing file.
Syntax:
FILE *fp = fopen("my_file.txt", "a");

19. What is Recursion?


Ans: Recursion in C is a process where a function calls itself directly or indirectly. This allows a
complex problem to be broken down into smaller sub-problems, each of which is solved by
invoking the same function. It must halt at a definite point to avoid infinite recursion. A recursive
function halts when condition for recursion fails.

20. What is the difference between malloc( ) and calloc( ) Functions?


Ans: Malloc is used for memory allocation and initialize garbage values. malloc () for allocating
the single block of memory.
Syntax:
*ptr-variable=(type-casting*)malloc(n*sizeof(DataType))
Example:
*ptr=(int*)malloc(5*sizeof(int));

Calloc
is same as malloc but it initialize 0 value.calloc () for allocating multipleblocks of memory.
Syntax:
*ptr-variable=(type-casting*)calloc(n,sizeof(DataType))
Example:
*ptr=(int*)calloc(5,sizeof(int));

21. Define Call by Value?


Ans: In call by value method, the value of the variable is passed to the function as parameter.
The value of the actual parameter can not be modified by formal parameter. Different Memory
is allocated for both actual and formal parameters.
•Actual parameter – This is the argument which is used in function call.
•Formal parameter – This is the argument which is used in function definition

22. Differentiate between function declaration and function definition.


Ans: The main difference between function declaration and function definition is that a function
declaration tells the compiler about a function's name, return type, and parameters, while a
function definition provides the actual body of the function.

Here is an example of a function declaration in C:

int add(int a, int b);

Here is an example of a function definition in C:


int add(int a, int b) {
return a + b;
}

23. Define Call by Reference?


Ans: Call by reference is a method that passes the reference or address of an actual parameter to
the function's formal parameters. This means that any changes made to the values inside the
function will also be reflected in the actual values.

24. Write about stdlib function?


Ans: The stdlib.h header file is a part of the C standard library and contains a variety of functions
for performing common tasks, such as memory allocation, data type conversion, and random
number generation. Some of the most commonly used functions in stdlib.h include:

 malloc(): Allocates a block of memory of the specified size and returns a pointer to the allocated
memory.
 free(): Frees the memory allocated by malloc().
 calloc(): Allocates an array of the specified size and initializes each element to zero.
 realloc(): Changes the size of an existing memory block.
 atoi(): Converts a string to an integer.
 atof(): Converts a string to a floating-point number.
 rand(): Generates a random number.
 srand(): Initializes the random number generator.
 exit(): Terminates the program.
 system(): Executes a system command.
 abort(): Terminates the program abnormally.

25. What are draw backs of linear search?


Ans: Linear search, also known as sequential search, has the following drawbacks:

 Time complexity: Linear search has a time complexity of O(n), which means that the time it
takes to search for an element increases linearly as the size of the dataset increases.
 Efficiency: Linear search is time consuming and not very efficient because it compares each
element one by one.
 Large datasets: Linear search is not suitable for large arrays.

26. What is sorting?


Ans: Sorting refers to the technique used for rearranging the data elements present in a string or
an array in any specified order, descending or ascending.

27. What are the advantages of bubble sort?


Ans: Bubble sort is a simple sorting algorithm that compares and swaps adjacent elements in a
list or array. It's often used by beginning developers because it's easy to write and doesn't require
advanced concepts. Bubble sort is an in place sorting algorithm. It does not require extra
space(memory)

28. Find out no of comparisons to sort { 2,1,7,4,8,6 }


Ans: lets find out no of comparisons to sort { 2,1,7,4,8,6 } using bubble sort i.e.,
n(n-1)/2
= 6(6-1)/2
=6(5)/2
=30/2
=15

29. What is the condition to apply binary search.


Binary search is a divide-and-conquer algorithm that repeatedly divides the search interval in
half until the target value is found or determined to be not in the list. Binary search is faster than
linear search except for small arrays. Binary search can only be applied to sorted arrays. The
data structure must be sorted in either ascending or descending order. so that the target
value can be identified and compared.

30. Write an algorithm for selection sort.

1. Declare an array of size, n.


2. Provide the n inputs such that the array is unsorted.

3. Loop i, from 0 to n-2

4. Inner loop j runs from i+1 to n-1

5. On each iteration of j:

 If arr[i] > arr[i] then we swap the number in order to sort the array in ascending order.

 Else continue

6. Print the sorted array

In this manner, the array will be sorted in ascending order will the lowest elem

You might also like