0% found this document useful (0 votes)
21 views5 pages

cs3251 UNIT I QBANK

programming in C

Uploaded by

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

cs3251 UNIT I QBANK

programming in C

Uploaded by

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

QUESTION BANK

UNIT I

PART A

1. Explain the global and local declaration in C program. U

Local variables are declared within a specific block of code, such as a function or method, and
have limited scope and lifetime, existing only within that block.

Global variables are declared outside of any function and can be accessed from any part of the
program, persisting throughout its execution.

Example,

#include <stdio.h>
int x = 5; // global variable
int main() {
int y = 10; // local variable
return 0;
}
2. How many bytes are occupied by the int, char, float, long int and double?
U

Data Type Size (bytes) Format Specifier

int 4 %d

long int 4 %ld

float 4 %f

double 8 %lf

char 1 %c

3. Discuss about preprocessor directives. U

#include , #define

#undef , #ifdef

#ifndef, #if

#else, #elif

#endif, #error , #pragma

4. Define Ternary operator with example. U

The ternary operator is a conditional operator that takes three operands: a condition, a
value to be returned if the condition is true, and a value to be returned if the condition is false.
It evaluates the condition and returns one of the two specified values based on whether the
condition is true or false.

Syntax: variable = Expression1 ? Expression2 : Expression3;


#include <stdio.h>
int main() {
// Define two variables
int a = 5;
int b = 9;
// Use ternary operator to find the maximum
Printf(“the maximum value is :%d,” (a > b) ? a : b);
Return 0;
}
5. Write a C program to swap the values of two variables (without temporary
variable) AP

#include <stdio.h>
int main()
{
int x = 10, y = 5;
x = x + y; // x now becomes 15
y = x - y; // y becomes 10
x = x - y; // x becomes 5
printf("After Swapping: x = %d, y = %d", x, y);
return 0;
}
6. Find the output for following C program.
AP
int main(){
int x = 20, y = 35;
x = y++ + x++;
y = ++y + ++x;
printf("%d %d n", x, y); }

output : 57 94

{x=y++ + x++;} :
x=y+x;//35+20
x++; //56
y++; //36
y=++y + ++x; :
++y;//37
++x;//57
y=y+x;//37+57

So x=57 y=94

7. Write a for loop in C to print from 10 to 1. AP


#include <stdio.h>
int main(void) {
int i;
for(i=10;i>=1;i--)
{
printf("%d ",i);
}
return 0;}
8. List the features of array. U
1) An array holds elements that have the same data type.
2) Array elements are stored in continuous memory locations.
3)It is a derived data type in C that can store elements of different data types such as int,
char, struct, etc.
4)randomly access any element in the array without touching any other element using its
index.
9. Given an array int a[10] = {101,102,103,104,105,106,107,108,109,110}. Show
the memory representation and calculate the length.
AP

Memory representation : Array elements are always stored in consecutive memory location

A[10] = 0 1 2 3 4 5 6 7 8 9
101 102 103 104 105 106 107 108 109 110
arr[0] arr[2] arr[6] arr[9]
lower bound upper bound
length of an array : upper bound – lower bound +1
= 9-0+1
=10
10. Write an example for compile time initialization of character arrays [strings]. AP
#include <stdio.h>
#include <string.h>

int main(void)
{
char name[] = {'c', 'h', 'r', 'i', 's', 't', 'o', 'p', 'h', 'e', 'r'};
char name2[] = "christopher";

printf("name \" %s\" is an array and name2 \"%s\" is a string.\n"


, name, name2);
return 0;
}

11. Construct an infinite loop using while? AP

while (1){} Here 1 is a non zero, value so the condition is always true. So it is an infinite loop.

12. How to compare two strings using function?


AP
strcmp() is a built-in library function that is used for string comparison. This function takes
two strings (array of characters) as arguments, compares these two strings and then returns 0,1,
or -1 as the result. It is defined inside <string.h> header file
syntax : strcmp(first_str, second_str );
#include <stdio.h>
#include <string.h>
int main()
{
char* first_str = "string";
char* second_str = "string";
printf("Return value of strcmp(): %d", strcmp(first_str, second_str));
return 0;
}
Output: Return value of strcmp(): 0
13. Illustrate the difference between ++a and a++? AP

++a means do the increment before the operation (pre increment)a++ means do the increment

after the operation (post increment)

Example:

a=5;x=a++; /* assign x=5*/y=a; /*now y assigns y=6*/x=++a; /*assigns x=7*/

14. Mention the various string manipulation function in C. U

S.No Function Purpose

1. strcpy(s1,s2) Copies string s2 into string s1.

2. strcat(s1,s2) Concatenates string s2 onto the end of string s1.

3. strlen(s1) Returns the length of string s1

4. strcmp(s1,s2) Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater
than 0 if s1>s2.

5. strchr(s1,ch) Returns a pointer to the first occurrence

of character ch in string s1.


6. strst(s1,s2) Returns a pointer to the first occurrence of sting s2 in string s1.

15. Write an example for assigning values to arrays. U

The declaration is preceded by the word static. The initial values are enclosed in braces,

Example

#include <stdio.h>main(){int x;static int values[] = { 1,2,3,4,5,6,7,8,9 };static char word[] = {

‘H’,’e’,’l’,’l’,’o’ };for( x = 0; x < 9; ++x ) printf(“Values [%d] is %d\n”, x, values[x]);}

16. List the applications of array.


U

The arrays are used in C programming frequently because,

Mathematical vectors and matrices can be easily implemented using arrays.

One dimensional array can be used as database where the elements are the records.

Strings, stacks, queue, heaps and hash tables can be implemented using arrays.

Sorting of elements into ascending order and descending order can be performed using arrays.

17. Illustrate declaration and initialisation of Multi-dimensional array.


U

Multi-dimensioned arrays have two or more index values which specify the element in the
array.

multi[i][j];

Declaration:

int m1[10][10];static int m2[2][2] = { {0,1}, {2,3} };

18. Given an array int salary[5]={10000,8500,15000,7500,9000}. Calculate the


address of salary [4] if the base address=1000.
U

Here, the size of the data type int is 2.The value for w=2Address of salary[4] =1000+2(4-

0)=1000+8=1008

PART – B

1.Write a C program to print the following pattern.


AP
1
12
123
1234
12345

2.Write a C program to calculate the area of a circle, square, rectangle.


AP
3. Write a C program to determine whether a given number is a prime or a composite
number. AP
4. To find the sum of even numbers.
AP
5. Write a C program to perform matrix addition using 2D array
AP
6. To get values for single dimensional array and print the same
AP
7. Write a C program to check whether a string is palindrome
AP
8. To perform string concatenation, string copy and find length of each string
AP
9. write a c program to perform matrix multiplication
AP
10. Write a C program to calculate number of vowels and constants
AP
11. Write a C program to calculate the average of first n numbers
AP
12. Write a C program to check whether a given year is leap or not.
AP

You might also like