Dynamic Method Dispatch



Method overriding forms the basis for one of Java’s most powerful concepts: dynamic method dispatch. Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. Dynamic method dispatch is important because this is how Java implements run-time polymorphism.
·       By restating an important principle: a super class reference variable can refer to a subclass object. Java uses this fact to resolve calls to overridden methods at run time. Here is how. When an overridden method is called through a super class reference, Java determines which version of that method to execute based upon the type of the object being referred to at the time the call occurs. Thus, this determination is made at run time. When different types of objects are referred to, different versions of an overridden method will be called.
·       In other words, it is the type of the object being referred to (not the type of the reference variable) that determines which version of an overridden method will be executed. Therefore, if a superclass contains a method that is overridden by a subclass, then when different types of objects are referred to through a superclass reference variable, different versions of the method are executed.
·        Here is an example that illustrates dynamic method dispatch:

// Dynamic Method Dispatch
 class A {
  void callme(){
   System.out.println("Inside A's callme method");
   }
 } 
class B extends A {// override callme()
  void callme() {
   System.out.println("Inside B's callme method");
  }
 } 
class C extends A {  // override callme()
  void callme() {
  System.out.println("Inside C's callme method");
   }
 } 
class Dispatch {
  public static void main(String args[]) {
   A a = new A(); // object of type A
   B b = new B(); // object of type B
   C c = new C(); // object of type C 
  A r; // obtain a reference of type A
   r = a; // r refers to an A object
   r.callme(); // calls A's version of callme
   r = b; // r refers to a B object
   r.callme(); // calls B's version of callme
   r = c; // r refers to a C object
   r.callme(); // calls C's version of callme
   }

 }

   OUTPUT


Why multiple inheritance is not supported in java?

 Why multiple inheritance is not supported in java?

To reduce the complexity and simplify the language, multiple inheritance is not supported in java.  Consider a scenario where A, B and C are three classes. The C class inherits A and B classes. If A and B classes have same method and you call it from child class object, there will be ambiguity to call method of A or B class. Since compile time errors are better than runtime errors, java renders compile time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error now. 
For example:
class A{
void msg(){
System.out.println("Hello");
}
   }
   class B{
    void msg(){
System.out.println("Welcome");
}
   }
   class C extends A,B{//suppose if it were
        public static void main(String args[]){
        C obj=new C();
        obj.msg();//Now which msg() method would be invoked?
    }
   }   

Computer Organization(2140707) Important Questions



1.  Explain how complement number system is useful in computer system. Discuss any one complement number system with Example.
2.      Define RTL. Explain how register transfer takes place in basic computer system.
3.      What is a micro operation? List and explain its categories.
4.      Draw and explain basic computer instruction formats.
5.      Draw the block diagram of control unit.
6.      Write an assembly language program to add 10 numbers from memory.
7.      What is register stack? Explain push and pop micro-operations.
8.      Explain pipelining technique. Draw the general structure of four segment pipeline.
9.      Draw flow chart for multiplication of two floating point numbers.
10.  How addressing mode is significant for referring memory? List and explain types of addressing modes.
11.  Explain RISC. List its characteristics.
12.  Explain asynchronous data transfer using timing diagrams.
13.  How main memory is useful in computer system? Explain the memory address map of RAM and ROM.
14.  Write a short note :
·         Memory reference instructions
·         Explain instruction formats with its types.
·         Daisy chain arbitration.
·         Instruction cycle
·         Passes of an assembler.
·         Content Addressable Memory
15.  What do you mean by register transfer? Explain in detail. Also discuss three-state bus buffer.
16.  Write a detailed note on instruction cycle with neat diagrams.
17.  Explain register stack and memory stack with neat sketches.\
18.  A computer uses a memory unit with 256K words of 32 bits each. A binary instruction code is stored in one word of memory. The instruction has four parts: an indirect bit, an operation code, a register code part to specify one of 64 registers, and an address part.
1. How many bits are there in operation code, the register code part, and the address part?
2. Draw the instruction word format and indicate the number of bits in each part.
3. How many bits are there in the data and address inputs of the memory?
19.  Elaborate Flynn’s classification for computers.
20.  Draw a flowchart for first pass of an assembler and explain the same in brief.
21.  Write a note on micro-program sequencer.
22.  What is meant by addressing modes? List and explain any five addressing modes by taking proper example(s).
23.  Discuss four-segment instruction pipeline with diagram(s).
24.  Explain Booth multiplication algorithm for multiplying binary integers in signed 2’s complement representation.
25.  Write a detailed note on Direct Memory Access (DMA).
26.  Write a brief note on:
·         Subroutine call and return
·         RISC
27.  Describe micro-programmed control organization in detail.
28.  Discuss associative mapping and direct mapping in organization of cache memory.
29.  Explain addition and subtraction operations with signed 2’s complement integer data. Support your answer by taking appropriate example(s).




QUESTION BANK FOR COMPUTER NETWORKS (2140709)


  1.   Explain any two application layer protocol.
  2.   Explain WWW and HTTP.
  3.   What is E-mail? How it works? Which protocol it uses?
  4.  Explain the architectural overview of the World Wide Web.
  5.  Explain Client side world wide web and server side world web.
  6.  Describe the Built in HTTP request method.
  7.   Explain the basic function of E-mail.
  8.  Explain MIME.
  9.   Explain the Domain Name system.
  10.  Explain the Domain Resource Records.
  11.  Explain socket Programming.
  12. Explain Client and server communication in brief.
  13.    Explain Implementation of Connection-Oriented Services.
  14.  Explain Virtual Circuit Network.
  15.  What is Datagram? Explain Datagram Network.
  16.  Virtual Circuit Vs Datagram Networks.
  17.  Describe Router. / Explain in detail Router.
  18.  What is Switching Fabric?
  19.  What is routing?
  20.  Explain Distance Vector Routing.
  21.  Explain link state routing with all the five steps in detail.
  22.  Write about OSPF. Which four classes of routers are distinguished by OSPF?
  23.  Explain in detail OSPF.
  24.  Explain Spanning tree routing.
  25.  What is Flooding?
  26.  Explain the Mobile-IP./ write short notes on mobile IP.
  27.  Explain and compare distance vector routing and link state routing algorithm.
  28.  Explain the leaky bucket algorithm.
  29.  Explain token bucket algorithm.
  30.  Explain the congestion control in datagram subnets. Write about hop by hop choke packets scheme.
  31.  Explain load shedding and jitter control strategies to handle the congestion.
  32.  Explain the term jitter.
  33.  Explain IP addressing scheme in detail.
  34.  Compare the IPv4 and IPv6 Header.
  35.  Explain the following concept:1 Tunneling 2) Network Address Translation 3)DHCP
  36. Write a note on ICMP.




Program to demonstrate Hybrid Inheritance.



Program:
#include<iostream.h>
#include<conio.h>
class student
{
int r_no;
public:
void get(int a)
{
r_no=a;
}
void put()
{
 cout<<"Rollnumber : "<<r_no<<endl;
 }
 };
 class test : virtual public student
 {
  protected :
  float s1,s2;
  public:
  void getmarks(float x,float y)
  {
   s1=x;
   s2=y;
   }
   void putmarks()
   {
    cout<<" Marks in Academics  "<<endl;
     cout<<" Marks in subject 1 : "<<s1<<endl;
      cout<<" Marks in subject 2 :  "<<s2<<endl;
      }
      };
      class sports :virtual public student
      {
       protected :
       float score;
       public:
       void getscore(float s)
       {
score=s;
}
void putscore()
{
 cout<<"Sports score is : "<<score<<endl;
 }
 };
 class result : public test, public sports
 {
 float total;
 public:
 void display()
 {
  total= ((s1+s2+score)/3);
  put();
  putmarks();
  putscore();
  cout<<"Result : " <<total <<endl;
  }};
  int main()
  {
  clrscr();
   result r;
   r.get(01);
   r.getmarks(89,95);
   r.getscore(100);
   r.display();
   getch();
   return 0;
   }



Write a program to demonstrate Hierarchical Inheritance.

Program :
#include<iostream.h>
#include<conio.h>
class m
{
 public :
  int m,n;
  public :
  void getmn(int x,int y)
  {
     m=x;
     n=y;
      }
  };
  class o : public m
  {
    public :
   void add(void)
   {
   cout<<"M= "<<m<<endl;
      cout<<"N= "<<n<<endl;
      cout<<"M+N= "<<m+n<<endl;


     }
   };
   class p : public m
   {
    public :
     void mul()
     {
      cout<<"M= "<<m<<endl;
      cout<<"N= "<<n<<endl;
      cout<<"M*N= "<<m*n<<endl;
      }
     };

    int main()
    {
    clrscr();
      p ob;
      o ob1;
      ob1.getmn(10,20);
      ob.getmn(30,20);
      ob1.add();
      ob.mul();

      getch();
      return 0;
      }

INTER PROCESS COMMUNICATION PROBLEMS


Classical IPC Problems:
1. Dining Philosophers Problem
2. The Readers and Writers Problem
3. The Sleeping Barber Problem

1.    Dining philosophers problems:
There are N philosphers sitting around a circular table eating spaghetti and discussing philosphy. The problem is that each philosopher needs 2 forks to eat, and there are only N forks, one between each 2 philosophers. Design an algorithm that the philosophers can follow that insures that none starves as long as each philosopher eventually stops eating, and such that the maximum number of philosophers can eat at once.
·       Philosophers eat/think
·        Eating needs 2 forks
·        Pick one fork at a time
·        How to prevent deadlock
The problem was designed to illustrate the problem of avoiding deadlock, a   system state in which no progress is possible.
One idea is to instruct each philosopher to behave as follows:
·       think until the left fork is available; when it is, pick it up
·        think until the right fork is available; when it is, pick it up
·        eat
·        put the left fork down
·        put the right fork down
·        repeat from the start.
This solution is incorrect: it allows the system to reach deadlock. Suppose that all five philosophers take their left forks simultaneously. None will be able to take their right forks, and there will be a deadlock.
We could modify the program so that after taking the left fork, the program checks to see if the right fork is available. If it is not, the philosopher puts down the left one, waits for some time, and then repeats the whole process. This proposal too, fails, although for a different reason. With a little bit of bad luck, all the philosophers could start the algorithm simultaneously, picking up their left forks, seeing that their right forks were not available, putting down their left forks, waiting, picking up their left forks again simultaneously, and so on, forever. A situation like this, in which all the programs continue to run indefinitely but fail to make any progress is called starvation.
The solution presented below is deadlock-free and allows the maximum parallelism for an arbitrary number of philosophers. It uses an array, state, to keep track of whether a philosopher is eating, thinking, or hungry (trying to acquire forks). A philosopher may move into eating state only if neither neighbor is eating. Philosopher i's neighbors are defined by the macros LEFT and RIGHT. In other words, if i is 2, LEFT is 1 and RIGHT is 3.

  Solution:  
#define N            5             /* number of philosophers */
 #define LEFT         (i+N-1)%N     /* number of i's left neighbor */
 #define RIGHT        (i+1)%N       /* number of i's right neighbor */
 #define THINKING     0             /* philosopher is thinking */
 #define HUNGRY       1             /* philosopher is trying to get forks */
#define EATING       2             /* philosopher is eating */
 typedef int semaphore;             /* semaphores are a special kind of int */
 int state[N];                      /* array to keep track of everyone's state */
semaphore mutex =1;               /* mutual exclusion for critical regions */
semaphore s[N];                    /* one semaphore per philosopher */
 void philosopher(int i)            /* i: philosopher number, from 0 to N1 */
{   
 while (TRUE)
  {                                         /* repeat forever */  
   think();                   /* philosopher is thinking */      
   take_forks(i);               /* acquire two forks or block */   
   eat();                   /* yum-yum, spaghetti */  
put_forks(i);            /* put both forks back on table */    
}
}
void take_forks(int i)             /* i: philosopher number, from 0 to N1 */
{    
down(&mutex);                 /* enter critical region */    
state[i] = HUNGRY;            /* record fact that philosopher i is hungry */   
 test(i);                      /* try to acquire 2 forks */    
up(&mutex);                   /* exit critical region */   
down(&s[i]);                  /* block if forks were not acquired */
}
void put_forks(i)                  /* i: philosopher number, from 0 to N1 */
{    
down(&mutex);                 /* enter critical region */    
state[i] = THINKING;          /* philosopher has finished eating */
     test(LEFT);                   /* see if left neighbor can now eat */    
test(RIGHT);                  /* see if right neighbor can now eat */   
 up(&mutex);                   /* exit critical region */
}
void test(i)                       /* i: philosopher number, from 0 to N1* /
{    
if (state[i] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING)
{          
state[i] = EATING;         
 up(&s[i]);   
 }
}


Readers Writer problems:
The dining philosopher’s problem is useful for modeling processes that are competing for exclusive access to a limited number of resources, such as I/O devices. Another famous problem is the readers and writers problem which models access to a database (Courtois et al., 1971). Imagine, for example, an airline reservation system, with many competing processes wishing to read and write it. It is acceptable to have multiple processes reading the database at the same time, but if one process is updating (writing) the database, no other process may have access to the database, not even a reader. The question is how do you program the readers and the writers?
One solution is shown below.
Solution to Readers Writer problems
typedef int semaphore;                  /* use your imagination */
semaphore mutex = 1;                    /* controls access to 'rc' */
semaphore db = 1;                       /* controls access to the database */
int rc = 0;                             /* # of processes reading or wanting to */
void reader(void)
{    
while (TRUE)
{                      /* repeat forever */          
down(&mutex);                /* get exclusive access to 'rc' */          
rc = rc + 1;                 /* one reader more now */         
 if (rc == 1) down(&db);      /* if this is the first reader ... */         
 up(&mutex);                  /* release exclusive access to 'rc' */         
 read_data_base();            /* access the data */         
 down(&mutex);                /* get exclusive access to 'rc' */         
 rc = rc  1;                 /* one reader fewer now */         
 if (rc == 0) up(&db);        /* if this is the last reader ... */         
 up(&mutex);                  /* release exclusive access to 'rc' */         
 use_data_read();             /* noncritical region */    
}
}
void writer(void)
{  
  while (TRUE)
{                      /* repeat forever */       
  think_up_data();              /* noncritical region */       
  down(&db);                    /* get exclusive access */       
  write_data_base();            /* update the data */       
  up(&db);                      /* release exclusive access */    
}
}
In this solution, the first reader to get access to the data base does a down on the semaphore db. Subsequent readers merely have to increment a counter, rc. As readers leave, they decrement the counter and the last one out does an up on the semaphore, allowing a blocked writer, if there is one, to get in.



How to crack Aptitude Question In GATE and many Competitive Examination

As you know the aptitude in the competitive examination is quite for more challenging to clear it. But believe me it is the easy stuff t...