Friday, December 6, 2013

C langauge important questions



c programming – question bank
unit -1 (2 marks)
1.      why C is called as Function oriented language ?
2.      Give out the syntax for scanf() and printf() with an example
3.      Give out the structure of C program
4.      Define constant and its types
5.      Define character set
6.      Define Identifiers with an example
7.      Define variables with an example
8.      Mention the rules for constructing character constants
9.      List out the various logical operators
10.  what is << operator and give out an example for it
11.  List out any 4 relational operator
12.  Mention any 5 Library functions
unit -1 (5 marks)
1.      write down the rules for constructing  Integer constant with examples
2.      write down the rules for constructing real constants in fractional form with examples
3.      write down the rules for constructing real constants in exponential form with examples
4.      write a program to add two given number  and give out the sample output for it.
5.      write a program to divide two given numbers and give out the sample output for it.
6.      write a short note on Arithmetic operators with examples
7.      write a short note on relational operators with examples
8.      write a short note on Bitwise operators with examples
9.      write a short note on Logical operators with examples
10.  write a program to check a given number is even or odd.

unit-1 (10 marks)

1.      write a detailed note on datatypes with example
2.      Discuss in detail about the real constants.
3.      write a detail note on Operators.
4.      write a detail note on a. relational operators b. bitwise operators
5.      write a program to swap two numbers using  a. with temporary variable b.without using temporary variable

unit-2 (2 marks)

1.      Define Conditional control structure and its types
2.      What is repetitive control structure?
3.      what is selective control structure ?
4.      what is nested if ? give out an example for it
5.      what is if-else ? give out its syntax
6.      Define else-if Ladder? give out an example for it
7.      Give out the syntax of for loop with an example
8.      distinguish between do-while and while
9.      what is the use of break statement
10.  what is continue  statement and why we use it ?
11.  what is the use of header file and why we include it in the program
12.  list down the steps involved in execution of c program

unit-2 (5 marks)

1.      write a short note on if structure with an example
2.      write a program to find the biggest of two given numbers
3.      write a program to find the factorial of a given number
4.      discuss briefly about else if ladder with an example program
5.      what is while loop ? using while loop find out the sum of first 20 natural numbers
6.      what is Switch case? give out its syntax with an example
7.      write a short note on nested for  structure with an example
8.      write a program to find the largest of 3 given numbers
9.      write a short note on a. break b. continue c.goto
10.  discuss briefly on ?: operator with an example

unit-2(10 marks)

1.      Discuss in detail about control structures
2.      write a detail note on following  with an example a. nested if b. for c.while
3.      write a program to generate Floyd’s triangle
4.      write a program to check a given number is prime or not
5.      what is nested control structure ? give out an example program for it.

unit -3 (2 marks)

1.      define automatic variables
2.      What is register variable? Why we use it?
3.      distinguish local variable and global variable
4.       what is extern keyword ?  Why we use it?
5.      Define static variable? how it differs automatic variables?
6.      define Function prototype ? give out its syntax?
7.      Define Function definition? give out its syntax?
8.      Define function call ? give out its syntax?
9.      List out the ambiguity in function call ?
10.  what is storage classes ? list out the different types?
11.  what is the use of strcmp()?
12.  List out any 5 string functions

Unit -3 (5marks)
1.      Write a short note on storage classes in c
2.      Write a short note on function definition with a example
3.      Write a short note on recursion with a example
4.      How will you return a value from a function with example
5.      Write a short note on register variables with example

unit-3 (10 marks)
1.      discuss in detail about storage classes with examples
2.      write a detailed note on functions
3.      write a program to find the factorial of a given number using recursion
4.      write a program to generate Fibonacci series
5.      write a program to find the product of two given numbers

unit-4 (2 marks)

1.      what is an array ?
2.      mention the memory representation of an array
3.      what is a char array ? how will you declare it ?
4.      what is 2D array ?
5.      Mention the memory mapping of 2D Array
6.      what is recursion? mention its benefits
7.      what is c preprocessor?
8.      mention any 4 prepocessing directives?
9.      what is the use of #include?
10.  what is a Linker ?
11.  what is a compiler ?
12.  what is a emulator?

unit-4(5 marks)

1.      define array ? give out an example program for array?
2.      write a program to find the sum and average of n given numbers?
3.      write a program to check a given string is palindrome or not ?
4.      write a short note on strings with example
5.      write a short note on string functions with example
6.      how to pass an entire array to a function . explain it with a example program
7.      write a program to print the following output:
0          1          1          2          3          5          8
8.      write a program to calculate sum of the digits of a given number
9.      how will you calculate the length and reverse of a string
10.  what is 2D array ? give out an example program for it

unit-4(10 marks)

1.      write a program to search a given element in an array
2.      write a detailed note on array and its operations
3.      write a program to sort the given numbers in ascending order
4.      write a detailed note on 2D array and implement it in matrix addition program
5.      write a detailed note on string and its functions

unit-5 (2 marks)

1.      what is a pointer ?
2.      what is a function pointer ?
3.      Define file
4.      mention any 4 file functions
5.      what is the use of fgetc () and fputc()
6.      give out the syntax of fopen() with an example
7.      define structure
8.      what is union?
9.      how will access structure ?
10.  what is far pointer ?
11.  what is call by value?
12.  what is call by reference?

unit-5(5 marks)

1.      write a short note on call by value with an example
2.      write a short note on call by reference with an example ?
3.      what is a pointer ? how will you pass pointer to an array?
4.      how will you pass a pointer to a function with an example
5.      write a short note on structure ? how structure members are accessed ?
6.      explain the concept of union with an example
7.      write a program to read the content of a file
8.      write a short note on file modes
9.       write a program to swap two numbers using call by reference ?
10.  how will you perform file copy
unit-5 (10 marks)
1.      write a detailed note on pointers.
2.      write a detailed note on structures
3.      discuss in detail about files
4.      write a program to find even and odd numbers and to store it in two separate files
5.      write a program to calculate student marksheet processing and store the information in a file

Monday, September 26, 2011

Different printf Statement

consider this code :

                                void main()
                                {
                                   int a=10,b=20;
                                   printf("%d...%d");
                                 }
o/p :
             10...20
the values will be printed even its variables are not mentioned. format specifier is necessary for that statement.


    




HOW TO PRINT THIS - printf("hello")

this can be done by the following code :

                                        printf("printf(\"hello\")");

o/p :

printf("hello")

Sunday, July 17, 2011

when will you declare a variable as a pointer variable

first understand the meaning of this declaration below :
                           
                                                 int a=10;
                                           
                                     
                                              
The statement describes the  variable (a) which holds the value 10 in a address in memory.Now the point to be noted is variable (a) contains a value.




But at the same time if the variable wants to hold address as a value like mentioned below

                                             a= address of another variable

then you have to declare this variable as a pointer variable like this
                                            int *a ;

Thursday, June 30, 2011

Snake game in C

The libraries used in this snake game are:
  • graphics.h
  • stdio.h
  • conio.h
  • dos.h
  • time.h
  • stdlib.h
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <time.h>


check();
end();
win();
int m[500],n[500],con=8,temp,a,i,j,t,s=500,z;
clock_t start,stop;
void main(void)

{
int gd=DETECT,gm,ch,maxx,maxy,x=13,y=14,p,q,spd=100;

initgraph(&gd,&gm,"D:\\TC\\BGI");


setcolor(WHITE);
settextstyle(3,0,6);
outtextxy(150,2," SNAKE GAME ");
settextstyle(6,0,2);
setcolor(RED);
outtextxy(20,80," Use Arrow Keys To Direct The Snake ");
setcolor(YELLOW);
outtextxy(20,140," Avoid The Head Of Snake Not To Hit Any Part Of Snake");
outtextxy(20,160," Pick The Beats Untill You Win The Game ");
setcolor(BLUE);
outtextxy(20,240," Press 'Esc' Anytime To Exit ");
outtextxy(20,220," Press Any Key To Continue ");

setcolor(GREEN);


outtextxy(400,380,"Made By :");
outtextxy(400,400,"Shubhang");
outtextxy(400,420,"Mohit");
outtextxy(400,440,"Rahul");



ch=getch();
if(ch==27) exit(0);
cleardevice();
maxx=getmaxx();
maxy=getmaxy();
for (z=0;z<3;z++)
{
sound(s);
delay(1000);
nosound();
delay(1000);
s+=500;
}
randomize();

p=random(maxx);
temp=p%13;
p=p-temp;
q=random(maxy);
temp=q%14;
q=q-temp;



start=clock();
a=0,i=0;
while(1)
{

 setcolor(WHITE);
 setfillstyle(SOLID_FILL,con+5);
 circle(p,q,5);
 floodfill(p,q,WHITE);

   if( kbhit() )
   {
     ch=getch(); if(ch==0) ch=getch();
     if(ch==72&& a!=2) a=1;
     if(ch==80&& a!=1) a=2;
     if(ch==75&& a!=4) a=3;
     if(ch==77&& a!=3) a=4;
      }
       else
     {
     if(ch==27
     ) break;
     }

       if(i<20){
     m[i]=x;
     n[i]=y;
     i++;
        }

   if(i>=20)

   {
     for(j=con;j>=0;j--){
      m[1+j]=m[j];
      n[1+j]=n[j];
       }
     m[0]=x;
     n[0]=y;

     setcolor(WHITE);
     setfillstyle(SOLID_FILL,con);
     circle(m[0],n[0],8);
     floodfill(m[0],n[0],WHITE);

     setcolor(WHITE);
     for(j=1;j<con;j++)
     {
    setfillstyle(SOLID_FILL,con+j%3);
     circle(m[j],n[j],5);
     floodfill(m[j],n[j],WHITE);
          }
       delay(spd);

    setcolor(BLACK);
    setfillstyle(SOLID_FILL,BLACK);
     circle(m[0],n[0],8);
     floodfill(m[0],n[0],BLACK);

     setcolor(BLACK);
    setfillstyle(SOLID_FILL,BLACK);
     circle(m[j],n[j],5);
     floodfill(m[j],n[j],BLACK);

     }
     stop=clock();
     t=(stop-start)/CLK_TCK;
     check();

    if(x==p&&y==q)
    {
    con=con+1;
    if(spd>=5)
    spd=spd-5;

    else spd=5;

    if(con>50)
    win();

    p=random(maxx);
    temp=p%13;
    p=p-temp;
    q=random(maxy);
    temp=q%14;
    q=q-temp;
        }

    if(a==1)
    y =  y-14;

    if(y<0)
    { temp=maxy%14;
    y=maxy-temp;
    }

    if(a==2)
    y =  y+14;

    if(y>maxy)
    y=0;

    if(a==3)
    x =  x-13;

    if(x<0)
    {
    temp=maxx%13;
    x=maxx-temp;
    }

    if(a==4)
    x =  x+13;

    if(x>maxx)
    x=0;

    if(a==0)
    {
    y = y+14 ;
    x=x+13;
    }

   }

 }


check()
{
   int a;
   for(a=1;a<con;a++)

if(m[0]==m[a] && n[0]==n[a]) end();
   else continue;
   return 0;
}

end()

{

    int j,i;
   setcolor(WHITE);
   for(i=0;i<5;i++)
   {
   delay(500);
   cleardevice();
   delay(500);
   for(j=0;j<=con;j++)
   {
    setfillstyle(SOLID_FILL,RED);
    circle(m[j],n[j],5);
    floodfill(m[j],n[j],RED);
   }

   }

    settextstyle(3,0,4);
    outtextxy(150,150,"    GAME OVER ");
    getch();
    getch();
    exit(0);
    return 0;
    }

win()
{
int j,i;
setcolor(WHITE);
   for(i=0;i<5;i++)
   {
   for(j=0;j<=con;j++)
   {
     setfillstyle(SOLID_FILL,con);
     circle(m[j],n[j],5);
     floodfill(m[j],n[j],WHITE);
   }

    delay(500);
    cleardevice();
    delay(500);
   }
  settextstyle(3,0,4);
  outtextxy(210,320," YOU WIN ");
  getch();
  exit(0);
  return 0;
}
NOTE : If the game shows any linking error 
then go to options>linkers>libraries and the tick on graphics.This
 method automatically link the graphics. 

Wednesday, June 29, 2011

swapping of two numbers without using temporary variable

swapping of two numbers using temporary variable :

                              int t;
                              t=a;
                              a=b;
                              b=t;
now consider swapping of two numbers without using the temporary variable:
                               a=a+b;
                               b=a-b;
                               a=a-b;

ex : consider a=10 and b=20
     a=10+20=30
     b=30-20=10
     a=30-10=20
now a=20 and b=10 after swapping

Tuesday, June 28, 2011

exit function


 The "exit" function terminates program execution and
returns the status value to the system. 
 
#include <stdlib.h>
exit( status );

The function begins its work by calling all the clean-up functions specified by "atexit" routines, in the reverse order that the functions were specified (so that the most recently specified clean-up function is executed first). These clean-up functions are executed in an environment that makes it look as if "main" has just returned. This means that automatic storage duration objects created by the program may not be accessed.
Next, "exit" flushes all I/O buffers and closes any streams that are currently open. Files created with the "tmpfile" function are removed.
Lastly, control is returned to the system. The system receives the status value indicating the success or failure of the program. The status is encoded in the program switch words in the same way as the UW Tools "exit" function; see "expl b lib exit" for details. In TSS, EXIT_FAILURE also issues a DRL T.ERR indicating an error.
For compatibility with other versions of C, you should only count on the lowest eight bits of "status" being returned to the caller.

exit(0) - exit with success
exit(1)-exit with failure