C Language Important Question Answer for BCA 2nd Semester

C Language Important Question for BCA 2nd Semester

C Language Important Question Answer for BCA 2nd Semester :- In this post we upload the most important questions of BCA 2nd semester c language with answer. the given question very very important. so I unloaded this all important question with answer. student can easily read and learn this all question and get highest marks in examination.

Content in The Article

C Language Important Question Answer for BCA 2nd Semester


(Long Questions)

1) what is an array? Explain 2D array with memory representation.

Ans. Array:

  • In C Programming, an array is a data structure that stores a fixed-size sequence of elements of the same type. It provides a way to efficiently organize and access a collection of values. Each element in an array is identified by an index, which represents its position within the array.
  • an array is a data structure that allows you to store a collection of elements of the same type. It provides a way to organize and access data efficiently. Each element in an array is identified by an index, which represents its position in the collection..
  • An array is a data type that stored data in sequential order. the array stored similar types of data.

Two-Dimensional Array [2D array]

<pre>

The 2-Dimensional Array has used a matrix from (row and Coolum).

Syntax of the 2D array: – This is the syntax of the 2-dimensional array.

<DataTypes>ArrayName[Row_size][Coloum_size];

Declaration & Initialization of 2D Array:-

To declare and initialize a 2D array in C, you can follow these steps:

Declare the 2D array by specifying the data type, array name, number of rows, and number of columns. For example, to declare a 2D array of integers with 3 rows and 4 columns:

int myArray[3][4];

Initialize the elements of the array with desired values. You can either initialize the array during declaration or assign values to individual elements later.

Initialize the array during declaration:

  • Enclose the initialization values within curly braces {}.
  • Separate the elements with commas ,.
  • Ensure that the number of elements matches the size of the array.

For example, to initialize a 2D array with 3 rows and 4 columns:

int myArray[3][4] = {
    {1, 2, 3, 4},
    {5, 6, 7, 8},
    {9, 10, 11, 12}
};

Assign values to individual elements after declaration:

  • Use the array indices to access specific elements.
  • Assign values using the assignment operator =.

For example:

int myArray[3][4];  // Declaration

myArray[0][0] = 1;
myArray[0][1] = 2;
myArray[0][2] = 3;
myArray[0][3] = 4;

myArray[1][0] = 5;
myArray[1][1] = 6;
myArray[1][2] = 7;
myArray[1][3] = 8;

myArray[2][0] = 9;
myArray[2][1] = 10;
myArray[2][2] = 11;
myArray[2][3] = 12;

The total size of 2d array

Size = Row_size X Coloum_size.

Ex –

int arr[2][5] = { { 2,6,7,8,9,},{3,10,25,23,8}};

memory representation of 2d array

Memory Representation of 2D Array

2) Define pointer? Explain all functions of DMA (Dynamic Memory Allocation).

Ans. Pointer: –

Define Pointer?

  • Pointer is a variable that contains the address of another variable. It means, it is a variable that points to any other variable.
  • Although this is itself a variable, this contains the address or memory address of any other variable.
  • It can be of type int, char, array, function, or even any other pointer.
  • Its size depends on the architecture.
  • Pointers in C Language can be declared using *(asterisk symbol).

Declaration of Pointer

The Declare of the pointer with using Asteric (*) Sign. This asteric sign also known as Indirection Operator.

Syntax

int *ptr;

Initialization of Pointer

initialization means assign the value at the declaration time.

int * ptr = &age;

The function of DMA (Dynamic Memory Allocation)

Dynamic Memory Allocation: – DMA stands for Dynamic Memory Allocation. it means we can allocate the memory after the execution of the program. and the compiler decided how much memory to allocate at the compiler time not run time.

Dynamic Memory Allocation Function

There are ways in which we can allocate memories dynamically in a heap. We use these four standard library functions often;

  1. malloc()
  2. calloc()
  3. free()
  4. realloc()
1. malloc():

malloc stands for memory allocation. This inbuilt function requests memory from the heap and returns a pointer to the memory. The pointer is of the void type and we can typecast it to any other data type of our choice.

All the values at the allocation time are initialized to garbage values. The function expects the memory space along with the size we want in bytes at the time it is used.

Syntax:

ptr = (ptr - type *)malloc(size_in_bytes)

Example:

int *ptr;
ptr = (int *)malloc(5 *sizeof(int));
2. calloc():

calloc stands for contiguous memory allocation. Similar to malloc, this function also requests memory from the heap and returns a pointer to the memory. Differences lie in the way we have to call it.

First, we have to send as parameters the number of blocks needed along with their size in bytes. Second, in calloc(), the values at the allocation time are initialized to 0 instead of garbage value unlike what happens in malloc().

Syntax:

ptr = (ptr - type *)calloc(n, size_in_bytes)

Example:

int *ptr;
ptr = (int *)calloc(5, sizeof(int));
3. realloc():

realloc stands for reallocation of memory. It is used in cases where the dynamic memory allocated previously is insufficient and there is a need of increasing the already allocated memory to store more data.

We also pass the previously declared memory address, and the new size of the memory in bytes while calling the function.

Syntax:

ptr = (ptr - type *)realloc(ptr, new_size_in_bytes)
Example:
ptr = (int *)realloc(ptr,10* sizeof(int));
4. free():

While discussing the disadvantages of dynamic memory allocation, it was mentioned that there is no automatic deletion of dynamically allocated memory when the pointer gets overwritten. So, to manually do it, we use the free() function to free up the allocated memory space. Therefore, free() is used to free up the space occupied by the allocated memory.

We just have to pass the pointer as a parameter inside the function and the address being pointed gets freed.

Syntax:

free(ptr);

3) What is the string? Explain all library functions of string.

Ans. String: –

A string is an array of characters. Data of the same type are stored in an array, for example, integers can be stored in an integer array, and similarly, a group of characters can be stored in a character array. This character array is also known as a string. A string is a one-dimensional array of characters that is terminated by a null (‘\0’).

 Library Function of String

We can use C’s string handling library functions to handle strings. The string.h library is used to perform string operations. It provides several functions for manipulating strings.

Following are some commonly used string handling functions:

1. strcat():

This function is used to concatenate the source string to the end of the target string. This function expects two parameters, first, the base address of the source string and then the base address of the target string. For example, “Hello” and “World” on concatenation would result in a string “HelloWorld”.

Here is how we can use the strcat( ) function:

#include <stdio.h>
#include <string.h>
int main()
{
    char s[] = "Hello";
    char t[] = "World";
    strcat(s, t);
    printf("String = %s", s);
}

Output:

String  = HelloWorld

2. strlen():

This function is used to count the number of characters present in a string.

Here is how we can use the strlen( ) function:

#include <stdio.h>
#include <string.h>
int main()
{
    char s[] = "Hello";
    int len = strlen(s);
    printf("Length = %d", len);
}

Output:

Length  = 5
3. strcpy():

This function is used to copy the contents of one string into the other. This function expects two parameters, first, the base address of the source string and then the base address of the target string.

Here is how we can use the strcpy( ) function:

#include <stdio.h>
#include <string.h>
int main()
{
    char s[] = "sdak24";
    char t[50];
    strcpy(t, s);
    printf("Source string = %s\n", s);
    printf("Target string = %s", t);
}

Output:

Source string  = sdak24
Target string  = sdak24
4. strcmp():

The strcmp() function is used to compare two strings to find out whether they are the same or different. It takes two strings as two of its parameter. It will compare two strings, character by character until there is a mismatch or the iterator reaches the end of one of the strings.

If both of the strings are identical, strcmp( ) returns a value of zero. If they are not identical, it will return a value less than zero, considering the ASCII value of the mismatched character in the first string is less than the mismatched character in the second string. Else, it will return a value greater than 0.

Here is how we can use the strcmp( ) function:

#include <stdio.h>
#include <string.h>
int main()
{
    char s[] = "Hello";
    char t[] = "World";
    int cmp = strcmp(s, t);
    printf("Comparison result = %d", cmp);
}

Output:

Comparison result = -1

5. strrev():

This function is used to return the reverse of the string.

Here is how we can use the strrev( ) function:

#include <stdio.h>
#include <string.h>
int main()
{
    char s[] = "Hello";
    printf("Reversed string = %s", strrev(s));
}

Output:

Reversed string  = olleH

4) What are structure and union? How to deferent unions by structure. What is the difference between them?

Ans.

Structure: –

Structures are usually used when we wish to store data of different data types together. For example, if we want to store information about a book, there could be a number of parameters defining a book.

Books have a title, an author name, the number of pages, and a price. All of the book attributes belong to different data types. The titles and author names must be strings, but the prices and number of pages must be numerical.

 One way to store the data is to construct individual arrays, and another method is to use a structure variable. It is to keep in mind that structure elements are always stored in contiguous memory locations.

Union: –

Just like Structures, the union is a user-defined data type.  All the members in unions share the same memory location. The union is a data type that allows different data belonging to different data types to be stored in the same memory locations. One of the advantages of using a union is that it provides an efficient way of reusing the memory location, as only one of its members can be accessed at a time. A union is used in the same way we declare and use a structure. The difference lies just in the way memory is allocated to their members.

How to different unions by structure

here are some differences between union and structure are given below:

The difference between Union and Structure

Basic for comparison Structure Union
Keyword The keyword “struct” is use to define a structure. The keyword “union” is used to define a union.
Space Consuming Consume more space than union. Consume less space than structure.
Memory Each member within a structure is assigned unique storage area of location. Memory allocated is shared by individual members of union.
Value Altering Altering the value of a member will not affect other members of the structure. Altering the value of any of the member will alter other member values.
Accessing members Individual member can be accessed at a time. Only one first member of a union can be initialized.
Initializations Several members of a structure can initialize at once. Only the first member of a union can be initialized.
Syntax
struct tag-name
{
data-type member-1;
data-type member-2;
- - - - - - - - - - - -
Data-type member-n;
 }
union tag-name
{
data-type member-1;
data-type member-2;
- - - - - - - - - - - -
Data-type member-n;
}

 

5) Define a macro. How to substitute the macro directives in our program?

Ans. Maco: – In c, the macro is used to define any constant value or any variable with its value in the entire program that will be replaced by the macro name. Where macro is contains the set of code that will be called when the macro name used in the program.

Substitute the macro directive in our program

The given example substitutes the macro in our program.

Ex –

#define A (2+3) // it macro
#define B (4+5) // it macro
void main()
{
 int C;
 C = A*B;
printf(“C= %d”,C);
}

Output:

C= 45

6) Explain Conditional Compilation directives. How to use it.

Ans. Conditional Compilation: – Conditional Compilation directives help to compile a specific portion of the program or skip the compilation of some specific part of the program based on some conditions.

Conditional Compilation Directives are –

Directive Name Directive Description
1) #ifdef Return true if macro is defined.
2) #ifndef Return true if macro is not defined.
3) #if Test if a compile time condition is true.
4) #else The alternative for #if
5) #elif #else and #if one statement
6) #endif ends preprocessor conditional
7) #undef Remove the definition of the specific macro

7) What is the file? Explain the modes of files.

Ans. File:- A file can be defined as a collection of related records that give a complete set of information about a certain item or entity. A file can be stored manually in a file cabinet or electronically in computer storage devices.

Modes of Files

We can use one of the following modes with the helpf of  the fopen() function.

Mode Description
“r” Open for reading. If the file does not exist, fopen() return null.
“rb” Open for writing. If the file does not exist, fopen() return null.
“w” Open for writing. If the file exists, the content will be over write.
“wb” Open for writing in binary. If the file exists, the content will be over write.
“a” Open for appended. If the file does not exist, it will be return created.

(Short Questions)

1) Difference between string and character array.

Ans.

String Character Array
 1). String is a collection of elements. 2). The character array is a collection of variables stored in contiguous memory.
2). The String always ended with Null Character ‘\0’. 2). The Character array ended with the last variable of the array.
3). Slow access compare to a character array. 3). Fast access compare to string.
4). The string used to static memory. 4). The character array is used for static memory.

2) What is making bit field.

Ans.  The masking bit field is the method of saving the waste memory. it saves memory by using set the bit.

Ex –

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
struct {
    uint32_t year:23;
    uint32_t day:5;
    uint32_t month:4;
} typedef Bitfield;
int main() {
    Bitfield date = {2020, 13,12 };
    printf("sizeof Bitfield: %lu bytes\n", sizeof(date));
    printf("date: %d/%d/%d \n", date.day, date.month, date.year);
    return EXIT_SUCCESS;
}

Output

sizeof Bitfield: 4 bytes
date: 13/12/2020 

3) What is the pointer? Explain.

  • Null Pointer
  • Void Pointer
  • Wild Pointer
  • Dangling Pointer

Ans. Pointer: – Pointer is the special variable which stored another variable address. There are some types of pointer.

Null Pointer: – Null pointer by assigning the null value at the time of pointer declaration. This method is useful when you do not assign any address to the pointer. A null pointer always contains value 0.

Void Pointer: – The void pointer is a generic pointer that isn’t associated with any data type. A void pointer can be type casted to any type, so it is instrumental in assigning the different types of variables to the void pointer.

Wild Pointer: – If a pointer isn’t initialized to anything, it’s called a wild pointer. Dereferencing a wild pointer has undefined behavior that may crash the program or give a garbage value.

Dangling Pointer: – A dangling pointer is a pointer that refers to a memory location that has been released or deleted.

4) Write the program to concatenate two string without using string library function?

#include<stdio.h>
#include<conio.h>
void main()
{
    char str1[10];
    char str2[10];
    char str[20];
    int i,j,k,l;

    printf("Enter first string: \n");
    scanf("%s", str1);

    printf("Enter second string: \n");
    scanf("%s", str2);

    for(i=0;i<10;i++){
    
        if(str1[i]=='\0')
            break;
    
    }
    for(j=0;j<10;j++){
        if(str1[j]=='\0')
            break;
    }

    for(k =0; k<i-1; k++){
        str[k] = str1[k];
    }
    
    int n = 0;
    for(l=k;l < i+j-2;l++){
        str[l] = str2[n++];
    }
    printf("The string is: %s", str);
    getch();
}

Output:

Enter first string: 
sdak
Enter second string: 
24
The string is: sda24

 5) Deference between call by value and call by reference

Call by value Call by Reference
Call by value passed the value. Call by reference passed to address.
Value copied actual parameter to formal parameter. Have the both parameter in a same memory address.
Change the formal parameter value the value cannot be reflect into actual parameter. Because it copied value. Change the value the can reflect actual parameter because both value having in a same memory address.

(Very Short)

1) What is bitwise operator? Explain left shift and right shift.

Ans. Bitwise Operator: – bitwise operator are used for manipulating the data at bit level, also called as (bit level programming) bitwise operator. Bit level programing consist at 0 and 1.

Operator Description
& Bitwise And
| Bitwise OR
^ Bitwise Exclusive OR [XOR]
<< Bitwise Left Shift
>> Bitwise Right Shift

 

Left shit and Right Shift

Left shift: – the left shift operator is multiply by 2.

Ex-

0 0 0 0 1 1 1 0

14

14 << 1

0 0 0 1 1 1 0 0

 

Output = 28

Right shift: – the left shift operator is divided by 2.

Ex-

0 0 0 0 1 1 1 0

14

14 >> 1

0 0 0 0 0 1 1 1

Output = 28

2) Why need of masking bit field.

Ans. Bit masking: Bit masks are used to access specific bits in a byte of data. This is often useful as a method of iteration, for example when sending a byte of data serially out a single pin. In this example, the pin needs to change it’s state from high to low for each bit in the byte to be transmitted

3) What is array? Explain 1D array.

Ans. Array: – An array is a collection of similar data types which stored in contiguous memory allocation. The array always started from 0 index and the array last element is n-1. The array started address called lower bounded. And last element called upper bound.

1 Dimensional Array: –

Declaration & Initialization of 1D array

Syntax – <Datatypes>arrayname[lenth];

int age[8] = {10,20,30,40,50,60,70,80}; 

1d array memory represent

4) Explain the difference between Binary files and text files.

Ans. There are some differences between Binary files and text files.

Text file Binary File
1 Its bit represent character. Its bits represent a custom data.
2 Less prone to get corrupt as change reflect as soon as made and can be undone. Can easily get corrupted, corrupted on even single bit change.
3 Store only plaint text in a file. Can store different types of data (audio, text, image) in a single file.
4 Widely used file format and can be opened in any text editor. Developed for an application and can be opened in that application only.
5 Mostly .text, .rtf, are used as extension to text files. Can have any application defined extension.

5) What is Nested Structure?

Ans. Nested Structure: – A structure inside another structure is called a nested structure.

Ex –

// C program to implement

// the above approach

#include <stdio.h>

// Declaration of the outer

// structure

struct Organisation

{

char organisation_name[20];

char org_number[20];

// Declaration of the employee

// structure

struct Employee

{

int employee_id;

char name[20];

int salary;

// This line will cause error because

// datatype struct Employee is present ,

// but Structure variable is missing.

};

};

// Driver code

int main()

{

// Structure variable of organisation

struct Organisation org;

printf(“%ld”, sizeof(org));

}


App – Dream Topper (Play Store)

Www.DreamTopper.in

Www.YouTube.com/Dreamtopper

 

  • Thanks

 


(Long Questions)

  • What is an array? Explain 2D array with memory representation.
  • Define pointer? Explain all function of DMA (Dynamic Memory Allocation).
  • What is string? Explain all library function of string.
  • What is structure and union? How to deferent union by structure. What is deference between them?
  • Define macro. How to substitute the macro directives in our program?
  • Explain Conditional Compilation directives. How to use it.
  • What is the file? Explain the modes of files.

(Short Questions)

  1. Difference between string and character array.
  2. What is making bit field.
  3. What is the pointer? Explain.
  4. Null Pointer
  5. Void Pointer
  6. Wild Pointer
  7. Dangling Pointer
  8. Write the program to concatenate two string without using string library function?
  9. Deference between call by value and call by reference

(Very Short)

  1. What is bitwise operator? Explain left shift and right shift.
  2. Why need of masking bit field.
  3. What is array? Explain 1D array.
  4. Explain difference between Binary File and text files.
  5. What is Nested Structure?

 

 

1 thought on “C Language Important Question Answer for BCA 2nd Semester”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top