0% found this document useful (0 votes)
39 views18 pages

Internal 1 QB

Uploaded by

sibikr46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views18 pages

Internal 1 QB

Uploaded by

sibikr46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Internal test 1 – U23CS101 C Programming

PART A

1. List the three types of Computer Language.


Computer languages are the languages through which the user can communicate with the computer by writing
program instructions.

2. Define interpreter.
• An interpreter is a software/program that reads one statement from the source code, translates it to the
machine code or virtual machine code, and then executes it right away.
• Interpreter is a program that translates source code written in high-level language to machine code.

3. State the difference between if and if else in C.


S. No If If…. Else

1 if statement is used to test a specific if…else statement is also used to test a specific
condition. condition and else statement follows an if statement
2 if condition is true, the block of codes if condition is false, then if block is skipped and control
within the if statement is executed. moves to the else and the code of blocks within the else
statement is executed
3 Syntax: Syntax:
if (condition) if (condition)
{ {
//Code of blocks // Code to execute if condition is true
} }
else
{
// Code to execute if condition is false
}

4. What is the purpose of the #define directive in C?


• #define directive in C is used to create macro definitions, which are essentially constants or shorthand
representations for values or expressions in code.
• Constant Values: It allows the definition of constants that can be used throughout the code.
Internal test 1 – U23CS101 C Programming
For example: #define PI 3.14
• In this example, PI will be replaced with the value 3.14 and the preprocessor will replace all instances
of PI with 3.14 during compilation
• Function-like Macros: It can create macros that accept arguments, similar to functions.
For example: #define SQUARE(x) ((x) * (x))
This enables the use of SQUARE(5), which the preprocessor replaces with ((5) * (5)).
• Code Clarity and Maintenance: #define is used to make code clearer and easier to maintain. Changes
to a value can be made in one location, reducing the risk of errors.

Syntax: #define NAME value


NAME: The symbolic name for the constant or macro.
value: The value or expression to replace the symbolic name with.

5. Write the syntax of an array in C programming?


Syntax:
data_type array_name[size];
• data_type: Specifies the type of elements the array will hold (e.g., int, float, char).
• array_name: The name of the array.
• size: Number of elements the array can store (must be a positive integer).

6. Define Compiler.
• Compiler is a program that converts human readable code into machine readable code. This process
is called compilation.
• A Compiler is a program or set of programs that converts source code written in a high-level language
to low-level language (assembly language or machine language).
• For example GCC C, Turbo C, Quick C etc. are different compilers for C programming language.

7. What is Expression?
• An expression in programming refers to a combination of variables, constants, operators, and function
calls that evaluates to a value.
• Expressions are fundamental building blocks in programming languages, that allows to perform
calculations, manipulate data, and control the flow of programs.
Example: C=A+B here A, B, C is operand and =, + is operator
Internal test 1 – U23CS101 C Programming

8. List the differences between a for loop and a while loop.


S. No FOR WHILE
1. For loop is used for iterating over a sequence While loop repeatedly executes a block of code
of iteration or a known number of iterations. as long as the specified condition is true.

2. The condition is checked before entering the The condition is checked before entering the
iterations. loop.
3 Syntax: Syntax:

for (initialization; condition; iteration) while (condition)


{ {
// code to be executed // code to be executed
} }

9. What is a header file in C? Provide an example.


• header file is a file with a .h extension that contains declarations for functions, macros, constants, and
data types to be shared across multiple source files.
• Header files are included at the beginning of a program file using the #include preprocessor directive,
that allow to use the functions and definitions they contain without rewriting the code.
Example: A commonly used header file is stdio.h, which is for standard input and output functions, like
printf and scanf.
#include <stdio.h>
int main()
{
printf("Hello, World!");
}

10.How do you declare a one-dimensional array in C?


One-dimensional array is declared by specifying the data type, followed by the array name and its size (the
number of elements it can hold) within square brackets [].
Syntax:
data_type array_name[size];
Example:
int a[25];
here datatype is integer
array name is a

array size is 25
Internal test 1 – U23CS101 C Programming

PART B

11. Discuss the structure of a C program with an example.

1. Documentation Section: The documentation section consists of a set of comment lines giving the name of
the program, the author and other details.

2. Link Section: The link section provides instructions to the compiler to link functions from the system
library such as using the #include directive.

3. Definition Section: The definition section defines all symbolic constants such as #define directive.

4. Global Declaration Section: There are some variables that are used in more than one function. Such
variables are called global variables and are declared in the global declaration section. This section also
declares all the user-defined functions.

5. main () function Section: Every C program must have one main function section. This section contains two
Internal test 1 – U23CS101 C Programming
parts; declaration part and executable part

i) Declaration part: The declaration part declares all the variables used in the executable part.
ii) Executable part: There is at least one statement in the executable part.

These two parts must appear between the opening and closing braces. The program execution begins at the
opening brace and ends at the closing brace. The closing brace of the main function is the logical end of the
program. All statements in the declaration and executable part end with a semicolon.

6. Sub-program Section:
The subprogram section contains all the user defined functions that are used to perform a specific task.

12.Elucidate the concept of C tokens and their significance in C programming.


❖ Each and every smallest individual unit in a C program is known as C tokens
❖ i.e. C program is also a collection of tokens, comments, and whitespaces.

1) KEYWORDS
• Keywords are predefined, reserved words used in programming that have special meanings to the compiler.
• Thus, the keywords cannot be used as variable names.
• C language has reserved 32 words as keywords
• All the Keywords are written in lower-case letters. Since C is case-sensitive
int distance ;
int roll_mo ;
float area ;
Internal test 1 – U23CS101 C Programming

2) IDENTIFIERS
• Identifiers are the names given to or names of variables, union, function name, structure names
• Identifier must follow some rules. Here are the rules:
1. All identifiers must start with either a letter( a to z or A to Z ) or currency character($) or an
underscore.
2. They must not begin with a digit
3. After the first character, an identifier can have any combination of characters.
4. C keywords cannot be used as an identifier.
5. Identifiers in C are case sensitive, foo and Foo are two different identifiers.
Example:

3) CONSTANTS OR LITERALS
• C constants refer to the variables that do not change their value during the program execution.
• They are also called as literals. Constants may belongs to any of the data type.
Syntax:
const data_type variable_name;
Example: int a=10;
Here a is variable, 10 is Constant
Internal test 1 – U23CS101 C Programming

4) STRING
❖ In C, strings are nothing but an array(group) of characters ended with a null character (‘\0’).
❖ This null character indicates the end of the string. Strings are always enclosed in double quotes.
❖ Whereas, a character is enclosed in single quotes in C and C++.
❖ Declarations for String:
char string[20] = {‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘\0’};
char string[20] = "atnyla";
char string [] = "atnyla";

5. SPECIAL SYMBOLS
The following Special symbols are used in C having some special meaning and it cannot be used for some
other purpose
, < > . _

( ) ; $ :

% [ ] # ?

' & { } "

^ ! * / |

- \ ~ +

6.OPERATORS:
❖ An operator is a symbol that operates on a value or a variable.
❖ For example: + is an operator to perform addition. C has a wide range of operators to perform
various operations.
❖ An operator is a symbol that takes one or more arguments and operates on them to produce a result.
Internal test 1 – U23CS101 C Programming

13. Explain the working of the while and do-while loops with examples. Compare their
flow of execution.
S.No while loop do-while loop
1 While the loop is an entry control loop The do-while loop is an exit control loop because in
because firstly, the condition is checked, this, first of all, the body of the loop is executed then
then the loop's body is executed. the condition is checked true or false.
2 If condition not true, statement of while loop Statement of the do-while loop must be executed at
may not be executed at all. least once.
3 while loop terminates when the condition As long as the condition is true, the compiler keeps
becomes false. executing the loop in the do-while loop.
4 In a while loop, the test condition variable In a do-while loop, the variable of test condition
must be initialized first to check the test Initialized in the loop also.
condition in the loop.
5 In a while loop, at the end of the condition,
In this, at the end of the condition, there is a
there is no semicolon. semicolon.
Syntax: Syntax:
while (condition) while (condition);
6 While loop is not used for creating menu- It is mostly used for creating menu-driven programs
driven programs. because at least one time; the loop is executed
whether the condition is true or false.
7 In a while loop, the number of executions In a do-while loop, irrespective of the condition
depends on the condition defined in the mentioned, a minimum of 1 execution occurs.
while block.
8 Syntax of while loop: Syntax of do-while loop:
while (condition) do
{ {
Block of statements; statements;
} }while(condition);
Statement X; Statement X;
Internal test 1 – U23CS101 C Programming
Program of while loop: Program of while loop:
#include<stdio.h> #include<stdio.h>
void main() void main()
{ {
int i=1; int i=1;
while(i<=3) do
{ {
printf("hello"); printf("hello");
i = i + 1; i = i + 1;
} }while(i<=3);
} }

OUTPUT: OUTPUT:
hellohellohello hellohellohello

14. Write a C program Program to take 5 values from the user and store
them in an array and Program to find the average of n numbers using
arrays
#include<stdio.h>
void main()
{
int values[5];
printf("Enter 5 integers: ");
// taking input and storing it in an array OUTPUT:
for(int i = 0; i < 5; ++i) Enter 5 integers: 11
{ 22
scanf("%d", &values[i]); 33
} 44
printf("Displaying integers: "); 55
// printing elements of an array Displaying integers: 11
for(int i = 0; i < 5; ++i) 22
{ 33
printf("%d\n", values[i]); 44
} 55
}

15. C Program to find the average of n numbers using arrays

#include <stdio.h>
void main()
{
int marks[10], i, n, sum = 0;
int average;
printf("Enter number of elements: ");
scanf("%d", &n);
Internal test 1 – U23CS101 C Programming
for(i=0; i < n; ++i)
{
printf("Enter number %d: ",i+1);
scanf("%d", &marks[i]);
sum += marks[i]; /*sum=sum+mark[i]*/
}
average = sum / n;
printf("Average = %d", average);
}
OUTPUT:
Enter number of elements: 3
Enter number 1: 66
Enter number 2: 88
Enter number 3: 99
Average = 84

16.Draw a flowchart for a program that finds the largest of three numbers. Explain the
steps involved.
• Flow chart is defined as graphical representation of the logic for problem solving.
• The purpose of flowchart is making the logic of the program clear in a visual representation.
Internal test 1 – U23CS101 C Programming

17.Explain the process of declaring, initializing, and accessing arrays in C, with


examples?
In C, arrays are used to store multiple values of the same type. Below is a guide on declaring, initializing,
and accessing arrays, along with examples.

1. Declaring an Array
To declare an array, specify the type of its elements, followed by the array name and the size in square
brackets.

Syntax:
type arrayName[size];

Example:
int numbers[5]; // Declares an array of 5 integers

2. Initializing an Array
An array can be initialized at the time of declaration. If not initialized, the values will be undefined (unless
it's static or global, which will be initialized to zero).

Syntax:
type arrayName[size] = {value1, value2, ..., valueN};

Example:
int numbers[5] = {1, 2, 3, 4, 5}; // Initializes the array with values
/*Partial initialization is possible; remaining elements will be set to zero:*/
int numbers[5] = {1, 2}; // numbers[2], numbers[3], and numbers[4] will be 0

3. Accessing Array Elements


Array elements are accessed using their index, which starts at 0. The array name is followed by the index
in square brackets.

Syntax:
arrayName[index]

Example:
#include <stdio.h>
void main()
{
int numbers[5] = {1, 2, 3, 4, 5};

// Accessing elements
printf("First element: %d\n", numbers[0]); // Output: 1
printf("Second element: %d\n", numbers[1]); // Output: 2
}

Output:
First element: 1
Second element: 2
Internal test 1 – U23CS101 C Programming

18. Write a C program to demonstrate the use of the break and continue statements in
loops. Explain how control is transferred in each case.
Break Statement
Usage: The break statement is used to exit a loop immediately. When the break statement is encountered,
control is transferred out of the loop entirely.

Example:
#include <stdio.h>
void main()
{
printf("Using break statement:\n");
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
printf("\nloop break", i);
break; // Exit the loop when i is 5
}
printf("%d ", i); // This will print numbers from 1 to 4
}

OUTPUT:
Using break statement:
1234
loop break

In the Example:
• The loop iterates from 1 to 10. When i equals 5, the break statement is executed.
• This causes the loop to terminate, and control transfers to the first statement after the loop.
• As a result, only numbers 1 through 4 are printed, and the message "Loop break" is displayed.

Continue:
Usage: The continue statement skips the rest of the code inside the current iteration of the loop and proceeds
to the next iteration.

Example:
#include <stdio.h>
void main()
{
printf("\n\nUsing continue statement:\n");
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
printf("\nskipped loop no: %d\n", i);
continue; // Skip the current iteration for even numbers
}
printf("%d ", i); // This will print odd numbers from 1 to 10
}
Internal test 1 – U23CS101 C Programming
}
OUTPUT:
Using continue statement:
1234
skipped loop no: 5
6 7 8 9 10
In the Example:
• The loop iterates from 1 to 10. When i is five (i.e., i == 5), the continue statement is executed.
• This causes the loop to skip the printf statement that follows the continue, moving directly to the next
iteration of the loop.
• As a result, only numbers (1, 2, 3, 4) are printed, when i=5 loop is skipped and 6, 7, 8, 9, 10 numbers are
displayed

19. Elucidate the concept of algorithm with an example program.


• An algorithm is a step-by-step procedure or formula for solving a problem.
• Algorithms provide a clear set of instructions that outline how to perform a specific task or calculation.
• They can vary in complexity from simple operations to more intricate processes.

Characteristics of a Good Algorithm

1. Clear and Unambiguous: Each step should be precisely defined.


2. Well-Defined Inputs and Outputs: The algorithm should specify what inputs it takes and what outputs it
produces.
3. Finiteness: The algorithm must always terminate after a finite number of steps.
4. Effectiveness: The steps must be feasible and executable in practice.
5. Generality: It should solve the problem for all instances, not just a particular case.

Advantages of Algorithms:

 It is easy to understand.
 Algorithm is a step-wise representation of a solution to a given problem.
Internal test 1 – U23CS101 C Programming
 In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to
convert it into an actual program.

Disadvantages of Algorithms:

 Writing an algorithm takes a long time so it is time-consuming.


 Branching and Looping statements are difficult to show in Algorithms.

Example

To find the average of 3 numbers


1. Start
2. Read the value of a,b,c
3. Read 3 numbers
4. Calculate average = a+b+c
5. Print the average.
6. Stop

20.Explain the purpose of preprocessor directives in C. How do #include and #define


work? Provide examples of their usage.
• Preprocessor directives in C are commands that are processed by the preprocessor before the actual
compilation of the code begins.
• These directives help manage the inclusion of files, define constants, and control conditional
compilation.
• They start with the # symbol and do not require a semicolon at the end.

Purpose of Preprocessor Directives


• File Inclusion: Allows the inclusion of header files that contain declarations of functions, macros, and
types.
• Macro Definition: Defines constants and macros that can simplify code and make it more readable.
• Conditional Compilation: Enables or disables parts of the code based on certain conditions, which is
useful for platform-specific code or debugging.
Key Directives
1. #include
The #include directive is used to include the contents of a file in the program. This is commonly used to
include standard libraries or user-defined header files.

Syntax:
#include <header.h> // For standard library headers
#include "header.h" // For user-defined headers

Example:
#include <stdio.h> // Includes the standard input-output library

int main()
{
printf("Hello, World!\n");
return 0;
}
Internal test 1 – U23CS101 C Programming
In this example, #include <stdio.h> allows the program to use the printf function, which is defined in the
standard I/O library.

2. #define
The #define directive is used to create macros, which are essentially constants or expressions that can be
reused throughout the code. This helps improve code maintainability.

Syntax:
#define NAME value
Example:
#include <stdio.h>
#include<stdio.h>
#define PI 3.14 // Define a constant for PI
#define SQUARE(x) (x * x) // Define a macro for squaring a number

void main()
{
printf("Value of PI: %f \n", PI); // Uses the defined constant
printf("Square of 5: %d \n", SQUARE(5)); // Uses the defined macro
}

OUTPUT:
Value of PI: 3.140000
Square of 5: 25

In this example:

#define PI 3.14159 creates a constant value for PI.


#define SQUARE(x) (x * x) creates a macro that calculates the square of a number. Whenever SQUARE(5)
is used, it gets replaced by (5 * 5) during preprocessing.

21.Discuss the various stages of the compilation process in C: preprocessing,


compilation, assembly, and linking. What happens in each stage?
Compilation:
The compilation is a process of converting the source code into object code. It is done with the help of
the compiler. The compiler checks the source code for the syntactical or structural errors, and if the source
code is error-free, then it generates the object code.

The c compilation process converts the source code taken as input into the object code or machine code.
The compilation process can be divided into four steps.
1.Pre-processing
2.Compiling
3.Assembling
Internal test 1 – U23CS101 C Programming
4.Linking

Preprocessor
• The preprocessor takes the source code as an input, and it removes all the comments from the source
code. The preprocessor takes the preprocessor directive and interprets it.
• For example, if <stdio.h>, the directive is available in the program, then the preprocessor interprets
the directive and replace this directive with the content of the 'stdio.h' file.
• The source code is the code which is written in a text editor and the source code file is given an
extension ".c". This source code is first passed to the preprocessor, and then the preprocessor expands
this code. After expanding the code, the expanded code is passed to the compiler.
Compiler
• The code is expanded by the preprocessor is passed to the compiler.
• The compiler converts this code into assembly code or the C compiler converts the pre-processed code
into assembly code.
Assembler
• The assembly code is converted into object code by using an assembler. The name of the object file
generated by the assembler is the same as the source file.
• The name of the source file is 'hello.c', then the name of the object file would be 'hello.obj'.
Linker
• All the programs written in C use library functions. These library functions are pre-compiled, and the
object code of these library files is stored with '.lib' (or '.a') extension.
• The main working of the linker is to combine the object code of library files with the object code of
our program. It links the object code of these files to our program. So the job of the linker is to link
the object code of our program with the object code of the library files and other files.
• For example, if we are using printf() function in a program, then the linker adds its associated code in
an output file
Internal test 1 – U23CS101 C Programming
1 Mark
1 Which are the fundamental data types in C? CO1 L1
a) Char b) Int
c) Float d) All of the above
2 What is the correct variable to declare a variable in C? CO1 L1
a) int a; b) int 1a;
c) int $a; d) int @a b;
3 How do you insert COMMENTS in C code? CO1 L1
a) _ _This is comment b) # This is comment
c) //This is comment d) /This is comment
4 Which of the following is operator? CO1 L1
a) a b) c=a+b

c) + d) None of the above

5 What does the following statement do in C? int x = 10; CO1 L1


a) It assigns 10 to a previously declared b) It declares a variable x without assigning a
variable x. value.
c) It declares a variable x and assigns d) It declares x as a constant with the value 10.
the value 10 to it.
6 What does the break statement do in a loop? CO3 L1
a) Exits the current loop or switch b) Restarts the loop
statement

c) Pauses the loop d) None of the above

7 Which stage of the compilation process involves replacing macros and CO3 L1
including header files?

a) Linking b) Assembly

c) Preprocessing d) Compilation

8 What is the final stage of the compilation process in C? CO3 L1

a) Assembly b) Linking

c) Preprocessing d) Interpretation

9 Which of the following is the correct syntax to declare an integer array of 10 CO4 L1
elements in C?
a) int arr;
b) int arr[10];
Internal test 1 – U23CS101 C Programming
c) int arr(10);
d) int[10] arr;
10 What is the index of the first element in a C array? CO4 L1

a) 0

b) 1

c) -1

d) Depends on array type

11 Who is the father of Computer? CO1 L1

a) Steve Jobs b) Charles Babbage

c) Dennis Ritchie d) Rasmus Lerdorf

12 How many keywords are there in C language? CO1 L1

a) 32 b) 33

c) 64 d) 18

13 What is the correct variable to declare a variable in C? CO1 L1

a) int a; b) int 1a;

c) int $a; d) int @a b;

14 Which function is used to print something on the screen in C? CO1 L1

a) scanf() b) printf()

c) getch() d) print()

15 What is the purpose of the continue statement in C? CO1 L1

a) Stops the program b) Skips the remaining part of the loop body and
continues with the next iteration

c) Exits the loop d) Repeats the loop indefinitely

You might also like