0% found this document useful (0 votes)
3 views12 pages

Pci Question Bank Ct2

The document contains a series of programming questions and answers related to arrays, strings, pointers, structures, and functions in C. It includes definitions, syntax, example programs, and explanations of various concepts such as reversing a number, performing arithmetic operations on pointers, and comparing strings. Additionally, it provides examples of function categories and basic operations like addition, subtraction, multiplication, and division using functions.

Uploaded by

Swayam Hulwan
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)
3 views12 pages

Pci Question Bank Ct2

The document contains a series of programming questions and answers related to arrays, strings, pointers, structures, and functions in C. It includes definitions, syntax, example programs, and explanations of various concepts such as reversing a number, performing arithmetic operations on pointers, and comparing strings. Additionally, it provides examples of function categories and basic operations like addition, subtraction, multiplication, and division using functions.

Uploaded by

Swayam Hulwan
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/ 12

2 marks Questions

1. Define array. List its types.


Ans: An array is a group of related items that store items with a common
name
Types:
• One dimensional
• Two dimensional
• Multi dimensional

2. State the syntax & use of strlen() and strcat() function.


Ans: Strlen():
This function counts the number of characters present in a string.
Syntax: var_name = strlen(array);
var_name = strlen(“string”);
Strcat():
This function concatenates the source string at the end of the
target string.
Syntax: strcat(target, source);

3. State the syntax to declare pointer variable with example.


Ans: Syntax:
Datatype *pointer name;
Eg. Int *ptr;
4. State the difference between array and string.
Ans:

5. Declare a structure student with elements roll_no and st_name.


Ans: struct student
{
int roll_no;
char st_name[20];
};
4 marks Questions

1. Write a program to reverse the number using functions


Ans: Program:
#include<stdio.h>
#include<conio.h>
int findReverse(int n)
{
int sum=0;
while (n!=0)
{
sum = sum*10 + n%10;
n /= 10;
}
return sum;
}

int main()
{
int number, reverse;

printf("Enter a positive integer: ");


scanf("%d", &number);

reverse = findReverse(number);

printf("The reverse of %d is: %d", number, reverse);

return 0;
}
Output:
Enter a positive integer: 325
The reverse of 325 is: 523
2. State 4 arithmetic operations to perform on pointer with example.
Ans: 1. Increment: ++pointername
Input:
#include <stdio.h>
#include <conio.h>
int main()
{
int a = 4, b = 5;
int *ptr1 = &a;
int *ptr2 = &b;
printf("Value of ptr1 is %d\n", ptr1);
printf("Value of ptr1 after increment is %d", ++ptr1);
return 0;
}
Output:
Value of ptr1 is 6487564
Value of ptr1 after increment is 6487568

2. Decrement: --pointername
Input:
#include <stdio.h>
#include <conio.h>
int main()
{
int a = 4, b = 5;
int *ptr1 = &a;
int *ptr2 = &b;
printf("Value of ptr1 is %d\n", ptr1);
printf("Value of ptr1 after decrement is %d", --ptr1);
return 0;
}
Output:
Value of ptr1 is 6487564
Value of ptr1 after decrement is 6487560
3. Addition: *ptr1 + *ptr2
Input:
#include <stdio.h>
#include <conio.h>
int main()
{
int a = 4, b = 5;
int *ptr1 = &a;
int *ptr2 = &b;
printf("Value of a + b is %d", *ptr1 + *ptr2);
return 0;
}
Output:
Value of a + b is 9

4. Substraction: *ptr1 - *ptr2


Input:
#include <stdio.h>
#include <conio.h>
int main()
{
int a = 4, b = 5;
int *ptr1 = &a;
int *ptr2 = &b;
printf("Value of a - b is %d", *ptr1 - *ptr2);
return 0;
}
Output:
Value of a - b is -1
3. Difference between character array and integer array.
Ans: Difference between character array and integer array.
1. Char array is an object capable of holding the value of type char
2. Char array takes less space to allocate memory.
3. String arrays are terminated with a null character.
4. Initialization can be done like char str[4] = {'a', 'b', 'c','\0'};

1. Integer array is an object capable of holding a value of type int.


2. Int array takes more space to allocate memory.
3. Integer arrays are not terminated with a null character.
4.Initialization can be done like int arr[4]={1,2,3,4};

4. With suitable example explain how 2 dimensional arrays are created.


Ans:

5. Explain any 2 string functions with example


Ans: Strlen():
This function counts the number of characters present in a string.
Syntax: var_name = strlen(array);
var_name = strcat(“string”);
Strcat():
This function concatenates the source string at the end of the
target string.
Syntax: strcat(target, source);
6. Write a program to accept 10 numbers in an array and arrange them
in ascending order.
Ans: Program:
#include <stdio.h>
#include <conio.h>
int main()
{
int element[10], i, j, temp;
printf("enter 10 integer numbers:");
for (i = 0; i < 10; i++)
{
scanf("%d", &element[i]);
}
for (i = 0; i < 10 - 1; i++)
{
for (j = i + 1; j < 10; j++)
{
if (element[i] > element[j])
{
temp = element[i]; // swapping element[i] with element[j]
element[i] = element[j];
element[j] = temp;
}
}
}
printf("Elements are now in ascending order:");
for (i = 0; i < 10; i++)
printf("%d\n", element[i]);
return 0;
}
Output:
enter 10 integer numbers:1 3 5 7 9 2 4 6 8 0
Elements are now in ascending order:
0
1
2
3
4
5
6
7
8
9

7. Explain the meaning of the following statements


int *a, b;
b = 20;
*a = b;
a = &b;
Ans: The meaning is as follows:
int *a, b; --- Declaring variable and pointer variable with datatype integer.
b = 20; --- Initializing value 20 to variable b.
*a = b; --- Initializing value of variable to pointer variable a.
a = &b; --- Initializing address of variable b to variable a.
8. List categories of functions and explain anyone with example.
Ans: Categories are as follows:
• Functions with arguments and with return value.
• Functions with arguments and without return value.
• Functions without arguments and without return value.
• Functions without arguments and with return value.
Example: Functions with arguments and with return value.
#include <stdio.h>
#include <conio.h>

int factorial(int);

int main()
{
int num, fact;
printf("Enter number: ");
scanf("%d", &num);

fact = factorial(num);
printf("Factorial of %d is %d", num, fact);

return 0;
}

int factorial(int x)
{
int fact;
if(x == 1)
{
return 1;
}
else{
fact = x * (x-1);
return fact;
}
}
Output:
Enter number: 6
Factorial of 6 is 30
9. Write a program to add, subtract, multiply and divide two integers
using functions.
Ans: Program:
#include <stdio.h>
#include <conio.h>

int addition(int a, int b);


int substraction(int a, int b);
int multiplication(int a, int b);
int division(int a, int b);

int main()
{
int num1, num2, sum, subtract, multiply, divide;
printf("Enter value of first number: ");
scanf("%d", &num1);
printf("Enter value of second number: ");
scanf("%d", &num2);
sum = addition(num1, num2);
printf("Addition of %d and %d is %d\n", num1, num2, sum);
subtract = substraction(num1, num2);
printf("Substraction of %d and %d is %d\n", num1, num2, subtract);
multiply = multiplication(num1, num2);
printf("Multiplication of %d and %d is %d\n", num1, num2, multiply);
divide = division(num1, num2);
printf("division of %d and %d is %d\n", num1, num2, divide);
return 0;
}

int addition(int a, int b)


{
int sum;
sum = a + b;
return sum;
}

int substraction(int a, int b)


{
int subtract;
subtract = a - b;
return subtract;
}

int multiplication(int a, int b)


{
int multiply;
multiply = a * b;
return multiply;
}

int division(int a, int b)


{
int divide;
divide = a / b;
return divide;
}

Output:
Enter value of first number: 40
Enter value of second number: 20
Addition of 40 and 20 is 60
Subtraction of 40 and 20 is 20
Multiplication of 40 and 20 is 800
division of 40 and 20 is 2

10. Write a program to read two strings and find whether they are equal
or not.
Ans: Program:
#include <stdio.h>
#include <conio.h>

int main()
{
int result;
char string1[] = "India";
char string2[] = "India";

result = strcmp(string1, string2);


if(result == 0)
{
printf("Both the strings are equal");
}
else
{
printf("Both the strings are not equal");
}

return 0;
}
Output:
Both the strings are equal

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy