Friday 26 April 2019

Top C Programming Interview Questions and Answers

Q #1) What are the key features in C programming language?
  • Portability – Platform independent language.
  • Modularity – Possibility to break down large programs into small modules.
  • Flexibility – The possibility to a programmer to control the language.
  • Speed – C comes with support for system programming and hence it is compiling and executes with high speed when comparing with other high-level languages.
  • Extensibility – Possibility to add new features by the programmer.
Q #2) What are the basic data types associated with C?
  • Int – Represent number (integer)
  • Float – Number with a fraction part.
  • Double – Double-precision floating point value
  • Char – Single character
  • Void – Special purpose type without any value.
Q #3) What is the description for syntax errors?
Ans) The mistakes when creating a program called syntax errors. Misspelled commands or incorrect case commands, an incorrect number of parameters when called a method /function, data type mismatches can identify as common examples for syntax errors.
Q #4) What is the process to create increment and decrement stamen in C?
Ans) There are two possible methods to perform this task.
1) Use increment (++) and decrement (-) operator.
Example When x=4, x++ returns 5 and x- returns 3.
2) Use conventional + or – sign.
When x=4, use x+1 to get 5 and x-1 to get 3.
Q #5) What are reserved words with a programming language?
Ans) The words that are part of the slandered C language library are called reserved words. Those reserved words have special meaning and it is not possible to use them for any activity other than its intended functionality.
Example void, return, int.
Q #6) What is the explanation for the dangling pointer in C?
Ans) When there is a pointer with pointing to a memory address of any variable, but after some time the variable was deleted from the memory location while keeping the pointer pointing to that location.
Q #7) Describe static function with its usage?
Ans) A function, which has a function definition prefixed with a static keyword is defined as a static function. The static function should call within the same source code.
Q #8) What is the difference between abs() and fabs() functions?
Ans) Both functions are to retrieve absolute value. abs() is for integer values and fabs() is for floating type numbers. Prototype for abs() is under the library file < stdlib.h > and fabs() is under < math.h >.
Q #9) Describe Wild Pointers in C?
Ans) Uninitialized pointers in the C code are known as Wild Pointers. These are a point to some arbitrary memory location and can cause bad program behavior or program crash.
Q #10) What is the difference between ++a and a++?
Ans) ‘++a”  is called prefixed increment and the increment will happen first on a variable. ‘a++' is called postfix increment and the increment happens after the value of a variable used for the operations.
Q #11) Describe the difference between = and == symbols in C programming?
Ans) ‘==' is the comparison operator which is use to compare the value or expression on the left-hand side with the value or expression on the right-hand side.
‘=' is the assignment operator which is use to assign the value of the right-hand side to the variable on the left-hand side.
Q #12) What is the explanation for prototype function in C?
Prototype function is a declaration of a function with the following information to the compiler.
  • Name of the function.
  • The return type of the function.
  • Parameters list of the function.
prototype function in C
In this example Name of the function is Sum, the return type is integer data type and it accepts two integer parameters.
Q #13) What is the explanation for cyclic nature of data types in C?
Ans) Some of the data types in C have special characteristic nature when a developer assign value beyond the range of the data type. There will be no any compiler error and the value change according to a cyclic order. This is called as cyclic nature and Char, int, long int data types have this property. Further float, double and long double data types do not have this property.
This is called as cyclic nature and Char, int, long int data types have this property. Further float, double and long double data types do not have this property.
Q #14) Describe the header file and its usage in C programming?
Ans) The file contains the definitions and prototypes of the functions being used in the program are called a header file. It is also known as a library file.
Example– The header file contains commands like printf and scanf is the stdio.h.
Q #15) There is a practice in coding to keep some code blocks in comment symbols than delete it when debugging. How this affect when debugging?
Ans) This concept called as commenting out and is the way to isolate some part of the code which scans possible reason for the error. Also, this concept helps to save time because if the code is not the reason for the issue it can simply uncomment.
Q #16) What are the general description for loop statement and available loop types in C?
Ans) A statement that allows executing statement or group of statements in repeated way is defined as a loop. Following diagram explains
Following diagram explains a general form of a loop.
There are 4 types of a loop statement in C.
  • While loop
  • For Loop
  • Do…While Loop
  • Nested Loop
Q #17) What is a nested loop?
Ans) A loop running within another loop is referred as a nested loop. The first loop is called Outer loop and inside the loop is called Inner loop. Inner loop executes the number of times define an outer loop.
Q #18) What is the general form of function in C?
Ans) Function definition in C contains four main sections.
function in C
  • Return Type -> Data type of the return value of the function.
  • Function Name -> The name of the function and it is important to have a meaningful name that describes the activity of the function.
  • Parameters -> The input values for the function that need to use perform the required action.
  • Function Body -> Collection of statement that needs to perform the required action.
Q #19) What is a pointer on a pointer in C programming language?
Ans) A pointer variable that contains the address of another pointer variable is called pointer on a pointer. This concept de-refers twice to point to the data held by a pointer variable.
pointer on pointer
In this example **y returns value of the variable a.
Q #20) What are the valid places to have keyword “Break”?
Ans) The purpose of the Break keyword is to bring the control out of the code block which is executing. It can appear only in Looping or switch statements.
Q #21) What is the behavioral difference when include header file in double quotes (“”) and angular braces (<>)?
Ans) When Header file include within double quotes (“”), compiler search first in the working directory for the particular header file. If not found then in the built in the include path. But when Header file include within angular braces (<>), the compiler only search in the working directory for the particular header file.
Q #22) What is a sequential access file?
Ans) In general programs store data into files and retrieve existing data from files. With the sequential access file such data saved in a sequential pattern. When retrieving data from such files each data need to read one by one until required information find.
Q #23) What is the method to save data in stack data structure type?
Ans) Data is stored in Stack data structure type using First in Last out (FILO) mechanism. Only top of the stack is accessible at a given instance. Storing mechanism is referred as a PUSH and retrieve is referred as a POP.
Q #24) What is the significance of C program algorithms?
Ans) The algorithm needs to create first and it contains step by step guidelines on how the solution should create. Also, it contains the steps to consider and the required calculations/operations within the program.
Q #25) What is the correct code to have following output in C using nested for loop?
correct code
output
Q #26) Explain the use of function toupper() with and example code?
Ans) Toupper() function is use to convert the value to uppercase when it uses with characters.
Code –
Code
Result –
Result
Q #27) What is the code in while loop that returns the output of given code?
while loop
while loop 1
Q #28) What is the incorrect operator form following list(== , <> , >= , <=) and what is the reason for the answer?
Ans) Incorrect operator is ‘<>'.This is the format correct when writing conditional statements, but it is not a correct operation to indicate not equal in C programming and it gives compilation error as follows.
Code –
incorrect operator form
Error –
Error
Q #29) Is it possible to use curly brackets ({}) to enclose single line code in C program?
Ans) Yes, it is working without any error. Some programmers like to use this to organize the code. But the main purpose of curly brackets is to group several lines of codes.
Q #30) Describe the modifier in C?
Ans) Modifier is a prefix to the basic data type which is used to indicate the modification for storage space allocation to a variable.
Example– In 32-bit processor storage space for int data type is 4.When we use it with modifier the storage space change as follows.
  • Long int -> Storage space is 8 bit
  • Short int -> Storage space is 2 bit
Q #31) What are the modifiers available in C programming language?
Ans) There are 5 modifiers available in C programming language as follows.
  • Short
  • Long
  • Signed
  • Unsigned
  • long long
Q #32) What is the process to generate random numbers in C programming language?
Ans) The command rand() is available to use for this purpose. The function returns any integer number beginning from zero(0). Following sample code demonstrate the use of rand().
Code –
random numbers
Output –
random numbers Output
Q #33) Describe newline escape sequence with a sample program?
Ans) New line escape sequence is represented by \n. This indicates the point that new line need to start to the compiler and the output creates accordingly. Following sample program demonstrate the use of newline escape sequence.
Code 
newline escape
Output screen 
Output screen
Q #34) Is that possible to store 32768 in an int data type variable?
Ans) Int data type only capable of storing values between – 32768 to 32767.To store 32768 a modifier needs to use with int data type. Long Int can use and also if there is no any negative values unsigned int is also possible to use.
Q #35) Is there any possibility to create customized header file with C programming language?
Ans) It is possible and easy to create a new header file. Create a file with function prototypes that needs to use inside the program. Include the file in ‘#include' section from its name.
Q #36) Describe dynamic data structure in C programming language?
Ans) Dynamic data structure is more efficient to the memory. The memory access occurs as needed by the program.
Q #37) Is that possible to add pointers to each other?
Ans) There is no possibility to add pointers together. Since pointer contains address details there is no way to retrieve the value from this operation.
Q #38) What is indirection?
Ans) If you have defined a pointer to a variable or any memory object, there is no direct reference to the value of the variable. This is called indirect reference. But when we declare a variable it has a direct reference to the value.
Q #39) What are the ways to a null pointer can use in C programming language?
Ans) Null pointers are possible to use in three ways.
  • As an error value.
  • As a sentinel value.
  • To terminate indirection in the recursive data structure.
Q #40) What is the explanation for modular programming?
Ans) The process of dividing the main program into executable subsection is called module programming. This concept promotes the reusability.

Tuesday 26 February 2019

Simple C Programs

    // Finding Largest Number and Smallest Number in a list of integers.
#include <stdio.h>
void main()
{
   int i,n,largest = 0,smallest = 30000;
   int a[100];
   printf("\n Enter how many integers : ");
   scanf("%d",&n);
   for(i = 0 ; i < n ; i++)
    {
        printf("\n Enter the integer %d  : ",i+1);
        scanf("%d",&a[i]); 
    }
   for(i = 0 ; i < n ; i++)
    {
        if(largest < a[i])
           largest = a[i];
        if(smallest > a[i])
           smallest = a[i];
    }
     printf("\n Largest Number in the list is %d \n",largest);   
     printf("\n Smallest Number in the list is %d \n",smallest); 
}



// Matrix Addition.
    #include <stdio.h>
   
    void main()
    {
       int i,j,m, n, p, q, a[100][100], b[100][100], c[100][100];
   
       printf("Enter the number of rows and columns of matrix A :\n");
       scanf("%d%d", &m, &n);
       printf("Enter the elements of  matrix A\n");
   
       for (i = 0; i < m; i++)
          for (j = 0; j < n; j++)
             scanf("%d", &a[i][j]);
       printf("Enter the number of rows and columns of matrix B :\n");
       scanf("%d%d", &p, &q);
     
   
       printf("Enter the elements of matrix B : \n");
   
       for (i = 0; i < p; i++)
          for (j = 0 ; j < q; j++)
             scanf("%d", &b[i][j]);
    if(m != p || n != q)
          printf("\n Matrix Addition is not Possible. \n");
    else
    {     
     
       for (i = 0; i < m; i++)
       {
          for (j = 0 ; j < n; j++)
          {
             c[i][j] = a[i][j] + b[i][j];
          }
       }

       printf("Resultant Matrix is :-\n");
     
       for (i = 0; i < m; i++)
       {
          for (j = 0 ; j < n; j++)
          {
                      printf("%d \t", c[i][j]);
          }
          printf("\n");
       }
     }
  }
   








// Matrix Multiplication.
    #include <stdio.h>
   
    void main()
    {
       int i,j,k,m, n, p, q, a[100][100], b[100][100], c[100][100];
   
       printf("Enter the number of rows and columns of matrix A :\n");
       scanf("%d%d", &m, &n);
       printf("Enter the elements of  matrix A\n");
   
       for (i = 0; i < m; i++)
          for (j = 0; j < n; j++)
             scanf("%d", &a[i][j]);
       printf("Enter the number of rows and columns of matrix B :\n");
       scanf("%d%d", &p, &q);
     
   
       printf("Enter the elements of matrix B : \n");
   
       for (i = 0; i < p; i++)
          for (j = 0 ; j < q; j++)
             scanf("%d", &b[i][j]);
    if(n != p)
          printf("\n Matrix Addition is not Possible. \n");
    else
    {     
     
       for (i = 0; i < m; i++)
       {
          for (j = 0 ; j < q; j++)
          {
            for(k=0 ; k < p; k++)
             c[i][j] = c[i][j] + a[i][k] * b[k][j];
          }
       }

       printf("Resultant Matrix is :-\n");
     
       for (i = 0; i < m; i++)
       {
          for (j = 0 ; j < q; j++)
          {
                      printf("%d \t", c[i][j]);
          }
          printf("\n");
       }
     }
  }








// String Operations.
#include <stdio.h>
#include <string.h>
void main()
{
    char str1[30],str2[30];
    int choice,result=0;
    int len1=0,len2=0;
    printf("\n Enter First String : ");
    scanf("%[^\n]",str1);
    printf("\n Enter Second String : ");
    scanf("%s",str2);
    printf(" %s \n %s",str1,str2);
    printf("\n Enter Your Choice : \n");
    printf("1. Copy Strings. \n");   
    printf("2. Compare Strings. \n");
    printf("3. Concatenate Strings. \n");   
    printf("4. Find Lengths. \n");
    printf("\n Enter Your Choice :");
    scanf("%d",&choice);
    switch(choice)
    {
       case 1 :
             { 
                strcpy(str2,str1);
                printf("\n %s %s \n",str1,str2);
                break;
             }

       case 2 :
             { 
                result = strcmp(str2,str1);
                if(result == 0)
                   printf("\n Both Strings are Same. ");
                else
                   printf("\n Both are different strings.");

                break;
             }

       case 3 :
             { 
                strcat(str2,str1);
                printf("\n %s %s \n",str1,str2);
                break;
             }

       case 4 :
             { 
                len1 = strlen(str1);
                printf("\n Legth of First String is %d \n",len1);
                len2 = strlen(str2);
                printf("\n Legth of Second String is %d \n",len2);
                break;
             }

         default : printf("\n Wrong Choice .");
        }
   

}



// Searching of given number in a given list.
#include <stdio.h>
void main()
{
   int i,n,ele,pos;
   int a[100];
   printf("\n Enter how many integers : ");
   scanf("%d",&n);
   for(i = 0 ; i < n ; i++)
    {
        printf("\n Enter the integer %d  : ",i+1);
        scanf("%d",&a[i]); 
    }
   printf("\n Enter element to be searched : ");
   scanf("%d",&ele);
   pos = -1;
   for(i = 0 ; i < n ; i++)
    {
        if(ele == a[i])
         { 
            pos = i + 1;
         }
     }
    if(pos == -1)
      printf("\n Search element not found in the list.\n"); 
    else
      printf("\n Element found in the list at %d position \n",pos);   
}



// Bubble Sort.
#include <stdio.h>
void main()
{
   int i,j,n,temp;
   int a[100];
   printf("\n Enter how many integers : ");
   scanf("%d",&n);
   for(i = 0 ; i < n ; i++)
    {
        printf("\n Enter the integer %d  : ",i+1);
        scanf("%d",&a[i]); 
    }
   for(i = 0 ; i < n-1 ; i++)
    {
      for(j = 0; j < n - i - 1; j++) 
        if(a[j] > a[j+1])
         { 
           temp = a[j];
           a[j] = a[j+1];
           a[j+1] = temp;
         }
     }
        printf("\n The given list of integers in ascending order is :\n");
   for(i = 0 ; i < n ; i++)
    {
        printf("%d\n",a[i]); 
    }
}





// Binary Search.
#include<stdio.h>

void main()
{
    int arr[50],i,n,x,flag=0,first,last,mid;

    printf("Enter size of array:");
    scanf("%d",&n);
    printf("\nEnter array element(ascending order)\n");

    for(i=0;i<n;++i)
        scanf("%d",&arr[i]);

    printf("\nEnter the element to search:");
    scanf("%d",&x);

    first=0;
    last=n-1;

    while(first<=last)
    {
        mid=(first+last)/2;

        if(x==arr[mid]){
            flag=1;
            break;
        }
        else
            if(x>arr[mid])
                first=mid+1;
            else
                last=mid-1;
    }

    if(flag==1)
        printf("\nElement found at position %d",mid+1);
    else
        printf("\nElement not found");

 
}

Simple C Programs

/* Employee pay slip generation */
#include <stdio.h>
#include <conio.h>
void main()
{
    int empno;
    char name[30];
    float basic,da,ta,hra,it,gross,net;
    printf("\n Enter employee number:");
    scanf("%d",&empno);
    printf("\n Enter employee name:");
    scanf("%s",name);
    printf("\n Enter basic salary:");
    scanf("%f",&basic);
    if(basic > 8000)
    {
       hra = basic * 40 / 100;
       da =  basic * 30 / 100;
       it =  basic * 10 / 100;
     }
     else
     {
       hra = basic * 30 / 100;
       da =  basic * 20 / 100;
       it =  0;
     }
      ta = basic * 10 /100;
      gross = basic + da + ta + hra;
      net = gross - it;
     clrscr();
     printf(" Employee Number is: %d",empno);
     printf("\n Employee Name is: %s",name);
     printf("\n Basic Salary is: %.2f",basic);
     printf("\n DA is: %.2f",da);
     printf("\n TA is: %.2f",ta);
     printf("\n HRA is: %.2f",hra);
     printf("\n IT is: %.2f",it);
     printf("\n Gross Salary is: %.2f",gross);
     printf("\n Net Salary is: %.2f",net);
}
Output:
Enter employee number:101
Enter employee name: Ganesh
Enter basic salary: 10000


Employee Number is: 101
Employee Name is: Ganesh
Basic Salary is: 10000.00
DA is: 3000.00
TA is: 1000.00
 HRA is: 4000.00
 IT is: 1000.00
 Gross Salary is: 18000.00
 Net Salary is: 17000.00


/* Finding Roots of a Quadratic Equation */

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
    int a,b,c;
    float r1,r2,d;
    clrscr();
    printf("\n Enter values ( > 0 )for calculating discremenent:");
    scanf("%d%d%d",&a,&b,&c);
    d = (float) b*b - 4.0 * a * c;
    if(d==0)
       {
printf("\n Roots are Real and Equal");
r1 = -b / (2.0 * a);
r2 = -b / (2.0 * a);
printf(" \nThe Roots are %f and %f",r1,r2);
       }
     else if(d>0)
       {
printf("\n Roots are Real and Unequal");
r1 = (-b + sqrt(d)) / (2.0 * a);
r2 = (-b - sqrt(d)) / (2.0 * a);
printf("\nThe Roots are %.2f and %.2f",r1,r2);
       }
     else
printf("\n Roots are imaginary");
}

Output:

Enter values ( > 0 )for calculating discremenent: 1 3 2
Roots are Real and Equal
The Roots are –1.00 and –2.00


/* Checking whether Prime number or not */

#include<stdio.h>
#include<conio.h>
void main()
{
   int n,flag, i;
   clrscr();
   printf("Enter a number:");
   scanf("%d",&n);
   flag=0;
   for(i=2;i<=n/2;i++)
    if(n % i == 0)
      {
flag++;
break;
      }
    if(flag == 0)
      printf("\nEntered number is a prime number");
    else
      printf("\nEntered number is not a prime number");
}

Output:

Enter a number: 97
Entered number is a prime number


/* Reverse of a given number */

#include<stdio.h>
#include<conio.h>
void main()
{
   int n,r,rev=0;
   clrscr();
   printf("\nEnter a number:");
   scanf("%d",&n);
   while(n != 0)
   {
     r = n % 10;
     rev = rev * 10 + r;
     n  = n / 10 ;
   }
   printf("\nReverse = %d",rev);
 }

Output:

Enter a number: 123
Reverse = 321


/* Factorial of a Number Using Recursion */

#include<stdio.h>
#include<conio.h>
int fact(int n)
{
    int f;
    if(n==0)
       return 1;
    else
       f= n * fact(n-1);
       return f;
}
void main()
{
   int n,f;
   clrscr();
   printf("\n Enter the value of n:");
   scanf("%d",&n);
   f = fact(n);
   printf("\n The factorial of %d is %d",n,f);
}

Output:

Enter the value of n: 6
The factorial of 6 is 720


/* Call by Reference */

#include<stdio.h>
#include<conio.h>
void swap(int *p, int *q)
{
   int temp;
   temp = *p;
   *p = *q;
   *q = temp;
}
void main()
{
   int a,b;
   clrscr();
   printf("Enter two numbers a and b:");
   scanf("%d%d",&a,&b);
   printf("\nBefore passing a = %d, b = %d",a,b);
   swap(&a,&b);
   printf("\nAfter passing a = %d, b = %d",a,b);
}

Output:

Enter two numbers a and b: 10 23
Before passing a = 10, b = 23
After passing a = 23, b = 10


/* Febonacci Series Using Static Variables */

#include<stdio.h>
#include<conio.h>
int fib(int i)
{
   static int f=0,s=1,temp=0;
    if(i==1)
      return 0;
    f=s;
    s=temp;
    temp = f + s;
    return temp;
}
void main()
{
   int i,n,s;
   clrscr();
   printf("Enter how many numbers of the series:");
   scanf("%d",&n);
   printf(“\nThe Febonacci Series is ….”);
   for(i=1;i<=n;i++)
     {
       s = fib(i);
       printf("\n%d",s);
     }
}

Output:

Enter how many numbers of the series: 8
The Febonacci Series is ….
0
1
1
2
3
5
8
13


/* Linear Search  */
#include<stdio.h>
#include<conio.h>
void main()
{
    int a[10],n,ele,flag =0,i;
    clrscr();
    printf("How many elements:");
    scanf("%d",&n);
    printf("\n Enter elements into the array:");
    for(i=0;i<n;i++)
    {
scanf("%d",&a[i]);
    }
    printf("\nEnter element to be searched:");
    scanf("%d",&ele);
    for(i=0;i<n;i++)
    {
if(ele == a[i])
{
    flag++;
    break;
}
     }
    if(flag == 1)
printf("\n Element %d found at %d",ele,i+1);
     else
printf("\n Element %d is not found",ele);
 }

Output

How many elements: 5
Enter elements into the array: 10
40
30
50
20
Enter element to be searched: 30
Element 30 found at 3 


/* Binary Search */
#include<stdio.h>
#include<conio.h>
void main()
{
   int a[10],flag=0,i,n,top,mid,bottom,key;
   clrscr();
   printf("\nEnter size of the array:");
   scanf("%d",&n);
   printf("\nEnter elements into the array in sorted order:");
   for(i=0;i<n;i++)
       scanf("%d",&a[i]);
   printf("\nEnter element to be searched:");
   scanf("%d",&key);
   top = 0;
   bottom = n-1;
   while(top <= bottom && flag == 0)
   {
       mid = (top + bottom) /2 ;
       if(a[mid] == key)
  flag = 1;
else if(a[mid] < key)
   top = mid + 1;
else
   bottom = mid - 1;
    }
    if(flag == 1)
       printf("\nSearch element %d found at position %d",key,mid+1);
    else
       printf("\nSearch element %d is not found.",key);
}

Output

Enter size of the array: 5
Enter elements into the array in sorted order: 10  20  30  40  50
Enter element to be searched: 40
Search element 40 found at position 4


/* Bubble Sort */
#include<stdio.h>
#include<conio.h>
void main()
{
    int a[10],n,i,j,t;
    clrscr();
    printf("\nEnter size of the array:");
    scanf("%d",&n);
    printf("\nEnter elements into the array:");
    for(i=0;i<n;i++)
       scanf("%d",&a[i]);
    for(i=0;i<n-1;i++)
      for(j=0;j<n-1-i;j++)
   if(a[j] > a[j+1])
   {
      t = a[j];
      a[j] = a[j+1];
      a[j+1] = t;
    }
     printf("\n The sorted array is :");
     for(i=0;i<n;i++)
printf("%d\t",a[i]);
}

Output

Enter size of the array: 5
Enter elements into the array: 10
40
30
50
20

The sorted array is :  10     20     30      40    50


/* Addition of two matrices */
#include <stdio.h>
#include <conio.h>
void main()
{
    int m1[3][3],m2[3][3],m3[3][3],i,j;
    clrscr();
    printf("Enter elements into matrix-1: \n");
    for(i=0;i<3;i++)
      for(j=0;j<3;j++)
scanf("%d",&m1[i][j]);
    printf("Enter elements into matrix-2: \n");
    for(i=0;i<3;i++)
      for(j=0;j<3;j++)
scanf("%d",&m2[i][j]);
    for(i=0;i<3;i++)
      for(j=0;j<3;j++)
  m3[i][j] = m1[i][j] + m2[i][j];
    printf("\n Sum : \n");
    for(i=0;i<3;i++)
     {
      for(j=0;j<3;j++)
printf("%3d",m3[i][j]);
      printf("\n");
     }
}

Output:
Enter elements into matrix-1:
1 2 3 4 5 6 7 8 9
Enter elements into matrix-2:
1 2 3 4 5 6 7 8 9
Sum :
   2    4    6
   8  10  12
 14  16  18



/* Matrix Multiplication */
#include <stdio.h>
#include <conio.h>
#include <process.h>
void main()
{
  int m1[10][10],m2[10][10],m3[10][10]={0};
  int i,j,k,r1,r2,c1,c2;
  clrscr();
  printf("Enter number of rows and number of columns of matrix-1:");
  scanf("%d%d",&r1,&c1);
  printf("Enter number of rows and number of columns of matrix-2:");
  scanf("%d%d",&r2,&c2);
  if(c1 != r2)
  {
     printf("\n Multiplication is not possible.");
     exit(0);
  }
  printf("\n Enter elements into matrix-1:");
  for(i=0;i<r1;i++)
    for(j=0;j<c1;j++)
       scanf("%d",&m1[i][j]);
  printf("\n Enter elements into matrix-2:");
  for(i=0;i<r2;i++)
    for(j=0;j<c2;j++)
       scanf("%d",&m2[i][j]);
  for(i=0;i<r1;i++)
    for(j=0;j<c2;j++)
       for(k=0;k<c1;k++)
  m3[i][j] = m3[i][j] + m1[i][k] * m2[k][j];
  printf("\n Product:\n");
  for(i=0;i<r1;i++)
   {
    for(j=0;j<c2;j++)
       printf("%5d",m3[i][j]);
    printf("\n");
   }
}

Output:
Enter number of rows and number of columns of matrix-1: 3 3
Enter number of rows and number of columns of matrix-2: 3 3
Enter elements into matrix-1: 1 2 3 4 5 6 7 8 9
Enter elements into matrix-2: 1 2 3 4 5 6 7 8 9
Product:
         30     36     42
         66     81     96
       102   126   150


/* Transpose of a Matrix */
#include <stdio.h>
#include <conio.h>
void main()
{
  int m1[10][10],m2[10][10],i,j,r,c;
  clrscr();
  printf("Enter number of rows and number of columns of the matrix:");
  scanf("%d%d",&r,&c);
  printf("\n Enter elements into matrix:");
  for(i=0;i<r;i++)
    for(j=0;j<c;j++)
       scanf("%d",&m1[i][j]);
  for(i=0;i<c;i++)
    for(j=0;j<r;j++)
  m2[i][j] = m1[j][i];
  printf("\n Original :\n");
  for(i=0;i<r;i++)
   {
    for(j=0;j<c;j++)
       printf("%5d",m1[i][j]);
    printf("\n");
   }
  printf("\n Transpose:\n");
  for(i=0;i<c;i++)
   {
    for(j=0;j<r;j++)
       printf("%5d",m2[i][j]);
    printf("\n");
   }
}

Output:
Enter number of rows and number of columns of the matrix: 4 3
Enter elements into matrix: 1 2 3 4 5 6 7 8 9 10 11 12
Original :
1    2     3
4      5     6
7      8     9
10  11   12
Transpose:
     1      4     7   10
     2      5     8   11
     3      6     9   12

/* String Concatenation */
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
    char st1[30],st2[30],st3[60];
    clrscr();
    printf("\n Enter string-1:");
    gets(st1);
    printf("\n Enter string-2:");
    gets(st2);
    strcpy(st3,st1);
    strcat(st3,st2);
    puts(st1);
    puts(st2);
    puts(st3);
 }


Output:
Enter string-1: gvp
Enter string-2: college

gvp
college
gvpcollege


/* Student Structure */
#include <stdio.h>
#include <conio.h>
struct stud
{
    int sno;
    char sname[10];
    int m1,m2,m3;
    float avg;
};
void main()
{
   struct stud s;
   clrscr();
   printf("Enter student number:");
   scanf("%d",&s.sno);
   printf("Enter student name:");
   scanf("%s",s.sname);
   printf("Enter marks in subject-1:");
   scanf("%d",&s.m1);
   printf("Enter marks in subject-2:");
   scanf("%d",&s.m2);
   printf("Enter marks in subject-3:");
   scanf("%d",&s.m3);
   s.avg = ( s.m1 + s.m2 + s.m3 ) / 3.0;
   clrscr();
   printf("Student Details: \n ");
   printf("Number    Name    Average \n");
   printf("%d   %s  %f",s.sno,s.sname,s.avg);
}

Output:
Enter student number: 1001
Enter student name: Ganesh
Enter marks in subject-1: 80
Enter marks in subject-2: 70
Enter marks in subject-3: 60

Student Details:
Number    Name    Average
1001         Ganesh   70.00


/* Greatest of three numbers */
#include <stdio.h>
#include <conio.h>
#define A printf("%d",a)
#define B printf("%d",b)
#define C printf("%d",c)
void main()
{
    int a,b,c;
    clrscr();
    printf("\n Enter Three numbers:");
    scanf("%d%d%d",&a,&b,&c);
    printf("\n Gretest of the three numbers is :");
    a > b ? (a>c ? A:C):(b>c ? B:C);
}

Output:
Enter Three numbers: 10
20
30

Gretest of the three numbers is : 30


Tuesday 29 January 2019

C Language MCQ's


 Multiple choice questions.
1.      Which is the C IDE:

a.       GNU gcc compiler
b.      Borland C++ compiler
c.       Turbo C++ IDE
d.      Visual Studio
e.      All of Above

2.      C us supported by the following operating system:

a.       Unix
b.      Linux
c.       Windows
d.      All of above

3.      C is the ________ language:

a.       Low level
b.      High Level
c.       Both Low and High Level
d.      None of these

4.      Which is not the feature of c language:

a.       Portable
b.      Terse
c.       Modular
d.      Efficient
e.      None of these

5.      You require to write and run c program:

a.       Operating system
b.      Text editor
c.       Compiler
d.      All of above

6.      which is the shortcut key to compile program in Turbo C IDE:

a.       Ctrl + F9
b.      Alt + F9
c.       Ctrl + F5
d.      None of these

7.      C has _______ keywords:

a.       30
b.      31
c.       32
d.      33

8.      Which is the type of int:

a.       Int
b.      Unsigned int
c.       Long
d.      Unsigned long
e.      All of above

9.      Which is not the valid integer:

a.       +345
b.      345UL
c.       123U
d.      123.0

10.  Which is the invalid octal:

a.       0346
b.      0452
c.       0255
d.      0840

11.  Which is invalid hexadecimal:

a.       0x345
b.      0xA132
c.       0xG120
d.      0x452F

12.  The ASCII value of Y:

a.       88
b.      89
c.       90
d.      91

13.  Which is not the fundamental data types:

a.       Char
b.      Array
c.       Int
d.      Float

14.  Variable is a:

a.       Location in memory
b.      Location in CPU Registers
c.       Both
d.      None of these

15.  Which is not the type of variable

a.       Extrern
b.      Register
c.       Global
d.      None of above

16.  Which is the invalid identifiers name:

a.       Pushp1
b.      _pushp
c.       1Pushp
d.      pushp_infotech

17.  int can store:

a.       Real numbers
b.      Characters
c.       String
d.      None of these

18.  Which is not the type of variable initializations:

a.       Static
b.      Dynamic
c.       Both
d.      None of these

19.  In c language ‘\a’ used for:

a.       Form feed
b.      Line Brack
c.       Alarm
d.      None of these

20.  The Arithmetic operator ‘%’ can be used with:

a.      int
b.      float
c.       double
d.      void

21.  ‘%d’ is the conversion letter for:

a.       char
b.      int
c.       float
d.      double

22.  printf(“%c”, 65); Out of this line is:

a.       65
b.      A
c.       Both
d.      None of these

23.  Binary operator needs:

a.       One operand
b.      Two operand
c.       Three operand
d.      None of these

24.  Which is the symbol for AND operator:

a.       ||
b.      &&
c.       $$
d.      None of these

25.  printf(“0 && 1 = %d\n”, 0 && 1); Out of this line is:

a.      0 && 1 = 0
b.      0 && 1 = 1
c.       0 && 1 = 2
d.      0 && 1 = 3

26.  int x=10;
            printf("%d",x++);      Output is:

a.      10
b.      11
c.       12
d.      None of these

27.  >> operator is used for:

a.      Right Shift
b.      Left Shift
c.       Both
d.      None of these

28.  char x=10;
            printf("%d",~x);         Output is:

a.       10
b.      -10
c.       -11
d.      None of these

29.    printf("%d",sizeof(int)); Output is:

a.       1
b.      2
c.       6
d.      10

30.  C program starts executing from:

a.      main()
b.      header file
c.       both
d.      None of these

31.  Which is the incorrect statement:
a.       Variable name can contain underscore.
b.      Variable name may start from digit.
c.       Variable name may not have white space character.
d.      Keyword can not be a variable name.
32.  Uninitialized variable may have:

a.      Garbage value.
b.      Can not be zero
c.       Both
d.      None of these.

33.  Which is the correct variable name:

a.       for
b.      goto
c.       character
d.      if

34.  Which is not the c keyword:

a.       typedef
b.      extern
c.       register
d.      local

35.  Which operator is used to assign value to variables:

a.      =
b.      +
c.       
d.      /

36.  ‘\n’ used for

a.       Alert
b.      New line
c.       Form feed
d.      Backspace

37.  printf(“%u”,&a); The output of this statement is:

a.       Value of a
b.      Address of a
c.       Both
d.      None of these.

38.  String is:

a.       Array of numbers.
b.      Array of characters.
c.       Both
d.      None of these.

39.  Which is the string termination character:

a.       ‘\n’
b.      ‘\b’
c.       ‘\0’
d.      None of these

40.  char *name= “India”;
strupr(name);
puts(name);  Output of this program is:

a.       India
b.      india
c.       INDIA
d.      iNDIA

41.  Which is not the string handling function:

a.       strlwr();
b.      strcat();
c.       strcmp()
d.      strrev();
e.       strlen();
f.        None of these.

42.  Which statement is wrong:
a.       A function may have arguments.
b.      A function may return value.
c.       A can be invoked many time in a single program.
d.      Function cannot be reused.
43.  A function can return only _______

a.      Single value
b.      Two Values.
c.       Many values.
d.      None of these

44.  Mathematical function are stored in _________ header file:

a.       stdio.h
b.      conio.h
c.       math.h
d.      string.h

45.  A function which invokes itself repeatedly until some condition is satisfied is called a ___________ function.

a.      Recursive
b.      System
c.       Library
d.      None of these

46.  ++ is ________ operator:

a.       Decrement
b.      Increment
c.       Add
d.      Plus-Plus

47.  Which is the incorrect statement:
a.       An array is the collection of variables.
b.      All array variables have same type.
c.       Array variables can be used individually.
d.      None of these.
48.  An array can be declared:

a.      Statically
b.      Dynamically
c.       Both
d.      None of these

49.  Array can be:

a.       Single Dimensional
b.      Multi Dimensional
c.       Both
d.      None of these

50.  Array index is always starts from:

a.      0
b.      1
c.       2
d.      3

51.  An array is ____________ data-structure:

a.      Linear
b.      Non-linear
c.       Hierarchical
d.      None of these

52.  Which is the false statement:
a.       An array of characters is called string.
b.      Array can be passed to function.
c.       Array is always reference type.
d.      None of these
53.  Array can be sorted by using:

a.       Bubble Sort
b.      Merge Sort
c.       Quick Sort
d.      All of above

54.  Which term is not related to function:

a.       Prototype
b.      Definition
c.       Call
d.      Receive

55.  Which is the type of function arguments:

a.       Formal
b.      Actual
c.       Both
d.      None of these

56.  Which not the input function:

a.       gets();
b.      getch();
c.       getchar();
d.      scanf();
e.       getche();
f.        None of these.

57.  Which is not the output function:

a.       printf();
b.      puts();
c.       puchar();
d.      putch();
e.      None of these.

58.  int add(int,int); in the given function prototype select the correct statement:

a.       int x=add(4.2,5);
b.      int x=add(4,25);
c.       int x=add(425);
d.      int x=add();

59.  A pointer variable can store ________
a.       Constant value
b.      Value of anther variable.
c.       Address of another variable
d.      None of these.
60.  int *ptr; here ptr can store the address of:

a.      int variable
b.      float variable
c.       double variable
d.      All of above

61.  int x=10;
int *y=&x; the variable y contains:

a.       Value of x;
b.      Address of x;
c.       Both
d.      None of these

62.  int **ptr; here ptr is:

a.       Pointer
b.      Pointer to pointer
c.       Both
d.      None of these

63.  In the call by reference we pass:

a.       Value of the variable
b.      Address of variable
c.       Both value and address
d.      None of these

64.  int a[3]={4,5,6};
printf(“%u”,a); What is the output:

a.       Value of first element.
b.      Address of first element
c.       Both
d.      None of these

65.  Which function is related to dynamic memory allocation:

a.       malloc();
b.      calloc()
c.       realloc()
d.      All of above.

66.  strcmp() function is used for:

a.       Copy two strings
b.      Compare two strings.
c.       Concatenation of two strings
d.      None of these

67.  Which is the formatted input function:

a.       getch();
b.      scanf();
c.       gets();
d.      getche();

68.  Which is the incorrect function prototype:

a.       int add(int,int);
b.      int add(float, int);
c.       float add(int,int);
d.      float add(float,int);

69.  Which is the incorrect function prototype based on c library:

a.       char *gets(char *string);
b.      int puts(const char*s);
c.       char *cgets(char *str);
d.      int cputs(const char*str);
e.      None of these

70.  Which is the type of files:

a.       Text
b.      Binary
c.       Both
d.      None of these

71.  Which function is not related to file handling:

a.       fopen();
b.      fclose();
c.       fprintf();
d.      printf();

72.  Which is the file opening mode:

a.       r
b.      w
c.       rb
d.      wb
e.       a
f.        All of above

73.  A structured programming have:

a.       Sequence
b.      Selection
c.       Iteration
d.      All of Above

74.  Which is not the selective control flow statement:

a.      while
b.      if
c.       Switch-case
d.      if-else

75.  Which is the correct example of label:

a.       Label;
b.      Label:
c.       Label,
d.      #Label

76.  Who is the manufacturer of C language:

a.       Herbert Schieldt
b.      Banjarne Stroups
c.       Dennis Ritchie
d.      None of these

77.  C is the __________ language:

a.       Object Oriented
b.      Structured
c.       Unified Modeling
d.      None of these.

78.  An efficient algorithm ______

a.       Takes efficient time
b.      Takes efficient memory
c.       Both
d.      None of these

79.  Which technique is related to internal code:

a.       Black Box
b.      White Box
c.       Alfa
d.      Beta

80.  Which is the type of white box testing:

a.       Path Testing
b.      Loop Testing
c.       Domain Testing.
d.      All of these