CSE Exam
Ahmad Hasan Sakib
April 2025
Contents
1 Pointer
2
2 Structure
3
3 Array
4
1
1
Pointer
Q.What is pointer?
The pointer in C language is a variable which stores the address of another variable. pointer can be
of any type.(int,char,array,function etx.)
The size of the pointer depends on the architecture.
declaration: datatype* ptr-name
Q.Why should the programmer have to initialize any pointer-type variable prior to use
it? [TT+SF]
Answer:
A pointer should always be given a proper value before using it. If it’s not initialized, it may point
to a random memory location, which can cause the program to crash or behave in unexpected ways. By
initializing a pointer with a valid address or setting it to NULL, we make sure it’s safe to use and avoid
problems in the program.
int * ptr ;
* ptr = 10;
// Uninitialized pointer
// Undefined behavior ( may crash )
int a = 10;
int * p = & a ;
// Initialized with a valid address
printf ( " % d " , * p ) ; // Safe access
Listing 1: Example of Pointer Initialization in C
Q.What does dereferencing a pointer mean ? Answer:
Dereferencing a pointer means getting the value stored in the memory address which the pointer
points to.
Q.What is a null pointer? Why it is significant? Answer:
A null pointer is a pointer that does not point to any memory location. It stores the base address of
the segment.The NULL pointer is a constant with a value of zero.
syntax: ¡data-type¿* ¡ptr-name¿=NULL;
Significance:A NULL pointer is important because it shows that the pointer is not pointing to any
memory. It helps the programmer know that the pointer is empty or not used yet. Before using a
pointer, we can check if it is NULL to avoid errors like crashes. This makes the program safer and easier
to understand. It also helps in finding mistakes during debugging.
Q.What are the benefits of pointers over the use of arrays? [TT+SF] Answer:
• Dynamic Memory: Pointers let you allocate memory during runtime, unlike arrays that have a
fixed size.
• Efficient Memory Use: With pointers, you can use exactly the amount of memory you need.
• Pointer Arithmetic: Pointers allow easy navigation through memory, making data manipulation
simpler.
• Passing Large Data: Pointers let you pass large structures or arrays to functions without copying
them.
• Linked Data Structures: Pointers are key for creating advanced data structures like linked lists,
which arrays can’t do.
2
2
Structure
Q.When should structure use?[TT+SF]
Answer: Uses of structure:
• Data base management
• Changing the size of the cursor
• Clearing the contents of the screen
• Placing the cursor at an appropriate position on screen
• Drawing any graphics shape on the screen
• Receiving a key from the keyboard
• Checking the memory size of the computer
• Finding out the list of equipment attached to the computer
• Formatting a floppy
• Hiding a file from the directory
• Displaying the directory of a disk
• Sending the output to printer
• Interacting with the mouse
Q. Define a structure for course to store information like, dept,code,title,credit?[TT+SF]
Answer:
struct course
{
char dept [10];
int code ;
char title [100];
float credit ;
};
Listing 2: Struct for course
Example usage:
int main () {
struct course c1 ={ ’ PHY ’ ,221 , ’ Mechanics ’ ,3.00}
printf ( " Department : ␣ % s \ n " , c1 . dept ) ;
printf ( " Code : ␣ % d \ n " , c1 . code ) ;
printf ( " Title : ␣ % s \ n " , c1 . title ) ;
printf ( " Credit : ␣ %.1 f \ n " , c1 . credit ) ;
}
3
3
Array
Q.Write the difference between following two declaration?
char dept[3]=”PHY”; , char dept[ ]=”PHY”;
Ans:
char dept[3]=”PHY” is not a valid c string because c strings are null-terminated(/0 at the end).
Here,”PHY” is a string literal containing three characters, but no null terminator (/0) is stored in the
array.
char dept[ ]=”PHY”, is valid c string.This declares an array of size 4 and initializes it with ”PHY”,
including the null terminator.
Q.In what ways does an array differ from an an ordinary variable?
Ans:
Q.Write a C program that will reverse the elements of the following array?
int arr[ ]={1,2,3,4,5}
Ans:
# include < stdio .h >
int main () {
int n [5]={1 ,2 ,3 ,4 ,5} , i ;
for ( i =4; i >=0; i - -) {
printf ( " % d \ t " ,n [ i ]) ;
}
return 0;
}
4
Q.Write a program that reads a 3x3 array of integers and than prints the row sum and
the col sum?
Ans:
# include < stdio .h >
int main () {
// Declare and initialize a 3 x3 matrix
int n [3][3]={{1 ,2 ,3} ,
{4 ,5 ,6} ,
{7 ,8 ,9}
};
int i ,j , sum =0; // Loop counters and sum variable
// Calculate and print sum of each row
printf ( " Row ␣ Sum : " ) ;
for ( i =0; i <3; i ++) {
for ( j =0; j <3; j ++) {
sum = sum + n [ i ][ j ]; // Add elements of the current row
}
printf ( " % d \ t " , sum ) ; // Print the sum of the current row
sum =0; // Reset sum for next row
}
// Calculate and print sum of each column
printf ( " \ ncol ␣ sum : ␣ " ) ;
for ( j =0; j <3; j ++) {
for ( i =0; i <3; i ++) {
sum = sum + n [ i ][ j ]; // Add elements of the current column
}
printf ( " % d \ t " , sum ) ; // Print the sum of the current column
sum =0; // Reset sum for next column
}
return 0; // End of program
}
5
Q.Write a program to add two square matrices?
Ans:
# include < stdio .h >
int main () {
int size ;
// Input the size of the square matrix
printf ( " Enter ␣ the ␣ size ␣ of ␣ the ␣ square ␣ matrices : ␣ " ) ;
scanf ( " % d " , & size ) ;
int A [ size ][ size ] , B [ size ][ size ] , Sum [ size ][ size ];
// Input elements of matrix A
printf ( " Enter ␣ elements ␣ of ␣ matrix ␣ A :\ n " ) ;
for ( int i = 0; i < size ; i ++) {
for ( int j = 0; j < size ; j ++) {
scanf ( " % d " , & A [ i ][ j ]) ;
}
}
// Input elements of matrix B
printf ( " Enter ␣ elements ␣ of ␣ matrix ␣ B :\ n " ) ;
for ( int i = 0; i < size ; i ++) {
for ( int j = 0; j < size ; j ++) {
scanf ( " % d " , & B [ i ][ j ]) ;
}
}
// Add the two matrices
for ( int i = 0; i < size ; i ++) {
for ( int j = 0; j < size ; j ++) {
Sum [ i ][ j ] = A [ i ][ j ] + B [ i ][ j ];
}
}
// Print the result
printf ( " Sum ␣ of ␣ the ␣ two ␣ matrices :\ n " ) ;
for ( int i = 0; i < size ; i ++) {
for ( int j = 0; j < size ; j ++) {
printf ( " % d \ t " , Sum [ i ][ j ]) ;
}
printf ( " \ n " ) ;
}
return 0;
}
6