Wednesday, January 27, 2010

Basic OPP questions, SystemVerilog Interview Question 1

What is object oriented programming :

OOP Can be described with following concepts :

  • Follows bottom up approach.
  • Emphasis is on data.
  • Programs are divided into objects.
  • Functions and data are bound together.
  • Communication is done through objects.
  • Data is hidden.
The following are the basic concepts of OOPs:
Classes, Objects, Data abstraction and encapsulation, Polymorphism, Inheritance, Message Passing, and Dynamic Binding.
Q. What is a class?
Class is an entity which consists of member data and member functions which operate on the member data bound together.

Q. What is an object?
Objects are instances of classes. Class is a collection of similar kind of objects. When a class is created it doesn’t occupy any memory, but when instances of class is created i.e., when objects are created they occupy memory space.
Q. What is encapsulation?
A1. Encapsulation is welding of code and data together into objects.

Q. What is inheritance?
A2. Inheritance is a mechanism through which a subclass inherits the properties and behavior of its superclass. The derived
class inherits the properties and method implementations of the base class and extends it by overriding methods and adding additional properties and methods.
 Q. What is polymorphism?
A3. In Greek this means "many shapes."As a consequence of inheritance and virtual functions, a single task (for example, drawing
a geometrical shape) can be implemented using the same name (like draw()) and implemented differently (via virtual functions) as each type in object hierarchy requires(circle.draw() or rectangle.draw()). Later, when a polymorphic  object (whose type is not known at compile time) executes the draw() virtual function, the correct implementation is chosen andexecuted at run time.
Q. What is the difference between function overloading and function overriding?
A. Overloading is a method that allows defining multiple member functions with the same name but different signatures. The compiler will pick the correct function based on the signature. Overriding is a method that allows the derived class to redefine the behavior of member functions which the derived class inherits from a base class. The signatures of both base class member function and derived class member function are the same; however, the implementation and, therefore, the behavior will differ
Q. What are the advantages of OOP?
  • Data hiding helps create secure programs.
  • Redundant code can be avoided by using inheritance.
  • Multiple instances of objects can be created.
  • Work can be divided easily based on objects.
  • Inheritance helps to save time and cost.
  • Easy upgrading of systems is possible using object oriented systems.
Q. Explain about the virtual task and methods .

Virtual tasks and functions are the ways to achieve the polymorphism in system verilog. Try to fun the following example and see it will help you understand the concept.

class base ;
virtual function int print;
$display("INSIDE BASE \n");
endfunction : print
endclass : base

class derived extends base;
function int print;
$display("INSIDE DERIVED \n");
endfunction : print
endclass : derived

program test ;

derived d1;
initial
begin
d1 = new();
d1.print();
callPrint (d1);
end

task callPrint (base b1);
$display("Inside callPrint \n");
b1.print;
endtask : callPrint

endprogram

 

What is the use of the abstract class?



A virtual class is a temple or place holder for the child classes. A virtual class is also called as the abstract class. A virtual class is declared with a virtual keyword like :
virtual class base;
endclass;
A virtual class instance or object can not be constucted but you can define the hadle to the virtual class.
A virtual class is a temple or place holder for the child classes. A virtual class is also called as the abstract class. A virtual class is declared with a virtual keyword like :
virtual class base;
   virtual task1;
   endtask;
   virtual task2;
   endtask;
endclass;
A virtual class instance or object can not be constucted but you can define the hadle to the virtual class.

 

virtual class baseframe;
...
virtual function void iam();
endfunction
...
endclass

class shortframe extends baseframe;
...
function void iam();
$display ("Short Frame");
endfunction
endclass

class longframe extends baseframe;
...
function void iam();
$display ("Long Frame");
endfunction
endclass

baseframe two; // OK

initial begin
two = new(4); // ERROR
...

What is the need of virtual interfaces ?

Virtual interfaces provide a mechanism for separating abstract models and test programs from the actual signals that make up the design. A virtual interface allows the same subprogram to operate on different portions of a design and to dynamically control the set of signals associated with the subprogram. Instead of referring to the actual set of signals directly, users are able to manipulate a set of virtual signals. Changes to the underlying design do not require the code using virtual interfaces to be rewritten. By abstracting the connectivity and functionality of a set of blocks, virtual interfaces promote code reuse.
Virtual interfaces can be declared as class properties, which can be initialized procedurally or by an argument to new(). This allows the same virtual interface to be used in different classes. The following example shows how the same transactor class can be used to interact with various different devices:

interface SBus; // A Simple bus interface
logic req, grant;
logic [7:0] addr, data;
endinterface

class SBusTransctor; // SBus transactor class
virtual SBus bus; // virtual interface of type Sbus
function new( virtual SBus s );
bus = s; // initialize the virtual interface
endfunction

task request(); // request the bus
bus.req <= 1'b1;
endtask

task wait_for_bus(); // wait for the bus to be granted
@(posedge bus.grant);
endtask

endclass

module devA( Sbus s ) ... endmodule // devices that use SBus
module devB( Sbus s ) ... endmodule
module top;
SBus s[1:4] (); // instantiate 4 interfaces
devA a1( s[1] ); // instantiate 4 devices
devB b1( s[2] );
devA a2( s[3] );
devB b2( s[4] );
initial begin

SbusTransactor t[1:4]; // create 4 bus-transactors and bind
t[1] = new( s[1] );
t[2] = new( s[2] );
t[3] = new( s[3] );
t[4] = new( s[4] );
// test t[1:4]
end
endmodule

In the preceding example, the transaction class SbusTransctor is a
simple reusable component. It is written without any global or
hierarchical references and is unaware of the particular device with
which it will interact. Nevertheless, the class can interact with any
number of devices (four in the example) that adhere to the interface’s
protocol.

1 comment:

  1. its really a good explanation of virtual interface.
    but i would like to add some more points:
    first :- a program or class can't have interface without virtual keyword.

    ReplyDelete

Search This Blog