ITP Short Answer Questions (AutoRecovered)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Short Answer Questions

Unit-1
1. List the different types of C tokens?
Ans: Tokens are the smallest units in the source code that are meaningful to the compiler. There are
six types of tokens in C.
 Keywords
 Identifiers
 Constants
 String Literals
 Operators
 Punctuation Symbols

2. Define Constants in C. Mention the types?


Ans: In C programming, constants are values that do not change during the execution of a program.
These values are assigned only once and retain the same value throughout the program's execution.
Constants in C are categorized into several types:
1. Integer Constants
2. Floating-point Constants
3. Character Constants
4. String Constants
5. Enumeration Constants
6. Symbolic Constants
3. Differentiate between variable and constant?
Ans:
 Variable: A variable is a storage location in the computer's memory where you can store and
manipulate data. The data stored in a variable can be changed during the program's execution.
 Constant: A constant, on the other hand, represents a value that remains unchanged throughout
the program. Once a constant is assigned a value, it cannot be modified during the program's
execution.

4. Is there any difference between pre increment and post increment? Explain with
examples?
Ans: Yes, pre-increment and post-increment are two different ways of incrementing a variable in
programming. The key difference is in the order of operations: pre-increment increments the variable
first, while post-increment uses the current value before incrementing.
Example:
1. Pre-increment 2. Post-increment
Int a=5; int c=5;
Int b=++a; int d=a++;
// Now a = 6, b = 6 // Now c =6 , d = 5

5. What is pseudo code? Explain?


Ans: Pseudocode is a high-level description of a computer program or algorithm that uses a mixture
of natural language and some programming language-like constructs. It is not meant to be executed by
a computer but is rather a way for humans to plan, understand, and communicate algorithms in a more
readable and informal manner.

Unit-II
1. What is break statement?
Ans: In programming, the break statement is a control flow statement that is used to terminate the
execution of a loop prematurely, causing the program to exit the loop even if the loop condition is
still true. The break statement is commonly associated with loops (such as for and while loops), but
it can also be used in switch statements.

2. What is Continue statement?


Ans: The continue statement is useful when you want to skip specific iterations of a loop based on a
certain condition without completely terminating the loop. It allows you to control the flow of the
loop and execute specific code only under certain conditions.

3. Differentiate between while and do-while loop?


Ans:
Aspect while Loop do-while Loop
Condition is evaluated
after the first execution of
Condition is the loop body. If the
Condition evaluated before condition is true, the loop
Evaluation loop execution. continues.
May not execute Guarantees at least one
the loop body if execution of the loop body
Execution the initial before checking the
Frequency condition is false. condition.
java while
Syntax (in (condition) { // java do { // loop body }
Java) loop body } while (condition);
java int i = 0;
while (i < 5) { // java int j = 0; do { // loop
Example loop body } body } while (j < 5);

4. Mention the various decisions making statement available in C?


Ans: In C programming, decision-making statements are used to control the flow of the program
based on certain conditions. The primary decision-making statements in C are:
1. If statement
2. If-else statement
3. Nested if-else statement
4. Switch statement
5. Write a program to check whether the person is eligible to vote?
Aim: C program for checking whether the person is eligible for vote or note
Source Code:
#include <stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18) {
printf("\nCongratulations! You are eligible to vote.\n");
} else {
printf("Sorry, you are not eligible to vote. You need to be at least 18 years old.\n");
}
return 0;
}
Output:
Enter your age : 20
Congratulations! You are eligible to vote.

Unit -3
1.What is string constant?
Ans: A string constant in programming refers to a sequence of characters enclosed within
double quotation marks. It is a way to represent textual data, such as words, sentences, or any
combination of characters.
Example:
const char *myString = "Hello, World!";
In this example:
"Hello, World!" is a string constant.

2.What is the use of strlen()?


Ans: In programming, strlen() is a function that is used to determine the length of a string.
Syntax:
size_t strlen(const char *str);
Parameters:
 str: A pointer to the null-terminated string whose length is to be calculated.
Return Value:
 The number of characters in the string pointed to by str, excluding the null terminator.

3.What is an array? Write the types of an array?


Ans: An array is a data structure in programming that allows you to store multiple values of
the same data type under a single name. Each value in an array is called an element, and
elements in an array are accessed using an index or a subscript.
Types of Arrays:
1. One-Dimensional Array
2. Two-Dimensional Array
3. Multi-Dimensional Array
4.Identify the way to assign an array to another array?
Ans: In many programming languages, assigning one array to another involves copying the
elements from one array to another.
Aim: C program for assign an array to another array
Source Code:
#include<stdio.h>
int main()
{
// a is Source array
int a[] = {1, 2, 3, 4, 5};
// b is Destination array
int b[5];
// Copy elements from source to destination
for (int i = 0; i < 5; i++)
{
b[i] = a[i];
}
// Display the contents of the destination array
printf("Destination Array: ");
for (int i = 0; i < 5; i++)
{
printf("%d ", b[i]);
}
return 0;
}

5.Write any two string handling functions and its meaning?


Ans:
1. strcpy() - The strcpy() function is used to copy the contents of one string to another. It stands
for "string copy."
Syntax:
char *strcpy(char *destination, const char *source);
2. strlen() - The strlen() function is used to find the length of a string, excluding the null
terminator. It stands for "string length."
Syntax:
size_t strlen(const char *str);
Unit-4
1.Differentiate between structure and union?
Feature Structure Union
All members share
Memory Each member has its the same memory
Allocation own memory space. space.
Size Size is the sum of Size is the size of
Calculation member sizes. the largest member.
Suitable when only
Suitable when multiple one property is
Memory properties are present active at a time to
Usage simultaneously. save memory.
Each member can be Only one member
Accessing accessed can be accessed at a
Members independently. time.
Each member can be Initializing one
initialized member overwrites
Initialization independently. the values of others.

2.Differentiate between pointer variable and normal variable?


Ans:
Pointer Variable: It Contains the memory address of another variable rather than the actual
value. Declared using a data type followed by an asterisk (*).
For example, int *ptr; //declares a pointer to an integer.
Normal Variable: It Directly holds the actual value it represents. It is declared with a data type
and a variable name.
For example, int num; //declares a variable of type integer.
3.Write the advantages of pointer?
Ans:
1) Dynamic Memory Allocation: Pointers allow for dynamic memory allocation, enabling
programs to allocate and deallocate memory during runtime, providing flexibility in
managing memory resources efficiently.
2) Efficient Data Structures: Pointers facilitate the implementation of complex data
structures like linked lists, trees, and graphs, enabling efficient memory usage and
manipulation.
3) Function Parameter Passing: Pointers enable the passing of large data structures to
functions without copying the entire data, improving performance and memory
efficiency.
4.What is the difference between arrays and pointers?
Ans:
Arrays: Contiguous block of memory is allocated to store elements of the array. Each element in
the array is accessed using an index, and the size of the array is fixed at compile-time.
Pointers: Pointers store memory addresses and can point to different locations in memory. They
provide a more dynamic approach to memory allocation, allowing for flexibility in size and
location.
5.What is a pointer?
Ans: A pointer is a variable in programming that stores the memory address of another
variable. Instead of directly holding a data value, a pointer holds the location (address) in
memory where a value is stored.
Syntax:
Datatype *pointer_name;

Unit-5
1. Discuss about the keyword Register with examples?
Ans:
In C, the register keyword is used as a hint to the compiler that a particular variable should be
stored in a register for faster access.
Example:
Aim: C program for register keyword usage
Source Code:
#include <stdio.h>
int main()
{
register int x; // Declare x as a register variable
int y;
// Assign values
x = 5;
y = 10;

// Use the register variable in a loop


for (register int i = 0; i < x; ++i)
{
y += i;
}
// Print the result
printf("Result: %d\n", y);
return 0;
}
Output:
Result: 20
2. What are the advantages of functions?
Ans:
Modularity: Functions allow you to break down a complex program into smaller, more manageable
modules.
Reusability: Once a function is defined, it can be reused in different parts of the program or even in
different programs altogether.
Abstraction: Functions provide a level of abstraction, allowing developers to focus on high-level
functionality without worrying about the details of the implementation.
3. What are the storage classes available in C?
Ans: C Storage Classes are used to describe the features of a variable/function. These
features basically include the scope, visibility, and lifetime which help us to trace the existence of
a particular variable during the runtime of a program. C language uses 4 storage classes, namely:
1. Auto
2. Extern
3. Register
4. Static
4. What are the elements of user-defined function?
Ans: A user-defined function has three important components:
1. Function declaration (or) Function Prototype
2. Function definition
3. Function call
Function Declaration: A function prototype is also known as a function declaration which
specifies the function’s name, function parameters, and return type. The function prototype
does not contain the body of the function.
Function Definition: Once the function has been called, the function definition contains the
actual statements that will be executed. All the statements of the function definition are
enclosed within { } braces.
Function Call: In order to transfer control to a user-defined function, we need to call it.
Functions are called using their names followed by round brackets. Their arguments are passed
inside the brackets.
5. What are actual parameters and formal parameters?
Ans:
Actual parameters: Actual parameters are the values that are passed to the function during a
function call. They are also known as arguments. Actual parameters can be of any data type such
as int, float, char, etc.
Syntax :
function_name(actual_parameter1, actual_parameter2, ...);

Formal parameters : They are used in the function definition to accept the values from the
caller of the function. Formal parameters can be of any data type such as int, float, char, etc.
Syntax :
return_type function_name(parameter1_type parameter1_name, parameter2_type
parameter2_name, ... )
{
// function body
}

You might also like